From 329bc97960d6fc6d15870bb1e7aec6a307957d35 Mon Sep 17 00:00:00 2001 From: Dreeam <61569423+Dreeam-qwq@users.noreply.github.com> Date: Thu, 20 Feb 2025 17:30:35 -0500 Subject: [PATCH] [ci skip] Update LeafConfig --- .../org/dreeam/leaf/config/ConfigModules.java | 9 ++- .../org/dreeam/leaf/config/LeafConfig.java | 48 +++++++++++----- .../dreeam/leaf/config/LeafGlobalConfig.java | 55 +++++++++++++++++-- 3 files changed, 90 insertions(+), 22 deletions(-) diff --git a/leaf-server/src/main/java/org/dreeam/leaf/config/ConfigModules.java b/leaf-server/src/main/java/org/dreeam/leaf/config/ConfigModules.java index d5208990..eedc98c5 100644 --- a/leaf-server/src/main/java/org/dreeam/leaf/config/ConfigModules.java +++ b/leaf-server/src/main/java/org/dreeam/leaf/config/ConfigModules.java @@ -13,7 +13,8 @@ import java.util.Set; public abstract class ConfigModules extends LeafConfig { - private static final Set modules = new HashSet<>(); + private static final Set MODULES = new HashSet<>(); + public LeafGlobalConfig config; public ConfigModules() { @@ -26,7 +27,7 @@ public abstract class ConfigModules extends LeafConfig { ConfigModules module = (ConfigModules) clazz.getConstructor().newInstance(); module.onLoaded(); - modules.add(module); + MODULES.add(module); for (Field field : getAnnotatedStaticFields(clazz, Experimental.class)) { if (!(field.get(null) instanceof Boolean enabled)) continue; if (enabled) { @@ -50,5 +51,9 @@ public abstract class ConfigModules extends LeafConfig { return fields; } + public static void clearModules() { + MODULES.clear(); + } + public abstract void onLoaded(); } diff --git a/leaf-server/src/main/java/org/dreeam/leaf/config/LeafConfig.java b/leaf-server/src/main/java/org/dreeam/leaf/config/LeafConfig.java index bd46640f..ba03feb4 100644 --- a/leaf-server/src/main/java/org/dreeam/leaf/config/LeafConfig.java +++ b/leaf-server/src/main/java/org/dreeam/leaf/config/LeafConfig.java @@ -1,6 +1,9 @@ package org.dreeam.leaf.config; import io.papermc.paper.configuration.GlobalConfiguration; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.NamedTextColor; +import net.minecraft.Util; import org.dreeam.leaf.config.modules.misc.SentryDSN; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; @@ -31,6 +34,8 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.bukkit.Bukkit; import org.bukkit.World; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; /* * Yoinked from: https://github.com/xGinko/AnarchyExploitFixes/ & https://github.com/LuminolMC/Luminol @@ -46,24 +51,28 @@ public class LeafConfig { private static LeafGlobalConfig leafGlobalConfig; + //private static int preMajorVer; + private static int preMinorVer; + //private static int currMajorVer; + private static int currMinorVer; + /* Load & Reload */ - public static void reload() { - try { - long begin = System.nanoTime(); - LOGGER.info("Reloading config..."); + public static @NotNull CompletableFuture reloadAsync(CommandSender sender) { + return CompletableFuture.runAsync(() -> { + try { + long begin = System.nanoTime(); - loadConfig(false); + ConfigModules.clearModules(); + loadConfig(false); - LOGGER.info("Successfully reloaded config in {}ms.", (System.nanoTime() - begin) / 1_000_000); - } catch (Exception e) { - LOGGER.error("Failed to reload config.", e); - } - } - - @Contract(" -> new") - public static @NotNull CompletableFuture reloadAsync() { - return new CompletableFuture<>(); + final String success = String.format("Successfully reloaded config in %sms.", (System.nanoTime() - begin) / 1_000_000); + Command.broadcastCommandMessage(sender, Component.text(success, NamedTextColor.GREEN)); + } catch (Exception e) { + Command.broadcastCommandMessage(sender, Component.text("Failed to reload config. See error in console!", NamedTextColor.RED)); + LOGGER.error(e); + } + }, Util.ioPool()); } public static void loadConfig() { @@ -194,6 +203,17 @@ public class LeafConfig { } } + // TODO + public static void loadConfigVersion(String preVer, String currVer) { + int currMinor; + int preMinor; + + // First time user + if (preVer == null) { + + } + } + /* Register Spark profiler extra server configurations */ private static List buildSparkExtraConfigs() { diff --git a/leaf-server/src/main/java/org/dreeam/leaf/config/LeafGlobalConfig.java b/leaf-server/src/main/java/org/dreeam/leaf/config/LeafGlobalConfig.java index 5490746f..298017a4 100644 --- a/leaf-server/src/main/java/org/dreeam/leaf/config/LeafGlobalConfig.java +++ b/leaf-server/src/main/java/org/dreeam/leaf/config/LeafGlobalConfig.java @@ -10,13 +10,18 @@ import java.util.Map; public class LeafGlobalConfig { - protected static ConfigFile configFile; + private static final String CURRENT_VERSION = "3.0"; private static final String CURRENT_REGION = Locale.getDefault().getCountry().toUpperCase(Locale.ROOT); // It will be in uppercase by default, just make sure - protected static final boolean isCN = CURRENT_REGION.equals("CN"); + private static final boolean isCN = CURRENT_REGION.equals("CN"); + + private static ConfigFile configFile; public LeafGlobalConfig(boolean init) throws Exception { configFile = ConfigFile.loadConfig(new File(LeafConfig.I_CONFIG_FOLDER, LeafConfig.I_GLOBAL_CONFIG_FILE)); - configFile.set("config-version", 3.0); + + LeafConfig.loadConfigVersion(getString("config-version"), CURRENT_VERSION); + configFile.set("config-version", CURRENT_VERSION); + configFile.addComments("config-version", pickStringRegionBased(""" Leaf Config GitHub Repo: https://github.com/Winds-Studio/Leaf @@ -42,6 +47,8 @@ public class LeafGlobalConfig { // Config Utilities + /* getAndSet */ + public void createTitledSection(String title, String path) { configFile.addSection(title); configFile.addDefault(path, null); @@ -97,6 +104,13 @@ public class LeafGlobalConfig { return configFile.getStringList(path); } + public ConfigSection getConfigSection(String path, Map defaultKeyValue, String comment) { + configFile.addDefault(path, null, comment); + configFile.makeSectionLenient(path); + defaultKeyValue.forEach((string, object) -> configFile.addExample(path + "." + string, object)); + return configFile.getConfigSection(path); + } + public ConfigSection getConfigSection(String path, Map defaultKeyValue) { configFile.addDefault(path, null); configFile.makeSectionLenient(path); @@ -104,10 +118,39 @@ public class LeafGlobalConfig { return configFile.getConfigSection(path); } - public ConfigSection getConfigSection(String path, Map defaultKeyValue, String comment) { - configFile.addDefault(path, null, comment); + /* get */ + + public Boolean getBoolean(String path) { + String value = configFile.getString(path, null); + + return value == null ? null : Boolean.parseBoolean(value); + } + + public String getString(String path) { + return configFile.getString(path, null); + } + + public Double getDouble(String path) { + String value = configFile.getString(path, null); + + return value == null ? null : Double.parseDouble(value); // TODO: Need to check whether need to handle NFE correctly + } + + public Integer getInt(String path) { + String value = configFile.getString(path, null); + + return value == null ? null : Integer.parseInt(value); // TODO: Need to check whether need to handle NFE correctly + } + + public List getList(String path) { + return configFile.getList(path, null); + } + + // TODO, check + public ConfigSection getConfigSection(String path) { + configFile.addDefault(path, null); configFile.makeSectionLenient(path); - defaultKeyValue.forEach((string, object) -> configFile.addExample(path + "." + string, object)); + //defaultKeyValue.forEach((string, object) -> configFile.addExample(path + "." + string, object)); return configFile.getConfigSection(path); }