From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: NONPLAYT <76615486+NONPLAYT@users.noreply.github.com> Date: Mon, 27 Mar 2023 13:16:41 +0300 Subject: [PATCH] Divine Configuration diff --git a/src/main/java/co/aikar/timings/TimingsExport.java b/src/main/java/co/aikar/timings/TimingsExport.java index 2cc44fbf8e5bd436b6d4e19f6c06b351e750cb31..de5086590c457546c6798bb064fe9af0cc6696b1 100644 --- a/src/main/java/co/aikar/timings/TimingsExport.java +++ b/src/main/java/co/aikar/timings/TimingsExport.java @@ -242,7 +242,8 @@ public class TimingsExport extends Thread { pair("spigot", mapAsJSON(Bukkit.spigot().getSpigotConfig(), null)), pair("bukkit", mapAsJSON(Bukkit.spigot().getBukkitConfig(), null)), pair("paper", mapAsJSON(Bukkit.spigot().getPaperConfig(), null)), // Pufferfish - pair("pufferfish", mapAsJSON(gg.pufferfish.pufferfish.PufferfishConfig.getConfigCopy(), null)) // Pufferfish + pair("pufferfish", mapAsJSON(gg.pufferfish.pufferfish.PufferfishConfig.getConfigCopy(), null)), // Pufferfish // DivineMC + pair("divinemc", mapAsJSON(gq.bxteam.divinemc.config.DivineConfig.getConfigCopy(), null)) // DivineMC )); new TimingsExport(listeners, parent, history).start(); diff --git a/src/main/java/gq/bxteam/divinemc/config/DivineCommand.java b/src/main/java/gq/bxteam/divinemc/config/DivineCommand.java new file mode 100644 index 0000000000000000000000000000000000000000..cbd2e01fb7d88c056b39a791cf0e4b4725ec5bb0 --- /dev/null +++ b/src/main/java/gq/bxteam/divinemc/config/DivineCommand.java @@ -0,0 +1,68 @@ +package gq.bxteam.divinemc.config; + +import java.io.IOException; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.NamedTextColor; +import net.md_5.bungee.api.ChatColor; +import net.minecraft.server.MinecraftServer; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; + +public class DivineCommand extends Command { + public DivineCommand() { + super("divinemc"); + this.description = "DivineMC related commands"; + this.usageMessage = "/divinemc [reload | version]"; + this.setPermission("bukkit.command.divinemc"); + } + + public static void init() { + MinecraftServer.getServer().server.getCommandMap().register("divinemc", "DivineMC", new DivineCommand()); + } + + @Override + public List tabComplete(CommandSender sender, String alias, String[] args, Location location) throws IllegalArgumentException { + if (args.length == 1) { + return Stream.of("reload", "version") + .filter(arg->arg.startsWith(args[0].toLowerCase())) + .collect(Collectors.toList()); + } + return Collections.emptyList(); + } + + @Override + public boolean execute(CommandSender sender, String commandLabel, String[] args) { + if (!testPermission(sender)) return true; + String prefix = ChatColor.of("#12fff6") + "" + ChatColor.BOLD + "DivineMC ยป " + ChatColor.of("#e8f9f9"); + + if (args.length != 1) { + sender.sendMessage(prefix + "Usage: " + usageMessage); + args = new String[]{"version"}; + } + + if (args[0].equalsIgnoreCase("reload")) { + MinecraftServer console = MinecraftServer.getServer(); + try { + DivineConfig.load(); + } catch (IOException e) { + sender.sendMessage(Component.text("Failed to reload.", NamedTextColor.RED)); + e.printStackTrace(); + return true; + } + console.server.reloadCount++; + + Command.broadcastCommandMessage(sender, prefix + "DivineMC configuration has been reloaded."); + } else if (args[0].equalsIgnoreCase("version")) { + Command.broadcastCommandMessage(sender, prefix + "This server is running " + Bukkit.getName() + " version " + Bukkit.getVersion() + " (Implementing API version " + Bukkit.getBukkitVersion() + ")"); + } + + return true; + } +} \ No newline at end of file diff --git a/src/main/java/gq/bxteam/divinemc/config/DivineConfig.java b/src/main/java/gq/bxteam/divinemc/config/DivineConfig.java new file mode 100644 index 0000000000000000000000000000000000000000..87346c66366a579f09199fd89f88170f4120c6e0 --- /dev/null +++ b/src/main/java/gq/bxteam/divinemc/config/DivineConfig.java @@ -0,0 +1,135 @@ +package gq.bxteam.divinemc.config; + +import java.io.File; +import java.io.IOException; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.util.List; + +import net.minecraft.server.MinecraftServer; + +import org.apache.logging.log4j.Level; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.MemoryConfiguration; +import org.jetbrains.annotations.Nullable; +import org.simpleyaml.configuration.comments.CommentType; +import org.simpleyaml.configuration.file.YamlFile; +import org.simpleyaml.exceptions.InvalidConfigurationException; + +public class DivineConfig { + + private static final YamlFile config = new YamlFile(); + private static int updates = 0; + + private static ConfigurationSection convertToBukkit(org.simpleyaml.configuration.ConfigurationSection section) { + ConfigurationSection newSection = new MemoryConfiguration(); + for (String key : section.getKeys(false)) { + if (section.isConfigurationSection(key)) { + newSection.set(key, convertToBukkit(section.getConfigurationSection(key))); + } else { + newSection.set(key, section.get(key)); + } + } + return newSection; + } + + public static ConfigurationSection getConfigCopy() { + return convertToBukkit(config); + } + + public static int getUpdates() { + return updates; + } + + public static void load() throws IOException { + File configFile = new File("divinemc.yml"); + + if (configFile.exists()) { + try { + config.load(configFile); + } catch (InvalidConfigurationException e) { + throw new IOException(e); + } + } + + getString("info.version", "2.0"); + setComment("info", + "DivineMC Configuration", + "Join our Discord at https://discord.gg/p7cxhw7E2M", + "Download new builds at https://github.com/DivineMC/DivineMC/releases/latest"); + + 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) { + MinecraftServer.LOGGER.warn("Failed to load configuration option from " + method.getName(), t); + } + } + } + + updates++; + + config.save(configFile); + } + + 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); + 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); + } + + private static boolean getBoolean(String key, @Nullable String oldKey, boolean defaultValue, String... comment) { + ensureDefault(key, defaultValue, comment); + return config.getBoolean(key, defaultValue); + } + + private static int getInt(String key, int defaultValue, String... comment) { + return getInt(key, null, defaultValue, comment); + } + + private static int getInt(String key, @Nullable String oldKey, int defaultValue, String... comment) { + ensureDefault(key, defaultValue, comment); + return config.getInt(key, defaultValue); + } + + private static double getDouble(String key, double defaultValue, String... comment) { + return getDouble(key, null, defaultValue, comment); + } + + private static double getDouble(String key, @Nullable String oldKey, double defaultValue, String... comment) { + ensureDefault(key, defaultValue, comment); + return config.getDouble(key, defaultValue); + } + + private static String getString(String key, String defaultValue, String... comment) { + return getOldString(key, null, defaultValue, comment); + } + + private static String getOldString(String key, @Nullable String oldKey, String defaultValue, String... comment) { + ensureDefault(key, defaultValue, comment); + return config.getString(key, defaultValue); + } + + private static List getStringList(String key, List defaultValue, String... comment) { + return getStringList(key, null, defaultValue, comment); + } + + private static List getStringList(String key, @Nullable String oldKey, List defaultValue, String... comment) { + ensureDefault(key, defaultValue, comment); + return config.getStringList(key); + } +} \ No newline at end of file diff --git a/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java b/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java index 582467e3419c23446b20d3076fbfce22115250a8..9623797f9c195f8e2117610b763a360f28e783ac 100644 --- a/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java +++ b/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java @@ -233,6 +233,8 @@ public class DedicatedServer extends MinecraftServer implements ServerInterface gg.pufferfish.pufferfish.PufferfishConfig.pufferfishFile = (java.io.File) options.valueOf("pufferfish-settings"); // Purpur gg.pufferfish.pufferfish.PufferfishConfig.load(); // Pufferfish gg.pufferfish.pufferfish.PufferfishCommand.init(); // Pufferfish + gq.bxteam.divinemc.config.DivineConfig.load(); // DivineMC + gq.bxteam.divinemc.config.DivineCommand.init(); // DivineMC this.setPvpAllowed(dedicatedserverproperties.pvp); this.setFlightAllowed(dedicatedserverproperties.allowFlight);