Remove old config options when unused
This commit is contained in:
parent
07e34f2e8d
commit
1fde72e522
1 changed files with 24 additions and 0 deletions
|
|
@ -92,6 +92,29 @@ public interface YamlConfig {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes unused keys from the configuration.
|
||||
*/
|
||||
static void removeOldKeys(YamlConfig def, YamlConfig conf) {
|
||||
removeOldKeys(def, conf, "");
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes unused keys from the configuration, starting from the root node.
|
||||
*/
|
||||
static void removeOldKeys(YamlConfig def, YamlConfig conf, String root) {
|
||||
if (def == null) return;
|
||||
for (String key : conf.getKeys()) {
|
||||
String defKey = (root.isEmpty() ? "" : root + ".") + key;
|
||||
Object value = conf.get(key);
|
||||
if (def.get(defKey) == null) {
|
||||
conf.set(key, null);
|
||||
} else if (value instanceof YamlConfig) {
|
||||
removeOldKeys(def, (YamlConfig) value, defKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initiates a Configuration from a file with associated defaults.
|
||||
* @param def The default Configuration to be applied.
|
||||
|
|
@ -100,6 +123,7 @@ public interface YamlConfig {
|
|||
*/
|
||||
static YamlConfig init(YamlConfig def, YamlConfig conf) {
|
||||
YamlConfig.addDefaults(def, conf);
|
||||
YamlConfig.removeOldKeys(def, conf);
|
||||
|
||||
try {
|
||||
conf.save();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue