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

The following examples show how to use net.minecraft.item.ItemStack#onItemUse() . 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: ItemNullifier.java    From enderutilities with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public EnumActionResult onItemUse(EntityPlayer player, World world, BlockPos pos,
        EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ)
{
    ItemStack stack = player.getHeldItem(hand);
    ItemStack useStack = this.getItemForUse(stack, player);

    if (useStack.isEmpty() == false && world.isBlockModifiable(player, pos.offset(facing)))
    {
        EntityUtils.setHeldItemWithoutEquipSound(player, hand, useStack);
        EnumActionResult result = useStack.onItemUse(player, world, pos, hand, facing, hitX, hitY, hitZ);

        if (world.isRemote == false && player.capabilities.isCreativeMode == false && useStack.isEmpty() == false)
        {
            tryInsertItemsToNullifier(useStack, stack, player);
        }

        EntityUtils.setHeldItemWithoutEquipSound(player, hand, stack);

        return result;
    }

    return super.onItemUse(player, world, pos, hand, facing, hitX, hitY, hitZ);
}
 
Example 2
Source File: ItemEnderTool.java    From enderutilities with GNU Lesser General Public License v3.0 5 votes vote down vote up
private boolean plantItemFromInventorySlot(World world, EntityPlayer player, EnumHand hand,
        IItemHandler inv, int slot, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ)
{
    boolean ret = false;
    ItemStack plantStack = inv.getStackInSlot(slot);

    if (plantStack.isEmpty() == false && plantStack.getItem() instanceof IPlantable)
    {
        plantStack = inv.extractItem(slot, 1, false);

        if (plantStack.isEmpty())
        {
            return false;
        }

        ItemStack stackHand = player.getHeldItem(hand);
        EntityUtils.setHeldItemWithoutEquipSound(player, hand, plantStack);

        if (plantStack.onItemUse(player, world, pos, hand, side, hitX, hitY, hitZ) == EnumActionResult.SUCCESS)
        {
            ret = true;
        }

        EntityUtils.setHeldItemWithoutEquipSound(player, hand, stackHand);

        if (plantStack.isEmpty() == false)
        {
            plantStack = InventoryUtils.tryInsertItemStackToInventory(inv, plantStack);

            if (plantStack.isEmpty() == false)
            {
                player.dropItem(plantStack, false, true);
            }
        }

        player.inventoryContainer.detectAndSendChanges();
    }

    return ret;
}
 
Example 3
Source File: ItemEnderTool.java    From enderutilities with GNU Lesser General Public License v3.0 4 votes vote down vote up
private EnumActionResult placeBlock(ItemStack stack, EntityPlayer playerIn, EnumHand hand,
        World worldIn, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ)
{
    // Items in the off-hand, let vanilla handle that case
    if (playerIn.getHeldItemOffhand().isEmpty() == false)
    {
        return EnumActionResult.PASS;
    }

    int origSlot = playerIn.inventory.currentItem;
    int slot = (origSlot >= InventoryPlayer.getHotbarSize() - 1 ? 0 : origSlot + 1);
    ItemStack targetStack = playerIn.inventory.getStackInSlot(slot);

    // If the tool is in the first slot of the hotbar and there is no ItemBlock in the second slot, we fall back to the last slot
    if (origSlot == 0 && (targetStack.isEmpty() || (targetStack.getItem() instanceof ItemBlock) == false))
    {
        slot = InventoryPlayer.getHotbarSize() - 1;
        targetStack = playerIn.inventory.getStackInSlot(slot);
    }

    // If the target stack is an ItemBlock, we try to place that in the world
    if (targetStack.isEmpty() == false && targetStack.getItem() instanceof ItemBlock)
    {
        // Check if we can place the block
        if (BlockUtils.checkCanPlaceBlockAt(worldIn, pos, side, ((ItemBlock) targetStack.getItem()).getBlock()))
        {
            ItemStack stackTool = playerIn.getHeldItem(hand);
            playerIn.inventory.setInventorySlotContents(slot, ItemStack.EMPTY);
            EntityUtils.setHeldItemWithoutEquipSound(playerIn, hand, targetStack);
            int sizeOrig = targetStack.getCount();
            EnumActionResult result = EnumActionResult.PASS;

            try
            {
                result = targetStack.onItemUse(playerIn, worldIn, pos, hand, side, hitX, hitY, hitZ);
            }
            catch (Exception e) {}

            EntityUtils.setHeldItemWithoutEquipSound(playerIn, hand, stackTool);

            if (playerIn.capabilities.isCreativeMode)
            {
                targetStack.setCount(sizeOrig);
            }

            // Return the items to their original slot
            playerIn.inventory.setInventorySlotContents(slot, targetStack.isEmpty() ? ItemStack.EMPTY : targetStack);
            playerIn.inventoryContainer.detectAndSendChanges();

            return result;
        }
    }

    return EnumActionResult.PASS;
}