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.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<K extends CustomTrait<L>, 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);
}
}

View File

@ -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 <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
*

View File

@ -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 <K extends Setting> void displayCurrentValue(@NotNull K setting,
@NotNull Settings<K> 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 <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}"
# 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)