From 878ad3aa67a8a13fccfdbc3594278b1b1c59a381 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Fri, 7 Oct 2022 16:05:36 +0200 Subject: [PATCH] Makes sure blacksmith messages include all arguments --- .../blacksmith/BlacksmithPlugin.java | 1 - .../command/BlackSmithConfigCommand.java | 90 ++++++++++++++----- .../command/BlackSmithEditCommand.java | 7 ++ 3 files changed, 74 insertions(+), 24 deletions(-) diff --git a/src/main/java/net/knarcraft/blacksmith/BlacksmithPlugin.java b/src/main/java/net/knarcraft/blacksmith/BlacksmithPlugin.java index bdc2926..542e068 100644 --- a/src/main/java/net/knarcraft/blacksmith/BlacksmithPlugin.java +++ b/src/main/java/net/knarcraft/blacksmith/BlacksmithPlugin.java @@ -92,7 +92,6 @@ public class BlacksmithPlugin extends JavaPlugin { // default value as the current value if the current value is null, as it's effectively the actual value. // Additionally, this would allow NPC settings to update when the default is changed, as long as no custom // value has been set. - //TODO: For messages, all arguments need to be combined (joined using spacebar) to make things work properly } /** diff --git a/src/main/java/net/knarcraft/blacksmith/command/BlackSmithConfigCommand.java b/src/main/java/net/knarcraft/blacksmith/command/BlackSmithConfigCommand.java index d844430..584149a 100644 --- a/src/main/java/net/knarcraft/blacksmith/command/BlackSmithConfigCommand.java +++ b/src/main/java/net/knarcraft/blacksmith/command/BlackSmithConfigCommand.java @@ -19,6 +19,8 @@ import org.bukkit.command.CommandSender; import org.bukkit.enchantments.Enchantment; import org.jetbrains.annotations.NotNull; +import java.util.Arrays; + import static net.knarcraft.blacksmith.formatting.StringFormatter.displayErrorMessage; import static net.knarcraft.blacksmith.formatting.StringFormatter.displaySuccessMessage; @@ -65,50 +67,92 @@ public class BlackSmithConfigCommand implements CommandExecutor { } } + //Display the current value of a setting if (args.length == 1) { - //TODO: See if there's a way to remove this duplication - String rawValue; - String correctCommandName; - boolean printRawValue = false; - if (detectedGlobalSetting != null) { - rawValue = String.valueOf(settings.getRawValue(detectedGlobalSetting)); - correctCommandName = detectedGlobalSetting.getCommandName(); - } else if (detectedNPCSetting != null) { - rawValue = String.valueOf(settings.getRawValue(detectedNPCSetting)); - correctCommandName = detectedNPCSetting.getCommandName(); - if (detectedNPCSetting.getPath().startsWith("defaults.messages")) { - printRawValue = true; - } - } else { - return false; - } - displaySuccessMessage(sender, TranslatableMessage.getCurrentValueMessage(correctCommandName, rawValue)); - if (printRawValue) { - sender.sendMessage(TranslatableMessage.getRawValueMessage( - rawValue.replace(ChatColor.COLOR_CHAR, '&'))); - } - return true; + return displayCurrentValue(detectedGlobalSetting, detectedNPCSetting, settings, sender); } else if (args.length == 2 && isSpecialCase(commandName)) { if (displaySpecialCaseValue(args[1], sender, detectedGlobalSetting, settings)) { return true; } } + //Update the value of a special-case setting if (args.length == 3 && updateSpecialCase(settings, detectedGlobalSetting, args, sender)) { return true; } + //Change the value of the specified setting + return changeValue(args, detectedGlobalSetting, detectedNPCSetting, settings, sender); + } + + /** + * Changes the value of the setting defined in the user's input + * + * @param args

The arguments given by the user

+ * @param detectedGlobalSetting

The global setting recognized from the input, if any

+ * @param detectedNPCSetting

The NPC setting recognized from the input, if any

+ * @param settings

The global settings object to get settings from

+ * @param sender

The command sender to display any output to

+ * @return

True if the value was successfully changed

+ */ + private boolean changeValue(String[] args, GlobalSetting detectedGlobalSetting, NPCSetting detectedNPCSetting, + GlobalSettings settings, CommandSender sender) { String newValue = args[1]; if (detectedGlobalSetting != null) { settings.changeValue(detectedGlobalSetting, newValue); displaySuccessMessage(sender, TranslatableMessage.getValueChangedMessage(detectedGlobalSetting.getCommandName(), newValue)); return true; } else if (detectedNPCSetting != null) { + //This makes sure all arguments are treated as a sentence + if (detectedNPCSetting.getValueType() == SettingValueType.STRING) { + newValue = String.join(" ", Arrays.asList(args).subList(1, args.length)); + } settings.changeValue(detectedNPCSetting, newValue); displaySuccessMessage(sender, TranslatableMessage.getValueChangedMessage(detectedNPCSetting.getNodeName(), newValue)); return true; + } else { + return false; } - return false; + } + + /** + * Displays the current value of the selected setting + * + * @param detectedGlobalSetting

The global setting recognized from the input, if any

+ * @param detectedNPCSetting

The NPC setting recognized from the input, if any

+ * @param settings

The global settings object to get settings from

+ * @param sender

The command sender to display any output to

+ * @return

True if a settings was successfully displayed

+ */ + private boolean displayCurrentValue(GlobalSetting detectedGlobalSetting, NPCSetting detectedNPCSetting, + GlobalSettings settings, CommandSender sender) { + String settingValue; + String correctCommandName; + boolean printRawValue = false; + + //Find the value of the specified setting + //TODO: See if there's a way to remove this duplication + if (detectedGlobalSetting != null) { + settingValue = String.valueOf(settings.getRawValue(detectedGlobalSetting)); + correctCommandName = detectedGlobalSetting.getCommandName(); + } else if (detectedNPCSetting != null) { + settingValue = String.valueOf(settings.getRawValue(detectedNPCSetting)); + correctCommandName = detectedNPCSetting.getCommandName(); + //For messages, print an additional raw value showing which color codes are used + if (detectedNPCSetting.getPath().startsWith("defaults.messages")) { + printRawValue = true; + } + } else { + return false; + } + //Display the current value of the setting + displaySuccessMessage(sender, TranslatableMessage.getCurrentValueMessage(correctCommandName, settingValue)); + //Print the value with any colors displayed as &a-f0-9 + if (printRawValue) { + sender.sendMessage(TranslatableMessage.getRawValueMessage( + settingValue.replace(ChatColor.COLOR_CHAR, '&'))); + } + return true; } /** diff --git a/src/main/java/net/knarcraft/blacksmith/command/BlackSmithEditCommand.java b/src/main/java/net/knarcraft/blacksmith/command/BlackSmithEditCommand.java index d8f5923..d48e2cb 100644 --- a/src/main/java/net/knarcraft/blacksmith/command/BlackSmithEditCommand.java +++ b/src/main/java/net/knarcraft/blacksmith/command/BlackSmithEditCommand.java @@ -3,6 +3,7 @@ package net.knarcraft.blacksmith.command; import net.citizensnpcs.api.CitizensAPI; import net.citizensnpcs.api.npc.NPC; import net.knarcraft.blacksmith.config.NPCSetting; +import net.knarcraft.blacksmith.config.SettingValueType; import net.knarcraft.blacksmith.formatting.StringFormatter; import net.knarcraft.blacksmith.formatting.TranslatableMessage; import net.knarcraft.blacksmith.formatting.Translator; @@ -14,6 +15,8 @@ import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.jetbrains.annotations.NotNull; +import java.util.Arrays; + import static net.knarcraft.blacksmith.formatting.StringFormatter.displaySuccessMessage; /** @@ -41,6 +44,10 @@ public class BlackSmithEditCommand implements CommandExecutor { String commandName = npcSetting.getCommandName(); if (commandName.equalsIgnoreCase(args[0])) { String newValue = args.length < 2 ? null : args[1]; + //This makes sure all arguments are treated as a sentence + if (npcSetting.getValueType() == SettingValueType.STRING && args.length > 2) { + newValue = String.join(" ", Arrays.asList(args).subList(1, args.length)); + } return changeNPCSetting(blacksmithTrait, npcSetting, newValue, sender); } }