diff --git a/divinemc-server/paper-patches/features/0002-Configuration.patch b/divinemc-server/paper-patches/features/0002-Configuration.patch index f4d4427..f99c7eb 100644 --- a/divinemc-server/paper-patches/features/0002-Configuration.patch +++ b/divinemc-server/paper-patches/features/0002-Configuration.patch @@ -37,7 +37,7 @@ index 6a08a42acdb2ee24b5e403b15fb825f3cb49c968..e51fd95bad5038ec7c1b85babf29799d 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 -index bab0d25e82f85c7b9524ae42e0bb41e6233d71cf..f263987076d352dbf6e4bb5c57a35a4593088bae 100644 +index bab0d25e82f85c7b9524ae42e0bb41e6233d71cf..f367efa0afae9792f87a842dfe7ba098a3ce7f2d 100644 --- a/src/main/java/org/bukkit/craftbukkit/Main.java +++ b/src/main/java/org/bukkit/craftbukkit/Main.java @@ -183,6 +183,15 @@ public class Main { @@ -49,7 +49,7 @@ index bab0d25e82f85c7b9524ae42e0bb41e6233d71cf..f263987076d352dbf6e4bb5c57a35a45 + acceptsAll(asList("divinemc", "divinemc-settings"), "File for DivineMC settings") + .withRequiredArg() + .ofType(File.class) -+ .defaultsTo(new File("config", "divinemc-global.yml")) ++ .defaultsTo(new File("divinemc.yml")) + .describedAs("Yml file"); + // DivineMC end - Configuration + diff --git a/divinemc-server/src/main/java/org/bxteam/divinemc/DivineConfig.java b/divinemc-server/src/main/java/org/bxteam/divinemc/DivineConfig.java index 6426504..caad4e7 100644 --- a/divinemc-server/src/main/java/org/bxteam/divinemc/DivineConfig.java +++ b/divinemc-server/src/main/java/org/bxteam/divinemc/DivineConfig.java @@ -1,5 +1,6 @@ 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.configuration.ConfigurationSection; @@ -13,6 +14,7 @@ import org.simpleyaml.exceptions.InvalidConfigurationException; import java.io.File; import java.io.IOException; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.List; @@ -33,7 +35,6 @@ public class DivineConfig { private static File configFile; public static final YamlFile config = new YamlFile(); - private static int updates = 0; private static ConfigurationSection convertToBukkit(org.simpleyaml.configuration.ConfigurationSection section) { ConfigurationSection newSection = new MemoryConfiguration(); @@ -51,10 +52,6 @@ public class DivineConfig { return convertToBukkit(config); } - public static int getUpdates() { - return updates; - } - public static void init(File configFile) throws IOException { DivineConfig.configFile = configFile; if (configFile.exists()) { @@ -65,25 +62,31 @@ public class DivineConfig { } } - getInt("config.version", CONFIG_VERSION); + getInt("version", CONFIG_VERSION); config.options().header(HEADER); - for (Method method : DivineConfig.class.getDeclaredMethods()) { - 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++; - - config.save(configFile); + readConfig(DivineConfig.class, null); } + 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); + } + private static void setComment(String key, String... comment) { if (config.contains(key)) { config.setComment(key, String.join("\n", comment), CommentType.BLOCK); diff --git a/divinemc-server/src/main/java/org/bxteam/divinemc/DivineWorldConfig.java b/divinemc-server/src/main/java/org/bxteam/divinemc/DivineWorldConfig.java index 75a43da..e258bbc 100644 --- a/divinemc-server/src/main/java/org/bxteam/divinemc/DivineWorldConfig.java +++ b/divinemc-server/src/main/java/org/bxteam/divinemc/DivineWorldConfig.java @@ -1,121 +1,50 @@ 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.jetbrains.annotations.Nullable; import org.simpleyaml.configuration.file.YamlFile; -import org.simpleyaml.exceptions.InvalidConfigurationException; -import java.io.File; 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 { - private static final String HEADER = """ - 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 YamlFile config; private final String worldName; private final World.Environment environment; public DivineWorldConfig(String worldName, World.Environment environment) throws IOException { + this.config = DivineConfig.config; this.worldName = worldName; this.environment = environment; init(); } public void init() throws IOException { - File configFile = new File("config", "divinemc-world.yml"); - - if (configFile.exists()) { - try { - config.load(configFile); - } catch (InvalidConfigurationException e) { - throw new IOException(e); - } - } - - config.getInt("config.version", CONFIG_VERSION); - config.options().header(HEADER); - - for (Method method : DivineWorldConfig.class.getDeclaredMethods()) { - 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 { - config.save(configFile); - } catch (IOException ex) { - LOGGER.error("Could not save {}", configFile, ex); - } + DivineConfig.readConfig(DivineWorldConfig.class, this); } - private void set(String path, Object val) { - config.addDefault("world-settings.default." + path, val); - config.set("world-settings.default." + path, val); - if (config.get("world-settings." + worldName + "." + path) != null) { - config.addDefault("world-settings." + worldName + "." + path, val); - config.set("world-settings." + worldName + "." + path, val); - } + public @Nullable String getString(String path, String def, String... comments) { + this.config.addDefault("world-settings.default." + path, def); + if (comments.length > 0) this.config.setComment("world-settings.default." + path, String.join("\n", comments)); + return this.config.getString("world-settings." + this.worldName + "." + path, this.config.getString("world-settings.default." + path)); } - private String getString(String path, String def) { - config.addDefault("world-settings.default." + path, def); - return config.getString("world-settings." + worldName + "." + path, config.getString("world-settings.default." + path)); + public boolean getBoolean(String path, boolean def, String... comments) { + this.config.addDefault("world-settings.default." + path, def); + if (comments.length > 0) this.config.setComment("world-settings.default." + path, String.join("\n", comments)); + return this.config.getBoolean("world-settings." + this.worldName + "." + path, this.config.getBoolean("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)); + public double getDouble(String path, double def, String... comments) { + this.config.addDefault("world-settings.default." + path, def); + if (comments.length > 0) this.config.setComment("world-settings.default." + path, String.join("\n", comments)); + return this.config.getDouble("world-settings." + this.worldName + "." + path, this.config.getDouble("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 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 getMap(String path, Map def) { - final Map fallback = this.getMap("world-settings.default." + path, def); - final Map value = this.getMap("world-settings." + worldName + "." + path, null); - return value.isEmpty() ? fallback : value; + public int getInt(String path, int def, String... comments) { + this.config.addDefault("world-settings.default." + path, def); + 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)); } public boolean snowballCanKnockback = true;