feat: add config query, set and reset support
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
--- /dev/null
|
||||
+++ b/src/main/java/me/earthme/luminol/commands/LuminolConfigCommand.java
|
||||
@@ -1,0 +_,80 @@
|
||||
@@ -1,0 +_,121 @@
|
||||
+package me.earthme.luminol.commands;
|
||||
+
|
||||
+import me.earthme.luminol.config.LuminolConfig;
|
||||
@@ -23,16 +23,26 @@
|
||||
+ this.setUsage("/luminolconfig");
|
||||
+ }
|
||||
+
|
||||
+ public void wrongUse(CommandSender sender) {
|
||||
+ sender.sendMessage(
|
||||
+ Component
|
||||
+ .text("Wrong use!")
|
||||
+ .color(TextColor.color(255, 0, 0))
|
||||
+ );
|
||||
+ }
|
||||
+
|
||||
+ @Override
|
||||
+ public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args, @Nullable Location location) throws IllegalArgumentException {
|
||||
+ final List<String> result = new ArrayList<>();
|
||||
+
|
||||
+ if (args.length == 1) {
|
||||
+ result.add("query");
|
||||
+ // result.add("set"); // TODO Later
|
||||
+ // result.add("reload"); // TODO Later
|
||||
+ result.add("set");
|
||||
+ result.add("reset");
|
||||
+ result.add("reload");
|
||||
+ } else if (args.length == 2 && args[0] != "reload") {
|
||||
+ result.addAll(LuminolConfig.getAllConfigPaths(args[1]));
|
||||
+ }
|
||||
+
|
||||
+ return result;
|
||||
+ }
|
||||
+
|
||||
@@ -46,11 +56,7 @@
|
||||
+ }
|
||||
+
|
||||
+ if (args.length < 1) {
|
||||
+ sender.sendMessage(
|
||||
+ Component
|
||||
+ .text("Wrong use!\n")
|
||||
+ .color(TextColor.color(255, 0, 0))
|
||||
+ );
|
||||
+ wrongUse(sender);
|
||||
+ return true;
|
||||
+ }
|
||||
+
|
||||
@@ -62,18 +68,53 @@
|
||||
+ .color(TextColor.color(0, 255, 0))
|
||||
+ ));
|
||||
+ }
|
||||
+
|
||||
+ case "set" -> {
|
||||
+ // TODO Later
|
||||
+ if (args.length == 2 || args.length > 3) {
|
||||
+ wrongUse(sender);
|
||||
+ return true;
|
||||
+ } else if (LuminolConfig.setConfig(args[1], args[2])) {
|
||||
+ LuminolConfig.reloadAsync().thenAccept(nullValue -> sender.sendMessage(
|
||||
+ Component
|
||||
+ .text("Set Config " + args[1] + " to " + args[2] + " successfully!")
|
||||
+ .color(TextColor.color(0, 255, 0))
|
||||
+ ));
|
||||
+ } else {
|
||||
+ sender.sendMessage(
|
||||
+ Component
|
||||
+ .text("Failed to set config " + args[1] + " to " + args[2] + "!")
|
||||
+ .color(TextColor.color(255, 0, 0))
|
||||
+ );
|
||||
+ }
|
||||
+ }
|
||||
+ case "reset" -> {
|
||||
+ if (args.length != 2) {
|
||||
+ wrongUse(sender);
|
||||
+ return true;
|
||||
+ } else {
|
||||
+ LuminolConfig.resetConfig(args[1]);
|
||||
+ LuminolConfig.reloadAsync().thenAccept(nullValue -> sender.sendMessage(
|
||||
+ Component
|
||||
+ .text("Reset Config " + args[1] + " to " + LuminolConfig.getConfig(args[1]) + " successfully!")
|
||||
+ .color(TextColor.color(0, 255, 0))
|
||||
+ ));
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
+ case "query" -> {
|
||||
+ // TODO Later
|
||||
+ if (args.length != 2) {
|
||||
+ wrongUse(sender);
|
||||
+ return true;
|
||||
+ } else {
|
||||
+ sender.sendMessage(
|
||||
+ Component
|
||||
+ .text("Config " + args[1] + " is " + LuminolConfig.getConfig(args[1]) + "!")
|
||||
+ .color(TextColor.color(0, 255, 0))
|
||||
+ );
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
+ default -> sender.sendMessage(
|
||||
+ Component
|
||||
+ .text("Unknown action!\n")
|
||||
+ .text("Unknown action!")
|
||||
+ .color(TextColor.color(255, 0, 0))
|
||||
+ );
|
||||
+ }
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
--- /dev/null
|
||||
+++ b/src/main/java/me/earthme/luminol/config/LuminolConfig.java
|
||||
@@ -1,0 +_,351 @@
|
||||
@@ -1,0 +_,378 @@
|
||||
+package me.earthme.luminol.config;
|
||||
+
|
||||
+import com.electronwill.nightconfig.core.CommentedConfig;
|
||||
@@ -37,12 +37,14 @@
|
||||
+ private static final File baseConfigFolder = new File("luminol_config");
|
||||
+ private static final File baseConfigFile = new File(baseConfigFolder, "luminol_global_config.toml");
|
||||
+ private static final Set<IConfigModule> allInstanced = new HashSet<>();
|
||||
+ public static boolean alreadyInited = false;
|
||||
+ private static final Map<String, Object> stagedConfigMap = new HashMap<>();
|
||||
+ private static final Map<String, Object> defaultvalueMap = new HashMap<>();
|
||||
+ public static boolean alreadyInit = false;
|
||||
+ private static CommentedFileConfig configFileInstance;
|
||||
+
|
||||
+ public static void setupLatch() {
|
||||
+ Bukkit.getCommandMap().register("luminolconfig", "luminol", new LuminolConfigCommand());
|
||||
+ alreadyInited = true;
|
||||
+ alreadyInit = true;
|
||||
+ }
|
||||
+
|
||||
+ public static void reload() {
|
||||
@@ -97,7 +99,7 @@
|
||||
+ throw new RuntimeException(e);
|
||||
+ }
|
||||
+
|
||||
+ configFileInstance.save();
|
||||
+ saveConfigs();
|
||||
+ }
|
||||
+
|
||||
+ private static void loadAllModules() throws IllegalAccessException {
|
||||
@@ -122,7 +124,7 @@
|
||||
+ for (Field field : fields) {
|
||||
+ int modifiers = field.getModifiers();
|
||||
+ if (Modifier.isStatic(modifiers) && !Modifier.isFinal(modifiers)) {
|
||||
+ boolean skipLoad = field.getAnnotation(DoNotLoad.class) != null || (alreadyInited && field.getAnnotation(HotReloadUnsupported.class) != null);
|
||||
+ boolean skipLoad = field.getAnnotation(DoNotLoad.class) != null || (alreadyInit && field.getAnnotation(HotReloadUnsupported.class) != null);
|
||||
+ ConfigInfo configInfo = field.getAnnotation(ConfigInfo.class);
|
||||
+
|
||||
+ if (skipLoad || configInfo == null) {
|
||||
@@ -133,6 +135,7 @@
|
||||
+
|
||||
+ field.setAccessible(true);
|
||||
+ final Object currentValue = field.get(null);
|
||||
+ if (!alreadyInit) defaultvalueMap.put(fullConfigKeyName, currentValue);
|
||||
+ boolean removed = fullConfigKeyName.equals("removed.removed_config.removed");
|
||||
+
|
||||
+ if (!configFileInstance.contains(fullConfigKeyName) || removed) {
|
||||
@@ -180,7 +183,21 @@
|
||||
+ continue;
|
||||
+ }
|
||||
+
|
||||
+ final Object actuallyValue = configFileInstance.get(fullConfigKeyName);
|
||||
+ Object actuallyValue;
|
||||
+ if (stagedConfigMap.containsKey(fullConfigKeyName)) {
|
||||
+ actuallyValue = stagedConfigMap.get(fullConfigKeyName);
|
||||
+ if (actuallyValue == null) actuallyValue = defaultvalueMap.get(fullConfigKeyName);
|
||||
+ stagedConfigMap.remove(fullConfigKeyName);
|
||||
+ } else {
|
||||
+ actuallyValue = configFileInstance.get(fullConfigKeyName);
|
||||
+ }
|
||||
+ try {
|
||||
+ actuallyValue = tryTransform(field.get(null).getClass(), actuallyValue);
|
||||
+ configFileInstance.set(fullConfigKeyName, actuallyValue);
|
||||
+ } catch (IllegalFormatConversionException e) {
|
||||
+ resetConfig(fullConfigKeyName);
|
||||
+ logger.error("Failed to transform config {}, reset to default!", fullConfigKeyName);
|
||||
+ }
|
||||
+ field.set(null, actuallyValue);
|
||||
+ }
|
||||
+ }
|
||||
@@ -202,31 +219,43 @@
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
+ public static boolean setConfigAndSave(String[] keys, Object value) {
|
||||
+ return setConfigAndSave(String.join(".", keys), value);
|
||||
+ }
|
||||
+
|
||||
+ public static boolean setConfigAndSave(String key, Object value) {
|
||||
+ if (setConfig(key, value)) {
|
||||
+ saveConfig();
|
||||
+ return true;
|
||||
+ }
|
||||
+ return false;
|
||||
+ }
|
||||
+
|
||||
+ public static boolean setConfig(String[] keys, Object value) {
|
||||
+ return setConfig(String.join(".", keys), value);
|
||||
+ }
|
||||
+
|
||||
+ public static boolean setConfig(String key, Object value) {
|
||||
+ if (configFileInstance.contains(key) && configFileInstance.get(key) != null) {
|
||||
+ configFileInstance.set(key, value);
|
||||
+ stagedConfigMap.put(key, value);
|
||||
+ return true;
|
||||
+ }
|
||||
+ return false;
|
||||
+ }
|
||||
+
|
||||
+ public static void saveConfig() {
|
||||
+ private static Object tryTransform(Class<?> targetType, Object value) {
|
||||
+ if (!targetType.isAssignableFrom(value.getClass())) {
|
||||
+ try {
|
||||
+ if (targetType == Integer.class) {
|
||||
+ value = Integer.parseInt(value.toString());
|
||||
+ } else if (targetType == Double.class) {
|
||||
+ value = Double.parseDouble(value.toString());
|
||||
+ } else if (targetType == Boolean.class) {
|
||||
+ value = Boolean.parseBoolean(value.toString());
|
||||
+ } else if (targetType == Long.class) {
|
||||
+ value = Long.parseLong(value.toString());
|
||||
+ } else if (targetType == Float.class) {
|
||||
+ value = Float.parseFloat(value.toString());
|
||||
+ } else if (targetType == String.class) {
|
||||
+ value = value.toString();
|
||||
+ }
|
||||
+ } catch (Exception e) {
|
||||
+ logger.error("Failed to transform value {}!", value);
|
||||
+ throw new IllegalFormatConversionException((char) 0, targetType);
|
||||
+ }
|
||||
+ }
|
||||
+ return value;
|
||||
+ }
|
||||
+
|
||||
+ public static void saveConfigs() {
|
||||
+ configFileInstance.save();
|
||||
+ }
|
||||
+
|
||||
@@ -235,9 +264,7 @@
|
||||
+ }
|
||||
+
|
||||
+ public static void resetConfig(String key) {
|
||||
+ configFileInstance.remove(key);
|
||||
+ configFileInstance.save();
|
||||
+ reload();
|
||||
+ stagedConfigMap.put(key, null);
|
||||
+ }
|
||||
+
|
||||
+ public static String getConfig(String[] keys) {
|
||||
@@ -245,10 +272,10 @@
|
||||
+ }
|
||||
+
|
||||
+ public static String getConfig(String key) {
|
||||
+ return configFileInstance.get(key);
|
||||
+ return configFileInstance.get(key).toString();
|
||||
+ }
|
||||
+
|
||||
+ public List<String> getAllConfigPaths(String prefix) {
|
||||
+ public static List<String> getAllConfigPaths(String prefix) {
|
||||
+ List<String> configPaths = getAllConfigPaths();
|
||||
+ return configPaths.stream().filter(path -> path.startsWith(prefix)).toList();
|
||||
+ }
|
||||
|
||||
Reference in New Issue
Block a user