Deserialize ItemMeta

Discussion in 'Plugin Development' started by kellerkindt, Feb 8, 2013.

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


    Currently, I cannot figure out any working way to deserialize an serialized ItemMeta.

    For example, I serialize an instance of "org.bukkit.craftbukkit.v1_4_R1.inventory.CraftMetaSkull",
    and tried to deserialize it after the server restarted.
    String className = "org.bukkit.craftbukkit.v1_4_R1.inventory.CraftMetaSkull";
    ConfigurationSerializable value = null;
    Map<String, Object> serialized    = new HashMap<String, Object>()
    // filling map serialized from file
    Class<? extends ConfigurationSerializable> clazz    = (Class<? extends ConfigurationSerializable>)Class.forName(className);
    value = ConfigurationSerialization.deserializeObject(serialized, clazz);
    In this example the value is always null.
    Trying to create a new instance through the constructor also fails,
    because it's protected - I think that ConfigurationSerialization fails because
    of this.

    value = ConfigurationSerialization.deserializeObject(serialized);
    Just gives me - as expected - "java.lang.Error: Unresolved compilation problem: The type org.bukkit.craftbukkit.v1_4_5.inventory.CraftMetaItem is not visible"

    Any idea to get the ItemMeta from a Map?
    I am really gonna get crazy :(
  2. Offline

    Wolvereness Bukkit Team Member

    You shouldn't be using anything in the org.bukkit.craftbukkit namespace. The configuration serialization will handle all of that. It does require all 'sub' objects to be pre-deserialized. They do this using the "==" key to specify the type alias. I'd really recommend just loading it as yaml if at all possible, as it'll handle everything for you.
  3. Offline


    Actually I am/was using/testing the mtehod you posted, but that will cause a "java.lang.Error: Unresolved compilation problem: The type org.bukkit.craftbukkit.v1_4_5.inventory.CraftMetaItem is not visible".

    I am storing the data for my plugin in xml files, I would recommend to use xml also for the ItemMeta.
    But I wouldn't reject yaml, if it isn't possible with xml. Could you just give me a short example how to use,
    yaml to (de-)serialize ItemMeta?

    btw: Thanks for the fast reply :)
  4. Offline

    Wolvereness Bukkit Team Member

    You'd need to post unredacted source for someone to figure out why you have a compilation problem. You should never be referencing / using CraftMeta at all. The method I linked to simply takes a map.
  5. Offline


    Sorry, had some issues with exporting and testing new code changes,
    but althougth, following code doesn't work.

     Caused by: java.lang.IllegalArgumentException: Args doesn't contain type key ('==')
           at org.bukkit.configuration.serialization.ConfigurationSerialization.deserializeObject(
    ItemMeta meta = someItemStack.getItemMeta();
    Map<String, Object> map = meta.serialize();
    // saving to file
    saveMap(map, someFile);
    // server restart
    // loading from file
    map = getMap(someFile);
    // trying to deserialize
    meta = (ItemMeta)ConfigurationSerialization.deserializeObject(map);
    If i run this, before saving
    for (String key : map.keySet()) {
            System.out.println (key+"--->"+map.get(key));
    it outputs only
    13:15:57 [INFO] meta-type--->SKULL
    I am using it here

    This one also throws "java.lang.IllegalArgumentException: Args doesn't contain type key ('==')"

    Map<String, Object> serialized    = itemMeta.serialize();
    Is this a bukkit issue?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: May 31, 2016
  6. Offline


    kellerkindt No, it's an issue with how you're serializing it. I know for a fact it is completely possible to do using only Bukkit.
  7. Offline


    So, could you please give me an example?
    Or a link to some source code?
  8. Offline


    Found the issue.
    The key "==" has to be set to the type of the to deserialize Object ("ItemMeta" in this case)
    Also very nice is to use Yaml.

    YamlConfiguration conf = new YamlConfiguration();
    File dest = new File("test.yaml");
    conf.set("itemmeta", value);;
    What I can't understand is, that nobody could give me just this little example,
    or was able to show me the issue in my previous posts >.<

Thread Status:
Not open for further replies.

Share This Page