[Tutorial] Removing Items From Inventory

Discussion in 'Resources' started by eMediaCrisis, Dec 29, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    eMediaCrisis

    Hello, I'm eMediaCrisis, and today I will be showing you how to manipulate player inventories.
    I'm making this tutorial because I know it was a pain to learn how to remove items
    with out having one left over, or not removing it at all.

    So, here is the code to remove an item from an inventory.
    Code:
    if (player.getItemInHand().getAmount() == 1)
    {
    player.setItemInHand(new ItemStack(Material.AIR, 1));
    event.setCancelled(true);
    }
     
    else
    {
    player.getInventory.removeItem(new ItemStack[] { // YOUR DECLARED ITEM STACK });
    }

    I find that it works really well, but if you have better code, I would like to see it too!
     
    iiHeroo likes this.
  2. Offline

    BungeeTheCookie

    eMediaCrisis
    PHP:
    @SuppressWarnings("deprecation")
    public static 
    void removeItem(Player p){
        
    ItemStack removeItem = new ItemStack(p.getItemInHand().getTypeId(), p.getItemInHand().getAmount() - 1);
        
    p.setItemInHand(removeItem);
        
    p.updateInventory();
    }
    I see this as a better solution.
     
  3. Offline

    iiHeroo


    Why use getTypeId when you can use getType?
     
  4. Offline

    BungeeTheCookie

    Free country. :p
     
  5. Offline

    iiHeroo


    Who said were all Americans? EvilSeph isn't American, nor was Dinnerbone.
     
  6. Offline

    BungeeTheCookie

    Well, there goes nothing
     
  7. Offline

    iiHeroo

Thread Status:
Not open for further replies.

Share This Page