Makes edit commands display the default value if no value is provided

This commit is contained in:
Kristian Knarvik 2024-05-07 13:54:45 +02:00
parent cb477cafb5
commit 75744ccdd0
4 changed files with 71 additions and 28 deletions

View File

@ -8,6 +8,7 @@ import net.knarcraft.blacksmith.config.SettingValueType;
import net.knarcraft.blacksmith.config.Settings; import net.knarcraft.blacksmith.config.Settings;
import net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage; import net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage;
import net.knarcraft.blacksmith.trait.CustomTrait; import net.knarcraft.blacksmith.trait.CustomTrait;
import net.knarcraft.blacksmith.util.ConfigCommandHelper;
import net.knarcraft.blacksmith.util.InputParsingHelper; import net.knarcraft.blacksmith.util.InputParsingHelper;
import net.knarcraft.blacksmith.util.TypeValidationHelper; import net.knarcraft.blacksmith.util.TypeValidationHelper;
import net.knarcraft.knarlib.formatting.StringFormatter; import net.knarcraft.knarlib.formatting.StringFormatter;
@ -22,6 +23,7 @@ import java.util.Arrays;
import java.util.logging.Level; import java.util.logging.Level;
import static net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage.getCurrentValueMessage; import static net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage.getCurrentValueMessage;
import static net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage.getDefaultValueMessage;
import static net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage.getValueChangedMessage; import static net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage.getValueChangedMessage;
/** /**
@ -159,25 +161,33 @@ public abstract class EditCommand<K extends CustomTrait<L>, L extends Setting> i
StringFormatter formatter = BlacksmithPlugin.getStringFormatter(); 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 // Display the description for how this setting is used
formatter.displaySuccessMessage(sender, setting.getDescription()); formatter.displaySuccessMessage(sender, setting.getDescription());
String rawValue = String.valueOf(settings.getRawValue(setting)); // Display the default value
if (InputParsingHelper.isEmpty(rawValue)) { formatter.displayNeutralMessage(sender, getDefaultValueMessage(commandName, defaultValue));
//Display the default value, if no custom value has been specified if (isMessage) {
rawValue = String.valueOf(globalSettings.getRawValue(setting)); ConfigCommandHelper.displayRaw(sender, defaultValue);
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);
} }
if (setting.isMessage()) {
sender.sendMessage(BlacksmithTranslatableMessage.getRawValueMessage( if (!isSet) {
rawValue.replace(ChatColor.COLOR_CHAR, '&'))); //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);
} }
} }

View File

@ -30,6 +30,11 @@ public enum BlacksmithTranslatableMessage implements TranslatableMessage {
*/ */
CURRENT_VALUE, 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 * 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(); return stringReplacer.replace();
} }
/**
* Gets the message to display when displaying a setting's default value
*
* @param setting <p>The setting whose value is shown</p>
* @param defaultValue <p>The default value of the setting</p>
* @return <p>The string to display to a user</p>
*/
@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 * Gets the message to display when displaying a setting's current value
* *

View File

@ -12,6 +12,8 @@ import org.jetbrains.annotations.NotNull;
import java.util.Arrays; 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; import static net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage.getValueChangedMessage;
/** /**
@ -55,33 +57,43 @@ public final class ConfigCommandHelper {
public static <K extends Setting> void displayCurrentValue(@NotNull K setting, public static <K extends Setting> void displayCurrentValue(@NotNull K setting,
@NotNull Settings<K> settings, @NotNull Settings<K> settings,
@NotNull CommandSender sender) { @NotNull CommandSender sender) {
String settingValue; StringFormatter formatter = BlacksmithPlugin.getStringFormatter();
String correctCommandName;
boolean printRawValue = false; boolean printRawValue = false;
//Find the value of the specified setting //Find the value of the specified setting
settingValue = String.valueOf(settings.getRawValue(setting)); String currentValue = String.valueOf(settings.getRawValue(setting));
correctCommandName = setting.getCommandName(); String defaultValue = String.valueOf(setting.getDefaultValue());
//For messages, print an additional raw value showing which color codes are used //For messages, print an additional raw value showing which color codes are used
if (setting.isPerNPC() && setting.isMessage()) { if (setting.isPerNPC() && setting.isMessage()) {
printRawValue = true; printRawValue = true;
} }
StringFormatter formatter = BlacksmithPlugin.getStringFormatter();
// Display the description of the setting // Display the description of the setting
formatter.displaySuccessMessage(sender, setting.getDescription()); formatter.displaySuccessMessage(sender, setting.getDescription());
// Display the current value of the setting // Display the setting's default value
formatter.displayNeutralMessage(sender, BlacksmithTranslatableMessage.getCurrentValueMessage( formatter.displayNeutralMessage(sender, getDefaultValueMessage(setting.getCommandName(), defaultValue));
correctCommandName, settingValue));
// Print the value with any colors displayed as &a-f0-9
if (printRawValue) { if (printRawValue) {
sender.sendMessage(BlacksmithTranslatableMessage.getRawValueMessage( displayRaw(sender, defaultValue);
settingValue.replace(ChatColor.COLOR_CHAR, '&'))); }
// 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 <p>The sender to display the raw value to</p>
* @param value <p>The value to display raw</p>
*/
public static void displayRaw(@NotNull CommandSender sender, @NotNull String value) {
sender.sendMessage(BlacksmithTranslatableMessage.getRawValueMessage(
value.replace(ChatColor.COLOR_CHAR, '&')));
}
} }

View File

@ -8,6 +8,8 @@ en:
VALUE_FOR_ITEM_CHANGED: "&7{setting} for {itemType} {item} set to &6{newValue}" VALUE_FOR_ITEM_CHANGED: "&7{setting} for {itemType} {item} set to &6{newValue}"
# The format used to display the current value of a setting # The format used to display the current value of a setting
CURRENT_VALUE: "&7Current value of {setting}:&r {currentValue}" 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 # 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}" 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) # Translation of the enchantment item type (used for VALUE_FOR_ITEM_CHANGED, CURRENT_VALUE_FOR_ITEM)