Solved Help with removing items

Discussion in 'Plugin Development' started by Dippoakabob, Nov 18, 2012.

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

    Dippoakabob

    I should know how to do this but I can't seem to remember how to get and remove and item from a players inventory. I want it to cost one chest to use the command (because it places one) but I have only figured out how to remove the entire stack of chests. Please help!
     
  2. Offline

    zack6849

    Code:
    ItemStack i = new ItemStack(Material.CHEST, 1);
    target.getInventory().remove(i);
    
     
  3. Offline

    Dippoakabob

    I tried that and It seemed to only remove stacks of one, if there was 2 (or more) in a stack it would have no effect.
    But I'll give it another go,
    Thanks.

    Yeah, I just tried and it will only remove it if there is only one item in the stack. Any other suggestions?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  4. Offline

    zack6849

    hm, no idea.
     
  5. Offline

    drtshock

    Check the player's inventory and run a while statement removing all itemstacks of chests as long as it's greater than 0
     
  6. Offline

    Dippoakabob

    If i needed to do that all I would have to do is
    Code:
    Target.getInventory().remove(Material.CHEST);
    But I don't want to remove all the chests. I need to remove one from a player's inventory. The issue with the first method was that if the player had a stack of 3 chests and I tried to remove the ItemStack of a single chest it would have no effect on the stack of three.
     
  7. Offline

    CeramicTitan

    player.getInventory().remove(new ItemStack(Material.CHEST, 1));
     
  8. @Dippoakabob

    Its really easy! here ya go:

    Code:
    inventory.remove(Material.CHEST);
    // Its the easy way to do it :)
    Also you will need to add:
    PlayerInventory Inventory = player.getInventory():

    Or if your in an event handler its:

    PlayerInventory Inventory = event.getPlayer().getInventory():

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  9. Offline

    bergerkiller

    SirLittlemonster7
    He said in response to that already:
    This is how I do it in my library (written outside the IDE, might contain errors)

    Code:
    for (int i = 0; i < inventory.getSize(); i++) {
        ItemStack item = inventory.getItem(i);
        if (item != null && item.getType() == Material.CHEST) {
            Itemstack newItem;
            if (item.getAmount() <= 1) {
                  newItem = null;
            } else {
                  newItem = item.cloneItemStack();
                  newItem.setAmount(newItem.getAmount() - 1);
            }
            inventory.setItem(i, newItem);
        }
    }
     
  10. Offline

    Dippoakabob

    bergerkiller
    Thanks! after some tweaking it works :D
    Thanks for all the help.
     
  11. Offline

    fireblast709

    Everyone, open de javadocs on page Inventory....
    Dippoakabob just use player.getInventory().removeItem(new ItemStack(Material.CHEST, 1));
     
  12. Offline

    bergerkiller

    fireblast709
    We've been there, that code removes a single item stack that has exactly one chest. If you have an item with 4 chests, it won't remove any of them.

    But maybe there is a function I overlooked, if so, would love to hear from it :)
     
  13. Offline

    fireblast709

  14. Offline

    bergerkiller

    fireblast709
    My bad, you are absolutely right :)
    Good to know this, so 'remove' will remove the exact item, and 'removeItem' will remove X amount of type Y from the inventory and return what it couldn't remove.
     
  15. Offline

    Dippoakabob

    That is SO helpful! I didn't see the removeItem() on there. :D
     
  16. Offline

    thefiscster510

    To sum this all up:

    player.getInventory().remove(Materiall mat) - Will remove every item of that material

    player.getInventory().remove(new ItemStack(material, int)) - Will remove only item stacks matching that item stack, and will not negate items from itemstacks that do not match it.

    player.getInvetory().removeItem(new ItemStack(material, int)) - Will do what he wanted :3..


    Just in case anyone couldn't follow..
     
Thread Status:
Not open for further replies.

Share This Page