From 32975ca35dfeaee279ecf13b0e54de9e0941cc27 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Wed, 24 Nov 2021 22:33:45 +0100 Subject: [PATCH] Rewrites a lot of the config command to only do the minimum necessary steps to load the changes Adds a ConfigTag class for helping to decide the action necessary for updating a given config option Splits the color setting in PortalSignDrawer as only one color is set at a time when the /config command is used Updates the configOptions map when a config option is changed --- .../stargate/command/CommandConfig.java | 99 +++++++++++++++++-- .../knarcraft/stargate/config/ConfigTag.java | 74 ++++++++++++++ .../stargate/config/EconomyConfig.java | 22 ++--- .../stargate/config/StargateConfig.java | 44 ++++++--- .../stargate/config/StargateGateConfig.java | 10 +- .../stargate/portal/PortalSignDrawer.java | 20 ++-- 6 files changed, 218 insertions(+), 51 deletions(-) create mode 100644 src/main/java/net/knarcraft/stargate/config/ConfigTag.java diff --git a/src/main/java/net/knarcraft/stargate/command/CommandConfig.java b/src/main/java/net/knarcraft/stargate/command/CommandConfig.java index 54aa1a5..8328352 100644 --- a/src/main/java/net/knarcraft/stargate/command/CommandConfig.java +++ b/src/main/java/net/knarcraft/stargate/command/CommandConfig.java @@ -2,6 +2,10 @@ package net.knarcraft.stargate.command; import net.knarcraft.stargate.Stargate; import net.knarcraft.stargate.config.ConfigOption; +import net.knarcraft.stargate.config.ConfigTag; +import net.knarcraft.stargate.portal.Portal; +import net.knarcraft.stargate.portal.PortalRegistry; +import net.knarcraft.stargate.portal.PortalSignDrawer; import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -55,7 +59,7 @@ public class CommandConfig implements CommandExecutor { FileConfiguration configuration = Stargate.getInstance().getConfig(); //Validate any sign colors - if (selectedOption == ConfigOption.MAIN_SIGN_COLOR || selectedOption == ConfigOption.HIGHLIGHT_SIGN_COLOR) { + if (ConfigTag.COLOR.isTagged(selectedOption)) { try { ChatColor.valueOf(value.toUpperCase()); } catch (IllegalArgumentException | NullPointerException ignored) { @@ -66,12 +70,20 @@ public class CommandConfig implements CommandExecutor { //Store the config values, accounting for the data type switch (selectedOption.getDataType()) { - case BOOLEAN -> configuration.set(selectedOption.getConfigNode(), Boolean.parseBoolean(value)); + case BOOLEAN -> { + boolean newValue = Boolean.parseBoolean(value); + if (selectedOption == ConfigOption.ENABLE_BUNGEE && newValue != Stargate.getGateConfig().enableBungee()) { + Stargate.getStargateConfig().startStopBungeeListener(newValue); + } + Stargate.getStargateConfig().getConfigOptionsReference().put(selectedOption, newValue); + configuration.set(selectedOption.getConfigNode(), newValue); + } case INTEGER -> { Integer intValue = getInteger(commandSender, selectedOption, value); if (intValue == null) { return; } else { + Stargate.getStargateConfig().getConfigOptionsReference().put(selectedOption, intValue); configuration.set(selectedOption.getConfigNode(), intValue); } } @@ -83,14 +95,68 @@ public class CommandConfig implements CommandExecutor { return; } } + if (ConfigTag.COLOR.isTagged(selectedOption)) { + if (!registerColor(selectedOption, value, commandSender)) { + return; + } + } + if (selectedOption == ConfigOption.LANGUAGE) { + Stargate.getStargateConfig().getLanguageLoader().setChosenLanguage(value); + } + Stargate.getStargateConfig().getConfigOptionsReference().put(selectedOption, value); + configuration.set(selectedOption.getConfigNode(), value); + } + default -> { + Stargate.getStargateConfig().getConfigOptionsReference().put(selectedOption, value); configuration.set(selectedOption.getConfigNode(), value); } - default -> configuration.set(selectedOption.getConfigNode(), value); } //Save the config file and reload if necessary Stargate.getInstance().saveConfig(); - reloadIfNecessary(commandSender); + + Stargate.getMessageSender().sendSuccessMessage(commandSender, "Config updated"); + + //Reload whatever is necessary + reloadIfNecessary(commandSender, selectedOption); + } + + /** + * Registers the chat color if + * + * @param selectedOption

The option to change

+ * @param commandSender

The command sender to alert if the color is invalid

+ * @param value

The new option value

+ */ + private boolean registerColor(ConfigOption selectedOption, String value, CommandSender commandSender) { + ChatColor parsedColor = parseColor(value); + if (parsedColor == null) { + commandSender.sendMessage(ChatColor.RED + "Invalid color given"); + return false; + } + + if (selectedOption == ConfigOption.FREE_GATES_COLOR) { + PortalSignDrawer.setFreeColor(parsedColor); + } else if (selectedOption == ConfigOption.MAIN_SIGN_COLOR) { + PortalSignDrawer.setMainColor(parsedColor); + } else if (selectedOption == ConfigOption.HIGHLIGHT_SIGN_COLOR) { + PortalSignDrawer.setHighlightColor(parsedColor); + } + return true; + } + + /** + * Parses a chat color + * + * @param value

The value to parse

+ * @return

The parsed color or null

+ */ + private ChatColor parseColor(String value) { + try { + return ChatColor.valueOf(value.toUpperCase()); + } catch (IllegalArgumentException | NullPointerException ignored) { + return null; + } } /** @@ -121,12 +187,27 @@ public class CommandConfig implements CommandExecutor { * Reloads the config if necessary * * @param commandSender

The command sender initiating the reload

+ * @param configOption

The changed config option

*/ - private void reloadIfNecessary(CommandSender commandSender) { - //TODO: Only update the config values which have changed and do the least amount of work necessary to load the - // changes. Only do a full reload if absolutely necessary, or when the partial reloading would be as - // inefficient as a full reload. - Stargate.getStargateConfig().reload(commandSender); + private void reloadIfNecessary(CommandSender commandSender, ConfigOption configOption) { + if (ConfigTag.requiresFullReload(configOption)) { + //Reload everything + Stargate.getStargateConfig().reload(commandSender); + } else if (ConfigTag.requiresPortalReload(configOption)) { + //Just unload and reload the portals + Stargate.getStargateConfig().unloadAllPortals(); + Stargate.getStargateConfig().loadAllPortals(); + } else if (ConfigTag.requiresLanguageReload(configOption)) { + //Reload the language loader + Stargate.getStargateConfig().getLanguageLoader().reload(); + //Re-draw all portal signs + for (Portal portal : PortalRegistry.getAllPortals()) { + portal.drawSign(); + } + } else if (ConfigTag.requiresEconomyReload(configOption)) { + //Load or unload Vault and Economy as necessary + Stargate.getStargateConfig().reloadEconomy(); + } } /** diff --git a/src/main/java/net/knarcraft/stargate/config/ConfigTag.java b/src/main/java/net/knarcraft/stargate/config/ConfigTag.java new file mode 100644 index 0000000..4896e20 --- /dev/null +++ b/src/main/java/net/knarcraft/stargate/config/ConfigTag.java @@ -0,0 +1,74 @@ +package net.knarcraft.stargate.config; + +import java.util.Arrays; + +/** + * A config tag groups config values by a property + */ +public enum ConfigTag { + + COLOR(new ConfigOption[]{ConfigOption.FREE_GATES_COLOR, ConfigOption.MAIN_SIGN_COLOR, ConfigOption.HIGHLIGHT_SIGN_COLOR}), + FOLDER(new ConfigOption[]{ConfigOption.GATE_FOLDER, ConfigOption.PORTAL_FOLDER}); + + private final ConfigOption[] taggedOptions; + + /** + * Instantiates a new config tag + * + * @param taggedOptions

The config options included in this tag

+ */ + ConfigTag(ConfigOption[] taggedOptions) { + this.taggedOptions = taggedOptions; + } + + /** + * Checks whether a config tag includes the given config option + * + * @param option

The config option to check

+ * @return

True of the config option is tagged

+ */ + public boolean isTagged(ConfigOption option) { + return Arrays.stream(taggedOptions).anyMatch((item) -> item == option); + } + + /** + * Checks whether a given config option requires a full reload to take effect + * + * @param option

The config option to check

+ * @return

True if changing the config option requires a full reload to take effect

+ */ + public static boolean requiresFullReload(ConfigOption option) { + return FOLDER.isTagged(option); + } + + /** + * Checks whether a given config option requires a portal reload to take effect + * + * @param option

The config option to check

+ * @return

True if changing the config option requires a portal reload to take effect

+ */ + public static boolean requiresPortalReload(ConfigOption option) { + return COLOR.isTagged(option) || FOLDER.isTagged(option) || option == ConfigOption.VERIFY_PORTALS; + } + + /** + * Checks whether a given config option requires the language loader to be reloaded + * + * @param option

The config option to check

+ * @return

True if the language loader requires a reload

+ */ + public static boolean requiresLanguageReload(ConfigOption option) { + return option == ConfigOption.LANGUAGE; + } + + /** + * Checks whether a given config option requires economy to be reloaded + * + * @param option

The config option to check

+ * @return

True if economy requires a reload

+ */ + public static boolean requiresEconomyReload(ConfigOption option) { + return option == ConfigOption.USE_ECONOMY; + } + +} diff --git a/src/main/java/net/knarcraft/stargate/config/EconomyConfig.java b/src/main/java/net/knarcraft/stargate/config/EconomyConfig.java index ba842d3..d19c379 100644 --- a/src/main/java/net/knarcraft/stargate/config/EconomyConfig.java +++ b/src/main/java/net/knarcraft/stargate/config/EconomyConfig.java @@ -31,7 +31,12 @@ public final class EconomyConfig { */ public EconomyConfig(Map configOptions) { this.configOptions = configOptions; - loadEconomyConfig(configOptions); + try { + String freeColor = (String) configOptions.get(ConfigOption.FREE_GATES_COLOR); + PortalSignDrawer.setFreeColor(ChatColor.valueOf(freeColor.toUpperCase())); + } catch (IllegalArgumentException | NullPointerException ignored) { + PortalSignDrawer.setFreeColor(ChatColor.DARK_GREEN); + } } /** @@ -220,21 +225,6 @@ public final class EconomyConfig { } } - /** - * Loads all config values related to economy - * - * @param configOptions

The loaded config options to get values from

- */ - private void loadEconomyConfig(Map configOptions) { - - try { - String freeColor = (String) configOptions.get(ConfigOption.FREE_GATES_COLOR); - PortalSignDrawer.setFreeColor(ChatColor.valueOf(freeColor.toUpperCase())); - } catch (IllegalArgumentException | NullPointerException ignored) { - PortalSignDrawer.setFreeColor(ChatColor.DARK_GREEN); - } - } - /** * Determines if a player can do a gate action for free * diff --git a/src/main/java/net/knarcraft/stargate/config/StargateConfig.java b/src/main/java/net/knarcraft/stargate/config/StargateConfig.java index 88caa6a..19ba95d 100644 --- a/src/main/java/net/knarcraft/stargate/config/StargateConfig.java +++ b/src/main/java/net/knarcraft/stargate/config/StargateConfig.java @@ -46,8 +46,6 @@ public final class StargateConfig { private String portalFolder; private String languageName = "en"; - private boolean debuggingEnabled = false; - private boolean permissionDebuggingEnabled = false; private final Map configOptions; /** @@ -65,6 +63,18 @@ public final class StargateConfig { languageLoader = new LanguageLoader(dataFolderPath + "/lang/"); } + /** + * Gets a direct reference to the config option map + * + *

This reference can be used to alter the value of config options. Values should only be altered after it's + * been verified that the value is valid.

+ * + * @return

A reference to the config options map

+ */ + public Map getConfigOptionsReference() { + return configOptions; + } + /** * Finish the config setup by loading languages, gates and portals, and loading economy if vault is loaded */ @@ -81,7 +91,7 @@ public final class StargateConfig { languageLoader.reload(); messageSender = new MessageSender(languageLoader); - if (debuggingEnabled) { + if (isDebuggingEnabled()) { languageLoader.debug(); } @@ -130,7 +140,7 @@ public final class StargateConfig { * @return

Whether debugging is enabled

*/ public boolean isDebuggingEnabled() { - return debuggingEnabled; + return (boolean) configOptions.get(ConfigOption.DEBUG); } /** @@ -139,7 +149,7 @@ public final class StargateConfig { * @return

Whether permission debugging is enabled

*/ public boolean isPermissionDebuggingEnabled() { - return permissionDebuggingEnabled; + return (boolean) configOptions.get(ConfigOption.PERMISSION_DEBUG); } /** @@ -186,6 +196,17 @@ public final class StargateConfig { * Un-loads all loaded data */ private void unload() { + //De-activate, close and unload all loaded portals + unloadAllPortals(); + + //Clear all loaded gates + GateHandler.clearGates(); + } + + /** + * Un-loads all loaded portals + */ + public void unloadAllPortals() { //De-activate all currently active portals for (Portal activePortal : activePortalsQueue) { activePortal.getPortalActivator().deactivate(); @@ -201,9 +222,6 @@ public final class StargateConfig { //Clear all loaded portals PortalRegistry.clearPortals(); - - //Clear all loaded gates - GateHandler.clearGates(); } /** @@ -256,7 +274,7 @@ public final class StargateConfig { //Update the language loader in case the loaded language changed languageLoader.setChosenLanguage(languageName); languageLoader.reload(); - if (debuggingEnabled) { + if (isDebuggingEnabled()) { languageLoader.debug(); } @@ -267,7 +285,7 @@ public final class StargateConfig { /** * Starts the listener for listening to BungeeCord messages */ - private void startStopBungeeListener(boolean start) { + public void startStopBungeeListener(boolean start) { Messenger messenger = Bukkit.getMessenger(); String bungeeChannel = "BungeeCord"; @@ -283,7 +301,7 @@ public final class StargateConfig { /** * Reloads economy by enabling or disabling it as necessary */ - private void reloadEconomy() { + public void reloadEconomy() { EconomyConfig economyConfig = getEconomyConfig(); if (economyConfig.isEconomyEnabled() && economyConfig.getEconomy() == null) { setupVaultEconomy(); @@ -342,10 +360,6 @@ public final class StargateConfig { portalFolder = (String) configOptions.get(ConfigOption.PORTAL_FOLDER); gateFolder = (String) configOptions.get(ConfigOption.GATE_FOLDER); - //Get enabled debug settings from the config - debuggingEnabled = (boolean) configOptions.get(ConfigOption.DEBUG); - permissionDebuggingEnabled = (boolean) configOptions.get(ConfigOption.PERMISSION_DEBUG); - //If users have an outdated config, assume they also need to update their default gates if (isMigrating) { GateHandler.writeDefaultGatesToFolder(gateFolder); diff --git a/src/main/java/net/knarcraft/stargate/config/StargateGateConfig.java b/src/main/java/net/knarcraft/stargate/config/StargateGateConfig.java index f1640e0..035ee98 100644 --- a/src/main/java/net/knarcraft/stargate/config/StargateGateConfig.java +++ b/src/main/java/net/knarcraft/stargate/config/StargateGateConfig.java @@ -190,13 +190,13 @@ public final class StargateGateConfig { private void loadSignColor(String mainSignColor, String highlightSignColor) { if (mainSignColor != null && highlightSignColor != null) { try { - PortalSignDrawer.setColors(ChatColor.valueOf(mainSignColor.toUpperCase()), - ChatColor.valueOf(highlightSignColor.toUpperCase())); - return; + PortalSignDrawer.setMainColor(ChatColor.valueOf(highlightSignColor.toUpperCase())); + PortalSignDrawer.setHighlightColor(ChatColor.valueOf(highlightSignColor.toUpperCase())); } catch (IllegalArgumentException | NullPointerException ignored) { + Stargate.logWarning("You have specified an invalid color in your config.yml. Defaulting to BLACK and WHITE"); + PortalSignDrawer.setMainColor(ChatColor.BLACK); + PortalSignDrawer.setHighlightColor(ChatColor.WHITE); } } - Stargate.logWarning("You have specified an invalid color in your config.yml. Defaulting to BLACK and WHITE"); - PortalSignDrawer.setColors(ChatColor.BLACK, ChatColor.WHITE); } } diff --git a/src/main/java/net/knarcraft/stargate/portal/PortalSignDrawer.java b/src/main/java/net/knarcraft/stargate/portal/PortalSignDrawer.java index 2921bf8..7b9cb2d 100644 --- a/src/main/java/net/knarcraft/stargate/portal/PortalSignDrawer.java +++ b/src/main/java/net/knarcraft/stargate/portal/PortalSignDrawer.java @@ -29,19 +29,27 @@ public class PortalSignDrawer { } /** - * Sets the main and highlighting sign colors + * Sets the highlighting sign color * - *

The main sign color is used for most text on the sign, while the highlighting color is used for the markings - * around portal names and network names ('>','<','-',')','(')

+ *

The highlighting color is used for the markings around portal names and network names ('>','<','-',')','(').

* - * @param newMainColor

The new main sign color

* @param newHighlightColor

The new highlight color

*/ - public static void setColors(ChatColor newMainColor, ChatColor newHighlightColor) { - mainColor = newMainColor; + public static void setHighlightColor(ChatColor newHighlightColor) { highlightColor = newHighlightColor; } + /** + * Sets the main sign color + * + *

The main sign color is used for most text on the sign.

+ * + * @param newMainColor

The new main sign color

+ */ + public static void setMainColor(ChatColor newMainColor) { + mainColor = newMainColor; + } + /** * Sets the color to use for marking free stargates *