diff --git a/patches/minecraft/net/minecraft/world/entity/LivingEntity.java.patch b/patches/minecraft/net/minecraft/world/entity/LivingEntity.java.patch index 35e083844e..bbbda37a66 100644 --- a/patches/minecraft/net/minecraft/world/entity/LivingEntity.java.patch +++ b/patches/minecraft/net/minecraft/world/entity/LivingEntity.java.patch @@ -209,6 +209,15 @@ } } +@@ -1175,7 +_,7 @@ + + for(InteractionHand interactionhand : InteractionHand.values()) { + ItemStack itemstack1 = this.m_21120_(interactionhand); +- if (itemstack1.m_150930_(Items.f_42747_)) { ++ if (itemstack1.m_150930_(Items.f_42747_) && net.minecraftforge.common.ForgeHooks.onLivingUseTotem(this, p_21263_, itemstack1, interactionhand)) { + itemstack = itemstack1.m_41777_(); + itemstack1.m_41774_(1); + break; @@ -1185,7 +_,7 @@ if (itemstack != null) { if (this instanceof ServerPlayer) { diff --git a/src/main/java/net/minecraftforge/common/ForgeHooks.java b/src/main/java/net/minecraftforge/common/ForgeHooks.java index ce444a6eef..9c998ea1a5 100644 --- a/src/main/java/net/minecraftforge/common/ForgeHooks.java +++ b/src/main/java/net/minecraftforge/common/ForgeHooks.java @@ -138,6 +138,7 @@ import net.minecraftforge.event.entity.living.LivingFallEvent; import net.minecraftforge.event.entity.living.LivingHurtEvent; import net.minecraftforge.event.entity.living.LivingKnockBackEvent; import net.minecraftforge.event.entity.living.LivingSetAttackTargetEvent; +import net.minecraftforge.event.entity.living.LivingUseTotemEvent; import net.minecraftforge.event.entity.living.LootingLevelEvent; import net.minecraftforge.event.entity.living.ShieldBlockEvent; import net.minecraftforge.event.entity.player.AdvancementEvent; @@ -317,6 +318,11 @@ public class ForgeHooks return event; } + public static boolean onLivingUseTotem(LivingEntity entity, DamageSource damageSource, ItemStack totem, InteractionHand hand) + { + return !MinecraftForge.EVENT_BUS.post(new LivingUseTotemEvent(entity, damageSource, totem, hand)); + } + public static float onLivingHurt(LivingEntity entity, DamageSource src, float amount) { LivingHurtEvent event = new LivingHurtEvent(entity, src, amount); diff --git a/src/main/java/net/minecraftforge/event/entity/living/LivingUseTotemEvent.java b/src/main/java/net/minecraftforge/event/entity/living/LivingUseTotemEvent.java new file mode 100644 index 0000000000..a1c05d64bf --- /dev/null +++ b/src/main/java/net/minecraftforge/event/entity/living/LivingUseTotemEvent.java @@ -0,0 +1,63 @@ +/* + * Copyright (c) Forge Development LLC and contributors + * SPDX-License-Identifier: LGPL-2.1-only + */ + +package net.minecraftforge.event.entity.living; + +import net.minecraft.world.InteractionHand; +import net.minecraft.world.damagesource.DamageSource; +import net.minecraft.world.entity.LivingEntity; +import net.minecraft.world.item.ItemStack; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.eventbus.api.Cancelable; +import net.minecraftforge.fml.LogicalSide; + +/** + * Fired when an Entity attempts to use a totem to prevent its death. + * + *
This event is {@linkplain Cancelable cancellable}, and does not {@linkplain HasResult have a result}. + * If this event is cancelled, the totem will not prevent the entity's death.
+ * + *This event is fired on the {@linkplain MinecraftForge#EVENT_BUS Forge event bus}, + * only on the {@linkplain LogicalSide#SERVER logical server}.
+ */ +@Cancelable +public class LivingUseTotemEvent extends LivingEvent +{ + private final DamageSource source; + private final ItemStack totem; + private final InteractionHand hand; + + public LivingUseTotemEvent(LivingEntity entity, DamageSource source, ItemStack totem, InteractionHand hand) + { + super(entity); + this.source = source; + this.totem = totem; + this.hand = hand; + } + + /** + * {@return the damage source that caused the entity to die} + */ + public DamageSource getSource() + { + return source; + } + + /** + * {@return the totem of undying being used from the entity's inventory} + */ + public ItemStack getTotem() + { + return totem; + } + + /** + * {@return the hand holding the totem} + */ + public InteractionHand getHandHolding() + { + return hand; + } +}