Solved Serialization of a class

Discussion in 'Plugin Development' started by HeroWorkbrine, Jul 5, 2014.

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

    HeroWorkbrine

    Hi everyone!

    I have another problem/question.
    I wanted to store the whole info of a class via serialization in a config file.
    I looked on this wiki page but i didn't understand it that well.

    I want searching further (Google & YouTube) but I could not find any tutorials so now I'm asking people the bukkit forum.

    How does serialization work and how do I use it?
     
  2. Yeah, that link is quite confusing.

    I'll give you an example of how serializing an Arena should work:
    Code:
    public class Arena implements ConfigurationSerializable {
        private int arenaID = -1;
        private boolean isEnabled = false;
     
        public Arena(int arenaID) {
            this(arenaID, true);
        }
     
        public Arena(int arenaID, boolean enabled) {
            this.arenaID = arenaID;
            this.isEnabled = enabled;
        }
     
        public int getID() {
            return this.arenaID;
        }
     
        public boolean isEnabled() {
            return this.isEnabled;
        }
     
        @Override
        public Map<String, Object> serialize() {
            Map<String, Object> serializedArena = new HashMap<String, Object>();
            serializedArena.put("ID", this.arenaID);
            serializedArena.put("Enabled", this.isEnabled);
            return serializedArena;
        }
     
        public static Arena deserialize(Map<String, Object> serializedArena) {
            int arenaID = serializedArena.get("ID");
            boolean isEnabled = serializedArena.get("Enabled");
            return new Arena(arenaID, isEnabled);
        }
    }
    
    Example of saving lots of data: https://github.com/KingFaris10/KingKits/blob/master/src/me/faris/kingkits/Kit.java#L199

    I'm not actually sure if you have to register it like: ConfigurationSerialization.registerClass(Arena.class);

    I've never done this and I don't see the point of it, until I view the ConfigurationSerialization code.

    Look at my edited message, I don't think you need to do that. However, this is how you use it.

    So continuing from my example of an Arena, to save it in a config like this:
    Code:
    Arenas:
        Arena1:
            ID: 1
            Enabled: true
    
    You would do something like this:
    Code:
    config.set("Arenas.Arena" + arena.getID(), arena.serialize());
    
    Then to load it, you would do something like this:
    Code:
    Arena arena = Arena.deserialize(config.getConfigurationSection("Arenas.Arena" + arena.getID()).getValues(false));
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  3. Offline

    HeroWorkbrine

  4. No problem, by the way, I just looked at the code for ConfigurationSerialization, it looks like you should register it anyway. In your onEnable(), using my example of an Arena again, do this:
    Code:
    ConfigurationSerialization.registerClass(Arena.class);
     
  5. Offline

    fireblast709

  6. Oops, I meant implements.
     
  7. Offline

    HeroWorkbrine

    What does it do?
     
  8. No idea, but you just should.
     
  9. Offline

    fireblast709

    HeroWorkbrine KingFaris11 It tells Bukkit (and in turn SnakeYAML) what a certain set of key-value pairs (the Map<String, Object> serialize() returns) belongs to.
     
    KingFaris11 likes this.
  10. Offline

    HeroWorkbrine

    Thanks for the info!
     
Thread Status:
Not open for further replies.

Share This Page