From f0eb941abc65315b82ce5bb1e05bea0510620ea0 Mon Sep 17 00:00:00 2001 From: Silly511 Date: Fri, 7 Apr 2017 17:34:26 -0500 Subject: [PATCH] New LivingDestroyBlockEvent for controlling if some entities can destroy blocks (#3628) --- .../entity/ai/EntityAIBreakDoor.java.patch | 11 ++++ .../entity/boss/EntityDragon.java.patch | 2 +- .../entity/boss/EntityWither.java.patch | 2 +- .../projectile/EntityWitherSkull.java.patch | 2 +- .../event/ForgeEventFactory.java | 6 ++ .../living/LivingDestroyBlockEvent.java | 63 +++++++++++++++++++ 6 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 patches/minecraft/net/minecraft/entity/ai/EntityAIBreakDoor.java.patch create mode 100644 src/main/java/net/minecraftforge/event/entity/living/LivingDestroyBlockEvent.java diff --git a/patches/minecraft/net/minecraft/entity/ai/EntityAIBreakDoor.java.patch b/patches/minecraft/net/minecraft/entity/ai/EntityAIBreakDoor.java.patch new file mode 100644 index 000000000..913f4c88f --- /dev/null +++ b/patches/minecraft/net/minecraft/entity/ai/EntityAIBreakDoor.java.patch @@ -0,0 +1,11 @@ +--- ../src-base/minecraft/net/minecraft/entity/ai/EntityAIBreakDoor.java ++++ ../src-work/minecraft/net/minecraft/entity/ai/EntityAIBreakDoor.java +@@ -21,7 +21,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))) + { + 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 a35c1a828..b13691f08 100644 --- a/patches/minecraft/net/minecraft/entity/boss/EntityDragon.java.patch +++ b/patches/minecraft/net/minecraft/entity/boss/EntityDragon.java.patch @@ -12,7 +12,7 @@ flag = true; } - else if (block != Blocks.field_180401_cv && block != Blocks.field_150343_Z && block != Blocks.field_150377_bs && block != Blocks.field_150357_h && block != Blocks.field_150384_bq && block != Blocks.field_150378_br) -+ else if (block.canEntityDestroy(iblockstate, this.field_70170_p, blockpos, this)) ++ else if (block.canEntityDestroy(iblockstate, this.field_70170_p, blockpos, this) && net.minecraftforge.event.ForgeEventFactory.onEntityDestroyBlock(this, blockpos, iblockstate)) { if (block != Blocks.field_150483_bI && block != Blocks.field_185776_dc && block != Blocks.field_185777_dd && block != Blocks.field_150411_aY && block != Blocks.field_185775_db) { diff --git a/patches/minecraft/net/minecraft/entity/boss/EntityWither.java.patch b/patches/minecraft/net/minecraft/entity/boss/EntityWither.java.patch index 36fcf1853..ff0738735 100644 --- a/patches/minecraft/net/minecraft/entity/boss/EntityWither.java.patch +++ b/patches/minecraft/net/minecraft/entity/boss/EntityWither.java.patch @@ -5,7 +5,7 @@ Block block = iblockstate.func_177230_c(); - if (iblockstate.func_185904_a() != Material.field_151579_a && func_181033_a(block)) -+ if (!block.isAir(iblockstate, this.field_70170_p, blockpos) && block.canEntityDestroy(iblockstate, field_70170_p, blockpos, this)) ++ if (!block.isAir(iblockstate, this.field_70170_p, blockpos) && block.canEntityDestroy(iblockstate, field_70170_p, blockpos, this) && net.minecraftforge.event.ForgeEventFactory.onEntityDestroyBlock(this, blockpos, iblockstate)) { flag = this.field_70170_p.func_175655_b(blockpos, true) || flag; } diff --git a/patches/minecraft/net/minecraft/entity/projectile/EntityWitherSkull.java.patch b/patches/minecraft/net/minecraft/entity/projectile/EntityWitherSkull.java.patch index 7a9cc9450..159e03aba 100644 --- a/patches/minecraft/net/minecraft/entity/projectile/EntityWitherSkull.java.patch +++ b/patches/minecraft/net/minecraft/entity/projectile/EntityWitherSkull.java.patch @@ -5,7 +5,7 @@ Block block = p_180428_4_.func_177230_c(); - if (this.func_82342_d() && EntityWither.func_181033_a(block)) -+ if (this.func_82342_d() && block.canEntityDestroy(p_180428_4_, p_180428_2_, p_180428_3_, this)) ++ if (this.func_82342_d() && block.canEntityDestroy(p_180428_4_, p_180428_2_, p_180428_3_, this) && net.minecraftforge.event.ForgeEventFactory.onEntityDestroyBlock(this.field_70235_a, p_180428_3_, p_180428_4_)) { f = Math.min(0.8F, f); } diff --git a/src/main/java/net/minecraftforge/event/ForgeEventFactory.java b/src/main/java/net/minecraftforge/event/ForgeEventFactory.java index 97305c2e1..bc60d6899 100644 --- a/src/main/java/net/minecraftforge/event/ForgeEventFactory.java +++ b/src/main/java/net/minecraftforge/event/ForgeEventFactory.java @@ -78,6 +78,7 @@ import net.minecraftforge.event.entity.EntityMountEvent; import net.minecraftforge.event.entity.EntityStruckByLightningEvent; import net.minecraftforge.event.entity.PlaySoundAtEntityEvent; import net.minecraftforge.event.entity.item.ItemExpireEvent; +import net.minecraftforge.event.entity.living.LivingDestroyBlockEvent; import net.minecraftforge.event.entity.living.LivingEntityUseItemEvent; import net.minecraftforge.event.entity.living.LivingExperienceDropEvent; import net.minecraftforge.event.entity.living.LivingHealEvent; @@ -659,4 +660,9 @@ public class ForgeEventFactory net.minecraftforge.common.MinecraftForge.EVENT_BUS.post(e); return e.getLevel(); } + + public static boolean onEntityDestroyBlock(EntityLivingBase entity, BlockPos pos, IBlockState state) + { + return !MinecraftForge.EVENT_BUS.post(new LivingDestroyBlockEvent(entity, pos, state)); + } } diff --git a/src/main/java/net/minecraftforge/event/entity/living/LivingDestroyBlockEvent.java b/src/main/java/net/minecraftforge/event/entity/living/LivingDestroyBlockEvent.java new file mode 100644 index 000000000..d8cab3d13 --- /dev/null +++ b/src/main/java/net/minecraftforge/event/entity/living/LivingDestroyBlockEvent.java @@ -0,0 +1,63 @@ +/* + * Minecraft Forge + * Copyright (c) 2016. + * + * 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 net.minecraft.block.Block; +import net.minecraft.block.state.IBlockState; +import net.minecraft.entity.Entity; +import net.minecraft.entity.EntityLivingBase; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.IBlockAccess; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.fml.common.eventhandler.Cancelable; + +/** + * Fired when the ender dragon or wither attempts to destroy a block and when ever a zombie attempts to break a door. Basically a event version of {@link Block#canEntityDestroy(IBlockState, IBlockAccess, BlockPos, Entity)}
+ *
+ * This event is {@link Cancelable}.
+ * If this event is canceled, the block will not be destroyed.
+ *
+ * This event does not have a result. {@link HasResult}
+ *
+ * This event is fired on the {@link MinecraftForge#EVENT_BUS}. + **/ +@Cancelable +public class LivingDestroyBlockEvent extends LivingEvent +{ + private final BlockPos pos; + private final IBlockState state; + + public LivingDestroyBlockEvent(EntityLivingBase entity, BlockPos pos, IBlockState state) + { + super(entity); + this.pos = pos; + this.state = state; + } + + public IBlockState getState() + { + return state; + } + + public BlockPos getPos() + { + return pos; + } +}