Solved Saving/loading inventories

Discussion in 'Spigot Plugin Development' started by Oberdiah, May 17, 2016.

  1. Hello, I've got a players inventory that I wish to store, and return at a later date.

    However, when I use code like this:
    Code (Text):
    ItemStack[] items = p.getInventory().getContents();
    p.getInventory().setContents(items);
    I get an exception: Invalid inventory size; expected 36 or less.

    Now, I can think of many ways to get around this, trimming the array, looping manually, etc, but it seems pretty counter-intuitive to have the API set up like that.

    Am I missing something, or maybe on an old version?
     
  2. Try using getStorageContents() and setStorageContents() rather than getContents() and setContents(). This will exclude certain inventory slots like armor, but it should get the main part of their inventory.
     
    • Agree Agree x 1
  3. I don't seem to have a getStorageContents() method for my inventories xD

    I'll update my spigot version, and see if that helps
     
  4. It would be very helpful to see all the code you're using to attempt this. We can't accomplish much when we aren't given much! ;)
     
  5. Try storing all the data in a hashmap by looping from 0 to your inventory size and store in a hashmap the id of where is the item and the item and then just loop in the hashmap and set the item to the place they need to go in the inventory ;)
     
  6. The problem really was the fact that gave an exception: Invalid inventory size; expected 36 or less.
    ItemStack[] items = p.getInventory().getContents();
    p.getInventory().setContents(items);

    As I said, I could fix the problem, I was just wondering why that was implemented that way into spigot, seeing as how unintuitive it is.

    Turns out I was using the wrong version, so it's been fixed :p
     
    • Funny Funny x 1
  7. Can you mark as solved please :)