Util Make your (custom) config update and copy/delete the defaults!

Discussion in 'Resources' started by FisheyLP, Jul 15, 2015.

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

    FisheyLP

    Don't you know the struggle? You had finished your awsome plugin and decided to make a new update. In the new update you put a new setting in the config.yml (in your ide). But when you export the plugin and reload the server, the new setting you put in the config.yml isn't in the config.yml of your plugin folder!

    Because of this I made a small but really helpful method. You actually need to have the .yml files in the same directory as your plugin.yml.
    And you need to run this method AFTER you have run saveDefaultConfig() or created your custom config(-file).
    Default config:
    Code:
    public void matchConfig() {
    try {
                InputStream is = getResource("config.yml");
                if (is != null) {
                    YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(is);
                    for (String key : defConfig.getConfigurationSection("").getKeys(false))
                        if (!getConfig().contains(key)) getConfig().set(key, defConfig.getConfigurationSection(key));
             
                    for (String key : getConfig().getConfigurationSection("").getKeys(false))
                        if (!defConfig.contains(key)) getConfig().set(key, null);
             
                    saveConfig();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
    }
    Custom config:
    Code:
    public void matchConfig(FileConfiguration config, File file) {
    try {
                InputStream is = getResource(file.getName());
                if (is != null) {
                    YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(is);
                    for (String key : defConfig.getConfigurationSection("").getKeys(false))
                        if (!config.contains(key)) config.set(key, defConfig.getConfigurationSection(key));
             
                    for (String key : config.getConfigurationSection("").getKeys(false))
                        if (!defConfig.contains(key)) config.set(key, null);
             
                    config.save(file);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
    }
    I explain what it does line by line:
    3. Get an InputStream from the file (you have in the same directory as your plugin.yml) with the .getResource method of JavaPlugin.
    4. Check if the InputStream is not null (if the file doesn't exist in your ide)
    5. Load a YamlConfiguration from the InputStream.
    6. Loop through all configuration sections in the defConfig.
    7. If the real config doesn't contain this path, set the path to the defConfigs configuration section.
    9.-10. It does the same as 6.-7. except that it searches the real config for paths that the defConfig doesn't contain and then remove it.
    12. Save the config.

    Extra infos: When looping over the configuration section .getKeys(false), it works like this (red colored is the where it iterates over):
    Foo:
    ..bar: true
    Test: '&6Hello World!'
     
    Last edited: Jul 15, 2015
  2. Offline

    Hawktasard

  3. Offline

    FisheyLP

  4. Offline

    teej107

    @FisheyLP this is why addDefault() exists
     
  5. Offline

    FisheyLP

    But you need to do this for every single section of the config. And I'm not sure if .addDefault removes things from the config that aren't there by default anymore
     
  6. Offline

    teej107

    And that's a problem why? The addDefault method doesn't remove things from the config unless you specifically set the value as null.
     
  7. Offline

    Agentleader1

    Sorry for a "dead thread bump" if you consider it so.

    This is a wonderful Util! However, this method will delete all comments in the config when updating/matching if you will. Is there a way we can fix that and have comments get put into the newer config.yml? In this event, I would like to copy comments of the "config in the ide" as you said, rather than keeping previous ones.

    ~Thanks!
     
  8. Online

    timtower Moderator Moderator

    @Agentleader1 That is unrelated to this Util though, that is default YAML/Bukkit behaviour.
     
  9. Offline

    Agentleader1

Thread Status:
Not open for further replies.

Share This Page