Config won't load (serialization) [SOLVED]

Discussion in 'Plugin Development' started by raGan., Jul 31, 2012.

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

    raGan.

    I am currently playing with configuration serialization and everything is working except for config loading. Config saving works, adding to config works, when I add to config and save it, I can see results in the file after test plugin is shut down, but every time I try to load, it loads empty config, despite of correctly formatted config from last run of plugin is still there. It won't even touch that file, no errors, nothing. I have no idea where the problem might be. Help me please. Thanks.
    Here is the code: http://pastebin.com/B8PwbRvf
     
  2. Offline

    scranner

    i may be a noob but it looks like you are creating a new config on each run, are you checking if the file exists so you dont create a new one?
    here is the code i use
    Code:
    public void onEnable()
        {
            loadConfiguration();
        }
    private void loadConfiguration()
        {
            File f = new File("plugins/ReferredBy/config.yml");
            if(!f.exists())
            {
                createconfig();
            }
        }
    private void createconfig()
        {
            getConfig().options().copyDefaults(true);
            saveConfig();
        }
     
  3. Offline

    raGan.

  4. Offline

    theguynextdoor

    No need to do all the File f = new File ... blablabla.
    Because Just doing this:

    Code:
    public void onEnable(){
      FileConfiguration config = getConfig();
      config.addDefault("Path.subpath", "String");
      config.options.copyDefaults(true);
      saveConfig();
    }
    Will create the config file if it does not exist and add the defaults you specify.


    As for the OPs code, you dont use constructors for your main class. If you need any code done when the plugins loads, it goes in the onEnable
     
  5. Offline

    raGan.

  6. Offline

    theguynextdoor

    If you are playing around with config serialization then take a look at this class, and how i use it.

    Code:
    package me.theguynextdoor.tribesnextdoor.tribe;
     
    import java.util.HashMap;
    import java.util.Map;
     
    import org.bukkit.Bukkit;
    import org.bukkit.Chunk;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.configuration.serialization.ConfigurationSerializable;
    import org.bukkit.entity.Player;
     
    public class Spawn implements ConfigurationSerializable {
        public double x, y, z;
        public float yaw;
        public float pitch;
        public World world;
        public Location loc;
        public Chunk chunk;
     
        public Spawn(Location loc) {
            x = loc.getBlockX();
            y = loc.getBlockY();
            z = loc.getBlockZ();
            yaw = loc.getYaw();
            pitch = loc.getPitch();
            world = loc.getWorld();
            chunk = loc.getChunk();
            this.loc = loc;
        }
     
        public Spawn(Map<String, Object> map) {
            x = (Double) map.get("x");
            y = (Double) map.get("y");
            z = (Double) map.get("z");
            yaw = Float.intBitsToFloat((Integer) map.get("yaw"));
            pitch = Float.intBitsToFloat((Integer) map.get("pitch"));
            world = Bukkit.getWorld(map.get("world").toString());
            loc = new Location(world, x, y, z, yaw, pitch);
            chunk = loc.getChunk();
        }
     
        public void teleport(Player player) {
            player.teleport(loc);
        }
     
        @Override
        public Map<String, Object> serialize() {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("world", loc.getWorld().getName());
            map.put("x", loc.getX());
            map.put("y", loc.getY());
            map.put("z", loc.getZ());
            map.put("yaw", Float.floatToIntBits(loc.getYaw()));
            map.put("pitch", Float.floatToIntBits(loc.getPitch()));
            return map;
        }
     
        public void setSpawn(Location loc) {
            this.loc = loc;
            this.x = loc.getBlockX();
            this.y = loc.getBlockY();
            this.z = loc.getBlockZ();
            this.yaw = loc.getYaw();
            this.pitch = loc.getPitch();
            this.world = loc.getWorld();
        }
     
    }
    
    Then to save it i do:

    Code:
    Spawn spawn = new Spawn(player.getLocation());
    config.set("Spawn", spawn.serialize);
    saveConfig();
    To load it i do:

    Spawn spawn = new Spawn(config.getConfigurationSection("Spawn").getValues(true);


    So as long as you are making the config as a specified previously, then using the config serialization as above, then everything should work. If it does not then post any errors you get, or maybe some more details on the problem
     
    nahkd123 and PogoStick29 like this.
  7. Offline

    raGan.

    This just won't do it since it always loads empty config. There aren't any keys, any configurationsections, anything.
     
  8. Offline

    theguynextdoor

    Are you setting you config up as i showed you in the above post? i,e:

    Code:
    public void onEnable(){
      FileConfiguration config = getConfig();
      config.addDefault("Path.subpath", "String");
      config.options.copyDefaults(true);
      saveConfig();
    }
     
  9. Offline

    raGan.

    Code:
    public void onEnable(){
      FileConfiguration config = getConfig(); // this config is always empty
      config.addDefault("Path.subpath", "String");
      config.options.copyDefaults(true);
      saveConfig();
    }
    I noticed that when I add testkey: testvalue to the config, it is recognized, but values like:
    Code:
    test:
      ==: QuesterPlayerProfile
      progress:
        0: 2
        1: 5
        2: 4
        3: 0
        4: 90
      name: test
      quest: cierny
      completed:
      - cigan - 3
      - cigan - 4
      - cigan - 1
      - cigan - 2
    are not, it seems like getConfig() is deserializing those automatically, because when I remove this line from constructor
    Code:
    ConfigurationSerialization.registerClass(PlayerProfile.class);
    it throws error on enable
    Code:
    15:55:58 [SEVERE] Cannot load plugins\Roller\config.yml
    org.bukkit.configuration.InvalidConfigurationException: org.yaml.snakeyaml.error.YAMLException: Could not deserialize object
            at org.bukkit.configuration.file.YamlConfiguration.loadFromString(YamlConfiguration.java:55)
            at org.bukkit.configuration.file.FileConfiguration.load(FileConfiguration.java:138)
            at org.bukkit.configuration.file.FileConfiguration.load(FileConfiguration.java:105)
            at org.bukkit.configuration.file.YamlConfiguration.loadConfiguration(YamlConfiguration.java:175)
            at org.bukkit.plugin.java.JavaPlugin.reloadConfig(JavaPlugin.java:117)
            at org.bukkit.plugin.java.JavaPlugin.getConfig(JavaPlugin.java:111)
            at com.molnardad.gmail.roller.Roller.onEnable(Roller.java:25)
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:217)
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:337)
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:381)
            at org.bukkit.craftbukkit.CraftServer.loadPlugin(CraftServer.java:257)
            at org.bukkit.craftbukkit.CraftServer.enablePlugins(CraftServer.java:239)
            at net.minecraft.server.MinecraftServer.t(MinecraftServer.java:373)
            at net.minecraft.server.MinecraftServer.a(MinecraftServer.java:360)
            at net.minecraft.server.MinecraftServer.init(MinecraftServer.java:189)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:424)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:492)
    Caused by: org.yaml.snakeyaml.error.YAMLException: Could not deserialize object
            at org.bukkit.configuration.file.YamlConstructor$ConstructCustomObject.construct(YamlConstructor.java:37)
            at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:183)
            at org.yaml.snakeyaml.constructor.BaseConstructor.constructMapping2ndStep(BaseConstructor.java:326)
            at org.yaml.snakeyaml.constructor.SafeConstructor.constructMapping2ndStep(SafeConstructor.java:143)
            at org.yaml.snakeyaml.constructor.BaseConstructor.constructMapping(BaseConstructor.java:307)
            at org.yaml.snakeyaml.constructor.SafeConstructor$ConstructYamlMap.construct(SafeConstructor.java:459)
            at org.bukkit.configuration.file.YamlConstructor$ConstructCustomObject.construct(YamlConstructor.java:26)
            at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:183)
            at org.yaml.snakeyaml.constructor.BaseConstructor.constructDocument(BaseConstructor.java:142)
            at org.yaml.snakeyaml.constructor.BaseConstructor.getSingleData(BaseConstructor.java:128)
            at org.yaml.snakeyaml.Yaml.loadFromReader(Yaml.java:480)
            at org.yaml.snakeyaml.Yaml.load(Yaml.java:399)
            at org.bukkit.configuration.file.YamlConfiguration.loadFromString(YamlConfiguration.java:53)
            ... 16 more
    Caused by: java.lang.IllegalArgumentException: Specified class does not exist ('QuesterPlayerProfile')
            at org.bukkit.configuration.serialization.ConfigurationSerialization.deserializeObject(ConfigurationSerialization.java:171)
            at org.bukkit.configuration.file.YamlConstructor$ConstructCustomObject.construct(YamlConstructor.java:35)
            ... 28 more
     
  10. Offline

    theguynextdoor

    What does you playerprofile class look like?
     
  11. Offline

    raGan.

    http://pastebin.com/S3JfQD1L I think I will add some message logging into serialize and deserialize.

    no message

    Serialization and deserialization works well when working with config in memory. When I add few objects into config and then try to get them again, it works. When I try to save it, it is saved as expected. Only thing I can't do is load objects from previously saved file. Object keys are not loaded, all other keys are.

    Finally found the issue. deserialize() method in class that implements ConfigurationSerializable needs to be STATIC.
    Now it's clear of course. Works as expected.

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

    Firefly

    That's weird, to save to my config with serializable objects, I don't have to do object.serialize I just do:

    Code:
    //I'll use Spawn as an example
     
    config.set("Spawn", spawn);
     
  13. Offline

    theguynextdoor

    That's cool, i didn't know that. It's just i had always done it as spawn.serialize
     
Thread Status:
Not open for further replies.

Share This Page