Solved Problems with Bukkit ConfigurationSerialization

Discussion in 'Spigot Plugin Development' started by Ficato, May 26, 2018.

  1. Hey there.

    I have two classes which use the Bukkit ConfigurationSerialization. Everything works perfectly. I can save them without a prob in a file. Now the problem is: As soon as I start the server again, my config file gets completly whiped clean.

    Thanks in regard.

    Ficato
     
  2. Serializator

    Supporter

    It's easier for us to help you if you show us some of the code you wrote for this.
     
  3. Yeah sure, one sec.
     
  4. My main class:
    Code (Java):
    package me.maxderhusky.gilga.core;

    import me.maxderhusky.gilga.config.PlayerConfig;
    import me.maxderhusky.gilga.mechanics.MechanicManager;
    import me.maxderhusky.gilga.player.GilgaLevel;
    import me.maxderhusky.gilga.player.GilgaPlayer;
    import me.maxderhusky.gilga.player.PlayerManager;
    import me.maxderhusky.gilga.util.MaxsAdvancements;
    import org.bukkit.configuration.serialization.ConfigurationSerialization;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin {
        static {
            ConfigurationSerialization.registerClass(GilgaPlayer.class, "GilgaPlayer");
            ConfigurationSerialization.registerClass(GilgaLevel.class, "GilgaLevel");
        }

        private static Plugin plugin;
        private static PlayerManager playerManager;

        @Override
        public void onEnable(){
            plugin = this;

            loadConfigs();
            loadComponents();
        }

        @Override
        public void onDisable(){
            playerManager.unload();
        }

        private void loadConfigs(){
            PlayerConfig.load();
        }

        private void loadComponents(){
            MaxsAdvancements.setup();

            playerManager = new PlayerManager();
            new MechanicManager();
        }

        public static Plugin getPlugin() {
            return plugin;
        }

        public static PlayerManager getPlayerManager() {
            return playerManager;
        }
    }
     

    The two classes to save:
    Code (Java):
    package me.maxderhusky.gilga.player;

    import org.bukkit.configuration.serialization.ConfigurationSerializable;
    import org.bukkit.configuration.serialization.SerializableAs;

    import java.util.HashMap;
    import java.util.Map;

    @SerializableAs("GilgaPlayer")
    public class GilgaPlayer implements Cloneable, ConfigurationSerializable {

        private String uuid;
        private GilgaLevel gilgaLevel;
        private String title;
        private String role;

        public GilgaPlayer(String uuid, GilgaLevel gilgaLevel, String title, String role) {
            this.uuid = uuid;
            this.gilgaLevel = gilgaLevel;
            this.title = title;
            this.role = role;
        }

        @Override
        public Map<String, Object> serialize() {
            Map<String, Object> map = new HashMap<>();

            map.put("UUID", uuid);
            map.put("GilgaLevel", gilgaLevel);
            map.put("Title", title);
            map.put("Role", role);

            return map;
        }

        public GilgaPlayer deserialize(Map<String, Object> map){
            String suuid = (String) map.get("UUID");
            GilgaLevel sgilgalevel = (GilgaLevel) map.get("GilgaLevel");
            String stitle = (String) map.get("Title");
            String srole = (String) map.get("Role");

            return new GilgaPlayer(suuid, sgilgalevel, stitle, srole);
        }

        public String getUuid() {
            return uuid;
        }

        public void setUuid(String uuid) {
            this.uuid = uuid;
        }

        public GilgaLevel getGilgaLevel() {
            return gilgaLevel;
        }

        public void setGilgaLevel(GilgaLevel gilgaLevel) {
            this.gilgaLevel = gilgaLevel;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getRole() {
            return role;
        }

        public void setRole(String role) {
            this.role = role;
        }
    }
     
    Code (Java):
    package me.maxderhusky.gilga.player;

    import org.bukkit.configuration.serialization.ConfigurationSerializable;
    import org.bukkit.configuration.serialization.SerializableAs;

    import java.util.HashMap;
    import java.util.Map;

    @SerializableAs("GilgaLevel")
    public class GilgaLevel implements Cloneable, ConfigurationSerializable {

        private int craftingLevel;
        private int craftingExp;

        public GilgaLevel(int craftingLevel, int craftingExp) {
            this.craftingLevel = craftingLevel;
            this.craftingExp = craftingExp;
        }

        @Override
        public Map<String, Object> serialize(){
            Map<String, Object> map = new HashMap<>();

            map.put("CraftLevel", craftingLevel);
            map.put("CraftExp", craftingExp);

            return map;
        }

        public GilgaLevel deserialize(Map<String, Object> map){
            int scraftlevel = (Integer) map.get("CraftLevel");
            int scraftexp = (Integer) map.get("CraftExp");

            return new GilgaLevel(scraftlevel, scraftexp);
        }
    }
     

    My config file:
    Code (Java):
    package me.maxderhusky.gilga.config;

    import me.maxderhusky.gilga.player.GilgaPlayer;
    import org.bukkit.configuration.file.YamlConfiguration;

    import java.io.File;
    import java.io.IOException;
    import java.util.UUID;

    public class PlayerConfig {

        private static File file = new File("plugins/GilgaKingdom/players.yml");
        private static YamlConfiguration config = YamlConfiguration.loadConfiguration(file);

        public static void load(){
            config.options().copyDefaults(true);
            save();
        }

        private static void save(){
            try{
                config.save(file);
            } catch (IOException ex){
                ex.printStackTrace();
            }
        }

        public static void setPlayer(UUID uuid, GilgaPlayer gp){
            config.set(uuid.toString(), gp);
            save();
        }

        public static GilgaPlayer getPlayer(UUID uuid){
            return (GilgaPlayer) config.get(uuid.toString());
        }

        public static boolean playerExists(UUID uuid){
            if(getPlayer(uuid) != null){
                return true;
            } else {
                return false;
            }
        }


    }
     
     
  5. Serializator

    Supporter

    What happens when you make your deserialization methods in your GilgaPlayer and GilgaLevel classes static?
     
    • Agree Agree x 1
  6. Wow thanks that solved it.