Search in sources :

Example 1 with ToolMetaItem

use of gregtech.api.items.toolitem.ToolMetaItem in project GregTech by GregTechCE.

the class TreeChopTask method run.

@Override
public boolean run() {
    ItemStack itemInMainHand = this.player.getHeldItemMainhand();
    boolean isPlayerNear = player.world == world && currentPos.distanceSq(player.posX, currentPos.getY(), player.posZ) <= 1024;
    boolean isPlayerConnected = player.connection.netManager.isChannelOpen() && isPlayerNear;
    if (!isPlayerConnected || itemInMainHand.isEmpty() || !isItemEqual(itemInMainHand)) {
        return false;
    }
    ToolMetaItem<?> toolMetaItem = (ToolMetaItem<?>) itemStack.getItem();
    MetaToolValueItem toolValueItem = toolMetaItem.getItem(itemStack);
    if (toolValueItem == null) {
        return false;
    }
    IToolStats toolStats = toolValueItem.getToolStats();
    int damagePerBlockBreak = toolStats.getToolDamagePerBlockBreak(itemStack);
    if (!finishedSearchingBlocks) {
        this.finishedSearchingBlocks = !attemptSearchWoodBlocks() || this.visitedBlockPos.size() >= MAX_BLOCKS_TO_SEARCH;
        if (finishedSearchingBlocks) {
            this.woodBlockPos.sort(new WoodBlockComparator());
        }
        return true;
    }
    if (toolMetaItem.isUsable(itemInMainHand, damagePerBlockBreak) && tryBreakAny()) {
        toolMetaItem.damageItem(itemInMainHand, damagePerBlockBreak, false);
        return true;
    }
    return false;
}
Also used : IToolStats(gregtech.api.items.toolitem.IToolStats) ToolMetaItem(gregtech.api.items.toolitem.ToolMetaItem) MetaToolValueItem(gregtech.api.items.toolitem.ToolMetaItem.MetaToolValueItem) ItemStack(net.minecraft.item.ItemStack)

Example 2 with ToolMetaItem

use of gregtech.api.items.toolitem.ToolMetaItem in project GregTech by GregTechCE.

the class ToolSense method onBlockPreBreak.

@Override
public boolean onBlockPreBreak(ItemStack stack, BlockPos blockPos, EntityPlayer player) {
    if (player.world.isRemote || player.capabilities.isCreativeMode) {
        return false;
    }
    ToolMetaItem<?> toolMetaItem = (ToolMetaItem<?>) stack.getItem();
    int damagePerBlockBreak = getToolDamagePerBlockBreak(stack);
    for (int x = -5; x <= 5; x++) {
        for (int z = -5; z <= 5; z++) {
            for (int y = -5; y <= 5; y++) {
                BlockPos offsetPos = blockPos.add(x, y, z);
                IBlockState blockState = player.world.getBlockState(offsetPos);
                if (player.world.isBlockModifiable(player, offsetPos) && toolMetaItem.isUsable(stack, damagePerBlockBreak)) {
                    if (blockState.getBlock() instanceof BlockCrops) {
                        player.world.playEvent(2001, offsetPos, Block.getStateId(blockState));
                        ToolUtility.applyHarvestBehavior(offsetPos, player);
                        toolMetaItem.damageItem(stack, damagePerBlockBreak, false);
                    } else if (blockState.getMaterial() == Material.PLANTS || blockState.getMaterial() == Material.LEAVES || blockState.getMaterial() == Material.VINE) {
                        player.world.playEvent(2001, offsetPos, Block.getStateId(blockState));
                        player.world.setBlockToAir(offsetPos);
                        toolMetaItem.damageItem(stack, damagePerBlockBreak, false);
                    }
                }
            }
        }
    }
    return true;
}
Also used : IBlockState(net.minecraft.block.state.IBlockState) BlockCrops(net.minecraft.block.BlockCrops) ToolMetaItem(gregtech.api.items.toolitem.ToolMetaItem) BlockPos(net.minecraft.util.math.BlockPos)

Example 3 with ToolMetaItem

use of gregtech.api.items.toolitem.ToolMetaItem in project GregTech by GregTechCE.

the class ToolJackHammer method onBlockDestroyed.

@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);
                }
            }
        }
    }
}
Also used : IBlockState(net.minecraft.block.state.IBlockState) ToolMetaItem(gregtech.api.items.toolitem.ToolMetaItem) EnumFacing(net.minecraft.util.EnumFacing) EntityPlayer(net.minecraft.entity.player.EntityPlayer) BlockPos(net.minecraft.util.math.BlockPos) FakePlayer(net.minecraftforge.common.util.FakePlayer)

Aggregations

ToolMetaItem (gregtech.api.items.toolitem.ToolMetaItem)3 IBlockState (net.minecraft.block.state.IBlockState)2 BlockPos (net.minecraft.util.math.BlockPos)2 IToolStats (gregtech.api.items.toolitem.IToolStats)1 MetaToolValueItem (gregtech.api.items.toolitem.ToolMetaItem.MetaToolValueItem)1 BlockCrops (net.minecraft.block.BlockCrops)1 EntityPlayer (net.minecraft.entity.player.EntityPlayer)1 ItemStack (net.minecraft.item.ItemStack)1 EnumFacing (net.minecraft.util.EnumFacing)1 FakePlayer (net.minecraftforge.common.util.FakePlayer)1