Java Code Examples for net.minecraft.item.ItemStack#canHarvestBlock()

The following examples show how to use net.minecraft.item.ItemStack#canHarvestBlock() . 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: ToolJackHammer.java    From GregTech with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public List<BlockPos> getAOEBlocks(ItemStack itemStack, EntityPlayer player, RayTraceResult rayTraceResult) {
    if(player.isCreative()) {
        return Collections.emptyList();
    }
    ArrayList<BlockPos> result = new ArrayList<>();
    BlockPos pos = rayTraceResult.getBlockPos();
    JackHammerMode jackHammerMode = MODE_SWITCH_BEHAVIOR.getModeFromItemStack(itemStack);
    EnumFacing horizontalFacing = player.getHorizontalFacing();
    int xSizeExtend = (jackHammerMode.getHorizontalSize() - 1) / 2;
    int ySizeExtend = (jackHammerMode.getVerticalSize() - 1) / 2;
    for (int x = -xSizeExtend; x <= xSizeExtend; x++) {
        for (int y = -ySizeExtend; y <= ySizeExtend; y++) {
            //do not check center block - it's handled now
            if (x == 0 && y == 0) continue;
            BlockPos offsetPos = rotate(pos, x, y, rayTraceResult.sideHit, horizontalFacing);
            IBlockState blockState = player.world.getBlockState(offsetPos);
            if(itemStack.canHarvestBlock(blockState)) {
                result.add(offsetPos);
            }
        }
    }
    return result;
}
 
Example 2
Source File: ItemHandUpgrade.java    From Cyberware with MIT License 6 votes vote down vote up
@SubscribeEvent
public void handleMining(HarvestCheck event)
{
	EntityPlayer p = event.getEntityPlayer();
	ItemStack test = new ItemStack(this, 1, 2);
	boolean rightArm = (p.getPrimaryHand() == EnumHandSide.RIGHT ? 
			(CyberwareAPI.isCyberwareInstalled(p, new ItemStack(CyberwareContent.cyberlimbs, 1, 1))) : 
			(CyberwareAPI.isCyberwareInstalled(p, new ItemStack(CyberwareContent.cyberlimbs, 1, 0))));
	if (rightArm && CyberwareAPI.isCyberwareInstalled(p, test) && p.getHeldItemMainhand() == null)
	{
		ItemStack pick = new ItemStack(Items.STONE_PICKAXE);
		if (pick.canHarvestBlock(event.getTargetBlock()))
		{
			event.setCanHarvest(true);
		}
	}
}
 
Example 3
Source File: ToolJackHammer.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void onBlockDestroyed(ItemStack stack, World world, IBlockState state, BlockPos pos, EntityLivingBase entity) {
    if(entity instanceof EntityPlayer && !(entity instanceof FakePlayer)) {
        EntityPlayer entityPlayer = (EntityPlayer) entity;
        EnumFacing sideHit = ToolUtility.getSideHit(world, pos, entityPlayer);
        int damagePerBlockBreak = getToolDamagePerBlockBreak(stack);
        JackHammerMode jackHammerMode = MODE_SWITCH_BEHAVIOR.getModeFromItemStack(stack);
        EnumFacing horizontalFacing = entity.getHorizontalFacing();
        int xSizeExtend = (jackHammerMode.getHorizontalSize() - 1) / 2;
        int ySizeExtend = (jackHammerMode.getVerticalSize() - 1) / 2;
        for (int x = -xSizeExtend; x <= xSizeExtend; x++) {
            for (int y = -ySizeExtend; y <= ySizeExtend; y++) {
                //do not check center block - it's handled now
                if (x == 0 && y == 0) continue;
                BlockPos offsetPos = rotate(pos, x, y, sideHit, horizontalFacing);
                IBlockState blockState = world.getBlockState(offsetPos);
                if(world.isBlockModifiable(entityPlayer, offsetPos) &&
                    blockState.getBlock().canHarvestBlock(world, offsetPos, entityPlayer) &&
                    blockState.getPlayerRelativeBlockHardness(entityPlayer, world, offsetPos) > 0.0f &&
                    stack.canHarvestBlock(blockState)) {
                    GTUtility.harvestBlock(world, offsetPos, entityPlayer);
                    ((ToolMetaItem) stack.getItem()).damageItem(stack, damagePerBlockBreak, false);
                }
            }
        }
    }
}