Java Code Examples for net.minecraftforge.common.ForgeHooks#onBlockBreakEvent()

The following examples show how to use net.minecraftforge.common.ForgeHooks#onBlockBreakEvent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BlockUtils.java    From enderutilities with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Breaks the block as a player, and thus drops the item(s) from it
 */
public static void breakBlockAsPlayer(World world, BlockPos pos, EntityPlayerMP playerMP, ItemStack toolStack)
{
    PlayerInteractionManager manager = playerMP.interactionManager;
    int exp = ForgeHooks.onBlockBreakEvent(world, manager.getGameType(), playerMP, pos);

    if (exp != -1)
    {
        IBlockState stateExisting = world.getBlockState(pos);
        Block blockExisting = stateExisting.getBlock();

        blockExisting.onBlockHarvested(world, pos, stateExisting, playerMP);
        boolean harvest = blockExisting.removedByPlayer(stateExisting, world, pos, playerMP, true);

        if (harvest)
        {
            blockExisting.onPlayerDestroy(world, pos, stateExisting);
            blockExisting.harvestBlock(world, playerMP, pos, stateExisting, world.getTileEntity(pos), toolStack);
        }
    }
}
 
Example 2
Source File: FakePlayerItemInWorldManager.java    From PneumaticCraft with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Attempts to harvest a block at the given coordinate
 */
@Override
public boolean tryHarvestBlock(int x, int y, int z){
    BlockEvent.BreakEvent event = ForgeHooks.onBlockBreakEvent(theWorld, getGameType(), thisPlayerMP, x, y, z);
    if(event.isCanceled()) {
        return false;
    } else {
        ItemStack stack = thisPlayerMP.getCurrentEquippedItem();
        if(stack != null && stack.getItem().onBlockStartBreak(stack, x, y, z, thisPlayerMP)) {
            return false;
        }
        Block block = theWorld.getBlock(x, y, z);
        int l = theWorld.getBlockMetadata(x, y, z);
        theWorld.playAuxSFXAtEntity(thisPlayerMP, 2001, x, y, z, Block.getIdFromBlock(block) + (theWorld.getBlockMetadata(x, y, z) << 12));
        boolean flag = false;

        ItemStack itemstack = thisPlayerMP.getCurrentEquippedItem();

        if(itemstack != null) {
            itemstack.func_150999_a(theWorld, block, x, y, z, thisPlayerMP);

            if(itemstack.stackSize == 0) {
                thisPlayerMP.destroyCurrentEquippedItem();
            }
        }

        if(removeBlock(x, y, z)) {
            block.harvestBlock(theWorld, thisPlayerMP, x, y, z, l);
            flag = true;
        }

        // Drop experience
        if(!isCreative() && flag && event != null) {
            block.dropXpOnBlockBreak(theWorld, x, y, z, event.getExpToDrop());
        }
        drone.addAir(null, -PneumaticValues.DRONE_USAGE_DIG);
        return true;
    }
}