Solved How to store the serial of an item in db ?

Discussion in 'Spigot Plugin Development' started by Z3dd7, Feb 8, 2018.

  1. Hello guys, I'm trying to come up with a method allowing to store any item in a SQL database and then restore it back. For the record, the serial of an item is a Map<String, Object>.

    For example the serial of a stack of 64 stone blocks would be something like {type: STONE, amount: 2}.
    My current method to handle this is to concatenate this in a string such as "type=STONE(java.lang.String);amount=2(java.lang.Number)". I can then build the serial map from this string and cast each value in the right class.

    However, some cases are more complicated. For example, a zombie villager spawn egg has the serial {type: MONSTER_EGG, meta: SPAWN_EGG_META:{meta-type=SPAWN_EGG, internal=H4sIAAAAAAAAAONiYOBi4HTNK8ksqQxJTOdgYMpMYZDMzcxLTS5KTCuxqsrPTcpMjS/LzMlJTE8tYmAAAMka2aQxAAAA, id=zombie_villager} where the value of meta is an instance of org.bukkit.craftbukkit.v1_12_R1.inventory.CraftMetaSpawnEgg.

    Because there is no CraftMetaSpawnEgg(String) constructor, and because it is version dependent (I don't want it to be), my method doesn't work here.
    Would you have any tip/solution to provide me ?
    #1 Z3dd7, Feb 8, 2018
    Last edited: Feb 11, 2018
  2. Any help appreciated !
  3. I'd try getConfig().set(key, itemStack) and see what it looks like in the config.
  4. there is a number of ways to do that:
    • use key-value pairs and describe them in a String (like JSON to serialize)
    • use NBT format and serialized an item into a String,
    • define your own serialization into String or blob
  5. I use NBT to store it in databases and internal configs: LINK
  6. Thanks for the tips, I finally went for a solution involving YamlConfiguration and its saveToString method. It works perfectly even with custom items :)

Share This Page