From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: violetc <58360096+s-yh-china@users.noreply.github.com> Date: Sat, 13 Aug 2022 17:54:19 +0800 Subject: [PATCH] LeavesConfig V2 diff --git a/src/main/java/top/leavesmc/leaves/LeavesConfig.java b/src/main/java/top/leavesmc/leaves/LeavesConfig.java index f380cd6a9849391f753a1c3d9a55e6b633781425..c232766bda8b253988b02bc14d4659f4b5553d96 100644 --- a/src/main/java/top/leavesmc/leaves/LeavesConfig.java +++ b/src/main/java/top/leavesmc/leaves/LeavesConfig.java @@ -24,7 +24,7 @@ import java.util.logging.Level; public final class LeavesConfig { public static final String CONFIG_HEADER = "Configuration file for Leaves."; - public static final int CURRENT_CONFIG_VERSION = 1; + public static final int CURRENT_CONFIG_VERSION = 2; private static final Object[] EMPTY = new Object[0]; @@ -59,12 +59,13 @@ public final class LeavesConfig { if (config.contains("config-version-please-do-not-modify-me")) { LeavesConfig.set("config-version-please-do-not-modify-me", null); } - LeavesConfig.configVersion = LeavesConfig.getInt("config-version", CURRENT_CONFIG_VERSION); + LeavesConfig.configVersion = LeavesConfig.getInt("config-version", CURRENT_CONFIG_VERSION); LeavesConfig.set("config-version", CURRENT_CONFIG_VERSION); - LeavesConfig.load(config); + updateConfigVersion(config); + LeavesConfig.load(config); - commands = new HashMap<>(); + commands = new HashMap<>(); if (top.leavesmc.leaves.LeavesConfig.fakeplayerSupport) { commands.put("bot", new BotCommand("bot")); @@ -73,20 +74,20 @@ public final class LeavesConfig { } public static void load(final YamlConfiguration config) { - for (Method method : LeavesConfig.class.getDeclaredMethods()) { - if (Modifier.isPrivate(method.getModifiers())) { - if (method.getParameterTypes().length == 0 && method.getReturnType() == Void.TYPE) { - try { - method.setAccessible(true); - method.invoke(null); - } catch (InvocationTargetException ex) { - throw Throwables.propagate(ex.getCause()); - } catch (Exception ex) { - Bukkit.getLogger().log(Level.SEVERE, "Error invoking " + method, ex); - } - } - } - } + for (Method method : LeavesConfig.class.getDeclaredMethods()) { + if (Modifier.isPrivate(method.getModifiers())) { + if (method.getParameterTypes().length == 0 && method.getReturnType() == Void.TYPE) { + try { + method.setAccessible(true); + method.invoke(null); + } catch (InvocationTargetException ex) { + throw Throwables.propagate(ex.getCause()); + } catch (Exception ex) { + Bukkit.getLogger().log(Level.SEVERE, "Error invoking " + method, ex); + } + } + } + } /* We re-save to add new options */ try { @@ -137,58 +138,79 @@ public final class LeavesConfig { return (List) LeavesConfig.config.getList(path, config.getList(path)); } + private static void updateConfigVersion(final YamlConfiguration config) { + if (configVersion < CURRENT_CONFIG_VERSION) { + playerCanEditSign = config.getBoolean("settings.player-can-edit-sign", playerCanEditSign); + snowballAndEggCanKnockback = config.getBoolean("settings.snowball-and-egg-can-knockback-player", snowballAndEggCanKnockback); + fakeplayerSupport = config.getBoolean("settings.fakeplayer.enable", fakeplayerSupport); + unableFakeplayerNames = (List) config.getList("settings.fakeplayer.unable-fakeplayer-names", Arrays.asList("player-name")); + shearsInDispenserCanZeroAmount = config.getBoolean("settings.shears-in-dispenser-can-zero-amount", shearsInDispenserCanZeroAmount); + redstoneShearsWrench = config.getBoolean("settings.redstone-shears-wrench", redstoneShearsWrench); + buddingAmethystCanPushByPiston = config.getBoolean("settings.budding-amethyst-can-push-by-piston", buddingAmethystCanPushByPiston); + spectatorDontGetAdvancement = config.getBoolean("settings.spectator-dont-get-advancement", spectatorDontGetAdvancement); + stickChangeArmorStandArmStatus = config.getBoolean("settings.stick-change-armorstand-arm-status", stickChangeArmorStandArmStatus); + noChatSign = config.getBoolean("settings.no-chat-sign", noChatSign); + + config.set("settings.player-can-edit-sign", null); + config.set("settings.snowball-and-egg-can-knockback-player", null); + config.set("settings.fakeplayer", null); + config.set("settings.shears-in-dispenser-can-zero-amount", null); + config.set("settings.redstone-shears-wrench", null); + config.set("settings.budding-amethyst-can-push-by-piston", null); + config.set("settings.spectator-dont-get-advancement", null); + config.set("settings.stick-change-armorstand-arm-status", null); + config.set("settings.no-chat-sign", null); + } + } + public static boolean playerCanEditSign = true; private static void playerCanEditSign() { - playerCanEditSign = getBoolean("settings.player-can-edit-sign", playerCanEditSign); + playerCanEditSign = getBoolean("settings.modify.player-can-edit-sign", playerCanEditSign); } public static boolean snowballAndEggCanKnockback = true; private static void snowballAndEggCanKnockback() { - snowballAndEggCanKnockback = getBoolean("settings.snowball-and-egg-can-knockback-player", snowballAndEggCanKnockback); + snowballAndEggCanKnockback = getBoolean("settings.modify.snowball-and-egg-can-knockback-player", snowballAndEggCanKnockback); } public static boolean fakeplayerSupport = true; private static void fakeplayerSupport() { - if (config.contains("settings.fakeplayer-support")) { - fakeplayerSupport = LeavesConfig.config.getBoolean("settings.fakeplayer-support", fakeplayerSupport); - LeavesConfig.config.set("settings.fakeplayer-support", null); - } - fakeplayerSupport = getBoolean("settings.fakeplayer.enable", fakeplayerSupport); + fakeplayerSupport = getBoolean("settings.modify.fakeplayer.enable", fakeplayerSupport); } - public static List unableFakeplayerNames; + public static List unableFakeplayerNames = List.of("player-name"); private static void unableFakeplayerNames() { - unableFakeplayerNames = getList("settings.fakeplayer.unable-fakeplayer-names", Arrays.asList("player-name")); + unableFakeplayerNames = getList("settings.modify.fakeplayer.unable-fakeplayer-names", unableFakeplayerNames); } public static boolean shearsInDispenserCanZeroAmount = false; private static void shearsInDispenserCanZeroAmount() { - shearsInDispenserCanZeroAmount = getBoolean("settings.shears-in-dispenser-can-zero-amount", shearsInDispenserCanZeroAmount); + shearsInDispenserCanZeroAmount = getBoolean("settings.modify.shears-in-dispenser-can-zero-amount", shearsInDispenserCanZeroAmount); } public static boolean redstoneShearsWrench = true; private static void redstoneShearsWrench() { - redstoneShearsWrench = getBoolean("settings.redstone-shears-wrench", redstoneShearsWrench); + redstoneShearsWrench = getBoolean("settings.modify.redstone-shears-wrench", redstoneShearsWrench); } public static boolean buddingAmethystCanPushByPiston = false; private static void buddingAmethystCanPushByPiston() { - buddingAmethystCanPushByPiston = getBoolean("settings.budding-amethyst-can-push-by-piston", buddingAmethystCanPushByPiston); + buddingAmethystCanPushByPiston = getBoolean("settings.modify.budding-amethyst-can-push-by-piston", buddingAmethystCanPushByPiston); } public static boolean spectatorDontGetAdvancement = false; - private static void spectatorDontGetAdvancement() { - spectatorDontGetAdvancement = getBoolean("settings.spectator-dont-get-advancement", spectatorDontGetAdvancement); + private static void spectatorDontGetAdvancement() { + spectatorDontGetAdvancement = getBoolean("settings.modify.spectator-dont-get-advancement", spectatorDontGetAdvancement); } public static boolean stickChangeArmorStandArmStatus = true; - private static void stickChangeArmorStandHasArm() { - stickChangeArmorStandArmStatus = getBoolean("settings.stick-change-armorstand-arm-status", stickChangeArmorStandArmStatus); + private static void stickChangeArmorStandHasArm() { + stickChangeArmorStandArmStatus = getBoolean("settings.modify.stick-change-armorstand-arm-status", stickChangeArmorStandArmStatus); } public static boolean noChatSign = true; private static void noChatSign() { - noChatSign = getBoolean("settings.no-chat-sign", noChatSign); + noChatSign = getBoolean("settings.modify.no-chat-sign", noChatSign); } public static boolean asyncMobSpawning = true; @@ -213,7 +235,7 @@ public final class LeavesConfig { this.worldDefaults = LeavesConfig.config.createSection("world-settings.default"); } - String worldSectionPath = LeavesConfig.configVersion < 0 ? this.worldName : "world-settings.".concat(this.worldName); + String worldSectionPath = LeavesConfig.configVersion < CURRENT_CONFIG_VERSION ? this.worldName : "world-settings.".concat(this.worldName); ConfigurationSection section = LeavesConfig.config.getConfigurationSection(worldSectionPath); this.configPath = worldSectionPath; if (LeavesConfig.createWorldSections) { @@ -242,7 +264,7 @@ public final class LeavesConfig { } } - if (LeavesConfig.configVersion < 1) { + if (LeavesConfig.configVersion < CURRENT_CONFIG_VERSION) { ConfigurationSection oldSection = LeavesConfig.config.getConfigurationSection(this.worldName); LeavesConfig.config.set("world-settings.".concat(this.worldName), oldSection); LeavesConfig.config.set(this.worldName, null); @@ -267,7 +289,7 @@ public final class LeavesConfig { boolean getBoolean(final String path, final boolean dfl) { final ConfigurationSection config = LeavesConfig.config.getConfigurationSection(this.configPath); this.worldDefaults.addDefault(path, Boolean.valueOf(dfl)); - if (LeavesConfig.configVersion < 1) { + if (LeavesConfig.configVersion < CURRENT_CONFIG_VERSION) { if (config != null && config.getBoolean(path) == dfl) { config.set(path, null); } @@ -277,7 +299,7 @@ public final class LeavesConfig { boolean getBooleanRaw(final String path, final boolean dfl) { final ConfigurationSection config = LeavesConfig.config.getConfigurationSection(this.configPath); - if (LeavesConfig.configVersion < 1) { + if (LeavesConfig.configVersion < CURRENT_CONFIG_VERSION) { if (config != null && config.getBoolean(path) == dfl) { config.set(path, null); } @@ -288,7 +310,7 @@ public final class LeavesConfig { int getInt(final String path, final int dfl) { final ConfigurationSection config = LeavesConfig.config.getConfigurationSection(this.configPath); this.worldDefaults.addDefault(path, Integer.valueOf(dfl)); - if (LeavesConfig.configVersion < 1) { + if (LeavesConfig.configVersion < CURRENT_CONFIG_VERSION) { if (config != null && config.getInt(path) == dfl) { config.set(path, null); } @@ -298,7 +320,7 @@ public final class LeavesConfig { int getIntRaw(final String path, final int dfl) { final ConfigurationSection config = LeavesConfig.config.getConfigurationSection(this.configPath); - if (LeavesConfig.configVersion < 1) { + if (LeavesConfig.configVersion < CURRENT_CONFIG_VERSION) { if (config != null && config.getInt(path) == dfl) { config.set(path, null); } @@ -309,7 +331,7 @@ public final class LeavesConfig { long getLong(final String path, final long dfl) { final ConfigurationSection config = LeavesConfig.config.getConfigurationSection(this.configPath); this.worldDefaults.addDefault(path, Long.valueOf(dfl)); - if (LeavesConfig.configVersion < 1) { + if (LeavesConfig.configVersion < CURRENT_CONFIG_VERSION) { if (config != null && config.getLong(path) == dfl) { config.set(path, null); } @@ -319,7 +341,7 @@ public final class LeavesConfig { long getLongRaw(final String path, final long dfl) { final ConfigurationSection config = LeavesConfig.config.getConfigurationSection(this.configPath); - if (LeavesConfig.configVersion < 1) { + if (LeavesConfig.configVersion < CURRENT_CONFIG_VERSION) { if (config != null && config.getLong(path) == dfl) { config.set(path, null); } @@ -330,7 +352,7 @@ public final class LeavesConfig { double getDouble(final String path, final double dfl) { final ConfigurationSection config = LeavesConfig.config.getConfigurationSection(this.configPath); this.worldDefaults.addDefault(path, Double.valueOf(dfl)); - if (LeavesConfig.configVersion < 1) { + if (LeavesConfig.configVersion < CURRENT_CONFIG_VERSION) { if (config != null && config.getDouble(path) == dfl) { config.set(path, null); } @@ -340,7 +362,7 @@ public final class LeavesConfig { double getDoubleRaw(final String path, final double dfl) { final ConfigurationSection config = LeavesConfig.config.getConfigurationSection(this.configPath); - if (LeavesConfig.configVersion < 1) { + if (LeavesConfig.configVersion < CURRENT_CONFIG_VERSION) { if (config != null && config.getDouble(path) == dfl) { config.set(path, null); }