ItemMeta → String → ItemMeta

felix

New member
Oct 22, 2022
3
0
1
ItemMeta can be turned into a string. I want to save this string in a database and later read it to create ItemMeta from it.

Is there an easy solution to create ItemMeta from that string again?

Sample string of item.getItemMeta().toString(): UNSPECIFIC_META:{meta-type=UNSPECIFIC, enchants={DIG_SPEED=5}, Damage=133}
 

felix

New member
Oct 22, 2022
3
0
1
In the JavaDocs of ConfigurationSerializable, it states that:
These objects MUST implement one of the following, in addition to the methods as defined by this interface:
  • A static method "deserialize" that accepts a single Map< String, Object> and returns the class.
  • A static method "valueOf" that accepts a single Map<String, Object> and returns the class.
  • A constructor that accepts a single Map<String, Object>.

Sadly, ItemMeta seems to be doing nothing of that kind.

I'm really not sure if the solution is obvious but I somehow seem to miss it or if this is actually in issue. I'm really thankful for any kind of help!
 

electronicboy

Administrator
Staff member
Dec 11, 2021
280
17
56
28
ItemMeta is configuration serialisable, it's just complex because, it's a complex data structure. You'd just shove it in a bukkit configuration object, or serialise the item to base64 if you don't need to care about mutation
 

felix

New member
Oct 22, 2022
3
0
1
ItemMeta is configuration serialisable, it's just complex because, it's a complex data structure. You'd just shove it in a bukkit configuration object, or serialise the item to base64 if you don't need to care about mutation
Can you maybe give me an example how I would to either of that?
 

electronicboy

Administrator
Staff member
Dec 11, 2021
280
17
56
28
for Configuration, you just store the ItemStack inside of a YamlConfiguration object, as you do with anything else you want to store inside of a config;

for b64, there are methods in paper to encode an ItemStack into a byte array, which you can then deal with however you want, people often just base64 the thing assuming they're not just storing it inside of some blob storage