Adding Items

Discussion in 'Spigot Plugin Development' started by Rlaxing, Jun 26, 2016.

  1. Getting a weird error when trying to add an item to a players inventory. Is this way not used anymore? I've looked it up and I am still getting an error saying its supposed to be an array of ItemStacks.

    Example:

    Code (Text):
    ItemStack compass = new ItemStack(Material.COMPASS);
    player.getInventory().addItem(compass);
    Throws this error.
    Code (Text):
    The method addItem(ItemStack[]) in the type Inventory is not applicable for the arguments (ItemStack)
     
  2. addItem is an array of ItemStacks

    :p
     
  3. I thought that addItem was varargs?
     
    • Agree Agree x 1
  4. I've seen people add one item all the time. What's the other way?
     
  5. You're using an outdated version of the API. In bukkit 1.10-R0.1-SNAPSHOT it does work the way that you're using it
     
  6. What Spigot version are you using? Because it should be a vararg.

    HashMap<Integer,ItemStack> addItem(ItemStack... items)
    Stores the given ItemStacks in the inventory.
     
  7. I think you need to specify the amount as well

    addItem(1, itemstack);
    or
    addItem(itemstack, 1);
     
  8. That's setItem. The OP is using a method from an outdated version of the API, which used an array of ItemStacks as an argument
     
    • Agree Agree x 1
  9. Ah
     
  10. Spigot 1649 because I run a protocol hacked server. Thats probably the issue, what version should I be compiling with then?
    Trust me I've tried that :)
     
  11.  
  12. I started developing in 1.8, and I remember that addItem took vararg as the argument at that point in time. So Bukkit 1.8.8-R0.1-SNAPSHOT or above
     
    • Agree Agree x 1
  13. Getting the same error with 1.8.8. Whats an alternate way I can simply add an item to an inventory? aha
     
  14. new ItemStack(Material.COMPASS, 1);
     
  15. Tried that.
     
  16. What would that change?

    new ItemStack(Material.COMPASS) just calls that constructor :p

    If you're going to remain in the same Spigot version, you would need to use:

    player.getInventory().addItem(new ItemStack { new ItemStack(Material.COMPASS) });
     
  17. Thats the same thing that @gussttaavvoo said. He was just using psuedo code, anyways it throws the same error.
     
  18. Code (Text):

    ItemStack example = new ItemStack(Material.STONE, 1);


    getInventory().addItem(new ItemStack[] { example });
     
     
    • Winner Winner x 1
  19. Legend, thanks man. No errors, hopefully the code works :)
     
  20. np.