From 75744ccdd071a1771fb388ff86cf79f13fd14ca9 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Tue, 7 May 2024 13:54:45 +0200 Subject: [PATCH] Makes edit commands display the default value if no value is provided --- .../blacksmith/command/EditCommand.java | 40 ++++++++++++------- .../BlacksmithTranslatableMessage.java | 19 +++++++++ .../blacksmith/util/ConfigCommandHelper.java | 38 ++++++++++++------ src/main/resources/strings.yml | 2 + 4 files changed, 71 insertions(+), 28 deletions(-) diff --git a/src/main/java/net/knarcraft/blacksmith/command/EditCommand.java b/src/main/java/net/knarcraft/blacksmith/command/EditCommand.java index a88f350..9ad543b 100644 --- a/src/main/java/net/knarcraft/blacksmith/command/EditCommand.java +++ b/src/main/java/net/knarcraft/blacksmith/command/EditCommand.java @@ -8,6 +8,7 @@ import net.knarcraft.blacksmith.config.SettingValueType; import net.knarcraft.blacksmith.config.Settings; import net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage; import net.knarcraft.blacksmith.trait.CustomTrait; +import net.knarcraft.blacksmith.util.ConfigCommandHelper; import net.knarcraft.blacksmith.util.InputParsingHelper; import net.knarcraft.blacksmith.util.TypeValidationHelper; import net.knarcraft.knarlib.formatting.StringFormatter; @@ -22,6 +23,7 @@ import java.util.Arrays; import java.util.logging.Level; import static net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage.getCurrentValueMessage; +import static net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage.getDefaultValueMessage; import static net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage.getValueChangedMessage; /** @@ -159,25 +161,33 @@ public abstract class EditCommand, L extends Setting> i StringFormatter formatter = BlacksmithPlugin.getStringFormatter(); + //Find the value of the specified setting + String commandName = setting.getCommandName(); + String currentValue = String.valueOf(settings.getRawValue(setting)); + String defaultValue = String.valueOf(setting.getDefaultValue()); + String marker = formatter.getUnFormattedMessage(BlacksmithTranslatableMessage.SETTING_OVERRIDDEN_MARKER); + boolean isMessage = setting.isMessage(); + boolean isSet = !InputParsingHelper.isEmpty(currentValue); + // Display the description for how this setting is used formatter.displaySuccessMessage(sender, setting.getDescription()); - String rawValue = String.valueOf(settings.getRawValue(setting)); - if (InputParsingHelper.isEmpty(rawValue)) { - //Display the default value, if no custom value has been specified - rawValue = String.valueOf(globalSettings.getRawValue(setting)); - formatter.displayNeutralMessage(sender, - getCurrentValueMessage(setting.getCommandName(), rawValue)); - } else { - //Add a marker if the value has been customized - String marker = BlacksmithPlugin.getStringFormatter().getUnFormattedMessage( - BlacksmithTranslatableMessage.SETTING_OVERRIDDEN_MARKER); - formatter.displayNeutralMessage(sender, - getCurrentValueMessage(setting.getCommandName(), rawValue) + marker); + // Display the default value + formatter.displayNeutralMessage(sender, getDefaultValueMessage(commandName, defaultValue)); + if (isMessage) { + ConfigCommandHelper.displayRaw(sender, defaultValue); } - if (setting.isMessage()) { - sender.sendMessage(BlacksmithTranslatableMessage.getRawValueMessage( - rawValue.replace(ChatColor.COLOR_CHAR, '&'))); + + if (!isSet) { + //Display the global value, if no custom value has been specified + currentValue = String.valueOf(globalSettings.getRawValue(setting)); + } + + // Display the value with a marker if it's customized + formatter.displayNeutralMessage(sender, getCurrentValueMessage(commandName, currentValue) + (isSet ? marker : "")); + + if (isMessage) { + ConfigCommandHelper.displayRaw(sender, currentValue); } } diff --git a/src/main/java/net/knarcraft/blacksmith/formatting/BlacksmithTranslatableMessage.java b/src/main/java/net/knarcraft/blacksmith/formatting/BlacksmithTranslatableMessage.java index eb41541..dfd2844 100644 --- a/src/main/java/net/knarcraft/blacksmith/formatting/BlacksmithTranslatableMessage.java +++ b/src/main/java/net/knarcraft/blacksmith/formatting/BlacksmithTranslatableMessage.java @@ -30,6 +30,11 @@ public enum BlacksmithTranslatableMessage implements TranslatableMessage { */ CURRENT_VALUE, + /** + * The message displayed when showing the default value of a configuration option + */ + DEFAULT_VALUE, + /** * The message displayed when showing the current value of a configuration option for one material or enchantment */ @@ -186,6 +191,20 @@ public enum BlacksmithTranslatableMessage implements TranslatableMessage { return stringReplacer.replace(); } + /** + * Gets the message to display when displaying a setting's default value + * + * @param setting

The setting whose value is shown

+ * @param defaultValue

The default value of the setting

+ * @return

The string to display to a user

+ */ + @NotNull + public static String getDefaultValueMessage(@NotNull String setting, @NotNull String defaultValue) { + return BlacksmithPlugin.getStringFormatter().replacePlaceholders(BlacksmithTranslatableMessage.DEFAULT_VALUE, + List.of("{setting}", "{defaultValue}"), + List.of(setting, defaultValue)); + } + /** * Gets the message to display when displaying a setting's current value * diff --git a/src/main/java/net/knarcraft/blacksmith/util/ConfigCommandHelper.java b/src/main/java/net/knarcraft/blacksmith/util/ConfigCommandHelper.java index 3b8e6b2..9814270 100644 --- a/src/main/java/net/knarcraft/blacksmith/util/ConfigCommandHelper.java +++ b/src/main/java/net/knarcraft/blacksmith/util/ConfigCommandHelper.java @@ -12,6 +12,8 @@ import org.jetbrains.annotations.NotNull; import java.util.Arrays; +import static net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage.getCurrentValueMessage; +import static net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage.getDefaultValueMessage; import static net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage.getValueChangedMessage; /** @@ -55,33 +57,43 @@ public final class ConfigCommandHelper { public static void displayCurrentValue(@NotNull K setting, @NotNull Settings settings, @NotNull CommandSender sender) { - String settingValue; - String correctCommandName; + StringFormatter formatter = BlacksmithPlugin.getStringFormatter(); boolean printRawValue = false; //Find the value of the specified setting - settingValue = String.valueOf(settings.getRawValue(setting)); - correctCommandName = setting.getCommandName(); + String currentValue = String.valueOf(settings.getRawValue(setting)); + String defaultValue = String.valueOf(setting.getDefaultValue()); //For messages, print an additional raw value showing which color codes are used if (setting.isPerNPC() && setting.isMessage()) { printRawValue = true; } - StringFormatter formatter = BlacksmithPlugin.getStringFormatter(); - // Display the description of the setting formatter.displaySuccessMessage(sender, setting.getDescription()); - // Display the current value of the setting - formatter.displayNeutralMessage(sender, BlacksmithTranslatableMessage.getCurrentValueMessage( - correctCommandName, settingValue)); - - // Print the value with any colors displayed as &a-f0-9 + // Display the setting's default value + formatter.displayNeutralMessage(sender, getDefaultValueMessage(setting.getCommandName(), defaultValue)); if (printRawValue) { - sender.sendMessage(BlacksmithTranslatableMessage.getRawValueMessage( - settingValue.replace(ChatColor.COLOR_CHAR, '&'))); + displayRaw(sender, defaultValue); + } + + // Display the current value of the setting + formatter.displayNeutralMessage(sender, getCurrentValueMessage(setting.getCommandName(), currentValue)); + if (printRawValue) { + displayRaw(sender, currentValue); } } + /** + * Displays a message with formatting codes shown + * + * @param sender

The sender to display the raw value to

+ * @param value

The value to display raw

+ */ + public static void displayRaw(@NotNull CommandSender sender, @NotNull String value) { + sender.sendMessage(BlacksmithTranslatableMessage.getRawValueMessage( + value.replace(ChatColor.COLOR_CHAR, '&'))); + } + } diff --git a/src/main/resources/strings.yml b/src/main/resources/strings.yml index 7f021e7..3fc0c1b 100644 --- a/src/main/resources/strings.yml +++ b/src/main/resources/strings.yml @@ -8,6 +8,8 @@ en: VALUE_FOR_ITEM_CHANGED: "&7{setting} for {itemType} {item} set to &6{newValue}" # The format used to display the current value of a setting CURRENT_VALUE: "&7Current value of {setting}:&r {currentValue}" + # The format used to display the default value of a setting + DEFAULT_VALUE: "&7Default value of {setting}:&r {defaultValue}" # The format used to display the current value of a setting for a specific material or enchantment CURRENT_VALUE_FOR_ITEM: "&7Current value of {setting} for {itemType} {item}:&r {currentValue}" # Translation of the enchantment item type (used for VALUE_FOR_ITEM_CHANGED, CURRENT_VALUE_FOR_ITEM)