From 265534954a9d30facd802c36f3c045c4147ff8be Mon Sep 17 00:00:00 2001 From: cech12 Date: Tue, 14 May 2024 23:14:22 +0200 Subject: [PATCH] fixed: Game crashed when using non-edible items (bow, ...) with Bunny Ears equipped --- CHANGELOG.md | 5 ++++- .../cech12/usefulhats/UsefulHatsEventUtils.java | 15 +++++++++++---- .../usefulhats/mixin/LivingEntityMixin.java | 5 ++++- .../java/de/cech12/usefulhats/init/ModItems.java | 5 ++++- .../java/de/cech12/usefulhats/init/ModItems.java | 5 ++++- 5 files changed, 27 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ed9952..1543d67 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,11 +3,14 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Forge Recommended Versioning](https://mcforge.readthedocs.io/en/latest/conventions/versioning/). -## [1.20.4-5.1.2.0] - 2024-05-11 +## [1.20.4-5.1.2.0] - 2024-05-14 ### Changed - Bunny Ears only get damage when jumping or when food is eaten - reduced Bunny Ears default durability from 600 to 450 +### Fixed +- Game crashed when using non-edible items (bow, ...) with Bunny Ears equipped + ## [1.20.4-5.1.1.1] - 2024-05-11 ### Fixed - Respiration effect of Aquanaut Helmet was removed when getting underwater (all loaders) diff --git a/common/src/main/java/de/cech12/usefulhats/UsefulHatsEventUtils.java b/common/src/main/java/de/cech12/usefulhats/UsefulHatsEventUtils.java index c70389d..17d81be 100644 --- a/common/src/main/java/de/cech12/usefulhats/UsefulHatsEventUtils.java +++ b/common/src/main/java/de/cech12/usefulhats/UsefulHatsEventUtils.java @@ -18,6 +18,8 @@ import net.minecraft.world.level.Level; import net.minecraft.world.level.block.state.BlockState; +import java.util.concurrent.atomic.AtomicReference; + public class UsefulHatsEventUtils { public static float onBreakSpeedCalculation(Player player, BlockState state, float actualSpeed) { @@ -62,14 +64,19 @@ public static void onLivingJump(LivingEntity entity) { .forEach(stack -> ((ILivingJumpListener) stack.getItem()).onLivingJumpEvent(entity, stack)); } - public static int onLivingStartsUsingItem(LivingEntity entity, ItemStack usedStack, int actualDuration) { - final int[] newDuration = {actualDuration}; + public static Integer onLivingStartsUsingItem(LivingEntity entity, ItemStack usedStack, int actualDuration) { + AtomicReference newDuration = new AtomicReference<>(actualDuration); if (entity instanceof Player player) { Services.REGISTRY.getEquippedHatItemStacks(player).stream() .filter(stack -> stack.getItem() instanceof IItemUseListener) - .forEach(stack -> newDuration[0] = ((IItemUseListener) stack.getItem()).onItemUseEventStart(player, usedStack, newDuration[0], stack)); + .forEach(stack -> { + Integer calculatedDuration = ((IItemUseListener) stack.getItem()).onItemUseEventStart(player, usedStack, newDuration.get(), stack); + if (calculatedDuration != null) { + newDuration.set(calculatedDuration); + } + }); } - return newDuration[0]; + return newDuration.get(); } public static void onEquip(LivingEntity entity, ItemStack stack) { diff --git a/fabric/src/main/java/de/cech12/usefulhats/mixin/LivingEntityMixin.java b/fabric/src/main/java/de/cech12/usefulhats/mixin/LivingEntityMixin.java index 955e151..6a4fee9 100644 --- a/fabric/src/main/java/de/cech12/usefulhats/mixin/LivingEntityMixin.java +++ b/fabric/src/main/java/de/cech12/usefulhats/mixin/LivingEntityMixin.java @@ -29,7 +29,10 @@ public void dropAllDeathLootProxy(DamageSource damageSource, CallbackInfo ci) { @Inject(method = "startUsingItem", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/item/ItemStack;getUseDuration()I", shift = At.Shift.BY, by = 2)) public void startUsingItemProxy(InteractionHand interactionHand, CallbackInfo ci) { LivingEntity entity = (LivingEntity) (Object) this; - useItemRemaining = UsefulHatsEventUtils.onLivingStartsUsingItem(entity, entity.getItemInHand(interactionHand), useItemRemaining); + Integer newValue = UsefulHatsEventUtils.onLivingStartsUsingItem(entity, entity.getItemInHand(interactionHand), useItemRemaining); + if (newValue != null) { + useItemRemaining = newValue; + } } @Inject(method = "equipmentHasChanged", at = @At("RETURN")) diff --git a/forge/src/main/java/de/cech12/usefulhats/init/ModItems.java b/forge/src/main/java/de/cech12/usefulhats/init/ModItems.java index 02bd15b..5107bdc 100644 --- a/forge/src/main/java/de/cech12/usefulhats/init/ModItems.java +++ b/forge/src/main/java/de/cech12/usefulhats/init/ModItems.java @@ -100,7 +100,10 @@ private static void onLivingJumpEvent(LivingEvent.LivingJumpEvent event) { private static void onLivingUseItemEventStart(LivingEntityUseItemEvent event) { if (!event.isCanceled() && event instanceof LivingEntityUseItemEvent.Start) { - event.setDuration(UsefulHatsEventUtils.onLivingStartsUsingItem(event.getEntity(), event.getItem(), event.getDuration())); + Integer newDuration = UsefulHatsEventUtils.onLivingStartsUsingItem(event.getEntity(), event.getItem(), event.getDuration()); + if (newDuration != null) { + event.setDuration(newDuration); + } } } diff --git a/neoforge/src/main/java/de/cech12/usefulhats/init/ModItems.java b/neoforge/src/main/java/de/cech12/usefulhats/init/ModItems.java index 1d72466..4483cd8 100644 --- a/neoforge/src/main/java/de/cech12/usefulhats/init/ModItems.java +++ b/neoforge/src/main/java/de/cech12/usefulhats/init/ModItems.java @@ -98,7 +98,10 @@ private static void onLivingJumpEvent(LivingEvent.LivingJumpEvent event) { private static void onLivingUseItemEventStart(LivingEntityUseItemEvent event) { if (event instanceof LivingEntityUseItemEvent.Start) { - event.setDuration(UsefulHatsEventUtils.onLivingStartsUsingItem(event.getEntity(), event.getItem(), event.getDuration())); + Integer newDuration = UsefulHatsEventUtils.onLivingStartsUsingItem(event.getEntity(), event.getItem(), event.getDuration()); + if (newDuration != null) { + event.setDuration(newDuration); + } } }