Add ItemStack to existing ItemStack in Inventory

Discussion in 'Spigot Plugin Development' started by Eykar, May 20, 2015.

  1. Instead of Inventory.addItem(is) adding the item to the next available slot, is it possible to have it added to an already existing ItemStack in the inventory, without iterating through the inventory myself? Similar to the way Essentials /i works. Seems like a useful thing that could be in the API but I can't find any methods that do what I want.

    Thanks in advance :)
  2. Make an array of the ItemStack and add it to that?
  3. Sorry, I'm not quite sure what you mean by that
  4. @Eykar one way or another, someone is going to have to iterate over the inventory contents. Why not just iterate over it yourself?
  5. I would do what Svenna and DarkSeraphim said iterate through every item in the inventory and save it to an ArrayList, then just overwrite it with the new amount if the current iteration is that item. Finally clear the inventory and add the ArrayList of items into the inventory. Or you could get away with just clearing the one slot and replacing it, that would work too.
  6. I figured it would be a waste of my time to not reuse existing code. I thought I should ask since so many plugins add items to inventories in this way that it is likely a standardized method would exist, however it does not so I guess I have to actually do some work :eek:.

    Thank you for the help all
  7. Unless you have access to the pre existing instance of the item stack, there is no way to alter the amount that I know of besides overwriting it.