Remove old config options when unused

This commit is contained in:
Frank van der Heijden 2021-01-30 14:06:24 +01:00
parent 07e34f2e8d
commit 1fde72e522
No known key found for this signature in database
GPG key ID: 26DA56488D314D11

View file

@ -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();