Inventory.insertNewItem(...) bug if inserting more items than the stack size

  • As the title says I was trying to insert e.g. iron ore to my inventory and when using 100 as the stacksize to be inserted only 64 items actually make their way to my inventory the rest 36 are not inserted :/


    Is that the intentional behaviour of this method? If yes I will adjust my plugin to add the amount in increments of the stacksize from the item definition to not have problems :D

  • Hmm... it's not really intentional behaviour, but I'm not sure if it's better to keep this behaviour or change it. This function returns the item that was added to the inventory, but if this method adds several items to the inventory, you could only retrieve a single item ;) On the other hand, I'm not sure if the returned item is important at all...

  • hmm true :/ though the returned item is kinda convenient when needing to check if the method actually added the item or if the inventory was full and null was added.


    A thought would be for it to be changed to return an item array with for the case of 100 iron ore would be an item[2] array with the values [64][36] in it, if the 36 wasn't able to be added then it would be [64][null] meaning the last items were not added successfully. Similarly for adding 150 iron ore it would be [64][64][22] or if only one slot was free [64][null][null] and if two [64][64][null]. Not sure how good a solution this is though, depends on your needs and the way you are handling things on the game's side.


    Another solution would be for the method to be changed into a boolean and it would first check if there is enough space for all items. If there is enough space for all items it adds them and returns true else if at least one item cannot be added then it doesn't add anything and returns false.

  • I'm not sure if it's really convenient if the function returns an array instead of an item (apart from having to create a new item even if only a single item was added)...
    Changing the function to return a boolean instead would work, but it might break existing plugins (and it requires you to make additional calls to retrieve the item). I guess the best solution in this case would be to restrict the stack size to 64 and mention it in the javadoc accordingly ?(

  • I guess the best solution in this case would be to restrict the stack size to 64 and mention it in the javadoc accordingly

    hmmm yes maybe this is in fact the best solution, just a short mention in the javadoc that the item addition/insertion is restricted to the item stacksize whatever that might be, 64 for ores, 8 for tomatoes etc.

Participate now!

Don’t have an account yet? Create a new account now and be part of our community!