Java Code Examples for net.minecraft.block.Block#removedByPlayer()

The following examples show how to use net.minecraft.block.Block#removedByPlayer() . 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 Wizardry with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Tries breaking a block safely and fires an event for it.
 *
 * @return Whether the block was successfully broken
 */
public static boolean breakBlock(@Nonnull World world, @Nonnull BlockPos pos, @Nullable IBlockState oldState, @Nonnull EntityPlayerMP player) {
	if (!world.isBlockLoaded(pos)) return false;

	if (!(player instanceof FakePlayer) && !hasEditPermission(pos, player)) return false;

	if (oldState == null) oldState = world.getBlockState(pos);

	BlockEvent.BreakEvent event = new BlockEvent.BreakEvent(world, pos, oldState, player);
	MinecraftForge.EVENT_BUS.post(event);

	if (event.isCanceled()) return false;

	TileEntity tile = world.getTileEntity(pos);
	Block block = oldState.getBlock();
	if (block.removedByPlayer(oldState, world, pos, player, true)) {
		block.onPlayerDestroy(world, pos, oldState);
		block.harvestBlock(world, player, pos, oldState, tile, player.getHeldItemMainhand());
		world.notifyBlockUpdate(pos, oldState, Blocks.AIR.getDefaultState(), 3);
	} else return false;

	return true;
}
 
Example 2
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 3
Source File: DiamondTofuToolHandler.java    From TofuCraftReload with MIT License 5 votes vote down vote up
private boolean canBreakExtraBlock(ItemStack stack, World world, EntityPlayer player, BlockPos pos, BlockPos refPos) {
    if (world.isAirBlock(pos)) {
        return false;
    }
    IBlockState state = world.getBlockState(pos);
    Block block = state.getBlock();
    if (tool.getDestroySpeed(stack, state) <= 1.0F)
        return false;

    IBlockState refState = world.getBlockState(refPos);
    float refStrength = ForgeHooks.blockStrength(refState, player, world, refPos);
    float strength = ForgeHooks.blockStrength(state, player, world, pos);

    if (!ForgeHooks.canHarvestBlock(block, player, world, pos) || refStrength / strength > 10f) {
        return false;
    }

    if (player.capabilities.isCreativeMode) {
        block.onBlockHarvested(world, pos, state, player);
        if (block.removedByPlayer(state, world, pos, player, false)) {
            block.onBlockDestroyedByPlayer(world, pos, state);
        }
        // send update to client
        if (!world.isRemote) {
            if (player instanceof EntityPlayerMP && ((EntityPlayerMP) player).connection != null) {
                ((EntityPlayerMP) player).connection.sendPacket(new SPacketBlockChange(world, pos));
            }
        }
        return false;
    }
    return true;
}
 
Example 4
Source File: FakePlayerItemInWorldManager.java    From PneumaticCraft with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Removes a block and triggers the appropriate events
 */
private boolean removeBlock(int par1, int par2, int par3){
    Block block = theWorld.getBlock(par1, par2, par3);
    int l = theWorld.getBlockMetadata(par1, par2, par3);
    block.onBlockHarvested(theWorld, par1, par2, par3, l, thisPlayerMP);
    boolean flag = block != null && block.removedByPlayer(theWorld, thisPlayerMP, par1, par2, par3);

    if(flag) {
        block.onBlockDestroyedByPlayer(theWorld, par1, par2, par3, l);
    }

    return flag;
}
 
Example 5
Source File: BreakBlockAction.java    From OpenModsLib with MIT License 4 votes vote down vote up
private boolean removeBlock(EntityPlayer player, BlockPos pos, IBlockState state, boolean canHarvest) {
	final Block block = state.getBlock();
	final boolean result = block.removedByPlayer(state, worldObj, pos, player, canHarvest);
	if (result) block.onBlockDestroyedByPlayer(worldObj, pos, state);
	return result;
}