Remove items from an inventory

Discussion in 'Resources' started by bergerkiller, Jul 24, 2011.

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

    bergerkiller

    If you want to perform transactions between players or simply want to restrict certain items, you may have to remove a certain amount of a certain type from an inventory. The standard remove function only removes actual stacks, and only one stack at a time. This function can solve that issue:
    Code:
        public static void removeInventoryItems(Inventory inv, ItemStack item) {
            removeInventoryItems(inv, item.getType(), item.getAmount());
        }
        public static void removeInventoryItems(Inventory inv, Material type, int amount) {
            for (ItemStack is : inv.getContents()) {
                if (is != null && is.getType() == type) {
                    int newamount = is.getAmount() - amount;
                    if (newamount > 0) {
                        is.setAmount(newamount);
                        break;
                    } else {
                        inv.remove(is);
                        amount = -newamount;
                        if (amount == 0) break;
                    }
                }
            }
        }
    You can add an inventory contains call before this function to ensure the player has all items you wish to remove.
     
  2. Offline

    baklit

    Is there a list of all the item names in java?
     
  3. Offline

    bergerkiller

    Item names consist of a type (Material) and the durability member. I see this function does lack a durability check for colored wool, this could be a problem. For general item types, look in the Material enumeration.
    Code:
    ItemStack item = new ItemStack(Material.WOOD, 5);
     
  4. Offline

    Specops343

    @bergerkiller
    Hey there, I'm getting an error in eclipse on
    Code:
          removeInventoryItems(inv, item.getType(), item.getAmount());
    on removeInventoryItems
    I'm not exactly sure what I'm doing wrong. My plugin, with the source in the jar:
    <Edit by Moderator: Redacted mediafire url>

    Mind taking a look?
     
    Last edited by a moderator: Nov 13, 2016
  5. Offline

    Coryf88

    @Specops343You have removeInventoryItems(Inventory, ItemStack) in ExtendedPvp and removeInventoryItems(Inventory, Material, int) in ExtendedPvpPlayerListener. Move one of them into the same class as the other.
     
  6. Offline

    (infected)

    BK! You can help me with making tutorials bud :)
    Is this what you used on Infected?
    Love that your helping others <3
     
  7. Offline

    OppositeGamer

    OK I don't quite understand? Where do I actually get to specify the item? LOL I'm new to bukkit development and new to Java but I do understand it. Trust me! I am just wandering how could I specify the removal of 20 diamonds?
     
  8. Offline

    VioVioCity

    The above code contains an error or Bukkit does altogether. If there are two of the same items with same quantity both will be removed. Example: 3 stacks of 20 diamonds. By removing the first stack all 3 are removed since they are the same item type & quantity. This will not happen if you attempt to remove 3 stacks of diamonds with all different quantities. Why?
     
  9. Offline

    zachoooo

    Fixed.
    Code:java
    1. package us.zsugano.itemsave.utils;
    2.  
    3. import org.bukkit.Material;
    4. import org.bukkit.inventory.Inventory;
    5. import org.bukkit.inventory.ItemStack;
    6.  
    7. public class InventoryUtil {
    8.  
    9. public static void removeInventoryItems(Inventory inv, ItemStack item) {
    10. removeInventoryItems(inv, item.getType(), item.getAmount());
    11. }
    12.  
    13. public static void removeInventoryItems(Inventory inv, Material type, int amount) {
    14. ItemStack[] items = inv.getContents();
    15. for (int i = 0; i < items.length; i++) {
    16. ItemStack is = items[i];
    17. if (is != null && is.getType() == type) {
    18. int newamount = is.getAmount() - amount;
    19. if (newamount > 0) {
    20. is.setAmount(newamount);
    21. break;
    22. } else {
    23. items[i] = new ItemStack(Material.AIR);
    24. amount = -newamount;
    25. if (amount == 0) break;
    26. }
    27. }
    28. }
    29. inv.setContents(items);
    30. }
    31.  
    32. public static int getTotalItems(Inventory inv, Material type) {
    33. int amount = 0;
    34. for (ItemStack is : inv.getContents()) {
    35. if (is != null) {
    36. if (is.getType() == type) {
    37. amount += is.getAmount();
    38. }
    39. }
    40. }
    41. return amount;
    42. }
    43.  
    44. }[/i][/i]


    Oh and sorry for necro bumping, but I felt like it needed to be updated to work correctly

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 17, 2016
    jtjj222 likes this.
Thread Status:
Not open for further replies.

Share This Page