Deserialize ItemMeta

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

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

    kellerkindt

    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.
    Code:
    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.


    Code:
    ...
    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

    http://jd.bukkit.org/apidocs/org/bu...ization.html#deserializeObject(java.util.Map)

    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

    kellerkindt

    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

    kellerkindt

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

    Code:
     Caused by: java.lang.IllegalArgumentException: Args doesn't contain type key ('==')
           at org.bukkit.configuration.serialization.ConfigurationSerialization.deserializeObject(ConfigurationSerialization.java:184)
    
    Code:
     
    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
    Code:
    for (String key : map.keySet()) {
            System.out.println (key+"--->"+map.get(key));
    }
    
    it outputs only
    Code:
    13:15:57 [INFO] meta-type--->SKULL
    
    I am using it here https://svn.dice-network.de/ShowCas...ernals/ConfigurationSerializationStorage.java

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

    Code:
    Map<String, Object> serialized    = itemMeta.serialize();
    ConfigurationSerialization.deserializeObject(serialized);
    
    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

    evilmidget38

    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

    kellerkindt

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

    kellerkindt

    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.

    Code:
    YamlConfiguration conf = new YamlConfiguration();
    File dest = new File("test.yaml");
                
    conf.set("itemmeta", value);
    conf.save(dest);
    
    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 >.<

    Regards.
    kellerkindt
     
Thread Status:
Not open for further replies.

Share This Page