Implements the scrapper global edit command
Changes setting quite a bit to avoid code duplication
This commit is contained in:
@ -0,0 +1,81 @@
|
||||
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.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.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) {
|
||||
String settingValue;
|
||||
String correctCommandName;
|
||||
boolean printRawValue = false;
|
||||
|
||||
//Find the value of the specified setting
|
||||
settingValue = String.valueOf(settings.getRawValue(setting));
|
||||
correctCommandName = setting.getCommandName();
|
||||
|
||||
//For messages, print an additional raw value showing which color codes are used
|
||||
if (setting.isPerNPC() && setting.isMessage()) {
|
||||
printRawValue = true;
|
||||
}
|
||||
|
||||
//Display the current value of the setting
|
||||
BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender,
|
||||
BlacksmithTranslatableMessage.getCurrentValueMessage(correctCommandName, settingValue));
|
||||
|
||||
//Print the value with any colors displayed as &a-f0-9
|
||||
if (printRawValue) {
|
||||
sender.sendMessage(BlacksmithTranslatableMessage.getRawValueMessage(
|
||||
settingValue.replace(ChatColor.COLOR_CHAR, '&')));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user