From 8546fd4f78ac73892a10371d711a694c50053ff9 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Tue, 9 Nov 2021 20:57:06 +0100 Subject: [PATCH] Fully implements the config command --- .../stargate/command/CommandConfig.java | 104 +++++++++++++++++- 1 file changed, 99 insertions(+), 5 deletions(-) diff --git a/src/main/java/net/knarcraft/stargate/command/CommandConfig.java b/src/main/java/net/knarcraft/stargate/command/CommandConfig.java index cf4f1d8..57ba4f2 100644 --- a/src/main/java/net/knarcraft/stargate/command/CommandConfig.java +++ b/src/main/java/net/knarcraft/stargate/command/CommandConfig.java @@ -6,6 +6,7 @@ import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; +import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; @@ -24,15 +25,98 @@ public class CommandConfig implements CommandExecutor { } } - if (args.length > 1) { - //TODO: Do stuff + if (args.length > 0) { + ConfigOption selectedOption = ConfigOption.getByName(args[0]); + if (selectedOption == null) { + return false; + } + if (args.length > 1) { + updateConfigValue(selectedOption, commandSender, args[1]); + } else { + //Display info and the current value of the given config value + printConfigOptionValue(commandSender, selectedOption); + } + return true; } else { - //TODO: Display list of config values + //Display all config options displayConfigValues(commandSender); } return true; } + /** + * Updates a config value + * + * @param selectedOption

The option which should be updated

+ * @param commandSender

The command sender that changed the value

+ * @param value

The new value of the config option

+ */ + private void updateConfigValue(ConfigOption selectedOption, CommandSender commandSender, String value) { + FileConfiguration configuration = Stargate.getInstance().getConfig(); + + //Validate any sign colors + if (selectedOption == ConfigOption.MAIN_SIGN_COLOR || selectedOption == ConfigOption.HIGHLIGHT_SIGN_COLOR) { + try { + ChatColor.valueOf(value.toUpperCase()); + } catch (IllegalArgumentException | NullPointerException ignored) { + commandSender.sendMessage(ChatColor.RED + "Invalid color given"); + return; + } + } + + //Store the config values, accounting for the data type + switch (selectedOption.getDataType()) { + case BOOLEAN -> configuration.set(selectedOption.getConfigNode(), Boolean.parseBoolean(value)); + case INTEGER -> { + try { + configuration.set(selectedOption.getConfigNode(), Integer.parseInt(value)); + } catch (NumberFormatException exception) { + commandSender.sendMessage(ChatColor.RED + "Invalid number given"); + return; + } + } + 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; + } + } + configuration.set(selectedOption.getConfigNode(), value); + } + default -> configuration.set(selectedOption.getConfigNode(), value); + } + + //Save the config file and reload if necessary + Stargate.getInstance().saveConfig(); + reloadIfNecessary(commandSender); + } + + /** + * Reloads the config if necessary + * + * @param commandSender

The command sender initiating the reload

+ */ + 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); + } + + /** + * Prints information about a config option and its current value + * + * @param sender

The command sender that sent the command

+ * @param option

The config option to print information about

+ */ + private void printConfigOptionValue(CommandSender sender, ConfigOption option) { + Object value = Stargate.getStargateConfig().getConfigOptions().get(option); + sender.sendMessage(getOptionDescription(option)); + sender.sendMessage(ChatColor.GREEN + "Current value: " + ChatColor.GOLD + value); + } + /** * Displays the name and a small description of every config value * @@ -42,9 +126,19 @@ public class CommandConfig implements CommandExecutor { sender.sendMessage(ChatColor.GREEN + Stargate.getBackupString("prefix") + ChatColor.GOLD + "Config values:"); for (ConfigOption option : ConfigOption.values()) { - sender.sendMessage(ChatColor.GOLD + option.getName() + ChatColor.WHITE + " - " + ChatColor.GREEN + - option.getDescription() + " (" + option.getDefaultValue() + ")"); + sender.sendMessage(getOptionDescription(option)); } } + /** + * Gets the description of a single config option + * + * @param option

The option to describe

+ * @return

A string describing the config option

+ */ + private String getOptionDescription(ConfigOption option) { + return ChatColor.GOLD + option.getName() + ChatColor.WHITE + " - " + ChatColor.GREEN + option.getDescription() + + ChatColor.DARK_GRAY + " (Default: " + ChatColor.GRAY + option.getDefaultValue() + ChatColor.DARK_GRAY + ")"; + } + }