From b8e411674b777778d4111b785567acf91b25f988 Mon Sep 17 00:00:00 2001 From: Judge40 Date: Sun, 1 Apr 2018 08:48:25 +0100 Subject: [PATCH] Create event for overriding mob griefing flag based on entity. (#4710) --- .../net/minecraft/entity/Entity.java.patch | 2 +- .../minecraft/entity/EntityLiving.java.patch | 9 ++++ .../entity/ai/EntityAIBreakDoor.java.patch | 2 +- .../entity/ai/EntityAIEatGrass.java.patch | 20 ++++++++ .../ai/EntityAIHarvestFarmland.java.patch | 11 ++++ .../entity/boss/EntityDragon.java.patch | 3 +- .../entity/boss/EntityWither.java.patch | 18 +++++++ .../entity/monster/EntityCreeper.java.patch | 11 ++++ .../entity/monster/EntityEnderman.java.patch | 18 +++++++ .../entity/monster/EntityEvoker.java.patch | 11 ++++ .../monster/EntitySilverfish.java.patch | 20 ++++++++ .../entity/monster/EntitySnowman.java.patch | 9 ++++ .../entity/passive/EntityRabbit.java.patch | 11 ++++ .../projectile/EntityLargeFireball.java.patch | 11 ++++ .../projectile/EntitySmallFireball.java.patch | 11 ++++ .../projectile/EntityWitherSkull.java.patch | 9 ++++ .../event/ForgeEventFactory.java | 10 ++++ .../event/entity/EntityMobGriefingEvent.java | 44 ++++++++++++++++ .../debug/EntityMobGriefingEventTest.java | 51 +++++++++++++++++++ 19 files changed, 278 insertions(+), 3 deletions(-) create mode 100644 patches/minecraft/net/minecraft/entity/ai/EntityAIEatGrass.java.patch create mode 100644 patches/minecraft/net/minecraft/entity/ai/EntityAIHarvestFarmland.java.patch create mode 100644 patches/minecraft/net/minecraft/entity/monster/EntityCreeper.java.patch create mode 100644 patches/minecraft/net/minecraft/entity/monster/EntityEvoker.java.patch create mode 100644 patches/minecraft/net/minecraft/entity/monster/EntitySilverfish.java.patch create mode 100644 patches/minecraft/net/minecraft/entity/passive/EntityRabbit.java.patch create mode 100644 patches/minecraft/net/minecraft/entity/projectile/EntityLargeFireball.java.patch create mode 100644 patches/minecraft/net/minecraft/entity/projectile/EntitySmallFireball.java.patch create mode 100644 src/main/java/net/minecraftforge/event/entity/EntityMobGriefingEvent.java create mode 100644 src/test/java/net/minecraftforge/debug/EntityMobGriefingEventTest.java diff --git a/patches/minecraft/net/minecraft/entity/Entity.java.patch b/patches/minecraft/net/minecraft/entity/Entity.java.patch index 2f2c5feab..2e0416efb 100644 --- a/patches/minecraft/net/minecraft/entity/Entity.java.patch +++ b/patches/minecraft/net/minecraft/entity/Entity.java.patch @@ -361,7 +361,7 @@ + { + return world.field_73012_v.nextFloat() < fallDistance - 0.5F + && this instanceof EntityLivingBase -+ && (this instanceof EntityPlayer || world.func_82736_K().func_82766_b("mobGriefing")) ++ && (this instanceof EntityPlayer || net.minecraftforge.event.ForgeEventFactory.getMobGriefingEvent(world, this)) + && this.field_70130_N * this.field_70130_N * this.field_70131_O > 0.512F; + } + /* ================================== Forge End =====================================*/ diff --git a/patches/minecraft/net/minecraft/entity/EntityLiving.java.patch b/patches/minecraft/net/minecraft/entity/EntityLiving.java.patch index 34b7b96b0..78f792a1b 100644 --- a/patches/minecraft/net/minecraft/entity/EntityLiving.java.patch +++ b/patches/minecraft/net/minecraft/entity/EntityLiving.java.patch @@ -8,6 +8,15 @@ } public boolean func_70686_a(Class p_70686_1_) +@@ -576,7 +577,7 @@ + super.func_70636_d(); + this.field_70170_p.field_72984_F.func_76320_a("looting"); + +- if (!this.field_70170_p.field_72995_K && this.func_98052_bS() && !this.field_70729_aU && this.field_70170_p.func_82736_K().func_82766_b("mobGriefing")) ++ if (!this.field_70170_p.field_72995_K && this.func_98052_bS() && !this.field_70729_aU && net.minecraftforge.event.ForgeEventFactory.getMobGriefingEvent(this.field_70170_p, this)) + { + for (EntityItem entityitem : this.field_70170_p.func_72872_a(EntityItem.class, this.func_174813_aQ().func_72314_b(1.0D, 0.0D, 1.0D))) + { @@ -702,10 +703,22 @@ protected void func_70623_bb() diff --git a/patches/minecraft/net/minecraft/entity/ai/EntityAIBreakDoor.java.patch b/patches/minecraft/net/minecraft/entity/ai/EntityAIBreakDoor.java.patch index 913f4c88f..78f527bfd 100644 --- a/patches/minecraft/net/minecraft/entity/ai/EntityAIBreakDoor.java.patch +++ b/patches/minecraft/net/minecraft/entity/ai/EntityAIBreakDoor.java.patch @@ -5,7 +5,7 @@ return false; } - else if (!this.field_75356_a.field_70170_p.func_82736_K().func_82766_b("mobGriefing")) -+ else if (!this.field_75356_a.field_70170_p.func_82736_K().func_82766_b("mobGriefing") || !this.field_75356_a.field_70170_p.func_180495_p(this.field_179507_b).func_177230_c().canEntityDestroy(this.field_75356_a.field_70170_p.func_180495_p(this.field_179507_b), this.field_75356_a.field_70170_p, this.field_179507_b, this.field_75356_a) || !net.minecraftforge.event.ForgeEventFactory.onEntityDestroyBlock(this.field_75356_a, this.field_179507_b, this.field_75356_a.field_70170_p.func_180495_p(this.field_179507_b))) ++ else if (!net.minecraftforge.event.ForgeEventFactory.getMobGriefingEvent(this.field_75356_a.field_70170_p, this.field_75356_a) || !this.field_75356_a.field_70170_p.func_180495_p(this.field_179507_b).func_177230_c().canEntityDestroy(this.field_75356_a.field_70170_p.func_180495_p(this.field_179507_b), this.field_75356_a.field_70170_p, this.field_179507_b, this.field_75356_a) || !net.minecraftforge.event.ForgeEventFactory.onEntityDestroyBlock(this.field_75356_a, this.field_179507_b, this.field_75356_a.field_70170_p.func_180495_p(this.field_179507_b))) { return false; } diff --git a/patches/minecraft/net/minecraft/entity/ai/EntityAIEatGrass.java.patch b/patches/minecraft/net/minecraft/entity/ai/EntityAIEatGrass.java.patch new file mode 100644 index 000000000..b1f6b330d --- /dev/null +++ b/patches/minecraft/net/minecraft/entity/ai/EntityAIEatGrass.java.patch @@ -0,0 +1,20 @@ +--- ../src-base/minecraft/net/minecraft/entity/ai/EntityAIEatGrass.java ++++ ../src-work/minecraft/net/minecraft/entity/ai/EntityAIEatGrass.java +@@ -78,7 +78,7 @@ + + if (field_179505_b.apply(this.field_151501_c.func_180495_p(blockpos))) + { +- if (this.field_151501_c.func_82736_K().func_82766_b("mobGriefing")) ++ if (net.minecraftforge.event.ForgeEventFactory.getMobGriefingEvent(this.field_151501_c, this.field_151500_b)) + { + this.field_151501_c.func_175655_b(blockpos, false); + } +@@ -91,7 +91,7 @@ + + if (this.field_151501_c.func_180495_p(blockpos1).func_177230_c() == Blocks.field_150349_c) + { +- if (this.field_151501_c.func_82736_K().func_82766_b("mobGriefing")) ++ if (net.minecraftforge.event.ForgeEventFactory.getMobGriefingEvent(this.field_151501_c, this.field_151500_b)) + { + this.field_151501_c.func_175718_b(2001, blockpos1, Block.func_149682_b(Blocks.field_150349_c)); + this.field_151501_c.func_180501_a(blockpos1, Blocks.field_150346_d.func_176223_P(), 2); diff --git a/patches/minecraft/net/minecraft/entity/ai/EntityAIHarvestFarmland.java.patch b/patches/minecraft/net/minecraft/entity/ai/EntityAIHarvestFarmland.java.patch new file mode 100644 index 000000000..dc23784f5 --- /dev/null +++ b/patches/minecraft/net/minecraft/entity/ai/EntityAIHarvestFarmland.java.patch @@ -0,0 +1,11 @@ +--- ../src-base/minecraft/net/minecraft/entity/ai/EntityAIHarvestFarmland.java ++++ ../src-work/minecraft/net/minecraft/entity/ai/EntityAIHarvestFarmland.java +@@ -29,7 +29,7 @@ + { + if (this.field_179496_a <= 0) + { +- if (!this.field_179504_c.field_70170_p.func_82736_K().func_82766_b("mobGriefing")) ++ if (!net.minecraftforge.event.ForgeEventFactory.getMobGriefingEvent(this.field_179504_c.field_70170_p, this.field_179504_c)) + { + return false; + } diff --git a/patches/minecraft/net/minecraft/entity/boss/EntityDragon.java.patch b/patches/minecraft/net/minecraft/entity/boss/EntityDragon.java.patch index 1bfd4d85d..21ac11760 100644 --- a/patches/minecraft/net/minecraft/entity/boss/EntityDragon.java.patch +++ b/patches/minecraft/net/minecraft/entity/boss/EntityDragon.java.patch @@ -7,7 +7,8 @@ - if (iblockstate.func_185904_a() != Material.field_151579_a && iblockstate.func_185904_a() != Material.field_151581_o) + if (!block.isAir(iblockstate, this.field_70170_p, blockpos) && iblockstate.func_185904_a() != Material.field_151581_o) { - if (!this.field_70170_p.func_82736_K().func_82766_b("mobGriefing")) +- if (!this.field_70170_p.func_82736_K().func_82766_b("mobGriefing")) ++ if (!net.minecraftforge.event.ForgeEventFactory.getMobGriefingEvent(this.field_70170_p, this)) { flag = true; } diff --git a/patches/minecraft/net/minecraft/entity/boss/EntityWither.java.patch b/patches/minecraft/net/minecraft/entity/boss/EntityWither.java.patch index cc3778126..98bbf2a3b 100644 --- a/patches/minecraft/net/minecraft/entity/boss/EntityWither.java.patch +++ b/patches/minecraft/net/minecraft/entity/boss/EntityWither.java.patch @@ -1,5 +1,23 @@ --- ../src-base/minecraft/net/minecraft/entity/boss/EntityWither.java +++ ../src-work/minecraft/net/minecraft/entity/boss/EntityWither.java +@@ -255,7 +255,7 @@ + + if (j1 <= 0) + { +- this.field_70170_p.func_72885_a(this, this.field_70165_t, this.field_70163_u + (double)this.func_70047_e(), this.field_70161_v, 7.0F, false, this.field_70170_p.func_82736_K().func_82766_b("mobGriefing")); ++ this.field_70170_p.func_72885_a(this, this.field_70165_t, this.field_70163_u + (double)this.func_70047_e(), this.field_70161_v, 7.0F, false, net.minecraftforge.event.ForgeEventFactory.getMobGriefingEvent(this.field_70170_p, this)); + this.field_70170_p.func_175669_a(1023, new BlockPos(this), 0); + } + +@@ -362,7 +362,7 @@ + { + --this.field_82222_j; + +- if (this.field_82222_j == 0 && this.field_70170_p.func_82736_K().func_82766_b("mobGriefing")) ++ if (this.field_82222_j == 0 && net.minecraftforge.event.ForgeEventFactory.getMobGriefingEvent(this.field_70170_p, this)) + { + int i1 = MathHelper.func_76128_c(this.field_70163_u); + int l1 = MathHelper.func_76128_c(this.field_70165_t); @@ -382,7 +382,7 @@ IBlockState iblockstate = this.field_70170_p.func_180495_p(blockpos); Block block = iblockstate.func_177230_c(); diff --git a/patches/minecraft/net/minecraft/entity/monster/EntityCreeper.java.patch b/patches/minecraft/net/minecraft/entity/monster/EntityCreeper.java.patch new file mode 100644 index 000000000..260fcb2ad --- /dev/null +++ b/patches/minecraft/net/minecraft/entity/monster/EntityCreeper.java.patch @@ -0,0 +1,11 @@ +--- ../src-base/minecraft/net/minecraft/entity/monster/EntityCreeper.java ++++ ../src-work/minecraft/net/minecraft/entity/monster/EntityCreeper.java +@@ -265,7 +265,7 @@ + { + if (!this.field_70170_p.field_72995_K) + { +- boolean flag = this.field_70170_p.func_82736_K().func_82766_b("mobGriefing"); ++ boolean flag = net.minecraftforge.event.ForgeEventFactory.getMobGriefingEvent(this.field_70170_p, this); + float f = this.func_70830_n() ? 2.0F : 1.0F; + this.field_70729_aU = true; + this.field_70170_p.func_72876_a(this, this.field_70165_t, this.field_70163_u, this.field_70161_v, (float)this.field_82226_g * f, flag); diff --git a/patches/minecraft/net/minecraft/entity/monster/EntityEnderman.java.patch b/patches/minecraft/net/minecraft/entity/monster/EntityEnderman.java.patch index 934f9ab79..ce0edefef 100644 --- a/patches/minecraft/net/minecraft/entity/monster/EntityEnderman.java.patch +++ b/patches/minecraft/net/minecraft/entity/monster/EntityEnderman.java.patch @@ -30,3 +30,21 @@ public boolean func_70823_r() { return ((Boolean)this.field_70180_af.func_187225_a(field_184719_bw)).booleanValue(); +@@ -486,7 +500,7 @@ + { + return false; + } +- else if (!this.field_179475_a.field_70170_p.func_82736_K().func_82766_b("mobGriefing")) ++ else if (!net.minecraftforge.event.ForgeEventFactory.getMobGriefingEvent(this.field_179475_a.field_70170_p, this.field_179475_a)) + { + return false; + } +@@ -551,7 +565,7 @@ + { + return false; + } +- else if (!this.field_179473_a.field_70170_p.func_82736_K().func_82766_b("mobGriefing")) ++ else if (!net.minecraftforge.event.ForgeEventFactory.getMobGriefingEvent(this.field_179473_a.field_70170_p, this.field_179473_a)) + { + return false; + } diff --git a/patches/minecraft/net/minecraft/entity/monster/EntityEvoker.java.patch b/patches/minecraft/net/minecraft/entity/monster/EntityEvoker.java.patch new file mode 100644 index 000000000..f30aa0699 --- /dev/null +++ b/patches/minecraft/net/minecraft/entity/monster/EntityEvoker.java.patch @@ -0,0 +1,11 @@ +--- ../src-base/minecraft/net/minecraft/entity/monster/EntityEvoker.java ++++ ../src-work/minecraft/net/minecraft/entity/monster/EntityEvoker.java +@@ -369,7 +369,7 @@ + { + return false; + } +- else if (!EntityEvoker.this.field_70170_p.func_82736_K().func_82766_b("mobGriefing")) ++ else if (!net.minecraftforge.event.ForgeEventFactory.getMobGriefingEvent(EntityEvoker.this.field_70170_p, EntityEvoker.this)) + { + return false; + } diff --git a/patches/minecraft/net/minecraft/entity/monster/EntitySilverfish.java.patch b/patches/minecraft/net/minecraft/entity/monster/EntitySilverfish.java.patch new file mode 100644 index 000000000..a2baf2bc7 --- /dev/null +++ b/patches/minecraft/net/minecraft/entity/monster/EntitySilverfish.java.patch @@ -0,0 +1,20 @@ +--- ../src-base/minecraft/net/minecraft/entity/monster/EntitySilverfish.java ++++ ../src-work/minecraft/net/minecraft/entity/monster/EntitySilverfish.java +@@ -184,7 +184,7 @@ + { + Random random = this.field_75457_a.func_70681_au(); + +- if (this.field_75457_a.field_70170_p.func_82736_K().func_82766_b("mobGriefing") && random.nextInt(10) == 0) ++ if (net.minecraftforge.event.ForgeEventFactory.getMobGriefingEvent(this.field_75457_a.field_70170_p, this.field_75457_a) && random.nextInt(10) == 0) + { + this.field_179483_b = EnumFacing.func_176741_a(random); + BlockPos blockpos = (new BlockPos(this.field_75457_a.field_70165_t, this.field_75457_a.field_70163_u + 0.5D, this.field_75457_a.field_70161_v)).func_177972_a(this.field_179483_b); +@@ -273,7 +273,7 @@ + + if (iblockstate.func_177230_c() == Blocks.field_150418_aU) + { +- if (world.func_82736_K().func_82766_b("mobGriefing")) ++ if (net.minecraftforge.event.ForgeEventFactory.getMobGriefingEvent(world, this.field_179464_a)) + { + world.func_175655_b(blockpos1, true); + } diff --git a/patches/minecraft/net/minecraft/entity/monster/EntitySnowman.java.patch b/patches/minecraft/net/minecraft/entity/monster/EntitySnowman.java.patch index 178e0e15c..632fa3575 100644 --- a/patches/minecraft/net/minecraft/entity/monster/EntitySnowman.java.patch +++ b/patches/minecraft/net/minecraft/entity/monster/EntitySnowman.java.patch @@ -9,6 +9,15 @@ { private static final DataParameter field_184749_a = EntityDataManager.func_187226_a(EntitySnowman.class, DataSerializers.field_187191_a); +@@ -104,7 +104,7 @@ + this.func_70097_a(DamageSource.field_76370_b, 1.0F); + } + +- if (!this.field_70170_p.func_82736_K().func_82766_b("mobGriefing")) ++ if (!net.minecraftforge.event.ForgeEventFactory.getMobGriefingEvent(this.field_70170_p, this)) + { + return; + } @@ -152,7 +152,7 @@ { ItemStack itemstack = p_184645_1_.func_184586_b(p_184645_2_); diff --git a/patches/minecraft/net/minecraft/entity/passive/EntityRabbit.java.patch b/patches/minecraft/net/minecraft/entity/passive/EntityRabbit.java.patch new file mode 100644 index 000000000..8f525fd80 --- /dev/null +++ b/patches/minecraft/net/minecraft/entity/passive/EntityRabbit.java.patch @@ -0,0 +1,11 @@ +--- ../src-base/minecraft/net/minecraft/entity/passive/EntityRabbit.java ++++ ../src-work/minecraft/net/minecraft/entity/passive/EntityRabbit.java +@@ -551,7 +551,7 @@ + { + if (this.field_179496_a <= 0) + { +- if (!this.field_179500_c.field_70170_p.func_82736_K().func_82766_b("mobGriefing")) ++ if (!net.minecraftforge.event.ForgeEventFactory.getMobGriefingEvent(this.field_179500_c.field_70170_p, this.field_179500_c)) + { + return false; + } diff --git a/patches/minecraft/net/minecraft/entity/projectile/EntityLargeFireball.java.patch b/patches/minecraft/net/minecraft/entity/projectile/EntityLargeFireball.java.patch new file mode 100644 index 000000000..ec2f1066f --- /dev/null +++ b/patches/minecraft/net/minecraft/entity/projectile/EntityLargeFireball.java.patch @@ -0,0 +1,11 @@ +--- ../src-base/minecraft/net/minecraft/entity/projectile/EntityLargeFireball.java ++++ ../src-work/minecraft/net/minecraft/entity/projectile/EntityLargeFireball.java +@@ -40,7 +40,7 @@ + this.func_174815_a(this.field_70235_a, p_70227_1_.field_72308_g); + } + +- boolean flag = this.field_70170_p.func_82736_K().func_82766_b("mobGriefing"); ++ boolean flag = net.minecraftforge.event.ForgeEventFactory.getMobGriefingEvent(this.field_70170_p, this.field_70235_a); + this.field_70170_p.func_72885_a((Entity)null, this.field_70165_t, this.field_70163_u, this.field_70161_v, (float)this.field_92057_e, flag, flag); + this.func_70106_y(); + } diff --git a/patches/minecraft/net/minecraft/entity/projectile/EntitySmallFireball.java.patch b/patches/minecraft/net/minecraft/entity/projectile/EntitySmallFireball.java.patch new file mode 100644 index 000000000..4bf04d83a --- /dev/null +++ b/patches/minecraft/net/minecraft/entity/projectile/EntitySmallFireball.java.patch @@ -0,0 +1,11 @@ +--- ../src-base/minecraft/net/minecraft/entity/projectile/EntitySmallFireball.java ++++ ../src-work/minecraft/net/minecraft/entity/projectile/EntitySmallFireball.java +@@ -57,7 +57,7 @@ + + if (this.field_70235_a != null && this.field_70235_a instanceof EntityLiving) + { +- flag1 = this.field_70170_p.func_82736_K().func_82766_b("mobGriefing"); ++ flag1 = net.minecraftforge.event.ForgeEventFactory.getMobGriefingEvent(this.field_70170_p, this.field_70235_a); + } + + if (flag1) diff --git a/patches/minecraft/net/minecraft/entity/projectile/EntityWitherSkull.java.patch b/patches/minecraft/net/minecraft/entity/projectile/EntityWitherSkull.java.patch index 159e03aba..d8084774b 100644 --- a/patches/minecraft/net/minecraft/entity/projectile/EntityWitherSkull.java.patch +++ b/patches/minecraft/net/minecraft/entity/projectile/EntityWitherSkull.java.patch @@ -9,3 +9,12 @@ { f = Math.min(0.8F, f); } +@@ -115,7 +115,7 @@ + } + } + +- this.field_70170_p.func_72885_a(this, this.field_70165_t, this.field_70163_u, this.field_70161_v, 1.0F, false, this.field_70170_p.func_82736_K().func_82766_b("mobGriefing")); ++ this.field_70170_p.func_72885_a(this, this.field_70165_t, this.field_70163_u, this.field_70161_v, 1.0F, false, net.minecraftforge.event.ForgeEventFactory.getMobGriefingEvent(this.field_70170_p, this.field_70235_a)); + this.func_70106_y(); + } + } diff --git a/src/main/java/net/minecraftforge/event/ForgeEventFactory.java b/src/main/java/net/minecraftforge/event/ForgeEventFactory.java index 8f63509f9..51e636e1a 100644 --- a/src/main/java/net/minecraftforge/event/ForgeEventFactory.java +++ b/src/main/java/net/minecraftforge/event/ForgeEventFactory.java @@ -82,6 +82,7 @@ import net.minecraftforge.common.util.BlockSnapshot; import net.minecraftforge.event.brewing.PlayerBrewedPotionEvent; import net.minecraftforge.event.brewing.PotionBrewEvent; import net.minecraftforge.event.entity.EntityEvent; +import net.minecraftforge.event.entity.EntityMobGriefingEvent; import net.minecraftforge.event.entity.EntityMountEvent; import net.minecraftforge.event.entity.EntityStruckByLightningEvent; import net.minecraftforge.event.entity.PlaySoundAtEntityEvent; @@ -756,4 +757,13 @@ public class ForgeEventFactory MinecraftForge.EVENT_BUS.post(new GetCollisionBoxesEvent(world, entity, aabb, outList)); return outList.isEmpty(); } + + public static boolean getMobGriefingEvent(World world, Entity entity) + { + EntityMobGriefingEvent event = new EntityMobGriefingEvent(entity); + MinecraftForge.EVENT_BUS.post(event); + + Result result = event.getResult(); + return result == Result.DEFAULT ? world.getGameRules().getBoolean("mobGriefing") : result == Result.ALLOW; + } } diff --git a/src/main/java/net/minecraftforge/event/entity/EntityMobGriefingEvent.java b/src/main/java/net/minecraftforge/event/entity/EntityMobGriefingEvent.java new file mode 100644 index 000000000..857104242 --- /dev/null +++ b/src/main/java/net/minecraftforge/event/entity/EntityMobGriefingEvent.java @@ -0,0 +1,44 @@ +/* + * Minecraft Forge + * Copyright (c) 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; + +import net.minecraft.entity.Entity; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.fml.common.eventhandler.Event.HasResult; +import net.minecraftforge.fml.common.eventhandler.Event.Result; + +/** + * EntityMobGriefingEvent is fired when mob griefing is about to occur and allows an event listener to specify whether it should or not.
+ * This event is fired when ever the {@code mobGriefing} game rule is checked.
+ *
+ * This event has a {@link HasResult result}: + *
  • {@link Result#ALLOW} means this instance of mob griefing is allowed.
  • + *
  • {@link Result#DEFAULT} means the {@code mobGriefing} game rule is used to determine the behaviour.
  • + *
  • {@link Result#DENY} means this instance of mob griefing is not allowed.

  • + * This event is fired on the {@link MinecraftForge#EVENT_BUS}. + */ +@HasResult +public class EntityMobGriefingEvent extends EntityEvent +{ + public EntityMobGriefingEvent(Entity entity) + { + super(entity); + } +} diff --git a/src/test/java/net/minecraftforge/debug/EntityMobGriefingEventTest.java b/src/test/java/net/minecraftforge/debug/EntityMobGriefingEventTest.java new file mode 100644 index 000000000..df1a19f9b --- /dev/null +++ b/src/test/java/net/minecraftforge/debug/EntityMobGriefingEventTest.java @@ -0,0 +1,51 @@ +/* + * Minecraft Forge + * Copyright (c) 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 net.minecraftforge.event.entity.EntityMobGriefingEvent; +import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; +import net.minecraftforge.fml.common.eventhandler.Event.Result; + +@Mod(modid = "entitymobgriefingeventtest", name = "EntityMobGriefingEventTest", version = "1.0", acceptableRemoteVersions = "*") +@Mod.EventBusSubscriber +public class EntityMobGriefingEventTest +{ + private static final boolean ENABLED = false; + + @SubscribeEvent + public static void onMobGriefing(EntityMobGriefingEvent event) + { + if (ENABLED) + { + String customName = event.getEntity().getCustomNameTag(); + + try + { + Result result = Result.valueOf(customName); + event.setResult(result); + } + catch (IllegalArgumentException iae) + { + // Thrown if the name tag did not match a result value, can be ignored and DEFAULT will still be used. + } + } + } +}