From cb2ec2a77bf3fc0df71e033aad0af6b92e17b444 Mon Sep 17 00:00:00 2001 From: ljfa-ag Date: Sat, 7 May 2016 12:14:34 +0200 Subject: [PATCH] Implement preventChainExpl differently The least hacky solution I guess --- .../ljfa/tntutils/asm/HooksExplosion.java | 2 +- .../tntutils/handlers/ExplosionHandler.java | 35 ++++++++++++++----- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/main/java/ljfa/tntutils/asm/HooksExplosion.java b/src/main/java/ljfa/tntutils/asm/HooksExplosion.java index bbddbcd..7233b6f 100644 --- a/src/main/java/ljfa/tntutils/asm/HooksExplosion.java +++ b/src/main/java/ljfa/tntutils/asm/HooksExplosion.java @@ -6,7 +6,7 @@ public class HooksExplosion { public static final float alwaysDropThreshold = 10.0f; - public static float getDropChance(Explosion expl) throws ReflectiveOperationException { + public static float getDropChance(Explosion expl) { float baseChance = 1.0f / expl.explosionSize; if(Config.dropChanceModifier == 0.0f) return baseChance; diff --git a/src/main/java/ljfa/tntutils/handlers/ExplosionHandler.java b/src/main/java/ljfa/tntutils/handlers/ExplosionHandler.java index 45eece5..aeb1395 100644 --- a/src/main/java/ljfa/tntutils/handlers/ExplosionHandler.java +++ b/src/main/java/ljfa/tntutils/handlers/ExplosionHandler.java @@ -1,8 +1,10 @@ package ljfa.tntutils.handlers; import ljfa.tntutils.Config; +import ljfa.tntutils.asm.HooksExplosion; import ljfa.tntutils.util.ListHelper; import ljfa.tntutils.util.Predicate; +import net.minecraft.block.BlockTNT; import net.minecraft.block.state.IBlockState; import net.minecraft.entity.Entity; import net.minecraft.entity.EntityLivingBase; @@ -11,6 +13,7 @@ import net.minecraft.entity.monster.EntityCreeper; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; import net.minecraftforge.event.world.ExplosionEvent; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; @@ -25,19 +28,35 @@ public void onExplosionStart(ExplosionEvent.Start event) { @SubscribeEvent public void onExplosionDetonate(final ExplosionEvent.Detonate event) { - if(event.getWorld().isRemote) + final World world = event.getWorld(); + if(world.isRemote) return; + //Block damage if(Config.disableBlockDamage || (Config.disableCreeperBlockDamage && event.getExplosion().exploder instanceof EntityCreeper)) event.getAffectedBlocks().clear(); - else if(Config.spareTileEntities || Config.blacklistActive) { - //Remove blacklisted blocks and tile entities (if configured) from the list - ListHelper.removeIf(event.getAffectedBlocks(), new Predicate() { - @Override - public boolean test(BlockPos pos) { - return shouldBePreserved(event.getWorld().getBlockState(pos)); + else { + if(Config.spareTileEntities || Config.blacklistActive) { + //Remove blacklisted blocks and tile entities (if configured) from the list + ListHelper.removeIf(event.getAffectedBlocks(), new Predicate() { + @Override + public boolean test(BlockPos pos) { + return shouldBePreserved(world.getBlockState(pos)); + } + }); + } + + if(Config.preventChainExpl) { + //Manually remove TNT blocks from the world before the actual explosion destroys them + //(which would cause a chain explosion) + for(BlockPos pos: event.getAffectedBlocks()) { + IBlockState state = world.getBlockState(pos); + if(state.getBlock() instanceof BlockTNT) { + state.getBlock().dropBlockAsItemWithChance(world, pos, state, HooksExplosion.getDropChance(event.getExplosion()), 0); + world.setBlockToAir(pos); + } } - }); + } } //Entity damage