From 827ca83cfd62e562220042eda4e48c96283818d3 Mon Sep 17 00:00:00 2001 From: ChampionAsh5357 Date: Thu, 14 Oct 2021 23:13:18 -0400 Subject: [PATCH] Add entity source to PotionAddedEvent (#8147) --- .../world/entity/LivingEntity.java.patch | 2 +- .../event/entity/living/PotionEvent.java | 22 ++++++++++++++++++- .../minecraftforge/debug/PotionEventTest.java | 2 +- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/patches/minecraft/net/minecraft/world/entity/LivingEntity.java.patch b/patches/minecraft/net/minecraft/world/entity/LivingEntity.java.patch index d2d45bcb8f..7524f3987e 100644 --- a/patches/minecraft/net/minecraft/world/entity/LivingEntity.java.patch +++ b/patches/minecraft/net/minecraft/world/entity/LivingEntity.java.patch @@ -84,7 +84,7 @@ return false; } else { MobEffectInstance mobeffectinstance = this.f_20945_.get(p_147208_.m_19544_()); -+ net.minecraftforge.common.MinecraftForge.EVENT_BUS.post(new net.minecraftforge.event.entity.living.PotionEvent.PotionAddedEvent(this, mobeffectinstance, p_147208_)); ++ net.minecraftforge.common.MinecraftForge.EVENT_BUS.post(new net.minecraftforge.event.entity.living.PotionEvent.PotionAddedEvent(this, mobeffectinstance, p_147208_, p_147209_)); if (mobeffectinstance == null) { this.f_20945_.put(p_147208_.m_19544_(), p_147208_); this.m_142540_(p_147208_, p_147209_); diff --git a/src/main/java/net/minecraftforge/event/entity/living/PotionEvent.java b/src/main/java/net/minecraftforge/event/entity/living/PotionEvent.java index b94e432f91..e59cfac453 100644 --- a/src/main/java/net/minecraftforge/event/entity/living/PotionEvent.java +++ b/src/main/java/net/minecraftforge/event/entity/living/PotionEvent.java @@ -22,6 +22,7 @@ package net.minecraftforge.event.entity.living; import javax.annotation.Nonnull; import javax.annotation.Nullable; +import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.effect.MobEffect; import net.minecraft.world.effect.MobEffectInstance; @@ -127,11 +128,19 @@ public class PotionEvent extends LivingEvent public static class PotionAddedEvent extends PotionEvent { private final MobEffectInstance oldEffect; - + private final Entity source; + + @Deprecated(forRemoval = true, since = "1.17.1") public PotionAddedEvent(LivingEntity living, MobEffectInstance oldEffect, MobEffectInstance newEffect) + { + this(living, oldEffect, newEffect, null); + } + + public PotionAddedEvent(LivingEntity living, MobEffectInstance oldEffect, MobEffectInstance newEffect, Entity source) { super(living, newEffect); this.oldEffect = oldEffect; + this.source = source; } /** @@ -152,6 +161,17 @@ public class PotionEvent extends LivingEvent { return oldEffect; } + + /** + * Returns the entity source of the effect, or {@code null} if none exists. + * + * @return the entity source of the effect, or {@code null} + */ + @Nullable + public Entity getPotionSource() + { + return source; + } } /** diff --git a/src/test/java/net/minecraftforge/debug/PotionEventTest.java b/src/test/java/net/minecraftforge/debug/PotionEventTest.java index 9a6d83c0b3..f65c0d93bb 100644 --- a/src/test/java/net/minecraftforge/debug/PotionEventTest.java +++ b/src/test/java/net/minecraftforge/debug/PotionEventTest.java @@ -53,7 +53,7 @@ public class PotionEventTest public static void onPotionAdded(PotionEvent.PotionAddedEvent event) { if (!event.getEntity().getCommandSenderWorld().isClientSide) - LOGGER.info("{} has a new PotionEffect {}, the old one was {}", event.getEntityLiving(), event.getPotionEffect(), event.getOldPotionEffect()); + LOGGER.info("{} has a new PotionEffect {} from {}, the old one was {}", event.getEntityLiving(), event.getPotionEffect(), event.getPotionSource(), event.getOldPotionEffect()); } @SubscribeEvent