Why are inventories not serializable?

Discussion in 'Plugin Development' started by 1Rogue, Sep 16, 2013.

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

    1Rogue

    A rather basic question, I find it a bit tenacious to store inventories from chests in the current way I am doing it (getting the values, converting to id with two maps for amount and type, storing). What drawbacks would there be for having inventories in Bukkit serializable and why isn't it implemented?
     
  2. Offline

    metalhedd

    ItemStack's are serializable, so you can serialize them one item at a time in a loop. also, your current method will lose all ItemMeta if you're only storing id/data bits
     
  3. Offline

    1Rogue


    Well there's half the battle I suppose, but why are Inventories as a whole not serializable?

    In regards to losing meta, I did have a catch in the code, I was just getting the idea across that it was rather monotonous.
     
  4. Offline

    Wolvereness Bukkit Team Member

    Well, the largest reason is that inventories are completely contextual. A chest inventory is located at some point in the world.

    However, if you're simply storing the contents, those are serializable. ItemStacks are ConfigurationSerializable, which means they can be stored in yaml. If you are referring to using the Java Serializable interface, then you should look at http://jd.bukkit.org/rb/apidocs/org/bukkit/util/io/BukkitObjectInputStream.html and http://jd.bukkit.org/rb/apidocs/org/bukkit/util/io/BukkitObjectOutputStream.html. Those streams provide a seamless layer between normal Java Serialization and the Bukkit ConfigurationSerializable API. When using it, you can simply write the contents array to a file, and read it back.
     
  5. Offline

    metalhedd

    I suppose because an inventory is generally just an array of ItemStacks, it wasn't really necessary? I'm fairly sure you could write a function to convert the Inventory into a map of SlotId->ItemStack and serialize that directly with config.setMap

    edit: sniped
     
  6. Offline

    1Rogue


    Awesome, thanks for the input and streams.
     
  7. Offline

    blablubbabc

    I think the even following works, without looping:
    config.set("the inventory contents", inventory.getContents());
    ItemStack[] contents = (ItemStack[]) config.get("the inventory contents", new ItemStack[0]);
    inventory.setContents(contents);
     
  8. Offline

    Wolvereness Bukkit Team Member

    This is erroneous. When deserializing yaml, that array will become a List (Series in yaml spec).
     
Thread Status:
Not open for further replies.

Share This Page