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/net/minecraft/server/dedicated/DedicatedServer.java b/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java index 7a442fb4cf78bcb1268359ee41a6de138edb76b6..924a1b7f444ab78512bb7773faf513a2a48fb485 100644 --- a/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java +++ b/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java @@ -57,6 +57,7 @@ import net.minecraft.world.level.GameType; import net.minecraft.world.level.block.entity.SkullBlockEntity; import net.minecraft.world.level.storage.LevelStorageSource; import org.slf4j.Logger; +import space.bxteam.divinemc.configuration.DivineConfig; // DivineMC // CraftBukkit start import net.minecraft.server.WorldLoader; @@ -250,6 +251,16 @@ public class DedicatedServer extends MinecraftServer implements ServerInterface gg.pufferfish.pufferfish.PufferfishConfig.load(); // Pufferfish gg.pufferfish.pufferfish.PufferfishCommand.init(); // Pufferfish + // DivineMC start + try { + DivineConfig.init((java.io.File) options.valueOf("divinemc-settings")); + } catch (Exception e) { + DedicatedServer.LOGGER.error("Unable to load server configuration", e); + return false; + } + DivineConfig.registerCommands(); + // DivineMC end + this.setPvpAllowed(dedicatedserverproperties.pvp); this.setFlightAllowed(dedicatedserverproperties.allowFlight); this.setMotd(dedicatedserverproperties.motd); diff --git a/src/main/java/net/minecraft/world/level/Level.java b/src/main/java/net/minecraft/world/level/Level.java index 34e8eb828be517d9ee3973597c74c5cac3d75c99..833983670727fc2844386ea2381358433f646c50 100644 --- a/src/main/java/net/minecraft/world/level/Level.java +++ b/src/main/java/net/minecraft/world/level/Level.java @@ -85,6 +85,7 @@ import net.minecraft.world.phys.AABB; import net.minecraft.world.phys.Vec3; import net.minecraft.world.phys.shapes.VoxelShape; import net.minecraft.world.scores.Scoreboard; +import space.bxteam.divinemc.configuration.DivineWorldConfig; // DivineMC // CraftBukkit start import java.util.HashMap; @@ -175,6 +176,7 @@ public abstract class Level implements LevelAccessor, AutoCloseable, ca.spottedl public final com.destroystokyo.paper.antixray.ChunkPacketBlockController chunkPacketBlockController; // Paper - Anti-Xray public final org.purpurmc.purpur.PurpurWorldConfig purpurConfig; // Purpur + public final DivineWorldConfig divinemcConfig; // DivineMC public static BlockPos lastPhysicsProblem; // Spigot private org.spigotmc.TickLimiter entityLimiter; private org.spigotmc.TickLimiter tileLimiter; @@ -887,6 +889,7 @@ public abstract class Level implements LevelAccessor, AutoCloseable, ca.spottedl this.spigotConfig = new org.spigotmc.SpigotWorldConfig(((net.minecraft.world.level.storage.PrimaryLevelData) worlddatamutable).getLevelName()); // Spigot this.paperConfig = paperWorldConfigCreator.apply(this.spigotConfig); // Paper - create paper world config this.purpurConfig = new org.purpurmc.purpur.PurpurWorldConfig(((net.minecraft.world.level.storage.PrimaryLevelData) worlddatamutable).getLevelName(), env); // Purpur + this.divinemcConfig = new DivineWorldConfig(((net.minecraft.world.level.storage.PrimaryLevelData) worlddatamutable).getLevelName(), env); // DivineMC this.playerBreedingCooldowns = this.getNewBreedingCooldownCache(); // Purpur this.generator = gen; this.world = new CraftWorld((ServerLevel) this, gen, biomeProvider, env); diff --git a/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/src/main/java/org/bukkit/craftbukkit/CraftServer.java index 81922bf93f8a96e6cf05944ca98a82cc5945a6c6..825066dbd54ef753cdc03cbbe9deb65438759449 100644 --- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java +++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java @@ -268,6 +268,8 @@ import net.md_5.bungee.api.chat.BaseComponent; // Spigot import javax.annotation.Nullable; // Paper import javax.annotation.Nonnull; // Paper +import space.bxteam.divinemc.configuration.DivineConfig; // DivineMC + public final class CraftServer implements Server { private final String serverName = io.papermc.paper.ServerBuildInfo.buildInfo().brandName(); // Paper private final String serverVersion; @@ -1101,6 +1103,7 @@ public final class CraftServer implements Server { org.spigotmc.SpigotConfig.init((File) this.console.options.valueOf("spigot-settings")); // Spigot this.console.paperConfigurations.reloadConfigs(this.console); org.purpurmc.purpur.PurpurConfig.init((File) console.options.valueOf("purpur-settings")); // Purpur + DivineConfig.init((File) console.options.valueOf("divinemc-settings")); // DivineMC for (ServerLevel world : this.console.getAllLevels()) { // world.serverLevelData.setDifficulty(config.difficulty); // Paper - per level difficulty world.setSpawnSettings(world.serverLevelData.getDifficulty() != Difficulty.PEACEFUL && config.spawnMonsters); // Paper - per level difficulty (from MinecraftServer#setDifficulty(ServerLevel, Difficulty, boolean)) @@ -1117,6 +1120,7 @@ public final class CraftServer implements Server { } world.spigotConfig.init(); // Spigot world.purpurConfig.init(); // Purpur + world.divinemcConfig.init(); // DivineMC } Plugin[] pluginClone = pluginManager.getPlugins().clone(); // Paper @@ -1135,6 +1139,7 @@ public final class CraftServer implements Server { io.papermc.paper.command.PaperCommands.registerCommands(this.console); // Paper this.spark.registerCommandBeforePlugins(this); // Paper - spark org.purpurmc.purpur.PurpurConfig.registerCommands(); // Purpur + DivineConfig.registerCommands(); // DivineMC this.overrideAllCommandBlockCommands = this.commandsConfiguration.getStringList("command-block-overrides").contains("*"); this.ignoreVanillaPermissions = this.commandsConfiguration.getBoolean("ignore-vanilla-permissions"); @@ -3112,6 +3117,13 @@ public final class CraftServer implements Server { } // Purpur end + // DivineMC start + @Override + public YamlConfiguration getDivineConfig() { + return DivineConfig.config; + } + // DivineMC end + @Override public void restart() { org.spigotmc.RestartCommand.restart(); diff --git a/src/main/java/org/bukkit/craftbukkit/Main.java b/src/main/java/org/bukkit/craftbukkit/Main.java index 430407b0cf06ba337c59cd576c7e40a8e5fe8229..67723070296f861c477ab484ad7f39db9eba3074 100644 --- a/src/main/java/org/bukkit/craftbukkit/Main.java +++ b/src/main/java/org/bukkit/craftbukkit/Main.java @@ -190,6 +190,14 @@ public class Main { .describedAs("Yml file"); // Purpur end + // DivineMC start + acceptsAll(asList("divinemc", "divinemc-settings"), "File for divinemc settings") + .withRequiredArg() + .ofType(File.class) + .defaultsTo(new File("divinemc.yml")) + .describedAs("Yml file"); + // DivineMC end + // Paper start acceptsAll(asList("server-name"), "Name of the server") .withRequiredArg() diff --git a/src/main/java/space/bxteam/divinemc/commands/DivineCommand.java b/src/main/java/space/bxteam/divinemc/commands/DivineCommand.java new file mode 100644 index 0000000000000000000000000000000000000000..06af4af987859081bcf23e91da02b295ec31eba4 --- /dev/null +++ b/src/main/java/space/bxteam/divinemc/commands/DivineCommand.java @@ -0,0 +1,66 @@ +package space.bxteam.divinemc.commands; + +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.level.ServerLevel; +import space.bxteam.divinemc.configuration.DivineConfig; +import org.bukkit.ChatColor; +import org.bukkit.Location; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; + +import java.io.File; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class DivineCommand extends Command { + public DivineCommand(String name) { + super(name); + this.description = "DivineMC related commands"; + this.usageMessage = "/divinemc [reload | version]"; + this.setPermission("bukkit.command.divinemc"); + } + + @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; + + if (args.length != 1) { + sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage); + return false; + } + + if (args[0].equalsIgnoreCase("reload")) { + Command.broadcastCommandMessage(sender, ChatColor.RED + "Please note that this command is not supported and may cause issues."); + Command.broadcastCommandMessage(sender, ChatColor.RED + "If you encounter any issues please use the /stop command to restart your server."); + + MinecraftServer console = MinecraftServer.getServer(); + DivineConfig.init((File) console.options.valueOf("divinemc-settings")); + for (ServerLevel level : console.getAllLevels()) { + level.divinemcConfig.init(); + level.resetBreedingCooldowns(); + } + console.server.reloadCount++; + + Command.broadcastCommandMessage(sender, ChatColor.GREEN + "DivineMC config reload complete."); + } else if (args[0].equalsIgnoreCase("version")) { + Command verCmd = org.bukkit.Bukkit.getServer().getCommandMap().getCommand("version"); + if (verCmd != null) { + return verCmd.execute(sender, commandLabel, new String[0]); + } + } + + return true; + } +} diff --git a/src/main/java/space/bxteam/divinemc/configuration/DivineConfig.java b/src/main/java/space/bxteam/divinemc/configuration/DivineConfig.java new file mode 100644 index 0000000000000000000000000000000000000000..27c45545e1e9ad6ea88e65c884c5a4955fc1c80c --- /dev/null +++ b/src/main/java/space/bxteam/divinemc/configuration/DivineConfig.java @@ -0,0 +1,157 @@ +package space.bxteam.divinemc.configuration; + +import com.google.common.base.Throwables; +import com.google.common.collect.ImmutableMap; +import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.block.state.BlockBehaviour; +import space.bxteam.divinemc.commands.*; +import net.minecraft.server.MinecraftServer; +import net.minecraft.world.level.block.Blocks; +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.configuration.ConfigurationSection; +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.logging.Level; + +@SuppressWarnings("unused") +public class DivineConfig { + private static final String HEADER = "This is the main configuration file for DivineMC.\n" + + "If you need help with the configuration or have any questions related to DivineMC,\n" + + "join us in our Discord server.\n" + + "\n" + + "Discord: https://discord.gg/p7cxhw7E2M \n" + + "Docs: https://docs.bx-team.space/divinemc \n" + + "New builds: https://github.com/DivineMC/DivineMC/releases/latest"; + private static File CONFIG_FILE; + public static YamlConfiguration config; + + private static Map commands; + + public static int version; + static boolean verbose; + + public static void init(File configFile) { + CONFIG_FILE = configFile; + config = new YamlConfiguration(); + try { + config.load(CONFIG_FILE); + } catch (IOException ignore) { + } catch (InvalidConfigurationException ex) { + Bukkit.getLogger().log(Level.SEVERE, "Could not load divinemc.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("divinemc", new DivineCommand("divinemc")); + + version = getInt("config-version", 3); + set("config-version", 3); + + readConfig(DivineConfig.class, null); + + Block.BLOCK_STATE_REGISTRY.forEach(BlockBehaviour.BlockStateBase::initCache); + } + + protected static void log(String s) { + if (verbose) { + log(Level.INFO, s); + } + } + + protected static void log(Level level, String s) { + Bukkit.getLogger().log(level, s); + } + + public static void registerCommands() { + for (Map.Entry entry : commands.entrySet()) { + MinecraftServer.getServer().server.getCommandMap().register(entry.getKey(), "DivineMC", 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 void set(String path, Object val) { + config.addDefault(path, val); + config.set(path, val); + } + + private static String getString(String path, String def) { + config.addDefault(path, def); + return config.getString(path, config.getString(path)); + } + + 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 List getList(String path, T def) { + config.addDefault(path, def); + return config.getList(path, config.getList(path)); + } + + static Map getMap(String path, Map def) { + if (def != null && config.getConfigurationSection(path) == null) { + config.addDefault(path, def); + return def; + } + return toMap(config.getConfigurationSection(path)); + } + + private static Map toMap(ConfigurationSection section) { + ImmutableMap.Builder builder = ImmutableMap.builder(); + if (section != null) { + for (String key : section.getKeys(false)) { + Object obj = section.get(key); + if (obj != null) { + builder.put(key, obj instanceof ConfigurationSection val ? toMap(val) : obj); + } + } + } + return builder.build(); + } +} diff --git a/src/main/java/space/bxteam/divinemc/configuration/DivineWorldConfig.java b/src/main/java/space/bxteam/divinemc/configuration/DivineWorldConfig.java new file mode 100644 index 0000000000000000000000000000000000000000..3e64e0d2d925f9fca1eb38cc07c20cae785ef39f --- /dev/null +++ b/src/main/java/space/bxteam/divinemc/configuration/DivineWorldConfig.java @@ -0,0 +1,80 @@ +package space.bxteam.divinemc.configuration; + +import org.apache.commons.lang.BooleanUtils; +import org.bukkit.World; +import org.bukkit.configuration.ConfigurationSection; + +import java.util.List; +import java.util.Map; +import java.util.function.Predicate; + +import static space.bxteam.divinemc.configuration.DivineConfig.log; + +@SuppressWarnings("unused") +public class DivineWorldConfig { + + private final String worldName; + private final World.Environment environment; + + public DivineWorldConfig(String worldName, World.Environment environment) { + this.worldName = worldName; + this.environment = environment; + init(); + } + + public void init() { + log("-------- World Settings For [" + worldName + "] --------"); + DivineConfig.readConfig(DivineWorldConfig.class, this); + } + + private void set(String path, Object val) { + DivineConfig.config.addDefault("world-settings.default." + path, val); + DivineConfig.config.set("world-settings.default." + path, val); + if (DivineConfig.config.get("world-settings." + worldName + "." + path) != null) { + DivineConfig.config.addDefault("world-settings." + worldName + "." + path, val); + DivineConfig.config.set("world-settings." + worldName + "." + path, val); + } + } + + private ConfigurationSection getConfigurationSection(String path) { + ConfigurationSection section = DivineConfig.config.getConfigurationSection("world-settings." + worldName + "." + path); + return section != null ? section : DivineConfig.config.getConfigurationSection("world-settings.default." + path); + } + + private String getString(String path, String def) { + DivineConfig.config.addDefault("world-settings.default." + path, def); + return DivineConfig.config.getString("world-settings." + worldName + "." + path, DivineConfig.config.getString("world-settings.default." + path)); + } + + private boolean getBoolean(String path, boolean def) { + DivineConfig.config.addDefault("world-settings.default." + path, def); + return DivineConfig.config.getBoolean("world-settings." + worldName + "." + path, DivineConfig.config.getBoolean("world-settings.default." + path)); + } + + private boolean getBoolean(String path, Predicate predicate) { + String val = getString(path, "default").toLowerCase(); + Boolean bool = BooleanUtils.toBooleanObject(val, "true", "false", "default"); + return predicate.test(bool); + } + + private double getDouble(String path, double def) { + DivineConfig.config.addDefault("world-settings.default." + path, def); + return DivineConfig.config.getDouble("world-settings." + worldName + "." + path, DivineConfig.config.getDouble("world-settings.default." + path)); + } + + private int getInt(String path, int def) { + DivineConfig.config.addDefault("world-settings.default." + path, def); + return DivineConfig.config.getInt("world-settings." + worldName + "." + path, DivineConfig.config.getInt("world-settings.default." + path)); + } + + private List getList(String path, T def) { + DivineConfig.config.addDefault("world-settings.default." + path, def); + return DivineConfig.config.getList("world-settings." + worldName + "." + path, DivineConfig.config.getList("world-settings.default." + path)); + } + + private Map getMap(String path, Map def) { + final Map fallback = DivineConfig.getMap("world-settings.default." + path, def); + final Map value = DivineConfig.getMap("world-settings." + worldName + "." + path, null); + return value.isEmpty() ? fallback : value; + } +}