Files
Blacksmith/src/main/java/net/knarcraft/blacksmith/util/ConfigCommandHelper.java

100 lines
4.3 KiB
Java

package net.knarcraft.blacksmith.util;
import net.knarcraft.blacksmith.BlacksmithPlugin;
import net.knarcraft.blacksmith.config.Setting;
import net.knarcraft.blacksmith.config.SettingValueType;
import net.knarcraft.blacksmith.config.Settings;
import net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage;
import net.knarcraft.knarlib.formatting.StringFormatter;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.command.CommandSender;
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;
/**
* A helper class for the configuration command
*/
public final class ConfigCommandHelper {
private ConfigCommandHelper() {
}
/**
* Changes the value of the setting defined in the user's input
*
* @param args <p>The arguments given by the user</p>
* @param detectedSetting <p>The setting recognized from the input, if any</p>
* @param settings <p>The global settings object to get settings from</p>
* @param sender <p>The command sender to display any output to</p>
*/
public static <K extends Setting> void changeValue(@NotNull String[] args,
@NotNull K detectedSetting,
@NotNull Settings<K> settings,
@NotNull CommandSender sender) {
String newValue = args[1];
//This makes sure all arguments are treated as a sentence
if (detectedSetting.getValueType() == SettingValueType.STRING) {
newValue = String.join(" ", Arrays.asList(args).subList(1, args.length));
}
settings.changeValue(detectedSetting, newValue);
BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender,
getValueChangedMessage(detectedSetting.getCommandName(), newValue));
}
/**
* Displays the current value of the selected setting
*
* @param setting <p>The global setting recognized from the input</p>
* @param settings <p>The global settings object to get settings from</p>
* @param sender <p>The command sender to display any output to</p>
*/
public static <K extends Setting> void displayCurrentValue(@NotNull K setting,
@NotNull Settings<K> settings,
@NotNull CommandSender sender) {
StringFormatter formatter = BlacksmithPlugin.getStringFormatter();
boolean printRawValue = false;
//Find the value of the specified setting
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;
}
// Display the description of the setting
formatter.displaySuccessMessage(sender, setting.getDescription());
// Display the setting's default value
formatter.displayNeutralMessage(sender, getDefaultValueMessage(setting.getCommandName(), defaultValue));
if (printRawValue) {
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, '&')));
}
}