From 2a5268c6993f2de7612789bfac2b674d04c87e89 Mon Sep 17 00:00:00 2001 From: mcenderdragon Date: Sun, 2 Dec 2018 02:13:13 +0100 Subject: [PATCH] added PotionHooks, closes #3867, #4375 (#4614) * solved merge confilct * improved var names & removed tabs * Added spaces around != * fixed typo --- .../entity/EntityLivingBase.java.patch | 78 ++++++--- .../entity/monster/EntitySpider.java.patch | 17 ++ .../event/entity/living/PotionEvent.java | 149 ++++++++++++++++++ .../minecraftforge/debug/PotionEventTest.java | 70 ++++++++ 4 files changed, 288 insertions(+), 26 deletions(-) create mode 100644 patches/minecraft/net/minecraft/entity/monster/EntitySpider.java.patch create mode 100644 src/main/java/net/minecraftforge/event/entity/living/PotionEvent.java create mode 100644 src/test/java/net/minecraftforge/debug/PotionEventTest.java diff --git a/patches/minecraft/net/minecraft/entity/EntityLivingBase.java.patch b/patches/minecraft/net/minecraft/entity/EntityLivingBase.java.patch index 11c921375f..67e2fe23f3 100644 --- a/patches/minecraft/net/minecraft/entity/EntityLivingBase.java.patch +++ b/patches/minecraft/net/minecraft/entity/EntityLivingBase.java.patch @@ -60,7 +60,33 @@ this.func_82142_c(this.func_70644_a(MobEffects.field_76441_p)); } } -@@ -819,6 +824,8 @@ +@@ -736,6 +741,7 @@ + { + PotionEffect potioneffect = this.field_70713_bf.get(p_70690_1_.func_188419_a()); + ++ net.minecraftforge.common.MinecraftForge.EVENT_BUS.post(new net.minecraftforge.event.entity.living.PotionEvent.PotionAddedEvent(this, potioneffect, p_70690_1_)); + if (potioneffect == null) + { + this.field_70713_bf.put(p_70690_1_.func_188419_a(), p_70690_1_); +@@ -751,6 +757,9 @@ + + public boolean func_70687_e(PotionEffect p_70687_1_) + { ++ net.minecraftforge.event.entity.living.PotionEvent.PotionApplicableEvent event = new net.minecraftforge.event.entity.living.PotionEvent.PotionApplicableEvent(this, p_70687_1_); ++ net.minecraftforge.common.MinecraftForge.EVENT_BUS.post(event); ++ if (event.getResult() != net.minecraftforge.fml.common.eventhandler.Event.Result.DEFAULT) return event.getResult() == net.minecraftforge.fml.common.eventhandler.Event.Result.ALLOW; + if (this.func_70668_bt() == EnumCreatureAttribute.UNDEAD) + { + Potion potion = p_70687_1_.func_188419_a(); +@@ -777,6 +786,7 @@ + + public void func_184589_d(Potion p_184589_1_) + { ++ if(net.minecraftforge.common.MinecraftForge.EVENT_BUS.post(new net.minecraftforge.event.entity.living.PotionEvent.PotionRemoveEvent(this, p_184589_1_))) return; + PotionEffect potioneffect = this.func_184596_c(p_184589_1_); + + if (potioneffect != null) +@@ -819,6 +829,8 @@ public void func_70691_i(float p_70691_1_) { @@ -69,7 +95,7 @@ float f = this.func_110143_aJ(); if (f > 0.0F) -@@ -839,6 +846,7 @@ +@@ -839,6 +851,7 @@ public boolean func_70097_a(DamageSource p_70097_1_, float p_70097_2_) { @@ -77,7 +103,7 @@ if (this.func_180431_b(p_70097_1_)) { return false; -@@ -927,9 +935,9 @@ +@@ -927,9 +940,9 @@ this.field_70718_bc = 100; this.field_70717_bb = (EntityPlayer)entity1; } @@ -89,7 +115,7 @@ if (entitywolf.func_70909_n()) { -@@ -1127,7 +1135,7 @@ +@@ -1127,7 +1140,7 @@ public void func_70669_a(ItemStack p_70669_1_) { @@ -98,7 +124,7 @@ for (int i = 0; i < 5; ++i) { -@@ -1139,12 +1147,17 @@ +@@ -1139,12 +1152,17 @@ vec3d1 = vec3d1.func_178789_a(-this.field_70125_A * 0.017453292F); vec3d1 = vec3d1.func_178785_b(-this.field_70177_z * 0.017453292F); vec3d1 = vec3d1.func_72441_c(this.field_70165_t, this.field_70163_u + (double)this.func_70047_e(), this.field_70161_v); @@ -117,7 +143,7 @@ if (!this.field_70729_aU) { Entity entity = p_70645_1_.func_76346_g(); -@@ -1165,18 +1178,26 @@ +@@ -1165,18 +1183,26 @@ if (!this.field_70170_p.field_72995_K) { @@ -149,7 +175,7 @@ } this.field_70170_p.func_72960_a(this, (byte)3); -@@ -1195,6 +1216,9 @@ +@@ -1195,6 +1221,9 @@ public void func_70653_a(Entity p_70653_1_, float p_70653_2_, double p_70653_3_, double p_70653_5_) { @@ -159,7 +185,7 @@ if (this.field_70146_Z.nextDouble() >= this.func_110148_a(SharedMonsterAttributes.field_111266_c).func_111126_e()) { this.field_70160_al = true; -@@ -1253,15 +1277,7 @@ +@@ -1253,15 +1282,7 @@ BlockPos blockpos = new BlockPos(i, j, k); IBlockState iblockstate = this.field_70170_p.func_180495_p(blockpos); Block block = iblockstate.func_177230_c(); @@ -176,7 +202,7 @@ } } -@@ -1287,6 +1303,9 @@ +@@ -1287,6 +1308,9 @@ public void func_180430_e(float p_180430_1_, float p_180430_2_) { @@ -186,7 +212,7 @@ super.func_180430_e(p_180430_1_, p_180430_2_); PotionEffect potioneffect = this.func_70660_b(MobEffects.field_76430_j); float f = potioneffect == null ? 0.0F : (float)(potioneffect.func_76458_c() + 1); -@@ -1303,7 +1322,7 @@ +@@ -1303,7 +1327,7 @@ if (iblockstate.func_185904_a() != Material.field_151579_a) { @@ -195,7 +221,7 @@ this.func_184185_a(soundtype.func_185842_g(), soundtype.func_185843_a() * 0.5F, soundtype.func_185847_b() * 0.75F); } } -@@ -1380,17 +1399,20 @@ +@@ -1380,17 +1404,20 @@ { if (!this.func_180431_b(p_70665_1_)) { @@ -217,7 +243,7 @@ this.func_110149_m(this.func_110139_bj() - p_70665_2_); } } -@@ -1447,6 +1469,11 @@ +@@ -1447,6 +1474,11 @@ public void func_184609_a(EnumHand p_184609_1_) { @@ -229,7 +255,7 @@ if (!this.field_82175_bq || this.field_110158_av >= this.func_82166_i() / 2 || this.field_110158_av < 0) { this.field_110158_av = -1; -@@ -1694,7 +1721,7 @@ +@@ -1694,7 +1726,7 @@ if (!this.field_70170_p.func_184143_b(axisalignedbb1)) { @@ -238,7 +264,7 @@ { this.func_70634_a(d11, this.field_70163_u + 1.0D, d12); return; -@@ -1702,14 +1729,14 @@ +@@ -1702,14 +1734,14 @@ BlockPos blockpos = new BlockPos(d11, this.field_70163_u - 1.0D, d12); @@ -255,7 +281,7 @@ { d1 = d11; d13 = this.field_70163_u + 2.0D; -@@ -1781,16 +1808,17 @@ +@@ -1781,16 +1813,17 @@ } this.field_70160_al = true; @@ -275,7 +301,7 @@ } protected float func_189749_co() -@@ -1874,7 +1902,8 @@ +@@ -1874,7 +1907,8 @@ if (this.field_70122_E) { @@ -285,7 +311,7 @@ } float f7 = 0.16277136F / (f6 * f6 * f6); -@@ -1894,7 +1923,8 @@ +@@ -1894,7 +1928,8 @@ if (this.field_70122_E) { @@ -295,7 +321,7 @@ } if (this.func_70617_f_()) -@@ -2054,6 +2084,7 @@ +@@ -2054,6 +2089,7 @@ public void func_70071_h_() { @@ -303,7 +329,7 @@ super.func_70071_h_(); this.func_184608_ct(); -@@ -2096,7 +2127,9 @@ +@@ -2096,7 +2132,9 @@ if (!ItemStack.func_77989_b(itemstack1, itemstack)) { @@ -313,7 +339,7 @@ if (!itemstack.func_190926_b()) { -@@ -2575,6 +2608,40 @@ +@@ -2575,6 +2613,40 @@ this.field_70752_e = true; } @@ -354,7 +380,7 @@ public abstract EnumHandSide func_184591_cq(); public boolean func_184587_cr() -@@ -2595,12 +2662,19 @@ +@@ -2595,12 +2667,19 @@ if (itemstack == this.field_184627_bm) { @@ -375,7 +401,7 @@ { this.func_71036_o(); } -@@ -2618,8 +2692,10 @@ +@@ -2618,8 +2697,10 @@ if (!itemstack.func_190926_b() && !this.func_184587_cr()) { @@ -387,7 +413,7 @@ if (!this.field_70170_p.field_72995_K) { -@@ -2700,7 +2776,10 @@ +@@ -2700,7 +2781,10 @@ if (!this.field_184627_bm.func_190926_b() && this.func_184587_cr()) { this.func_184584_a(this.field_184627_bm, 16); @@ -399,7 +425,7 @@ this.func_184602_cy(); } } -@@ -2724,7 +2803,8 @@ +@@ -2724,7 +2808,8 @@ { if (!this.field_184627_bm.func_190926_b()) { @@ -409,7 +435,7 @@ } this.func_184602_cy(); -@@ -2852,6 +2932,31 @@ +@@ -2852,6 +2937,31 @@ return true; } @@ -441,7 +467,7 @@ public boolean func_190631_cK() { return true; -@@ -2861,4 +2966,30 @@ +@@ -2861,4 +2971,30 @@ public void func_191987_a(BlockPos p_191987_1_, boolean p_191987_2_) { } diff --git a/patches/minecraft/net/minecraft/entity/monster/EntitySpider.java.patch b/patches/minecraft/net/minecraft/entity/monster/EntitySpider.java.patch new file mode 100644 index 0000000000..44d3fe72b9 --- /dev/null +++ b/patches/minecraft/net/minecraft/entity/monster/EntitySpider.java.patch @@ -0,0 +1,17 @@ +--- ../src-base/minecraft/net/minecraft/entity/monster/EntitySpider.java ++++ ../src-work/minecraft/net/minecraft/entity/monster/EntitySpider.java +@@ -139,7 +139,13 @@ + + public boolean func_70687_e(PotionEffect p_70687_1_) + { +- return p_70687_1_.func_188419_a() == MobEffects.field_76436_u ? false : super.func_70687_e(p_70687_1_); ++ if(p_70687_1_.func_188419_a() == MobEffects.field_76436_u) ++ { ++ net.minecraftforge.event.entity.living.PotionEvent.PotionApplicableEvent event = new net.minecraftforge.event.entity.living.PotionEvent.PotionApplicableEvent(this, p_70687_1_); ++ net.minecraftforge.common.MinecraftForge.EVENT_BUS.post(event); ++ return event.getResult() == net.minecraftforge.fml.common.eventhandler.Event.Result.ALLOW; ++ } ++ return super.func_70687_e(p_70687_1_); + } + + public boolean func_70841_p() diff --git a/src/main/java/net/minecraftforge/event/entity/living/PotionEvent.java b/src/main/java/net/minecraftforge/event/entity/living/PotionEvent.java new file mode 100644 index 0000000000..772317acf8 --- /dev/null +++ b/src/main/java/net/minecraftforge/event/entity/living/PotionEvent.java @@ -0,0 +1,149 @@ +/* + * Minecraft Forge + * Copyright (c) 2016-2018. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation version 2.1 + * of the License. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package net.minecraftforge.event.entity.living; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +import net.minecraft.entity.EntityLivingBase; +import net.minecraft.potion.Potion; +import net.minecraft.potion.PotionEffect; +import net.minecraftforge.fml.common.eventhandler.Cancelable; + +/** + * This Event and its subevents gets fired from {@link EntityLivingBase} on the {@link MinecraftForge#EVENT_BUS}.
+ */ +public class PotionEvent extends LivingEvent +{ + @Nullable + protected final PotionEffect effect; + + public PotionEvent(EntityLivingBase living, PotionEffect effect) + { + super(living); + this.effect = effect; + } + /** + * Retuns the PotionEffect. + */ + @Nullable + public PotionEffect getPotionEffect() + { + return effect; + } + + /** + * This Event is fired when a Potion is about to get removed from an Entity. + * This Event is {@link Cancelable}. + * This Event does not have a result. + */ + @Cancelable + public static class PotionRemoveEvent extends PotionEvent + { + private final Potion potion; + + public PotionRemoveEvent(EntityLivingBase living, Potion potion) + { + super(living, living.getActivePotionMap().get(potion)); + this.potion = potion; + } + + /** + * @return the Potion which is tried to remove from the Entity. + */ + public Potion getPotion() + { + return this.potion; + } + + /** + * @return the PotionEffect. In the remove event this can be null if the Entity does not have a {@link Potion} of the right type active. + */ + @Override + @Nullable + public PotionEffect getPotionEffect() + { + return super.getPotionEffect(); + } + } + + /** + * This Event is fired to check if a Potion can get applied to an Entity. + * This Event is not {@link Cancelable} + * This Event has a result {@link HasResult}. + * ALLOW will apply this potion effect. + * DENY will not apply this potion effect. + * DEFAULT will run vanilla logic to determine if this potion isApplicable. + */ + @HasResult + public static class PotionApplicableEvent extends PotionEvent + { + public PotionApplicableEvent(EntityLivingBase living, PotionEffect effect) + { + super(living, effect); + } + + /** + * @return the PotionEffect. + */ + @Override + @Nonnull + public PotionEffect getPotionEffect() + { + return super.getPotionEffect(); + } + } + + /** + * This Event is fired when a new Potion is added to the Entity. This is also fired if the Entity already has this effect but with different duration/level. + * This Event is not {@link Cancelable} + * This Event does not have a Result. + */ + public static class PotionAddedEvent extends PotionEvent + { + private final PotionEffect oldEffect; + + public PotionAddedEvent(EntityLivingBase living, PotionEffect oldEffect, PotionEffect newEffect) + { + super(living, newEffect); + this.oldEffect = oldEffect; + } + + /** + * @return the added PotionEffect. This is the umerged PotionEffect if the old PotionEffect is not null. + */ + @Override + @Nonnull + public PotionEffect getPotionEffect() + { + return super.getPotionEffect(); + } + + /** + * @return the old PotionEffect. THis can be null if the entity did not have an effect of this kind before. + */ + @Nullable + public PotionEffect getOldPotionEffect() + { + return oldEffect; + } + } + +} diff --git a/src/test/java/net/minecraftforge/debug/PotionEventTest.java b/src/test/java/net/minecraftforge/debug/PotionEventTest.java new file mode 100644 index 0000000000..1cb254a11e --- /dev/null +++ b/src/test/java/net/minecraftforge/debug/PotionEventTest.java @@ -0,0 +1,70 @@ +/* + * Minecraft Forge + * Copyright (c) 2016-2018. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation version 2.1 + * of the License. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package net.minecraftforge.debug; + +import org.apache.logging.log4j.Logger; + +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.event.entity.living.PotionEvent; +import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.fml.common.Mod.EventHandler; +import net.minecraftforge.fml.common.event.FMLInitializationEvent; +import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; +import net.minecraftforge.fml.common.eventhandler.Event.Result; +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; + +@Mod(modid = "potioneventtest", name = "PotionEventTest", version = "1.0.0", acceptableRemoteVersions = "*") +public class PotionEventTest +{ + public static final boolean ENABLE = false; + private Logger log; + + @EventHandler + public void preInit(FMLPreInitializationEvent event) + { + log = event.getModLog(); + } + + @EventHandler + public void init(FMLInitializationEvent event) + { + if (ENABLE) + MinecraftForge.EVENT_BUS.register(this); + } + + @SubscribeEvent + public void onPotionAdded(PotionEvent.PotionAddedEvent event) + { + log.info("{} has a new PotionEffect {}, the old one was {}", event.getEntityLiving(), event.getPotionEffect(), event.getOldPotionEffect()); + } + + @SubscribeEvent + public void isPotionApplicable(PotionEvent.PotionApplicableEvent event) + { + event.setResult(Result.ALLOW); + log.info("Allowed Potion {} for Entity {}", event.getPotionEffect(), event.getEntityLiving()); + } + + @SubscribeEvent + public void onPotionRemove(PotionEvent.PotionRemoveEvent event) + { + log.info("Effect {} got Removed from {}", event.getPotionEffect(), event.getEntityLiving()); + } +}