9
0
mirror of https://github.com/BX-Team/DivineMC.git synced 2025-12-22 08:19:19 +00:00

split configs

This commit is contained in:
NONPLAYT
2025-02-26 01:07:41 +03:00
parent 0d7ef76d71
commit 2929307e60
8 changed files with 139 additions and 86 deletions

View File

@@ -2,7 +2,6 @@ package org.bxteam.divinemc;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.MemoryConfiguration;
import org.bxteam.divinemc.entity.pathfinding.PathfindTaskRejectPolicy;
@@ -14,11 +13,9 @@ 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.logging.Level;
@SuppressWarnings("unused")
public class DivineConfig {
@@ -32,6 +29,8 @@ public class DivineConfig {
New builds: https://github.com/BX-Team/DivineMC/releases/latest""";
public static final Logger LOGGER = LogManager.getLogger(DivineConfig.class.getSimpleName());
public static final int CONFIG_VERSION = 5;
private static File configFile;
public static final YamlFile config = new YamlFile();
private static int updates = 0;
@@ -66,53 +65,35 @@ public class DivineConfig {
}
}
getString("config.version", "5");
getBoolean("config.verbose", false);
getInt("config.version", CONFIG_VERSION);
config.options().header(HEADER);
config.options().copyDefaults(true);
readConfig(configFile, config, DivineConfig.class, null);
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);
}
public static void readConfig(Class<?> clazz, @Nullable Object instance) {
readConfig(configFile, config, clazz, instance);
}
private static void readConfig(File configFile, YamlFile config, Class<?> clazz, @Nullable Object instance) {
for (Method method : clazz.getDeclaredMethods()) {
if (!Modifier.isPrivate(method.getModifiers())) continue;
if (method.getParameterTypes().length != 0) continue;
if (method.getReturnType() != Void.TYPE) continue;
try {
method.setAccessible(true);
method.invoke(instance);
} catch (InvocationTargetException ex) {
throw new RuntimeException(ex.getCause());
} catch (Exception ex) {
Bukkit.getLogger().log(Level.SEVERE, "Error invoking " + method, ex);
}
}
try {
config.save(configFile);
} catch (IOException ex) {
Bukkit.getLogger().log(Level.SEVERE, "Could not save " + configFile, ex);
}
}
private static void setComment(String key, String... comment) {
if (config.contains(key)) {
config.setComment(key, String.join("\n", comment), CommentType.BLOCK);
}
}
private static void ensureDefault(String key, Object defaultValue, String... comment) {
if (!config.contains(key)) {
config.set(key, defaultValue);
if (comment.length > 0) config.setComment(key, String.join("\n", comment), CommentType.BLOCK);
}
}
private static void ensureDefault(String key, Object defaultValue, String... comment) {
if (!config.contains(key)) config.set(key, defaultValue);
if (comment.length > 0) config.setComment(key, String.join("\n", comment), CommentType.BLOCK);
}
private static boolean getBoolean(String key, boolean defaultValue, String... comment) {
return getBoolean(key, null, defaultValue, comment);

View File

@@ -1,65 +1,120 @@
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.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")
@SuppressWarnings({"unused", "DuplicatedCode"})
public class DivineWorldConfig {
private final YamlFile config;
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 String worldName;
private final World.Environment environment;
public DivineWorldConfig(String worldName, World.Environment environment) {
this.config = DivineConfig.config;
public DivineWorldConfig(String worldName, World.Environment environment) throws IOException {
this.worldName = worldName;
this.environment = environment;
init();
}
public void init() {
DivineConfig.readConfig(DivineWorldConfig.class, this);
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);
}
}
private void set(String path, Object val) {
this.config.addDefault("settings.world-settings.default." + path, val);
this.config.set("settings.world-settings.default." + path, val);
if (this.config.get("settings.world-settings." + worldName + "." + path) != null) {
this.config.addDefault("settings.world-settings." + worldName + "." + path, val);
this.config.set("settings.world-settings." + worldName + "." + path, 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);
}
}
private String getString(String path, String def) {
this.config.addDefault("settings.world-settings.default." + path, def);
return this.config.getString("settings.world-settings." + worldName + "." + path, this.config.getString("settings.world-settings.default." + path));
config.addDefault("world-settings.default." + path, def);
return config.getString("world-settings." + worldName + "." + path, config.getString("world-settings.default." + path));
}
private boolean getBoolean(String path, boolean def) {
this.config.addDefault("settings.world.default." + path, def);
return this.config.getBoolean("settings.world-settings." + worldName + "." + path, this.config.getBoolean("settings.world-settings.default." + path));
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) {
this.config.addDefault("settings.world-settings.default." + path, def);
return this.config.getDouble("settings.world-settings." + worldName + "." + path, this.config.getDouble("settings.world-settings.default." + path));
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) {
this.config.addDefault("settings.world-settings.default." + path, def);
return this.config.getInt("settings.world-settings." + worldName + "." + path, this.config.getInt("settings.world-settings.default." + path));
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) {
this.config.addDefault("settings.world-settings.default." + path, def);
return this.config.getList("settings.world-settings." + worldName + "." + path, this.config.getList("settings.world-settings.default." + path));
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("settings.world-settings.default." + path, def);
final Map<String, Object> value = this.getMap("settings.world-settings." + worldName + "." + path, null);
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;
}

View File

@@ -44,7 +44,11 @@ public final class ReloadCommand extends DivineSubCommandPermission {
}
for (ServerLevel level : server.getAllLevels()) {
level.divineConfig.init();
try {
level.divineConfig.init();
} catch (IOException e) {
MinecraftServer.LOGGER.error("Failed to reload DivineMC world config for level " + level.dimension().location(), e);
}
level.resetBreedingCooldowns();
}
server.server.reloadCount++;