mirror of
https://github.com/Winds-Studio/Leaf.git
synced 2025-12-22 08:29:28 +00:00
318 lines
12 KiB
Diff
318 lines
12 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: Dreeam <61569423+Dreeam-qwq@users.noreply.github.com>
|
|
Date: Wed, 12 Oct 2022 10:42:15 -0400
|
|
Subject: [PATCH] Leaf Config
|
|
|
|
|
|
diff --git a/build.gradle.kts b/build.gradle.kts
|
|
index a8a720045804cded8f8dffc1bfdd20710b8f0c82..a53b30ab2315fc0b2ce07d9449042842e6ca3b39 100644
|
|
--- a/build.gradle.kts
|
|
+++ b/build.gradle.kts
|
|
@@ -21,6 +21,9 @@ dependencies {
|
|
exclude("io.papermc.paper", "paper-api")
|
|
}
|
|
// Gale end - project setup
|
|
+
|
|
+ implementation("com.electronwill.night-config:toml:3.6.7") // Leaf - Night config
|
|
+
|
|
// Paper start
|
|
implementation("org.jline:jline-terminal-jansi:3.21.0")
|
|
implementation("net.minecrell:terminalconsoleappender:1.3.0")
|
|
diff --git a/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java b/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java
|
|
index 2c8eb9294890955f71382ed3884874cc827bab5e..bebafdf670ef1783ccae3b93fcda7d0eaef38d3e 100644
|
|
--- a/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java
|
|
+++ b/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java
|
|
@@ -212,6 +212,7 @@ public class DedicatedServer extends MinecraftServer implements ServerInterface
|
|
galeConfigurations.initializeGlobalConfiguration(this.registryAccess());
|
|
galeConfigurations.initializeWorldDefaultsConfiguration(this.registryAccess());
|
|
// Gale end - Gale configuration
|
|
+ org.dreeam.leaf.config.LeafConfig.loadConfig(); // Leaf
|
|
// Paper start - fix converting txt to json file; convert old users earlier after PlayerList creation but before file load/save
|
|
if (this.convertOldUsers()) {
|
|
this.getProfileCache().save(false); // Paper
|
|
diff --git a/src/main/java/org/dreeam/leaf/config/ConfigInfo.java b/src/main/java/org/dreeam/leaf/config/ConfigInfo.java
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..60b81df55d33a9546ac77bf3b8ef667cb03094db
|
|
--- /dev/null
|
|
+++ b/src/main/java/org/dreeam/leaf/config/ConfigInfo.java
|
|
@@ -0,0 +1,10 @@
|
|
+package org.dreeam.leaf.config;
|
|
+
|
|
+import java.lang.annotation.Retention;
|
|
+import java.lang.annotation.RetentionPolicy;
|
|
+
|
|
+@Retention(RetentionPolicy.RUNTIME)
|
|
+public @interface ConfigInfo {
|
|
+ String baseName();
|
|
+ String comments() default "";
|
|
+}
|
|
diff --git a/src/main/java/org/dreeam/leaf/config/DoNotLoad.java b/src/main/java/org/dreeam/leaf/config/DoNotLoad.java
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..42ce82d388336906a91547b81f6f70766f2b10f0
|
|
--- /dev/null
|
|
+++ b/src/main/java/org/dreeam/leaf/config/DoNotLoad.java
|
|
@@ -0,0 +1,8 @@
|
|
+package org.dreeam.leaf.config;
|
|
+
|
|
+import java.lang.annotation.Retention;
|
|
+import java.lang.annotation.RetentionPolicy;
|
|
+
|
|
+@Retention(RetentionPolicy.RUNTIME)
|
|
+public @interface DoNotLoad {
|
|
+}
|
|
diff --git a/src/main/java/org/dreeam/leaf/config/EnumConfigCategory.java b/src/main/java/org/dreeam/leaf/config/EnumConfigCategory.java
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..273bdcaafb3953e2a9830851d9ec7ec4a79f9d17
|
|
--- /dev/null
|
|
+++ b/src/main/java/org/dreeam/leaf/config/EnumConfigCategory.java
|
|
@@ -0,0 +1,22 @@
|
|
+package org.dreeam.leaf.config;
|
|
+
|
|
+public enum EnumConfigCategory {
|
|
+
|
|
+ ASYNC("async"),
|
|
+ PERFORMANCE("performance"),
|
|
+ NETWORK("network"),
|
|
+ FIXES("fixes"),
|
|
+ MISC("misc"),
|
|
+ GAMEPLAY("gameplay"),
|
|
+ WIP("wip");
|
|
+
|
|
+ private final String baseKeyName;
|
|
+
|
|
+ EnumConfigCategory(String baseKeyName) {
|
|
+ this.baseKeyName = baseKeyName;
|
|
+ }
|
|
+
|
|
+ public String getBaseKeyName() {
|
|
+ return this.baseKeyName;
|
|
+ }
|
|
+}
|
|
diff --git a/src/main/java/org/dreeam/leaf/config/IConfigModule.java b/src/main/java/org/dreeam/leaf/config/IConfigModule.java
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..2e23a83de1d2f3bbbd7014b265ed7a195e83e799
|
|
--- /dev/null
|
|
+++ b/src/main/java/org/dreeam/leaf/config/IConfigModule.java
|
|
@@ -0,0 +1,22 @@
|
|
+package org.dreeam.leaf.config;
|
|
+
|
|
+import com.electronwill.nightconfig.core.file.CommentedFileConfig;
|
|
+import org.jetbrains.annotations.NotNull;
|
|
+
|
|
+public interface IConfigModule {
|
|
+
|
|
+ EnumConfigCategory getCategory();
|
|
+ String getBaseName();
|
|
+
|
|
+ default void onLoaded(CommentedFileConfig configInstance) {
|
|
+ }
|
|
+
|
|
+ default <T> T get(String keyName, T defaultValue, @NotNull CommentedFileConfig config) {
|
|
+ if (!config.contains(keyName)) {
|
|
+ config.set(keyName, defaultValue);
|
|
+ return defaultValue;
|
|
+ }
|
|
+
|
|
+ return config.get(keyName);
|
|
+ }
|
|
+}
|
|
diff --git a/src/main/java/org/dreeam/leaf/config/LeafConfig.java b/src/main/java/org/dreeam/leaf/config/LeafConfig.java
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..86cb8a187d751c2e2842a0998ac07bcff15ca3cf
|
|
--- /dev/null
|
|
+++ b/src/main/java/org/dreeam/leaf/config/LeafConfig.java
|
|
@@ -0,0 +1,193 @@
|
|
+package org.dreeam.leaf.config;
|
|
+
|
|
+import com.electronwill.nightconfig.core.file.CommentedFileConfig;
|
|
+import org.jetbrains.annotations.NotNull;
|
|
+
|
|
+import java.io.File;
|
|
+import java.io.IOException;
|
|
+import java.lang.reflect.Field;
|
|
+import java.lang.reflect.InvocationTargetException;
|
|
+import java.lang.reflect.Modifier;
|
|
+import java.net.JarURLConnection;
|
|
+import java.net.URL;
|
|
+import java.net.URLDecoder;
|
|
+import java.nio.charset.StandardCharsets;
|
|
+import java.util.*;
|
|
+import java.util.jar.JarEntry;
|
|
+import java.util.jar.JarFile;
|
|
+
|
|
+import org.apache.logging.log4j.LogManager;
|
|
+import org.apache.logging.log4j.Logger;
|
|
+
|
|
+public class LeafConfig {
|
|
+ public static final Logger logger = LogManager.getLogger("LeafConfig");
|
|
+ private static final File baseConfigFolder = new File("leaf_config");
|
|
+ private static final File baseConfigFile = new File(baseConfigFolder, "leaf_global_config.toml");
|
|
+ private static final Set<IConfigModule> allInstanced = new HashSet<>();
|
|
+ private static CommentedFileConfig configFileInstance;
|
|
+
|
|
+ public static void loadConfig() throws IOException {
|
|
+ baseConfigFolder.mkdirs();
|
|
+
|
|
+ if (!baseConfigFile.exists()) {
|
|
+ baseConfigFile.createNewFile();
|
|
+ }
|
|
+
|
|
+ configFileInstance = CommentedFileConfig.ofConcurrent(baseConfigFile);
|
|
+
|
|
+ configFileInstance.load();
|
|
+ configFileInstance.getComment("""
|
|
+ Leaf Config
|
|
+ Github Repo: https://github.com/Winds-Studio/Leaf
|
|
+ Discord: dreeam___ | QQ: 2682173972""");
|
|
+
|
|
+ try {
|
|
+ instanceAllModule();
|
|
+ loadAllModules();
|
|
+ } catch (Exception e) {
|
|
+ logger.error("Failed to load config modules!", e);
|
|
+ }
|
|
+
|
|
+ configFileInstance.save();
|
|
+ }
|
|
+
|
|
+ private static void loadAllModules() throws IllegalAccessException {
|
|
+ for (IConfigModule instanced : allInstanced) {
|
|
+ loadForSingle(instanced);
|
|
+ }
|
|
+ }
|
|
+
|
|
+ private static void instanceAllModule() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
|
|
+ for (Class<?> clazz : getClasses("org.dreeam.leaf.config.modules")) {
|
|
+ if (IConfigModule.class.isAssignableFrom(clazz)) {
|
|
+ allInstanced.add((IConfigModule) clazz.getConstructor().newInstance());
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+
|
|
+ private static void loadForSingle(@NotNull IConfigModule singleConfigModule) throws IllegalAccessException {
|
|
+ final EnumConfigCategory category = singleConfigModule.getCategory();
|
|
+
|
|
+ Field[] fields = singleConfigModule.getClass().getDeclaredFields();
|
|
+
|
|
+ for (Field field : fields) {
|
|
+ int modifiers = field.getModifiers();
|
|
+ if (Modifier.isStatic(modifiers) && !Modifier.isFinal(modifiers)) {
|
|
+ boolean skipLoad = field.getAnnotation(DoNotLoad.class) != null;
|
|
+ ConfigInfo configInfo = field.getAnnotation(ConfigInfo.class);
|
|
+
|
|
+ if (skipLoad || configInfo == null) {
|
|
+ continue;
|
|
+ }
|
|
+
|
|
+ final String fullConfigKeyName = category.getBaseKeyName() + "." + singleConfigModule.getBaseName() + "." + configInfo.baseName();
|
|
+
|
|
+ field.setAccessible(true);
|
|
+ final Object currentValue = field.get(null);
|
|
+
|
|
+ if (!configFileInstance.contains(fullConfigKeyName)) {
|
|
+ if (currentValue == null) {
|
|
+ throw new UnsupportedOperationException("Config " + singleConfigModule.getBaseName() + "tried to add an null default value!");
|
|
+ }
|
|
+
|
|
+ final String comments = configInfo.comments();
|
|
+
|
|
+ if (!comments.isBlank()) {
|
|
+ configFileInstance.setComment(fullConfigKeyName, comments);
|
|
+ }
|
|
+
|
|
+ configFileInstance.add(fullConfigKeyName, currentValue);
|
|
+ continue;
|
|
+ }
|
|
+
|
|
+ final Object actuallyValue = configFileInstance.get(fullConfigKeyName);
|
|
+ field.set(null, actuallyValue);
|
|
+ }
|
|
+ }
|
|
+
|
|
+ singleConfigModule.onLoaded(configFileInstance);
|
|
+ }
|
|
+
|
|
+ public static @NotNull Set<Class<?>> getClasses(String pack) {
|
|
+ Set<Class<?>> classes = new LinkedHashSet<>();
|
|
+ String packageDirName = pack.replace('.', '/');
|
|
+ Enumeration<URL> dirs;
|
|
+
|
|
+ try {
|
|
+ dirs = Thread.currentThread().getContextClassLoader().getResources(packageDirName);
|
|
+ while (dirs.hasMoreElements()) {
|
|
+ URL url = dirs.nextElement();
|
|
+ String protocol = url.getProtocol();
|
|
+ if ("file".equals(protocol)) {
|
|
+ String filePath = URLDecoder.decode(url.getFile(), StandardCharsets.UTF_8);
|
|
+ findClassesInPackageByFile(pack, filePath, classes);
|
|
+ } else if ("jar".equals(protocol)) {
|
|
+ JarFile jar;
|
|
+ try {
|
|
+ jar = ((JarURLConnection) url.openConnection()).getJarFile();
|
|
+ Enumeration<JarEntry> entries = jar.entries();
|
|
+ findClassesInPackageByJar(pack, entries, packageDirName, classes);
|
|
+ } catch (IOException e) {
|
|
+ throw new RuntimeException(e);
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+ } catch (IOException e) {
|
|
+ throw new RuntimeException(e);
|
|
+ }
|
|
+
|
|
+ return classes;
|
|
+ }
|
|
+
|
|
+ private static void findClassesInPackageByFile(String packageName, String packagePath, Set<Class<?>> classes) {
|
|
+ File dir = new File(packagePath);
|
|
+
|
|
+ if (!dir.exists() || !dir.isDirectory()) {
|
|
+ return;
|
|
+ }
|
|
+
|
|
+ File[] dirfiles = dir.listFiles((file) -> file.isDirectory() || file.getName().endsWith(".class"));
|
|
+ if (dirfiles != null) {
|
|
+ for (File file : dirfiles) {
|
|
+ if (file.isDirectory()) {
|
|
+ findClassesInPackageByFile(packageName + "." + file.getName(), file.getAbsolutePath(), classes);
|
|
+ } else {
|
|
+ String className = file.getName().substring(0, file.getName().length() - 6);
|
|
+ try {
|
|
+ classes.add(Class.forName(packageName + '.' + className));
|
|
+ } catch (ClassNotFoundException e) {
|
|
+ throw new RuntimeException(e);
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+
|
|
+ private static void findClassesInPackageByJar(String packageName, Enumeration<JarEntry> entries, String packageDirName, Set<Class<?>> classes) {
|
|
+ while (entries.hasMoreElements()) {
|
|
+ JarEntry entry = entries.nextElement();
|
|
+ String name = entry.getName();
|
|
+
|
|
+ if (name.charAt(0) == '/') {
|
|
+ name = name.substring(1);
|
|
+ }
|
|
+
|
|
+ if (name.startsWith(packageDirName)) {
|
|
+ int idx = name.lastIndexOf('/');
|
|
+
|
|
+ if (idx != -1) {
|
|
+ packageName = name.substring(0, idx).replace('/', '.');
|
|
+ }
|
|
+
|
|
+ if (name.endsWith(".class") && !entry.isDirectory()) {
|
|
+ String className = name.substring(packageName.length() + 1, name.length() - 6);
|
|
+ try {
|
|
+ classes.add(Class.forName(packageName + '.' + className));
|
|
+ } catch (ClassNotFoundException e) {
|
|
+ throw new RuntimeException(e);
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+}
|