Resolve configuration resetting concern

This commit is contained in:
CmdrKittens
2020-04-09 22:21:22 -04:00
parent 55a314a5c6
commit aaf98cad02
2 changed files with 11 additions and 6 deletions
@@ -311,7 +311,7 @@ public class PlayerVaults extends JavaPlugin {
private void loadConfig() { private void loadConfig() {
File configYaml = new File(this.getDataFolder(), "config.yml"); File configYaml = new File(this.getDataFolder(), "config.yml");
if (configYaml.exists()) { if (!(new File(this.getDataFolder(), "config.conf").exists()) && configYaml.exists()) {
this.config.setFromConfig(this.getLogger(), this.getConfig()); this.config.setFromConfig(this.getLogger(), this.getConfig());
try { try {
Files.move(configYaml.toPath(), this.getDataFolder().toPath().resolve("old_unused_config.yml")); Files.move(configYaml.toPath(), this.getDataFolder().toPath().resolve("old_unused_config.yml"));
@@ -18,6 +18,8 @@
package com.drtshock.playervaults.config; package com.drtshock.playervaults.config;
import com.drtshock.playervaults.PlayerVaults; import com.drtshock.playervaults.PlayerVaults;
import com.drtshock.playervaults.config.annotation.Comment;
import com.drtshock.playervaults.config.annotation.ConfigName;
import com.drtshock.playervaults.config.annotation.WipeOnReload; import com.drtshock.playervaults.config.annotation.WipeOnReload;
import com.drtshock.playervaults.lib.com.typesafe.config.Config; import com.drtshock.playervaults.lib.com.typesafe.config.Config;
import com.drtshock.playervaults.lib.com.typesafe.config.ConfigFactory; import com.drtshock.playervaults.lib.com.typesafe.config.ConfigFactory;
@@ -27,8 +29,6 @@ import com.drtshock.playervaults.lib.com.typesafe.config.ConfigValueFactory;
import com.drtshock.playervaults.lib.com.typesafe.config.ConfigValueType; import com.drtshock.playervaults.lib.com.typesafe.config.ConfigValueType;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
import com.drtshock.playervaults.config.annotation.Comment;
import com.drtshock.playervaults.config.annotation.ConfigName;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@@ -91,7 +91,11 @@ public class Loader {
Loader.types.add(String.class); Loader.types.add(String.class);
} }
private static @NonNull ConfigValue loadNode(@NonNull Config current, @NonNull Object object) throws IllegalAccessException { private static @NonNull ConfigValue loadNode(@NonNull Config config, @NonNull Object object) throws IllegalAccessException {
return loadNode(config, "", object);
}
private static @NonNull ConfigValue loadNode(@NonNull Config config, String path, @NonNull Object object) throws IllegalAccessException {
Map<String, ConfigValue> map = new HashMap<>(); Map<String, ConfigValue> map = new HashMap<>();
for (Field field : Loader.getFields(object.getClass())) { for (Field field : Loader.getFields(object.getClass())) {
if (field.isSynthetic()) { if (field.isSynthetic()) {
@@ -108,7 +112,8 @@ public class Loader {
ConfigName configName = field.getAnnotation(ConfigName.class); ConfigName configName = field.getAnnotation(ConfigName.class);
Comment comment = field.getAnnotation(Comment.class); Comment comment = field.getAnnotation(Comment.class);
String confName = configName == null || configName.value().isEmpty() ? field.getName() : configName.value(); String confName = configName == null || configName.value().isEmpty() ? field.getName() : configName.value();
ConfigValue curValue = Loader.getOrNull(current, confName); String newPath = path.isEmpty() ? confName : (path + '.' + confName);
ConfigValue curValue = Loader.getOrNull(config, newPath);
boolean needsValue = curValue == null; boolean needsValue = curValue == null;
ConfigValue newValue; ConfigValue newValue;
@@ -131,7 +136,7 @@ public class Loader {
} }
} }
} else { } else {
newValue = Loader.loadNode(current, defaultValue); newValue = Loader.loadNode(config, newPath, defaultValue);
} }
if (comment != null) { if (comment != null) {
newValue = newValue.withOrigin(newValue.origin().withComments(Arrays.asList(comment.value().split("\n")))); newValue = newValue.withOrigin(newValue.origin().withComments(Arrays.asList(comment.value().split("\n"))));