Files
Purpur/patches/server/0002-Purpur-config-files.patch

275 lines
12 KiB
Diff

From 59f6f2e5c04bcb8dd2dcfabde239f9683f623288 Mon Sep 17 00:00:00 2001
From: William Blake Galbreath <blake.galbreath@gmail.com>
Date: Thu, 9 May 2019 18:09:43 -0500
Subject: [PATCH] Purpur config files
---
.../net/minecraft/server/DedicatedServer.java | 8 ++
src/main/java/net/minecraft/server/World.java | 3 +
.../java/net/pl3x/purpur/PurpurConfig.java | 101 ++++++++++++++++++
.../net/pl3x/purpur/PurpurWorldConfig.java | 63 +++++++++++
.../org/bukkit/craftbukkit/CraftServer.java | 2 +
.../java/org/bukkit/craftbukkit/Main.java | 8 ++
6 files changed, 185 insertions(+)
create mode 100644 src/main/java/net/pl3x/purpur/PurpurConfig.java
create mode 100644 src/main/java/net/pl3x/purpur/PurpurWorldConfig.java
diff --git a/src/main/java/net/minecraft/server/DedicatedServer.java b/src/main/java/net/minecraft/server/DedicatedServer.java
index 3d452fe0e..22e81ee10 100644
--- a/src/main/java/net/minecraft/server/DedicatedServer.java
+++ b/src/main/java/net/minecraft/server/DedicatedServer.java
@@ -172,6 +172,14 @@ public class DedicatedServer extends MinecraftServer implements IMinecraftServer
return false;
}
com.destroystokyo.paper.PaperConfig.registerCommands();
+ // Purpur start
+ try {
+ net.pl3x.purpur.PurpurConfig.init((File) options.valueOf("purpur-settings"));
+ } catch (Exception e) {
+ DedicatedServer.LOGGER.error("Unable to load server configuration", e);
+ return false;
+ }
+ // Purpur end
com.destroystokyo.paper.VersionHistoryManager.INSTANCE.getClass(); // load version history now
// Paper end
diff --git a/src/main/java/net/minecraft/server/World.java b/src/main/java/net/minecraft/server/World.java
index cae59ed37..fdd316779 100644
--- a/src/main/java/net/minecraft/server/World.java
+++ b/src/main/java/net/minecraft/server/World.java
@@ -94,6 +94,8 @@ public abstract class World implements IIBlockAccess, GeneratorAccess, AutoClose
public final com.destroystokyo.paper.PaperWorldConfig paperConfig; // Paper
+ public final net.pl3x.purpur.PurpurWorldConfig purpurConfig; // Purpur
+
public final co.aikar.timings.WorldTimingsHandler timings; // Paper
public static BlockPosition lastPhysicsProblem; // Spigot
private org.spigotmc.TickLimiter entityLimiter;
@@ -116,6 +118,7 @@ public abstract class World implements IIBlockAccess, GeneratorAccess, AutoClose
protected World(WorldData worlddata, DimensionManager dimensionmanager, BiFunction<World, WorldProvider, IChunkProvider> bifunction, GameProfilerFiller gameprofilerfiller, boolean flag, org.bukkit.generator.ChunkGenerator gen, org.bukkit.World.Environment env) {
this.spigotConfig = new org.spigotmc.SpigotWorldConfig( worlddata.getName() ); // Spigot
this.paperConfig = new com.destroystokyo.paper.PaperWorldConfig(worlddata.getName(), this.spigotConfig); // Paper
+ this.purpurConfig = new net.pl3x.purpur.PurpurWorldConfig(worlddata.getName(), this.paperConfig, this.spigotConfig); // Purpur
this.generator = gen;
this.world = new CraftWorld((WorldServer) this, gen, env);
this.ticksPerAnimalSpawns = this.getServer().getTicksPerAnimalSpawns(); // CraftBukkit
diff --git a/src/main/java/net/pl3x/purpur/PurpurConfig.java b/src/main/java/net/pl3x/purpur/PurpurConfig.java
new file mode 100644
index 000000000..28f80e1a5
--- /dev/null
+++ b/src/main/java/net/pl3x/purpur/PurpurConfig.java
@@ -0,0 +1,101 @@
+package net.pl3x.purpur;
+
+import com.google.common.base.Throwables;
+import org.bukkit.Bukkit;
+import org.bukkit.configuration.InvalidConfigurationException;
+import org.bukkit.configuration.file.YamlConfiguration;
+
+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;
+
+public class PurpurConfig {
+ private static final int CONFIG_VERSION = 1;
+ private static final String HEADER = "This is the main configuration file for Purpur.";
+ private static File CONFIG_FILE;
+ static YamlConfiguration config;
+ static int version;
+
+ public static void init(File configFile) {
+ CONFIG_FILE = configFile;
+ config = new YamlConfiguration();
+ try {
+ config.load(CONFIG_FILE);
+ } catch (IOException ex) {
+ } catch (InvalidConfigurationException ex) {
+ Bukkit.getLogger().log(Level.SEVERE, "Could not load purpur.yml, please correct your syntax errors", ex);
+ throw Throwables.propagate(ex);
+ }
+ config.options().header(HEADER);
+ config.options().copyDefaults(true);
+
+ version = getInt("config-version", CONFIG_VERSION);
+ set("config-version", version);
+
+ readConfig(PurpurConfig.class, null);
+ }
+
+ protected static void log(String s) {
+ log(Level.INFO, s);
+ }
+
+ protected static void log(Level level, String s) {
+ Bukkit.getLogger().log(level, s);
+ }
+
+ static void readConfig(Class<?> clazz, Object instance) {
+ 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) {
+ Bukkit.getLogger().log(Level.SEVERE, "Error invoking " + method, ex);
+ }
+ }
+ }
+ }
+
+ try {
+ config.save(CONFIG_FILE);
+ } catch (IOException ex) {
+ Bukkit.getLogger().log(Level.SEVERE, "Could not save " + CONFIG_FILE, ex);
+ }
+ }
+
+ private static void set(String path, Object val) {
+ config.set(path, val);
+ }
+
+ private static boolean getBoolean(String path, boolean def) {
+ config.addDefault(path, def);
+ return config.getBoolean(path, config.getBoolean(path));
+ }
+
+ private static double getDouble(String path, double def) {
+ config.addDefault(path, def);
+ return config.getDouble(path, config.getDouble(path));
+ }
+
+ private static int getInt(String path, int def) {
+ config.addDefault(path, def);
+ return config.getInt(path, config.getInt(path));
+ }
+
+ private static <T> List getList(String path, T def) {
+ config.addDefault(path, def);
+ return config.getList(path, config.getList(path));
+ }
+
+ private static String getString(String path, String def) {
+ config.addDefault(path, def);
+ return config.getString(path, config.getString(path));
+ }
+}
diff --git a/src/main/java/net/pl3x/purpur/PurpurWorldConfig.java b/src/main/java/net/pl3x/purpur/PurpurWorldConfig.java
new file mode 100644
index 000000000..30f11fe26
--- /dev/null
+++ b/src/main/java/net/pl3x/purpur/PurpurWorldConfig.java
@@ -0,0 +1,63 @@
+package net.pl3x.purpur;
+
+import com.destroystokyo.paper.PaperWorldConfig;
+import org.bukkit.configuration.file.YamlConfiguration;
+import org.spigotmc.SpigotWorldConfig;
+
+import java.util.List;
+
+import static net.pl3x.purpur.PurpurConfig.log;
+
+public class PurpurWorldConfig {
+
+ private final String worldName;
+ private final PaperWorldConfig paperConfig;
+ private final SpigotWorldConfig spigotConfig;
+ private final YamlConfiguration config;
+ private boolean verbose;
+
+ public PurpurWorldConfig(String worldName, PaperWorldConfig paperConfig, SpigotWorldConfig spigotConfig) {
+ this.worldName = worldName;
+ this.paperConfig = paperConfig;
+ this.spigotConfig = spigotConfig;
+ this.config = PurpurConfig.config;
+ init();
+ }
+
+ public void init() {
+ log("-------- World Settings For [" + worldName + "] --------");
+ PurpurConfig.readConfig(PurpurWorldConfig.class, this);
+ }
+
+ private void set(String path, Object val) {
+ config.set("world-settings.default." + path, val);
+ if (config.get("world-settings." + worldName + "." + path) != null) {
+ config.set("world-settings." + worldName + "." + path, val);
+ }
+ }
+
+ private boolean getBoolean(String path, boolean def) {
+ config.addDefault("world-settings.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 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));
+ }
+}
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
index 347e3f918..69ecd9cc1 100644
--- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
@@ -788,6 +788,7 @@ public final class CraftServer implements Server {
org.spigotmc.SpigotConfig.init((File) console.options.valueOf("spigot-settings")); // Spigot
com.destroystokyo.paper.PaperConfig.init((File) console.options.valueOf("paper-settings")); // Paper
+ net.pl3x.purpur.PurpurConfig.init((File) console.options.valueOf("purpur-settings")); // Purpur
for (WorldServer world : console.getWorlds()) {
world.worldData.setDifficulty(config.difficulty);
world.setSpawnFlags(config.spawnMonsters, config.spawnAnimals);
@@ -804,6 +805,7 @@ public final class CraftServer implements Server {
}
world.spigotConfig.init(); // Spigot
world.paperConfig.init(); // Paper
+ world.purpurConfig.init(); // Purpur
}
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 405ea5c46..2d60e7588 100644
--- a/src/main/java/org/bukkit/craftbukkit/Main.java
+++ b/src/main/java/org/bukkit/craftbukkit/Main.java
@@ -137,6 +137,14 @@ public class Main {
.describedAs("Yml file");
// Paper end
+ // Purpur Start
+ acceptsAll(asList("purpur", "purpur-settings"), "File for purpur settings")
+ .withRequiredArg()
+ .ofType(File.class)
+ .defaultsTo(new File("purpur.yml"))
+ .describedAs("Yml file");
+ // Purpur end
+
// Paper start
acceptsAll(asList("server-name"), "Name of the server")
.withRequiredArg()
--
2.20.1