Remove all items of 1 kind?!?!?

Discussion in 'Plugin Development' started by B3N909, Jul 14, 2014.

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

    B3N909

    How do I remove all the payers lava buckets they have in their inventory! Thanks...
     
  2. Offline

    Aventium

    You would use the remove method, player.getInventory().remove(Material.LAVA_BUCKET);
     
  3. Offline

    TheMcScavenger

  4. Offline

    Phantom_64

    B3N909
    Loop through the player's inventory contents using the advanced for loop and check if the type of the ItemStack is Material.LAVA_BUCKET. If it is, remove it from the player's inventory.
     
  5. Offline

    Deleted user

    Code:
    for (ItemStack item : player.getInventory().getContents()) {
    if (item.getType == Material.ITEM_TO_REMOVE)
    player.getInventory().remove(item);
    }
    
    that may throw a NPE due to altering the list as we iterate through it.
    If this occurs then just saved the items to remove in a List and then iterate through
    the list and remove those items from the inventory, like so:

    Code:
    ArrayList<ItemStack> removeList = new ArrayList<ItemStack>();
     
    for (ItemStack item : player.getInventory().getContents()) {
    if (item.getType == Material.ITEM_TO_REMOVE)
    removeList.add(item);
    }
     
    for (ItemStack item : removeList) {
    player.getInventory().remove(item);
    }
    
    Also it may be worth it to update the player's inventory after this change. (although the method is deprecated it still works)
     
  6. Phantom_64 Eballer48
    Or just use what Aventium suggested and use the remove() method with a Material parameter. It removes all stacks that match the given Material.
     
  7. Offline

    Deleted user

    Except the Bukkit API doesn't always work like it should.
     
  8. Offline

    Necrodoom

    Make a bug report explaining the issue.
     
Thread Status:
Not open for further replies.

Share This Page