Basic inventory methods

Discussion in 'Resources' started by p000ison, Aug 17, 2012.

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

    p000ison

    Hey Community,

    since the craftbukkit inventory methods aren't very good I made my own. They are not tested! :p

    Code:
    /*
               DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
                        Version 2, December 2004
     
    Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
     
    Everyone is permitted to copy and distribute verbatim or modified
    copies of this license document, and changing it is allowed as long
    as the name is changed.
     
                DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
      TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
     
      0. You just DO WHAT THE FUCK YOU WANT TO.
     
    */
     
        /**
        * Removes a item from a inventory
        *
        * @param inventory The inventory to remove from.
        * @param mat      The material to remove .
        * @param amount    The amount to remove.
        * @param damage    The data value or -1 if this does not matter.
        * @return If the inventory has not enough items, this will return the amount of items which were not removed.
        */
        public static int remove(Inventory inventory, Material mat, int amount, short damage)
        {
            ItemStack[] contents = inventory.getContents();
            int removed = 0;
            for (int i = 0; i < contents.length; i++) {
                ItemStack item = contents[i];
     
                if (item == null || !item.getType().equals(mat)) {
                    continue;
                }
     
                if (damage != (short) -1 && item.getDurability() != damage) {
                    continue;
                }
     
                int remove = item.getAmount() - amount - removed;
     
                if (removed > 0) {
                    removed = 0;
                }
     
                if (remove <= 0) {
                    removed += Math.abs(remove);
                    contents[i] = null;
                } else {
                    item.setAmount(remove);
                }
            }
            return removed;
        }
     
     
        /**
        * Checks weather the inventory contains a item or not.
        *
        * @param inventory The inventory to check..
        * @param mat      The material to check .
        * @param amount    The amount to check.
        * @param damage    The data value or -1 if this does not matter.
        * @return The amount of items the player has not. If this return 0 then the check was successfull.
        */
        public static int contains(Inventory inventory, Material mat, int amount, short damage)
        {
            ItemStack[] contents = inventory.getContents();
            int searchAmount = 0;
            for (ItemStack item : contents) {
     
                if (item == null || !item.getType().equals(mat)) {
                    continue;
                }
     
                if (damage != -1 && item.getDurability() == damage) {
                    continue;
                }
     
                searchAmount += item.getAmount();
            }
            return searchAmount - amount;
        }
     
    wiedzmin137, Code0 and Skyost like this.
  2. Offline

    Code0

    Awesome thread! Helped me alot :).
     
  3. Offline

    PandazNWafflez

    p000ison Seeing as this got bumped, why did you choose that license? That means people can't distribute to the public anything they make that contains that code.
     
    p000ison likes this.
  4. Offline

    p000ison

    ye, was in the beginning of my coding career, switched to wtfpl :D
     
    PandazNWafflez likes this.
  5. Offline

    PandazNWafflez

    Haha best license ever :p
     
Thread Status:
Not open for further replies.

Share This Page