Skip to content

Commit

Permalink
fixed: Game crashed when using non-edible items (bow, ...) with Bunny…
Browse files Browse the repository at this point in the history
… Ears equipped
  • Loading branch information
cech12 committed May 14, 2024
1 parent 49eb6b2 commit 2655349
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 8 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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<Integer> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
5 changes: 4 additions & 1 deletion forge/src/main/java/de/cech12/usefulhats/init/ModItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

Expand Down

0 comments on commit 2655349

Please sign in to comment.