Config Help Please! (Serialization, etc.)

Discussion in 'Plugin Development' started by MrTwiggy, Jul 13, 2012.

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

    MrTwiggy

    Hey! So, I have a list of custom objects that I need to save and load to/from the config.yml.

    Just as an example, the class is "Level", and it holds quite a few variables, with multiple 'int's, 'String's, 'Location's, as well as a 'World'.

    That, in itself, seems hard enough to save/write. On top of that though, I need to be able to save a LIST of those objects. IE:

    List<Level> levels; <---- I need to save/load this to a config.yml file.

    I really need some help in this. I looked at the bukkit page on Serialization/Deserialization of objects, however it's extremely short, and is missing a LOT of info on what/how I need to do it. Just as a base, this is my knowledge so far:


    Code:
    //THIS IS MY CLASS DECLARATION
    @SerializableAs("Level")
    public class Level implements ConfigurationSerializable
    {
     
    //THIS IS INSIDE MY MAIN CLASS
    static
        {
            ConfigurationSerialization.registerClass(Level.class, "Level");
        }
     
    //THIS IS INSIDE MY 'LEVEL' CLASS
    public Level deserialize(Map map)
        {
            return this;
        }
     
        @Override
        public Map<String, Object> serialize() {
            // TODO Auto-generated method stub
            return null;
        }

    This is pretty much as far as I have gotten, and as much as I could find. I just need a method/some lines of code to save the List<Level> levels = new ArrayList<Level>(); to the config.yml, and some lines of code that I can put in 'onEnable()' to load the saved data into the List<Level> levels.

    I tried something like:
    Code:
    //THIS IS INSIDE onDisable()
    Level testLevel = new Level();
    testLevel.serialize();
    getConfig().set("TestLevel", testLevel);
    saveConfig();
    But it didn't work.

    I really need some help!

    Bump
    Up
    My
    Post

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

    Sagacious_Zed Bukkit Docs

    MrTwiggy
    You did not implement the methods according to the specification. Please read the JavaDoc for ConfiguraitonSerialization.
     
  3. Offline

    MrTwiggy

    As I said, I've looked around on the Bukkit wiki, as well as where I could in the javadoc and couldn't find anything specific and helpful enough for a newbie on this topic, such as myself.
     
  4. Offline

    Sagacious_Zed Bukkit Docs

    your serialize method does not return a map representation of your class. It returns null, which is not confirming.

    your deserialize method does not return an instance of your class with the correct values.
     
  5. Offline

    MrTwiggy

    What do you mean 'with the correct values'? Also, how do I create a map representation of my class?
     
  6. Offline

    Sagacious_Zed Bukkit Docs

    The easiest way to think about deserialization is to make a constructor that takes a hahsmap which initialize your class.

    The serialize method must place all fields into a hashmap where the keys are string as specified by the return type.
     
Thread Status:
Not open for further replies.

Share This Page