Setting variables in config

Discussion in 'Spigot Plugin Development' started by Legotyrell, May 24, 2017.

  1. I have a class that grabs every variable in my config, resets it the the default config and sets all variables to the variables I grabbed from the old one. The problem I'm having is that plugin.saveConfig() dosn't copy the comments. Is there a way to do it

    - If configVersion is less than latestConfigVersion
    - Set every variable in the config to a variable in code
    - Delete the config
    - Create a new config
    - Set the code variables to the new config
    - Save the config (saveConfig() works but does not save the comments)
    #1 Legotyrell, May 24, 2017
    Last edited: May 26, 2017
  2. use saveDefaultConfig(); at your onEnable instead of copying defaults and saveconfig.
  3. I've tried that but it doesn't work
    Here is the code
    (It may be messy but it's my way of coding)

    Code (Text):
    package com.tyrellplayz.serverextras.config;

    import java.util.List;

    import org.bukkit.Bukkit;
    import org.bukkit.craftbukkit.libs.jline.internal.Log;

    import com.tyrellplayz.serverextras.ServerExtras;

    public class ConfigUpdater {
        // Config Version
        public String latestConfigVersion = "0.2.1";
        // Config Strings
        private String serverType;
        private String serverName;
        private String serverCommand;
        private String displayName;
        private String twitter;
        private String discord;
        private String planetMinecraft;
        private String minecraftSeverNet;
        // Config Ints
        private Integer minecraftListOrg;
        // Config Bools
        private Boolean voting;
        private Boolean updateChecker;
        private Boolean updateMessage;
        public ServerExtras plugin;
        public ConfigUpdater(ServerExtras pl){
            plugin = pl;
        public String configVersion(){
                return plugin.getConfig().getString("configVersion");
            }catch (Exception e) {
                Log.error("[" + plugin.pFile.getName() + "] Config: Error while getting 'configVersion'");
                return "";
        public void checkConfigVersion(){
            Integer result = latestConfigVersion.compareTo(configVersion());
            if(result < 0 ){
                // latestConfigVersion is less than configVersion
                Log.error("[" + plugin.pFile.getName() + "] Config Version is not vaild. Reseting config");
                File configFile = new File(plugin.getDataFolder(), "config.yml");
            else if(result > 0){
                // latestConfigVersion is greater than configVersion
      "[" + plugin.pFile.getName() + "] Updating Config...");
                // They are both equal
      "[" + plugin.pFile.getName() + "] Config is upto date");

        private void updateConfig() {
            // Get all Strings
            serverType = plugin.getConfig().getString("serverType");
            serverName = plugin.getConfig().getString("serverName");
            serverCommand = plugin.getConfig().getString("serverCommand");
            displayName = plugin.getConfig().getString("displayName");
            twitter = plugin.getConfig().getString("twitter");
            discord = plugin.getConfig().getString("discord");
            planetMinecraft = plugin.getConfig().getString("planetMinecraft");
            minecraftSeverNet = plugin.getConfig().getString("minecraftSeverNet");
            //Get all Ints
            minecraftListOrg = plugin.getConfig().getInt("minecraftListOrg");
            // Get all Bools
            voting = plugin.getConfig().getBoolean("voting");
            updateChecker = plugin.getConfig().getBoolean("updateChecker");
            updateMessage = plugin.getConfig().getBoolean("updateMessage");
            // Get all Lists
            List<String> voteRewards = plugin.getConfig().getStringList("voteRewards");
            List<String> serverDescription = plugin.getConfig().getStringList("serverDescription");
            //Reset Config
            File configFile = new File(plugin.getDataFolder(), "config.yml");

            // Want to set the vars in config to the vars in the old config
            plugin.getConfig().set("configVersion", latestConfigVersion);
            // Set all Strings
            plugin.getConfig().set("serverType", serverType);
            plugin.getConfig().set("serverName", serverName);
            plugin.getConfig().addDefault("serverCommand", serverCommand);
            plugin.getConfig().set("displayName", displayName);
            plugin.getConfig().set("twitter", twitter);
            plugin.getConfig().set("discord", discord);
            plugin.getConfig().set("planetMinecraft", planetMinecraft);
            plugin.getConfig().set("minecraftSeverNet", minecraftSeverNet);
            // Set all Ints
            plugin.getConfig().set("minecraftListOrg", minecraftListOrg);
            // Set all Bools
            plugin.getConfig().set("voting", voting);
            plugin.getConfig().set("updateChecker", updateChecker);
            plugin.getConfig().set("updateMessage", updateMessage);
            // Set all Lists
            plugin.getConfig().set("voteRewards", voteRewards);
            plugin.getConfig().set("serverDescription", serverDescription);
  "[" + plugin.pFile.getName() + "] Config Updated");

    #3 Legotyrell, May 24, 2017
    Last edited: May 24, 2017
  4. so you are referring to a custom one plugin.
    Code (Java):
    public void saveDefaultConfig()
            if (!configFile.exists())
                this.plugin.saveResource(fileName, false);
    create your own saveDefaultConfig() method then call it onEnable() at the class that extends JavaPlugin
  5. What @Lifeonblack wrote may work, but I know that this also works:

    Code (Text):;
  6. It's not custom @Lifeonblack.
    Code (Text):
            File configFile = new File(plugin.getDataFolder(), "config.yml");
    Is there to delete the file and thats all it does.
    Plus saveResource() does not work and save() does not exist
  7. oh I see so you want to reload the plugin's config if it is not in latest I suppose,
    the easiest way is to add onEnable() saveDefaultConfig();
    if you think your config is not updated delete it in plugin data folder and reload the plugin it will create the latest config in the resource.
  8. I want the vars in the old config to be set in the new one so you don't have to go through the new config and set the vars from the old one. saveConfig() does it but does not keep the comments that is the entire point of this post.
  9. how many times should I say? placing saveDefaultConfig(); onEnable() will save the COMMENTS, will LOAD the new config if it isn't created yet, So if you want to have the New config.yml you just have to delete the old config.yml I get the point of your topic so please try this solution before arguing.
  10. Ok, tried it. Creates the Config but does not set the vars from the old config.
    Another option could be to update the config but don't change existing data