1.16.5 How to serialize an inventory and all nbts AND the inventory name?

Discussion in 'Spigot Plugin Development' started by CoderBoyo, Jun 17, 2021.

  1. Would there be a way to serialize an inventory with all nbts and the name of the inventory?
    I am using an api to serialize the inventory and it works great only think is it doesn't include the name of the inventory. Thanks for the help!

    NOTE: I think the reason this happens is all existing serializes just serialize the Itemstack not the actual inventory variable.
    #1 CoderBoyo, Jun 17, 2021
    Last edited: Jun 17, 2021
  2. Code (Java):

    import java.util.List;
    import java.util.Map;
    import java.util.HashMap;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.configuration.serialization.ConfigurationSerializable;

    public record SerinalizableInventory(String title, List<ItemStack> items) implements ConfigurationSerializable {

        public Map<String, Object> serialize() {
            var map = new HashMap<String, Object>();
            map.put("title", title());
            map.put("items", items());
            return map;

        public static SerializableInventory deserialize(Map<String, Object> map) {
            var title = (String) map.get("title");
            var items = (List<ItemStack>) map.get("items");
            return new SerializableInventory(title, items);
    Then you can just use values of SerializableInventory in a Configuration, or you use a BukkitObjectInputStream/BukkitObjectOutputStream if you need a binary format.

    More info on this technique: https://bukkit.fandom.com/wiki/Configuration_API_Reference#Serializing_and_Deserializing_Objects
  3. How would you use this? Also is there a way that actually serializes it and not just saves it?
    Note I dont know how to use bukkitobjectimputstream
    #3 CoderBoyo, Jun 17, 2021
    Last edited: Jun 17, 2021
  4. You have to use the method setTitle on 1.13-1.16 to set the title, because in 1.13+ the name of an inventory is saved into the inventory view and not the inventory. Thus you need to supply it doing
    Code (Java):
  5. Maybe you could add something like this:
    Code (Text):
    ItemStackSerializer.serializeInventory(inv, title)
    And serialize the title as well?
  6. I could add it later, for now if you want you can just store the title alongside the other string. Or you can make a pr ;).
  7. BukkitObjectInputStream works like a regular ObjectInputStream, but you can read values from it whose type implement ConfigurationSerializable.
    You can read the documention here: https://hub.spigotmc.org/javadocs/spigot/org/bukkit/util/io/BukkitObjectInputStream.html
    and for writing values there is BukkitObjectOutputStream: https://hub.spigotmc.org/javadocs/spigot/org/bukkit/util/io/BukkitObjectOutputStream.html
  8. Bukkit itemstack configuration doesn't serialize all nbt well, tough. Therefore I created my lib.
  9. That used to be true a long time ago, but it has been doing that for a while. There's a unhandledTags field in CraftMetaItem if I recall correctly.
  10. I don't know if it's been corrected, but I use nbts to serialize and it is obviously the fastest and most bug-free way to do it. Moreover, you can store serialized items (which is a string) in txt or json files without needing a BukkitObjectOutputStream or FileConfiguration.