Skip to content

Commit

Permalink
Implement preventChainExpl differently
Browse files Browse the repository at this point in the history
The least hacky solution I guess
  • Loading branch information
ljfa-ag committed May 7, 2016
1 parent f8d66ca commit cb2ec2a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/main/java/ljfa/tntutils/asm/HooksExplosion.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
35 changes: 27 additions & 8 deletions src/main/java/ljfa/tntutils/handlers/ExplosionHandler.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand All @@ -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<BlockPos>() {
@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<BlockPos>() {
@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
Expand Down

0 comments on commit cb2ec2a

Please sign in to comment.