Post break event when automatically mining things

Closes #614
This commit is contained in:
Ellpeck 2017-02-04 16:07:58 +01:00
parent 245f357dfe
commit 4130e0bdf9
4 changed files with 21 additions and 9 deletions

View file

@ -20,7 +20,6 @@ import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.event.ForgeEventFactory;
import net.minecraftforge.fluids.IFluidBlock;
import java.util.List;
@ -87,9 +86,9 @@ public class TileEntityBreaker extends TileEntityInventoryBase{
Block blockToBreak = stateToBreak.getBlock();
if(!this.isPlacer && blockToBreak != null && !this.world.isAirBlock(coordsBlock) && !(blockToBreak instanceof BlockLiquid) && !(blockToBreak instanceof IFluidBlock) && blockToBreak.getBlockHardness(stateToBreak, this.world, coordsBlock) >= 0.0F){
List<ItemStack> drops = blockToBreak.getDrops(this.world, coordsBlock, stateToBreak, 0);
float chance = ForgeEventFactory.fireBlockHarvesting(drops, this.world, coordsBlock, this.world.getBlockState(coordsBlock), 0, 1, false, null);
float chance = WorldUtil.fireFakeHarvestEventsForDropChance(drops, this.world, coordsBlock);
if(this.world.rand.nextFloat() <= chance){
if(chance > 0 && this.world.rand.nextFloat() <= chance){
if(WorldUtil.addToInventory(this.slots, drops, false)){
this.world.playEvent(2001, coordsBlock, Block.getStateId(stateToBreak));
this.world.setBlockToAir(coordsBlock);

View file

@ -18,7 +18,6 @@ import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.energy.IEnergyStorage;
import net.minecraftforge.event.ForgeEventFactory;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
@ -86,9 +85,9 @@ public class TileEntityDirectionalBreaker extends TileEntityInventoryBase{
Block blockToBreak = this.world.getBlockState(coordsBlock).getBlock();
if(blockToBreak != null && !this.world.isAirBlock(coordsBlock) && blockToBreak.getBlockHardness(this.world.getBlockState(coordsBlock), this.world, this.pos) > -1.0F){
List<ItemStack> drops = blockToBreak.getDrops(this.world, coordsBlock, this.world.getBlockState(coordsBlock), 0);
float chance = ForgeEventFactory.fireBlockHarvesting(drops, this.world, coordsBlock, this.world.getBlockState(coordsBlock), 0, 1, false, null);
float chance = WorldUtil.fireFakeHarvestEventsForDropChance(drops, this.world, coordsBlock);
if(this.world.rand.nextFloat() <= chance){
if(chance > 0 && this.world.rand.nextFloat() <= chance){
if(WorldUtil.addToInventory(this.slots, drops, false)){
this.world.playEvent(2001, coordsBlock, Block.getStateId(this.world.getBlockState(coordsBlock)));
this.world.setBlockToAir(coordsBlock);

View file

@ -25,7 +25,6 @@ import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.energy.IEnergyStorage;
import net.minecraftforge.event.ForgeEventFactory;
import net.minecraftforge.fluids.IFluidBlock;
import net.minecraftforge.oredict.OreDictionary;
@ -126,9 +125,9 @@ public class TileEntityMiner extends TileEntityInventoryBase implements IButtonR
if(!block.isAir(this.world.getBlockState(pos), this.world, pos)){
if(block.getHarvestLevel(this.world.getBlockState(pos)) <= ItemDrill.HARVEST_LEVEL && state.getBlockHardness(this.world, pos) >= 0F && !(block instanceof BlockLiquid) && !(block instanceof IFluidBlock) && this.isMinable(block, meta)){
List<ItemStack> drops = block.getDrops(this.world, pos, this.world.getBlockState(pos), 0);
float chance = ForgeEventFactory.fireBlockHarvesting(drops, this.world, pos, this.world.getBlockState(pos), 0, 1, false, null);
float chance = WorldUtil.fireFakeHarvestEventsForDropChance(drops, this.world, pos);
if(this.world.rand.nextFloat() <= chance){
if(chance > 0 && this.world.rand.nextFloat() <= chance){
if(WorldUtil.addToInventory(this.slots, drops, false)){
this.world.playEvent(2001, pos, Block.getStateId(this.world.getBlockState(pos)));
this.world.setBlockToAir(pos);

View file

@ -40,11 +40,13 @@ 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.energy.CapabilityEnergy;
import net.minecraftforge.energy.IEnergyStorage;
import net.minecraftforge.event.ForgeEventFactory;
import net.minecraftforge.event.world.BlockEvent;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandler;
@ -450,4 +452,17 @@ public final class WorldUtil{
}
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;
}
}