[YAML] Set and save unset values

Discussion in 'Plugin Development' started by blackvoid, Jan 23, 2012.

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

    blackvoid

    Im currently switching over to yaml configs in one of my plugins.
    The only problem I have is if a yaml variable doesnt exist in the yml file, I want to set it and save it.
    I've tried (add/set)Default with no luck. So if I decide to add a new variable, it wont get added to the yml file.

    Currently the code looks like this.
    Code:
                Config = new YamlConfiguration();
                if(Create == false){
                    Config.load(Settings);
                }else{
                    Config.createSection("General");
                    Config.createSection("sql");
                    Config.createSection("Other");
                    Config.set("General.ConnectionType", "file");
                    Config.set("General.UpdateInterval", 60);
                    Config.set("General.debug", false);
                    Config.set("General.DisableOnFailure", false);
                    Config.set("General.DisconnectMessage", "You are not on the whitelist!");
                    Config.set("sql.type", "mysql");
                    Config.set("sql.host", "localhost");
                    Config.set("sql.username", "username");
                    Config.set("sql.password", "password");
                    Config.set("sql.table", "table");
                    Config.set("sql.database", "minecraft");
                    Config.set("sql.query", "SELECT {name} FROM `{table}`;");
                    Config.set("sql.UserField", "name");
                    Config.set("Other.url", "http://mywebsite.com/whitelist.php");
                    Config.set("Other.file", "white-list.txt");
                }
                Config.save(Settings);
     
  2. Alright, first off, your going to want to use the 'default config' feature. Basically, create a new file in your main class (Generally config.yml) and add all the defaults. Then, if the file doesn't exist, do this:
    Code:java
    1.  
    2. Config.options().loadDefaults(true);
    3.  

    Also, generally variable names are lower case :) (Config -> config)
     
    blackvoid likes this.
Thread Status:
Not open for further replies.

Share This Page