InventoryManager: Improved Item Manipulation (addItem, etc.)

Discussion in 'Resources' started by Television, May 12, 2011.

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

    Television

    InventoryManager

    Essentially, this class contains various improved versions of the inventory item-handling functions - addItem, remove, and contains - as well as a new method, quantify. It can be initialized with a combination of an Inventory object, a Player object, and a Location object.
    
    addItem: Adds an item to the Inventory, or the Player's inventory. If there are already stacks of the same type of item, with the same metadata, it adds as much as possible to those, respecting the maximum stack size. It won't try to stack items past their limit, or stack non-stackable items. If it runs out of room, and it can figure out a location for the Inventory - either through the specified Player or Location object - it will drop the rest there. Otherwise, it returns whatever it wasn't able to add.
    new InventoryManager(user).addItem(new ItemStack(Material.STONE, 12));
    new InventoryManager(chestInventory, chestLocation).addItem(new ItemStack(Material.DIRT, 78));
    .
    remove: Pass this an ItemStack, and it will remove as much as possible of that amount of that item from the inventory. There are two optional parameters: ]meta] and [durability]. [meta] is, by default, true, and when it's left this way, the remove function will only remove items that match the given stack's metadata. [durability] is, by default, false, but when it's true, it will only remove items that match the given stack's durability. This function will go through each stack of the specified item until either enough is removed or it runs out of stacks, removing the specified amount even if it's spread over multiple stacks. Returns whatever it couldn't remove, so if you want to remove three pickaxes, but the user only has two, it would return an ItemStack of one pickaxe.
    new InventoryManager(chestInventory).remove(new ItemStack(Material.GREEN_RECORD, 5));
    if(new InventoryManager(player).remove(new ItemStack(Material.GOLD_RECORD, 2)).getAmount()>0) player.sendMessage(ChatColor.RED+"Hmm...");
    .
    contains: This function takes an ItemStack, and returns a boolean stating whether or not the inventory contains at least that amount of that item, regardless of how many stacks it's spread over or where it is. Like remove, it can be passed both [meta] and [durability] parameters.
    if(new InventoryManager(player).contains(new ItemStack(Material.COAL, 12))) player.sendMessage(ChatColor.YELLOW+"Goooood...");
    .
    quantify: Given an ItemStack, quantify will count up how many of that item is in the inventory, and return it in one ItemStack with an amount equal to the total quantity. So, if you specify red wool, and the inventory contains a total of seventy blocks of red wool, it will return an ItemStack of seventy red wool. Like the above, it also takes optional [meta] and [durability] parameters.
    ItemStack total = new ItemStack(inventory).quantify(new ItemStack(Material.SAND));
    .
    Source/Download: https://github.com/Television/it.undo.okay.utils/blob/master/InventoryManager.java
     
  2. Offline

    Lolmewn

    Handy!
     
  3. Offline

    tha d0ctor

    cool, it would be a bit more helpful if formatted a little differently
     
  4. Offline

    Flenix

    Sorry, I'm a noob developing my first plugin,

    Do I just download the source and stick it next to my other files? or do I need to hook into it the same way I did for bukkit?

    Also do I actually need the whole thing, I'll only be using the contains part - I want to check a players inventory and if they have a certain item within range of a sign, it'll send a redstone current from the sign. Also the metadata support is really needed for me :)
     
  5. is This now Unsupported?
     
  6. Offline

    Tauryuu

    Aww. This could have been so useful.
     
  7. Offline

    PatPeter

    404 Not Found.
     
  8. Offline

    chaseoes

    Possibly because it was posted over a year ago?
     
  9. Offline

    PatPeter

Thread Status:
Not open for further replies.

Share This Page