plugin.saveConfig() deleting fields

Discussion in 'Plugin Development' started by Zethariel, Mar 19, 2015.

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

    Zethariel

    Hello,

    I have this little problem - I sometimes need to add or move around stuff in the plugin's config file, but whenever I save the file, the rest of it just lifts up and vanishes.

    Best code example:
    Code:
    this.saveDefaultConfig();
         
         YamlConfiguration config = (YamlConfiguration) this.getConfig();
         String currentVersion = config.getString("version");
         logger.info("VERSION: "+config.getString("version"));
         switch(currentVersion){
         case "1.0":
           config.set("constans.privateWarpCap",10);
           config.set("version","1.01");
           break;
         default:
           
           break;
         }
         this.saveConfig();
    
    After this.saveConfig() is called, only the things I set is being saved to file. I presume the rest of the values are then taken from the internal file...

    Is there a way around this? I have no desire to delete whatever changes the player has made, and this is the best method I found for seamless updates of my plugin (as in, without asking the user to overwrite the config file every time)
     
  2. Why do you cast the config to YamlConfiguration? Just use the methods getConfig(), getConfig().set("...", "...") and saveConfig()
    And you need
    saveDefaultConfig(); in the onEnable() method
     
  3. Offline

    Zethariel

    getConfig() is a method that does things, and I don't need to do those things if I simply want to read/modify data - so I store the FileConfiguration in a variable. The cast is a remnant of other attempts of trying to fix this.

    Unfortunately, your answer does not touch on the subject of the weird save behaviour
     
  4. Offline

    Zethariel

    I did try it before I started casting it into YamlConfiguration. I eventually started just roughly loading the config file by myself and changing/saving the values, but now reloadConfig() does not read the updated config. Irrational behaviour
     
Thread Status:
Not open for further replies.

Share This Page