9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2025-12-28 11:29:11 +00:00

[ci skip] Update LeafConfig

This commit is contained in:
Dreeam
2025-02-20 17:30:35 -05:00
parent 1e6bd865d6
commit 329bc97960
3 changed files with 90 additions and 22 deletions

View File

@@ -13,7 +13,8 @@ import java.util.Set;
public abstract class ConfigModules extends LeafConfig {
private static final Set<ConfigModules> modules = new HashSet<>();
private static final Set<ConfigModules> 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();
}

View File

@@ -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<Void> 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<Void> 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<String> buildSparkExtraConfigs() {

View File

@@ -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<String, Object> 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<String, Object> 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<String, Object> 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<String> 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);
}