How to handle updated configs

Discussion in 'Plugin Development' started by Codisimus, Apr 6, 2013.

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

    Codisimus

    I was wondering how devs handle updated configs. For instance if I add a new setting to the config, how would I let the user know that they are missing some options by using an outdated config file?
     
  2. Offline

    noraver

    by the sting of errors that pop up on the console lol sorry i looked at this as well could not find anything even updating the config.yml in my own plugin will not replace the one that is already made lintel they delete it there plug i don't want to overwrite anything they have saved :(
     
  3. Offline

    Codisimus

    noraver Well there shouldn't be any errors, the default values will be loaded from your config.
     
  4. Offline

    noraver

    if i added more stings to my plugin then added it to the config,
    if the user does not replace or delete there old config then there would be conflict.
    The plugin would be trying to read the config and with non of the stuff added is what i was saying..
     
  5. Offline

    Codisimus

    noraver Then you must not be using Bukkit's configuration API.
     
  6. Offline

    noraver

    Codisimus i am :cool: and i have no problems with my plugin i was just throwing out example
     
  7. Offline

    Scizzr

    You could just set the options if they're not set already and then let them know how many things were added. I use this frequently:


    Code:
    public class Main extends JavaPlugin {
        File fileFolder;
        public void onEnable {
            fileFolder = getDataFolder();
        }
    }
     
     
     
    public class Config {
        Main plugin;
       
        public Config(Main plugin) {
            this.plugin = plugin;
        }
       
        //default values
        public boolean allowOps = false;
    }
     
     
     
    public class ConfigMain {
        Main plugin;
        boolean changed = false;
     
        public ConfigMain (Main plugin) {
            this.plugin = plugin;
            init();
        }
     
        public void init() {
            File file = new File(plugin.fileFolder + "/config.yml");
     
            if (!file.exists()) {
                try {
                    file.createNewFile();
                } catch (Exception ex) {  }
            }
     
            loadConfig();
        }
     
        public void loadConfig() {
            File file = plugin.fileConfig;
     
            try {
                config.load(file);
            } catch (Exception ex) {  }
     
            // Config - Main
            checkOption(config, "allowOps", plugin.config.allowops);
            plugin.config.allowops = config.getBoolean("allowOps");
     
            if (changed) {
                //config.options().header("Comments!");
                try { config.save(file); } catch (Exception ex) {  }
            }
        }
     
        void checkOption(YamlConfiguration config, String node, Object def) {
            if (!config.isSet(node)) {
                config.set(node, def);
                changed = true;
            }
        }
     
        void editOption(YamlConfiguration config, String nodeOld, String nodeNew) {
            if (config.isSet(nodeOld)) {
                if (nodeNew != null) {
                    config.set(nodeNew, config.get(nodeOld));
                }
           
                config.set(nodeOld, null);
                changed = true;
            }
        }
    }
    
    Let me know if this works for ya.
    Credits for the logic go to MiracleM4n (mChatSuite plugin)
     
Thread Status:
Not open for further replies.

Share This Page