mirror of
https://github.com/BX-Team/DivineMC.git
synced 2025-12-22 16:29:23 +00:00
another config update (i think final)
This commit is contained in:
@@ -37,7 +37,7 @@ index 6a08a42acdb2ee24b5e403b15fb825f3cb49c968..e51fd95bad5038ec7c1b85babf29799d
|
|||||||
|
|
||||||
Plugin[] pluginClone = pluginManager.getPlugins().clone(); // Paper
|
Plugin[] pluginClone = pluginManager.getPlugins().clone(); // Paper
|
||||||
diff --git a/src/main/java/org/bukkit/craftbukkit/Main.java b/src/main/java/org/bukkit/craftbukkit/Main.java
|
diff --git a/src/main/java/org/bukkit/craftbukkit/Main.java b/src/main/java/org/bukkit/craftbukkit/Main.java
|
||||||
index bab0d25e82f85c7b9524ae42e0bb41e6233d71cf..f263987076d352dbf6e4bb5c57a35a4593088bae 100644
|
index bab0d25e82f85c7b9524ae42e0bb41e6233d71cf..f367efa0afae9792f87a842dfe7ba098a3ce7f2d 100644
|
||||||
--- a/src/main/java/org/bukkit/craftbukkit/Main.java
|
--- a/src/main/java/org/bukkit/craftbukkit/Main.java
|
||||||
+++ b/src/main/java/org/bukkit/craftbukkit/Main.java
|
+++ b/src/main/java/org/bukkit/craftbukkit/Main.java
|
||||||
@@ -183,6 +183,15 @@ public class Main {
|
@@ -183,6 +183,15 @@ public class Main {
|
||||||
@@ -49,7 +49,7 @@ index bab0d25e82f85c7b9524ae42e0bb41e6233d71cf..f263987076d352dbf6e4bb5c57a35a45
|
|||||||
+ acceptsAll(asList("divinemc", "divinemc-settings"), "File for DivineMC settings")
|
+ acceptsAll(asList("divinemc", "divinemc-settings"), "File for DivineMC settings")
|
||||||
+ .withRequiredArg()
|
+ .withRequiredArg()
|
||||||
+ .ofType(File.class)
|
+ .ofType(File.class)
|
||||||
+ .defaultsTo(new File("config", "divinemc-global.yml"))
|
+ .defaultsTo(new File("divinemc.yml"))
|
||||||
+ .describedAs("Yml file");
|
+ .describedAs("Yml file");
|
||||||
+ // DivineMC end - Configuration
|
+ // DivineMC end - Configuration
|
||||||
+
|
+
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package org.bxteam.divinemc;
|
package org.bxteam.divinemc;
|
||||||
|
|
||||||
|
import com.google.common.base.Throwables;
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
import org.bukkit.configuration.ConfigurationSection;
|
import org.bukkit.configuration.ConfigurationSection;
|
||||||
@@ -13,6 +14,7 @@ import org.simpleyaml.exceptions.InvalidConfigurationException;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.lang.reflect.Modifier;
|
import java.lang.reflect.Modifier;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -33,7 +35,6 @@ public class DivineConfig {
|
|||||||
|
|
||||||
private static File configFile;
|
private static File configFile;
|
||||||
public static final YamlFile config = new YamlFile();
|
public static final YamlFile config = new YamlFile();
|
||||||
private static int updates = 0;
|
|
||||||
|
|
||||||
private static ConfigurationSection convertToBukkit(org.simpleyaml.configuration.ConfigurationSection section) {
|
private static ConfigurationSection convertToBukkit(org.simpleyaml.configuration.ConfigurationSection section) {
|
||||||
ConfigurationSection newSection = new MemoryConfiguration();
|
ConfigurationSection newSection = new MemoryConfiguration();
|
||||||
@@ -51,10 +52,6 @@ public class DivineConfig {
|
|||||||
return convertToBukkit(config);
|
return convertToBukkit(config);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int getUpdates() {
|
|
||||||
return updates;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void init(File configFile) throws IOException {
|
public static void init(File configFile) throws IOException {
|
||||||
DivineConfig.configFile = configFile;
|
DivineConfig.configFile = configFile;
|
||||||
if (configFile.exists()) {
|
if (configFile.exists()) {
|
||||||
@@ -65,21 +62,27 @@ public class DivineConfig {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
getInt("config.version", CONFIG_VERSION);
|
getInt("version", CONFIG_VERSION);
|
||||||
config.options().header(HEADER);
|
config.options().header(HEADER);
|
||||||
|
|
||||||
for (Method method : DivineConfig.class.getDeclaredMethods()) {
|
readConfig(DivineConfig.class, null);
|
||||||
if (Modifier.isStatic(method.getModifiers()) && Modifier.isPrivate(method.getModifiers()) && method.getParameterCount() == 0 && method.getReturnType() == Void.TYPE && !method.getName().startsWith("lambda")) {
|
|
||||||
method.setAccessible(true);
|
|
||||||
try {
|
|
||||||
method.invoke(null);
|
|
||||||
} catch (Throwable t) {
|
|
||||||
LOGGER.warn("Failed to load configuration option from " + method.getName(), t);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
updates++;
|
static void readConfig(Class<?> clazz, Object instance) throws IOException {
|
||||||
|
for (Method method : clazz.getDeclaredMethods()) {
|
||||||
|
if (Modifier.isPrivate(method.getModifiers())) {
|
||||||
|
if (method.getParameterTypes().length == 0 && method.getReturnType() == Void.TYPE) {
|
||||||
|
try {
|
||||||
|
method.setAccessible(true);
|
||||||
|
method.invoke(instance);
|
||||||
|
} catch (InvocationTargetException ex) {
|
||||||
|
throw Throwables.propagate(ex.getCause());
|
||||||
|
} catch (Exception ex) {
|
||||||
|
LOGGER.error("Error invoking {}", method, ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
config.save(configFile);
|
config.save(configFile);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,121 +1,50 @@
|
|||||||
package org.bxteam.divinemc;
|
package org.bxteam.divinemc;
|
||||||
|
|
||||||
import com.google.common.base.Throwables;
|
|
||||||
import org.apache.logging.log4j.LogManager;
|
|
||||||
import org.apache.logging.log4j.Logger;
|
|
||||||
import org.bukkit.World;
|
import org.bukkit.World;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.simpleyaml.configuration.file.YamlFile;
|
import org.simpleyaml.configuration.file.YamlFile;
|
||||||
import org.simpleyaml.exceptions.InvalidConfigurationException;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.lang.reflect.InvocationTargetException;
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.lang.reflect.Modifier;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
@SuppressWarnings({"unused", "DuplicatedCode"})
|
@SuppressWarnings("unused")
|
||||||
public class DivineWorldConfig {
|
public class DivineWorldConfig {
|
||||||
private static final String HEADER = """
|
private final YamlFile config;
|
||||||
This is the world configuration file for DivineMC.
|
|
||||||
Configuration options here apply to all worlds, unless you specify overrides inside
|
|
||||||
the world-specific config file inside each world folder.
|
|
||||||
|
|
||||||
If you need help with the configuration or have any questions related to DivineMC,
|
|
||||||
join us in our Discord server.
|
|
||||||
|
|
||||||
Discord: https://discord.gg/p7cxhw7E2M
|
|
||||||
Docs: https://bxteam.org/docs/divinemc
|
|
||||||
New builds: https://github.com/BX-Team/DivineMC/releases/latest""";
|
|
||||||
|
|
||||||
public static final Logger LOGGER = LogManager.getLogger(DivineWorldConfig.class.getSimpleName());
|
|
||||||
public static final int CONFIG_VERSION = 5;
|
|
||||||
|
|
||||||
private static final YamlFile config = new YamlFile();
|
|
||||||
private final String worldName;
|
private final String worldName;
|
||||||
private final World.Environment environment;
|
private final World.Environment environment;
|
||||||
|
|
||||||
public DivineWorldConfig(String worldName, World.Environment environment) throws IOException {
|
public DivineWorldConfig(String worldName, World.Environment environment) throws IOException {
|
||||||
|
this.config = DivineConfig.config;
|
||||||
this.worldName = worldName;
|
this.worldName = worldName;
|
||||||
this.environment = environment;
|
this.environment = environment;
|
||||||
init();
|
init();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void init() throws IOException {
|
public void init() throws IOException {
|
||||||
File configFile = new File("config", "divinemc-world.yml");
|
DivineConfig.readConfig(DivineWorldConfig.class, this);
|
||||||
|
|
||||||
if (configFile.exists()) {
|
|
||||||
try {
|
|
||||||
config.load(configFile);
|
|
||||||
} catch (InvalidConfigurationException e) {
|
|
||||||
throw new IOException(e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
config.getInt("config.version", CONFIG_VERSION);
|
public @Nullable String getString(String path, String def, String... comments) {
|
||||||
config.options().header(HEADER);
|
this.config.addDefault("world-settings.default." + path, def);
|
||||||
|
if (comments.length > 0) this.config.setComment("world-settings.default." + path, String.join("\n", comments));
|
||||||
for (Method method : DivineWorldConfig.class.getDeclaredMethods()) {
|
return this.config.getString("world-settings." + this.worldName + "." + path, this.config.getString("world-settings.default." + path));
|
||||||
if (Modifier.isPrivate(method.getModifiers())) {
|
|
||||||
if (method.getParameterTypes().length == 0 && method.getReturnType() == Void.TYPE) {
|
|
||||||
try {
|
|
||||||
method.setAccessible(true);
|
|
||||||
method.invoke(this);
|
|
||||||
} catch (InvocationTargetException ex) {
|
|
||||||
throw Throwables.propagate(ex.getCause());
|
|
||||||
} catch (Exception ex) {
|
|
||||||
LOGGER.error("Error invoking {}", method.getName(), ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
public boolean getBoolean(String path, boolean def, String... comments) {
|
||||||
config.save(configFile);
|
this.config.addDefault("world-settings.default." + path, def);
|
||||||
} catch (IOException ex) {
|
if (comments.length > 0) this.config.setComment("world-settings.default." + path, String.join("\n", comments));
|
||||||
LOGGER.error("Could not save {}", configFile, ex);
|
return this.config.getBoolean("world-settings." + this.worldName + "." + path, this.config.getBoolean("world-settings.default." + path));
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void set(String path, Object val) {
|
public double getDouble(String path, double def, String... comments) {
|
||||||
config.addDefault("world-settings.default." + path, val);
|
this.config.addDefault("world-settings.default." + path, def);
|
||||||
config.set("world-settings.default." + path, val);
|
if (comments.length > 0) this.config.setComment("world-settings.default." + path, String.join("\n", comments));
|
||||||
if (config.get("world-settings." + worldName + "." + path) != null) {
|
return this.config.getDouble("world-settings." + this.worldName + "." + path, this.config.getDouble("world-settings.default." + path));
|
||||||
config.addDefault("world-settings." + worldName + "." + path, val);
|
|
||||||
config.set("world-settings." + worldName + "." + path, val);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getString(String path, String def) {
|
public int getInt(String path, int def, String... comments) {
|
||||||
config.addDefault("world-settings.default." + path, def);
|
this.config.addDefault("world-settings.default." + path, def);
|
||||||
return config.getString("world-settings." + worldName + "." + path, config.getString("world-settings.default." + path));
|
if (comments.length > 0) this.config.setComment("world-settings.default." + path, String.join("\n", comments));
|
||||||
}
|
return this.config.getInt("world-settings." + this.worldName + "." + path, this.config.getInt("world-settings.default." + path));
|
||||||
|
|
||||||
private boolean getBoolean(String path, boolean def) {
|
|
||||||
config.addDefault("settings.world.default." + path, def);
|
|
||||||
return config.getBoolean("world-settings." + worldName + "." + path, config.getBoolean("world-settings.default." + path));
|
|
||||||
}
|
|
||||||
|
|
||||||
private double getDouble(String path, double def) {
|
|
||||||
config.addDefault("world-settings.default." + path, def);
|
|
||||||
return config.getDouble("world-settings." + worldName + "." + path, config.getDouble("world-settings.default." + path));
|
|
||||||
}
|
|
||||||
|
|
||||||
private int getInt(String path, int def) {
|
|
||||||
config.addDefault("world-settings.default." + path, def);
|
|
||||||
return config.getInt("world-settings." + worldName + "." + path, config.getInt("world-settings.default." + path));
|
|
||||||
}
|
|
||||||
|
|
||||||
private <T> List<?> getList(String path, T def) {
|
|
||||||
config.addDefault("world-settings.default." + path, def);
|
|
||||||
return config.getList("world-settings." + worldName + "." + path, config.getList("world-settings.default." + path));
|
|
||||||
}
|
|
||||||
|
|
||||||
private Map<String, Object> getMap(String path, Map<String, Object> def) {
|
|
||||||
final Map<String, Object> fallback = this.getMap("world-settings.default." + path, def);
|
|
||||||
final Map<String, Object> value = this.getMap("world-settings." + worldName + "." + path, null);
|
|
||||||
return value.isEmpty() ? fallback : value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean snowballCanKnockback = true;
|
public boolean snowballCanKnockback = true;
|
||||||
|
|||||||
Reference in New Issue
Block a user