From 0e969ebc5b4fb3dc1fda606ee76773ec70b48a3a Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Fri, 10 Mar 2017 23:10:40 +0100 Subject: [PATCH] fire block breaking events when automatically mining things --- .../mod/tile/TileEntityBreaker.java | 4 ++-- .../mod/tile/TileEntityDirectionalBreaker.java | 4 ++-- .../mod/tile/TileEntityMiner.java | 4 ++-- .../actuallyadditions/mod/util/WorldUtil.java | 15 +++++++++++++++ 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityBreaker.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityBreaker.java index bce7af8eb..de3be8165 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityBreaker.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityBreaker.java @@ -87,9 +87,9 @@ public class TileEntityBreaker extends TileEntityInventoryBase{ Block blockToBreak = stateToBreak.getBlock(); if(!this.isPlacer && blockToBreak != null && !this.worldObj.isAirBlock(coordsBlock) && !(blockToBreak instanceof BlockLiquid) && !(blockToBreak instanceof IFluidBlock) && blockToBreak.getBlockHardness(stateToBreak, this.worldObj, coordsBlock) >= 0.0F){ List drops = blockToBreak.getDrops(this.worldObj, coordsBlock, stateToBreak, 0); - float chance = ForgeEventFactory.fireBlockHarvesting(drops, this.worldObj, coordsBlock, this.worldObj.getBlockState(coordsBlock), 0, 1, false, null); + float chance = WorldUtil.fireFakeHarvestEventsForDropChance(drops, this.worldObj, coordsBlock); - if(this.worldObj.rand.nextFloat() <= chance){ + if(chance > 0 && this.worldObj.rand.nextFloat() <= chance){ if(WorldUtil.addToInventory(this, drops, false, true)){ this.worldObj.playEvent(2001, coordsBlock, Block.getStateId(stateToBreak)); this.worldObj.setBlockToAir(coordsBlock); diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityDirectionalBreaker.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityDirectionalBreaker.java index 33f0ea8ab..bbb736ac4 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityDirectionalBreaker.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityDirectionalBreaker.java @@ -86,9 +86,9 @@ public class TileEntityDirectionalBreaker extends TileEntityInventoryBase implem Block blockToBreak = this.worldObj.getBlockState(coordsBlock).getBlock(); if(blockToBreak != null && !this.worldObj.isAirBlock(coordsBlock) && blockToBreak.getBlockHardness(this.worldObj.getBlockState(coordsBlock), this.worldObj, this.pos) > -1.0F){ List drops = blockToBreak.getDrops(this.worldObj, coordsBlock, this.worldObj.getBlockState(coordsBlock), 0); - float chance = ForgeEventFactory.fireBlockHarvesting(drops, this.worldObj, coordsBlock, this.worldObj.getBlockState(coordsBlock), 0, 1, false, null); + float chance = WorldUtil.fireFakeHarvestEventsForDropChance(drops, this.worldObj, coordsBlock); - if(this.worldObj.rand.nextFloat() <= chance){ + if(chance > 0 && this.worldObj.rand.nextFloat() <= chance){ if(WorldUtil.addToInventory(this, drops, false, true)){ this.worldObj.playEvent(2001, coordsBlock, Block.getStateId(this.worldObj.getBlockState(coordsBlock))); this.worldObj.setBlockToAir(coordsBlock); diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityMiner.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityMiner.java index 4ef9bad98..3c4d0bd0a 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityMiner.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityMiner.java @@ -103,9 +103,9 @@ public class TileEntityMiner extends TileEntityInventoryBase implements ICustomE if(!block.isAir(this.worldObj.getBlockState(pos), this.worldObj, pos)){ if(block.getHarvestLevel(this.worldObj.getBlockState(pos)) <= ItemDrill.HARVEST_LEVEL && state.getBlockHardness(this.worldObj, pos) >= 0F && !(block instanceof BlockLiquid) && !(block instanceof IFluidBlock) && this.isMinable(block, meta)){ List drops = block.getDrops(this.worldObj, pos, this.worldObj.getBlockState(pos), 0); - float chance = ForgeEventFactory.fireBlockHarvesting(drops, this.worldObj, pos, this.worldObj.getBlockState(pos), 0, 1, false, null); + float chance = WorldUtil.fireFakeHarvestEventsForDropChance(drops, this.worldObj, pos); - if(this.worldObj.rand.nextFloat() <= chance){ + if(chance > 0 && this.worldObj.rand.nextFloat() <= chance){ if(WorldUtil.addToInventory(this, drops, false, true)){ this.worldObj.playEvent(2001, pos, Block.getStateId(this.worldObj.getBlockState(pos))); this.worldObj.setBlockToAir(pos); diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/util/WorldUtil.java b/src/main/java/de/ellpeck/actuallyadditions/mod/util/WorldUtil.java index 54dc63ad9..53f35b8ca 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/util/WorldUtil.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/util/WorldUtil.java @@ -41,9 +41,11 @@ import net.minecraft.world.World; import net.minecraft.world.WorldServer; import net.minecraftforge.common.ForgeHooks; import net.minecraftforge.common.IPlantable; +import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.common.util.FakePlayer; import net.minecraftforge.common.util.FakePlayerFactory; import net.minecraftforge.event.ForgeEventFactory; +import net.minecraftforge.event.world.BlockEvent; import net.minecraftforge.fluids.*; import net.minecraftforge.fluids.capability.CapabilityFluidHandler; import net.minecraftforge.fluids.capability.IFluidHandler; @@ -415,4 +417,17 @@ public final class WorldUtil{ } return false; } + + public static float fireFakeHarvestEventsForDropChance(List drops, World world, BlockPos pos){ + if(!world.isRemote && world instanceof WorldServer){ + FakePlayer fake = FakePlayerFactory.getMinecraft((WorldServer)world); + IBlockState state = world.getBlockState(pos); + + BlockEvent.BreakEvent event = new BlockEvent.BreakEvent(world, pos, state, fake); + if(!MinecraftForge.EVENT_BUS.post(event)){ + return ForgeEventFactory.fireBlockHarvesting(drops, world, pos, state, 0, 1, false, fake); + } + } + return 0F; + } }