fire block breaking events when automatically mining things

This commit is contained in:
Ellpeck 2017-03-10 23:10:40 +01:00
parent 770e02c65a
commit 0e969ebc5b
4 changed files with 21 additions and 6 deletions

View file

@ -87,9 +87,9 @@ public class TileEntityBreaker extends TileEntityInventoryBase{
Block blockToBreak = stateToBreak.getBlock(); 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){ if(!this.isPlacer && blockToBreak != null && !this.worldObj.isAirBlock(coordsBlock) && !(blockToBreak instanceof BlockLiquid) && !(blockToBreak instanceof IFluidBlock) && blockToBreak.getBlockHardness(stateToBreak, this.worldObj, coordsBlock) >= 0.0F){
List<ItemStack> drops = blockToBreak.getDrops(this.worldObj, coordsBlock, stateToBreak, 0); List<ItemStack> 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)){ if(WorldUtil.addToInventory(this, drops, false, true)){
this.worldObj.playEvent(2001, coordsBlock, Block.getStateId(stateToBreak)); this.worldObj.playEvent(2001, coordsBlock, Block.getStateId(stateToBreak));
this.worldObj.setBlockToAir(coordsBlock); this.worldObj.setBlockToAir(coordsBlock);

View file

@ -86,9 +86,9 @@ public class TileEntityDirectionalBreaker extends TileEntityInventoryBase implem
Block blockToBreak = this.worldObj.getBlockState(coordsBlock).getBlock(); 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){ if(blockToBreak != null && !this.worldObj.isAirBlock(coordsBlock) && blockToBreak.getBlockHardness(this.worldObj.getBlockState(coordsBlock), this.worldObj, this.pos) > -1.0F){
List<ItemStack> drops = blockToBreak.getDrops(this.worldObj, coordsBlock, this.worldObj.getBlockState(coordsBlock), 0); List<ItemStack> 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)){ if(WorldUtil.addToInventory(this, drops, false, true)){
this.worldObj.playEvent(2001, coordsBlock, Block.getStateId(this.worldObj.getBlockState(coordsBlock))); this.worldObj.playEvent(2001, coordsBlock, Block.getStateId(this.worldObj.getBlockState(coordsBlock)));
this.worldObj.setBlockToAir(coordsBlock); this.worldObj.setBlockToAir(coordsBlock);

View file

@ -103,9 +103,9 @@ public class TileEntityMiner extends TileEntityInventoryBase implements ICustomE
if(!block.isAir(this.worldObj.getBlockState(pos), this.worldObj, pos)){ 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)){ 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<ItemStack> drops = block.getDrops(this.worldObj, pos, this.worldObj.getBlockState(pos), 0); List<ItemStack> 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)){ if(WorldUtil.addToInventory(this, drops, false, true)){
this.worldObj.playEvent(2001, pos, Block.getStateId(this.worldObj.getBlockState(pos))); this.worldObj.playEvent(2001, pos, Block.getStateId(this.worldObj.getBlockState(pos)));
this.worldObj.setBlockToAir(pos); this.worldObj.setBlockToAir(pos);

View file

@ -41,9 +41,11 @@ import net.minecraft.world.World;
import net.minecraft.world.WorldServer; import net.minecraft.world.WorldServer;
import net.minecraftforge.common.ForgeHooks; import net.minecraftforge.common.ForgeHooks;
import net.minecraftforge.common.IPlantable; import net.minecraftforge.common.IPlantable;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.util.FakePlayer; import net.minecraftforge.common.util.FakePlayer;
import net.minecraftforge.common.util.FakePlayerFactory; import net.minecraftforge.common.util.FakePlayerFactory;
import net.minecraftforge.event.ForgeEventFactory; import net.minecraftforge.event.ForgeEventFactory;
import net.minecraftforge.event.world.BlockEvent;
import net.minecraftforge.fluids.*; import net.minecraftforge.fluids.*;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler; import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandler; import net.minecraftforge.fluids.capability.IFluidHandler;
@ -415,4 +417,17 @@ public final class WorldUtil{
} }
return false; return false;
} }
public static float fireFakeHarvestEventsForDropChance(List<ItemStack> 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;
}
} }