List of usage examples for net.minecraftforge.event ForgeEventFactory fireBlockHarvesting
public static float fireBlockHarvesting(NonNullList<ItemStack> drops, World world, BlockPos pos, BlockState state, int fortune, float dropChance, boolean silkTouch, PlayerEntity player)
From source file:blusunrize.immersiveengineering.common.blocks.metal.TileEntityExcavator.java
ItemStack digBlock(BlockPos pos) {
if (!(world instanceof WorldServer))
return ItemStack.EMPTY;
FakePlayer fakePlayer = FakePlayerUtil.getFakePlayer(world);
IBlockState blockstate = world.getBlockState(pos);
Block block = blockstate.getBlock();
if (block != null && !world.isAirBlock(pos)
&& blockstate.getPlayerRelativeBlockHardness(fakePlayer, world, pos) != 0) {
if (!block.canHarvestBlock(world, pos, fakePlayer))
return ItemStack.EMPTY;
block.onBlockHarvested(world, pos, blockstate, fakePlayer);
if (block.removedByPlayer(blockstate, world, pos, fakePlayer, true)) {
block.onPlayerDestroy(world, pos, blockstate);
if (block.canSilkHarvest(world, pos, blockstate, fakePlayer)) {
ArrayList<ItemStack> items = new ArrayList<ItemStack>();
Item bitem = Item.getItemFromBlock(block);
if (bitem == Items.AIR)
return ItemStack.EMPTY;
ItemStack itemstack = new ItemStack(bitem, 1, block.getMetaFromState(blockstate));
if (!itemstack.isEmpty())
items.add(itemstack);
ForgeEventFactory.fireBlockHarvesting(items, world, pos, blockstate, 0, 1.0f, true, fakePlayer);
for (int i = 0; i < items.size(); i++)
if (i != 0) {
EntityItem ei = new EntityItem(world, pos.getX() + .5, pos.getY() + .5, pos.getZ() + .5,
items.get(i).copy());
this.world.spawnEntity(ei);
}//from www . j ava 2 s. c o m
world.playEvent(2001, pos, Block.getStateId(blockstate));
if (items.size() > 0)
return items.get(0);
} else {
block.harvestBlock(world, fakePlayer, pos, blockstate, world.getTileEntity(pos),
ItemStack.EMPTY);
world.playEvent(2001, pos, Block.getStateId(blockstate));
}
}
}
return ItemStack.EMPTY;
}
From source file:buildcraft.lib.misc.BlockUtil.java
License:Minecraft Mod Public
public static NonNullList<ItemStack> getItemStackFromBlock(WorldServer world, BlockPos pos, GameProfile owner) { IBlockState state = world.getBlockState(pos); Block block = state.getBlock();/*w w w . j a v a 2 s.co m*/ if (block == null || block.isAir(state, world, pos)) { return null; } List<ItemStack> dropsList = block.getDrops(world, pos, state, 0); EntityPlayer fakePlayer = FakePlayerUtil.INSTANCE.getFakePlayer(world, pos, owner); float dropChance = ForgeEventFactory.fireBlockHarvesting(dropsList, world, pos, state, 0, 1.0F, false, fakePlayer); NonNullList<ItemStack> returnList = NonNullList.create(); for (ItemStack s : dropsList) { if (world.rand.nextFloat() <= dropChance) { returnList.add(s); } } return returnList; }
From source file:com.buuz135.industrial.proxy.event.CocoaBeanRightClickHarvesting.java
License:Open Source License
@SubscribeEvent public static void onPlayerRightClick(PlayerInteractEvent.RightClickBlock event) { if (!BlockRegistry.plantInteractorBlock.isRightClickCocoBeansEnabled()) return;//from ww w . j av a2 s .co m if (event.getWorld().isRemote) return; if (event.getHand() != EnumHand.MAIN_HAND) return; if (event.getEntityPlayer() == null) return; IBlockState state = event.getWorld().getBlockState(event.getPos()); if (state.getBlock() instanceof BlockCocoa && state.getValue(BlockCocoa.AGE) == 2) { ItemStack main = event.getEntityPlayer().getHeldItemMainhand(); int fortune = EnchantmentHelper.getEnchantmentLevel(Enchantments.FORTUNE, main); NonNullList<ItemStack> drops = NonNullList.create(); state.getBlock().getDrops(drops, event.getWorld(), event.getPos(), state, fortune); if (drops.size() > 0) { drops.get(0).shrink(1); } ForgeEventFactory.fireBlockHarvesting(drops, event.getWorld(), event.getPos(), state, fortune, 1f, false, event.getEntityPlayer()); event.getWorld().setBlockState(event.getPos(), state.withProperty(BlockCocoa.AGE, 0)); for (ItemStack stack : drops) ItemHandlerHelper.giveItemToPlayer(event.getEntityPlayer(), stack); } }
From source file:de.ellpeck.actuallyadditions.mod.tile.TileEntityBreaker.java
private void doWork() { IBlockState state = this.world.getBlockState(this.pos); EnumFacing sideToManipulate = WorldUtil .getDirectionByPistonRotation(state.getBlock().getMetaFromState(state)); BlockPos coordsBlock = this.pos.offset(sideToManipulate); IBlockState stateToBreak = this.world.getBlockState(coordsBlock); 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); if (this.world.rand.nextFloat() <= chance) { if (WorldUtil.addToInventory(this.slots, drops, false)) { this.world.playEvent(2001, coordsBlock, Block.getStateId(stateToBreak)); this.world.setBlockToAir(coordsBlock); WorldUtil.addToInventory(this.slots, drops, true); this.markDirty(); }// w w w . j a va 2s.c o m } } else if (this.isPlacer) { int theSlot = WorldUtil.findFirstFilledSlot(this.slots); this.slots.setStackInSlot(theSlot, WorldUtil.useItemAtSide(sideToManipulate, this.world, this.pos, this.slots.getStackInSlot(theSlot))); if (!StackUtil.isValid(this.slots.getStackInSlot(theSlot))) { this.slots.setStackInSlot(theSlot, StackUtil.getNull()); } } }
From source file:de.ellpeck.actuallyadditions.mod.tile.TileEntityDirectionalBreaker.java
private void doWork() { if (this.storage.getEnergyStored() >= ENERGY_USE * RANGE) { IBlockState state = this.world.getBlockState(this.pos); EnumFacing sideToManipulate = WorldUtil .getDirectionByPistonRotation(state.getBlock().getMetaFromState(state)); for (int i = 0; i < RANGE; i++) { BlockPos coordsBlock = this.pos.offset(sideToManipulate, i + 1); 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); if (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); WorldUtil.addToInventory(this.slots, drops, true); this.storage.extractEnergyInternal(ENERGY_USE, false); this.markDirty(); }//from www .j av a 2s . com } } } } }
From source file:de.ellpeck.actuallyadditions.mod.tile.TileEntityMiner.java
private boolean mine() { int actualUse = ENERGY_USE_PER_BLOCK * (this.onlyMineOres ? 3 : 1); if (this.storage.getEnergyStored() >= actualUse) { BlockPos pos = new BlockPos(this.pos.getX() + this.checkX, this.checkY, this.pos.getZ() + this.checkZ); IBlockState state = this.world.getBlockState(pos); Block block = state.getBlock();/* w w w .ja va 2 s .c om*/ int meta = block.getMetaFromState(state); 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); if (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); WorldUtil.addToInventory(this.slots, drops, true); this.markDirty(); this.storage.extractEnergyInternal(actualUse, false); this.shootParticles(pos.getX(), pos.getY(), pos.getZ()); } else { return false; } } } } return true; } return false; }
From source file:mod.rankshank.arbitraria.common.entity.EntityMobileBlock.java
public void destroyBlock(boolean doDrops) { world.playEvent(2001, new BlockPos(this), Block.getStateId(state)); if (doDrops) { BlockPos pos = new BlockPos(this); List<ItemStack> items = block.getDrops(world, pos, state, 0); if (items.isEmpty()) Block.spawnAsEntity(world, pos, new ItemStack(block, 1, block.getMetaFromState(state))); else {//ww w.j av a 2 s . co m float chance = ForgeEventFactory.fireBlockHarvesting(items, world, pos, state, 0, 1, false, null); items.stream().filter(item -> rand.nextFloat() <= chance) .forEach(item -> Block.spawnAsEntity(world, pos, item)); } } setDead(); }