diff --git a/src/main/java/net/knarcraft/stargate/command/CommandConfig.java b/src/main/java/net/knarcraft/stargate/command/CommandConfig.java index 19260aa..0f6dccb 100644 --- a/src/main/java/net/knarcraft/stargate/command/CommandConfig.java +++ b/src/main/java/net/knarcraft/stargate/command/CommandConfig.java @@ -9,6 +9,7 @@ import net.knarcraft.stargate.portal.PortalRegistry; import net.knarcraft.stargate.portal.PortalSignDrawer; import net.md_5.bungee.api.ChatColor; import org.apache.commons.lang.StringUtils; +import org.bukkit.Material; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; @@ -16,6 +17,9 @@ import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; +import java.util.ArrayList; +import java.util.List; + /** * This command represents the config command for changing config values */ @@ -37,7 +41,11 @@ public class CommandConfig implements CommandExecutor { return false; } if (args.length > 1) { - updateConfigValue(selectedOption, commandSender, args[1]); + if (selectedOption.getDataType() == OptionDataType.STRING_LIST) { + updateListConfigValue(selectedOption, commandSender, args); + } else { + updateConfigValue(selectedOption, commandSender, args[1]); + } } else { //Display info and the current value of the given config value printConfigOptionValue(commandSender, selectedOption); @@ -72,14 +80,7 @@ public class CommandConfig implements CommandExecutor { //Store the config values, accounting for the data type switch (selectedOption.getDataType()) { - 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 BOOLEAN -> updateBooleanConfigValue(selectedOption, value, configuration); case INTEGER -> { Integer intValue = getInteger(commandSender, selectedOption, value); if (intValue == null) { @@ -90,35 +91,158 @@ public class CommandConfig implements CommandExecutor { } } case STRING -> { - if (selectedOption == ConfigOption.GATE_FOLDER || selectedOption == ConfigOption.PORTAL_FOLDER || - selectedOption == ConfigOption.DEFAULT_GATE_NETWORK) { - if (value.contains("../") || value.contains("..\\")) { - commandSender.sendMessage(ChatColor.RED + "Path traversal characters cannot be used"); - 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); + updateStringConfigValue(selectedOption, commandSender, value); configuration.set(selectedOption.getConfigNode(), value); } - case STRING_LIST -> { - if (selectedOption == ConfigOption.PER_SIGN_COLORS) { - commandSender.sendMessage(ChatColor.RED + value); - } - } default -> { Stargate.getStargateConfig().getConfigOptionsReference().put(selectedOption, value); configuration.set(selectedOption.getConfigNode(), value); } } + saveAndReload(selectedOption, commandSender); + } + + /** + * Updates a boolean config value + * + * @param selectedOption
The option which should be updated
+ * @param valueThe new value of the config option
+ * @param configurationThe configuration file to save to
+ */ + private void updateBooleanConfigValue(ConfigOption selectedOption, String value, FileConfiguration configuration) { + 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); + } + + /** + * Updates a string config value + * + * @param selectedOptionThe option which should be updated
+ * @param commandSenderThe command sender that changed the value
+ * @param valueThe new value of the config option
+ */ + private void updateStringConfigValue(ConfigOption selectedOption, CommandSender commandSender, String value) { + if (selectedOption == ConfigOption.GATE_FOLDER || selectedOption == ConfigOption.PORTAL_FOLDER || + selectedOption == ConfigOption.DEFAULT_GATE_NETWORK) { + if (value.contains("../") || value.contains("..\\")) { + commandSender.sendMessage(ChatColor.RED + "Path traversal characters cannot be used"); + 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); + } + + /** + * Updates a config value + * + * @param selectedOptionThe option which should be updated
+ * @param commandSenderThe command sender that changed the value
+ * @param argumentsThe arguments for the new config option
+ */ + private void updateListConfigValue(ConfigOption selectedOption, CommandSender commandSender, String[] arguments) { + FileConfiguration configuration = Stargate.getInstance().getConfig(); + + if (selectedOption == ConfigOption.PER_SIGN_COLORS) { + if (arguments.length < 4) { + Stargate.getMessageSender().sendErrorMessage(commandSender, "Usage: /sg config perSignColors " + + "The command sender that triggered the command
+ * @param argumentsThe arguments given by the user
+ * @returnThe per-sign color string to update with, or null if the input was invalid
+ */ + private String parsePerSignColorInput(CommandSender commandSender, String[] arguments) { + //Make sure the sign type is an actual sign + if (Material.matchMaterial(arguments[1] + "_SIGN") == null) { + Stargate.getMessageSender().sendErrorMessage(commandSender, "The given sign type is invalid"); + return null; + } + String colorString = arguments[1] + ":"; + + //Validate the colors given by the user + String[] errorMessage = new String[]{"The given main sign color is invalid!", "The given highlight sign color is invalid!"}; + String[] newColors = new String[2]; + for (int i = 0; i < 2; i++) { + if (validatePerSignColor(arguments[i + 2])) { + newColors[i] = arguments[i + 2]; + } else { + Stargate.getMessageSender().sendErrorMessage(commandSender, errorMessage[i]); + return null; + } + } + colorString += String.join(",", newColors); + return colorString; + } + + /** + * Updates the per-sign colors with the given input + * + * @param signTypeThe sign type that is updated
+ * @param colorStringThe new color string to replace any previous value with
+ * @param configurationThe file configuration to update with the new per-sign colors
+ */ + private void updatePerSignColors(String signType, String colorString, FileConfiguration configuration) { + ListThe color chosen by the user
+ * @returnTrue if the given color is valid
+ */ + private boolean validatePerSignColor(String color) { + ChatColor newHighlightColor = parseColor(color); + return newHighlightColor != null || color.equalsIgnoreCase("default") || + color.equalsIgnoreCase("inverted"); + } + + /** + * Saves the configuration file and reloads as necessary + * + * @param selectedOptionThe config option that was changed
+ * @param commandSenderThe command sender that executed the config command
+ */ + private void saveAndReload(ConfigOption selectedOption, CommandSender commandSender) { //Save the config file and reload if necessary Stargate.getInstance().saveConfig(); diff --git a/src/main/java/net/knarcraft/stargate/command/ConfigTabCompleter.java b/src/main/java/net/knarcraft/stargate/command/ConfigTabCompleter.java index a324d39..c37de81 100644 --- a/src/main/java/net/knarcraft/stargate/command/ConfigTabCompleter.java +++ b/src/main/java/net/knarcraft/stargate/command/ConfigTabCompleter.java @@ -3,6 +3,8 @@ package net.knarcraft.stargate.command; import net.knarcraft.stargate.config.ConfigOption; import net.knarcraft.stargate.config.OptionDataType; import net.md_5.bungee.api.ChatColor; +import org.bukkit.Material; +import org.bukkit.Tag; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.command.TabCompleter; @@ -17,15 +19,26 @@ import java.util.List; */ public class ConfigTabCompleter implements TabCompleter { + private ListSome or all of the valid values for the option
*/ private ListThe available languages
+ * @param selectedOptionThe selected option
+ * @param argsThe arguments given by the user
+ * @returnSome or all of the valid values for the option
*/ - private ListAll available colors
- */ - private ListA list of chat colors
+ * @param argsThe arguments given by the user
+ * @returnThe options to give the user
+ */ + private ListThe string to make into a list
+ * @returnA list containing the string value
+ */ + private ListThe available chat colors
*/ private ListThe string to make into a list
- * @returnA list containing the string value
+ * Initializes the list of all available languages */ - private ListTrue if changing the config option requires a "reload of colors" to take effect
*/ public static boolean requiresColorReload(ConfigOption configOption) { - return COLOR.isTagged(configOption) && configOption != ConfigOption.FREE_GATES_COLOR; + return (COLOR.isTagged(configOption) && configOption != ConfigOption.FREE_GATES_COLOR); } /**