Files
MiraiMC/patches/server/0002-Mirai-Configuration.patch
2021-10-11 22:08:41 +02:00

269 lines
10 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Etil <81570777+etil2jz@users.noreply.github.com>
Date: Mon, 11 Oct 2021 21:46:23 +0200
Subject: [PATCH] Mirai Configuration
diff --git a/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java b/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java
index e2901132b78126c0a4eb04363dfe6a0dccd1313f..2b246dd34ca0ff3da6d49a0eab2ee0af75b13642 100644
--- a/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java
+++ b/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java
@@ -6,10 +6,7 @@ import com.mojang.authlib.GameProfile;
import com.mojang.authlib.GameProfileRepository;
import com.mojang.authlib.minecraft.MinecraftSessionService;
import com.mojang.datafixers.DataFixer;
-import java.io.BufferedReader;
-import java.io.BufferedWriter;
-import java.io.IOException;
-import java.io.InputStreamReader;
+import java.io.*;
import java.net.InetAddress;
import java.net.Proxy;
import java.nio.charset.StandardCharsets;
@@ -236,6 +233,16 @@ public class DedicatedServer extends MinecraftServer implements ServerInterface
// Paper end
gg.airplane.AirplaneConfig.load(); // Airplane - config
gg.airplane.commands.AirplaneCommands.init(); // Airplane - command
+
+ // Mirai start
+ try {
+ xyz.arthurb.mirai.MiraiConfig.init((File) options.valueOf("mirai-settings"));
+ } catch (Exception e) {
+ DedicatedServer.LOGGER.error("Unable to load server configuration", e);
+ return false;
+ }
+ xyz.arthurb.mirai.MiraiConfig.registerCommands();
+ // Mirai end
this.setPvpAllowed(dedicatedserverproperties.pvp);
this.setFlightAllowed(dedicatedserverproperties.allowFlight);
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
index 03ec1bbc0fc4354549108868e2514dd42e85ff1d..4e3ca2d239ef3585eb0a3dd92e78b638597b3539 100644
--- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
@@ -907,6 +907,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
+ xyz.arthurb.mirai.MiraiConfig.init((File) console.options.valueOf("mirai-settings")); // Mirai
for (ServerLevel world : this.console.getAllLevels()) {
// world.serverLevelData.setDifficulty(config.difficulty); // Paper - per level difficulty
world.setSpawnSettings(world.serverLevelData.getDifficulty() != Difficulty.PEACEFUL && config.spawnMonsters, config.spawnAnimals); // Paper - per level difficulty (from MinecraftServer#setDifficulty(ServerLevel, Difficulty, boolean))
diff --git a/src/main/java/org/bukkit/craftbukkit/Main.java b/src/main/java/org/bukkit/craftbukkit/Main.java
index 22e9dd17f62103c5061435099ce96a3d70d54808..5d0801fc73f5888398efd65cb5e9eabbdf7748e5 100644
--- a/src/main/java/org/bukkit/craftbukkit/Main.java
+++ b/src/main/java/org/bukkit/craftbukkit/Main.java
@@ -160,6 +160,14 @@ public class Main {
.defaultsTo(new File[] {})
.describedAs("Jar file");
// Paper end
+
+ // Mirai start
+ acceptsAll(asList("mirai", "mirai-settings"), "File for mirai settings")
+ .withRequiredArg()
+ .ofType(File.class)
+ .defaultsTo(new File("mirai.yml"))
+ .describedAs("Yml file");
+ // Mirai end
}
};
diff --git a/src/main/java/xyz/arthurb/mirai/MiraiConfig.java b/src/main/java/xyz/arthurb/mirai/MiraiConfig.java
new file mode 100644
index 0000000000000000000000000000000000000000..03c7a724d42f64946b8cd01ea7065fb02dd3fb7a
--- /dev/null
+++ b/src/main/java/xyz/arthurb/mirai/MiraiConfig.java
@@ -0,0 +1,191 @@
+package xyz.arthurb.mirai;
+
+import com.destroystokyo.paper.PaperCommand;
+import com.google.common.base.Throwables;
+import net.minecraft.server.MinecraftServer;
+import org.bukkit.Bukkit;
+import org.bukkit.command.Command;
+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.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+import java.util.logging.Level;
+import java.util.regex.Pattern;
+
+public class MiraiConfig {
+
+ private static File CONFIG_FILE;
+ private static final String HEADER = "This is the main configuration file for options provided by Mirai.";
+
+ /*========================================================================*/
+ public static YamlConfiguration config;
+ static int version;
+ static Map<String, Command> commands;
+ private static boolean verbose;
+ private static boolean fatalError;
+ /*========================================================================*/
+
+ public static void init(File configFile) {
+ CONFIG_FILE = configFile;
+ config = new YamlConfiguration();
+ try {
+ config.load(CONFIG_FILE);
+ } catch (IOException ignored) {
+ } catch (InvalidConfigurationException ex) {
+ Bukkit.getLogger().log(Level.SEVERE, "Could not load mirai.yml, please correct your syntax errors.", ex);
+ throw Throwables.propagate(ex);
+ }
+ config.options().header(HEADER);
+ config.options().copyDefaults(true);
+ verbose = getBoolean("verbose", false);
+
+ commands = new HashMap<>();
+ commands.put("mirai", new PaperCommand("mirai"));
+
+ version = getInt("config-version", 1);
+ set("config-version", 1);
+ readConfig(MiraiConfig.class, null);
+ }
+
+ protected static void logError(String s) {
+ Bukkit.getLogger().severe(s);
+ }
+
+ protected static void fatal(String s) {
+ fatalError = true;
+ throw new RuntimeException("Fatal mirai.yml config error: " + s);
+ }
+
+ protected static void log(String s) {
+ if (verbose) {
+ Bukkit.getLogger().info(s);
+ }
+ }
+
+ public static void registerCommands() {
+ for (Map.Entry<String, Command> entry : commands.entrySet()) {
+ MinecraftServer.getServer().server.getCommandMap().register(entry.getKey(), "Paper", entry.getValue());
+ }
+ }
+
+ 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 final Pattern SPACE = Pattern.compile(" ");
+ private static final Pattern NOT_NUMERIC = Pattern.compile("[^-\\d.]");
+
+ public static int getSeconds(String str) {
+ str = SPACE.matcher(str).replaceAll("");
+ final char unit = str.charAt(str.length() - 1);
+ str = NOT_NUMERIC.matcher(str).replaceAll("");
+ double num;
+ try {
+ num = Double.parseDouble(str);
+ } catch (Exception e) {
+ num = 0D;
+ }
+ switch (unit) {
+ case 'd':
+ num *= (double) 60 * 60 * 24;
+ break;
+ case 'h':
+ num *= (double) 60 * 60;
+ break;
+ case 'm':
+ num *= (double) 60;
+ break;
+ default:
+ case 's':
+ break;
+ }
+ return (int) num;
+ }
+
+ protected static String timeSummary(int seconds) {
+ String time = "";
+
+ if (seconds > 60 * 60 * 24) {
+ time += TimeUnit.SECONDS.toDays(seconds) + "d";
+ seconds %= 60 * 60 * 24;
+ }
+
+ if (seconds > 60 * 60) {
+ time += TimeUnit.SECONDS.toHours(seconds) + "h";
+ seconds %= 60 * 60;
+ }
+
+ if (seconds > 0) {
+ time += TimeUnit.SECONDS.toMinutes(seconds) + "m";
+ }
+ return time;
+ }
+
+ 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 float getFloat(String path, float def) {
+ // TODO: Figure out why getFloat() always returns the default value.
+ return (float) getDouble(path, (double) def);
+ }
+
+ 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 (List<T>) 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));
+ }
+
+ public static boolean hidePlayerIps = false;
+
+ private static void hidePlayerIps() {
+ hidePlayerIps = getBoolean("settings.hide-player-ips", hidePlayerIps);
+ }
+
+}
\ No newline at end of file