Makes sure blacksmith messages include all arguments

This commit is contained in:
Kristian Knarvik 2022-10-07 16:05:36 +02:00
parent 7d682c93b4
commit 878ad3aa67
3 changed files with 74 additions and 24 deletions

View File

@ -92,7 +92,6 @@ public class BlacksmithPlugin extends JavaPlugin {
// default value as the current value if the current value is null, as it's effectively the actual value. // default value as the current value if the current value is null, as it's effectively the actual value.
// Additionally, this would allow NPC settings to update when the default is changed, as long as no custom // Additionally, this would allow NPC settings to update when the default is changed, as long as no custom
// value has been set. // value has been set.
//TODO: For messages, all arguments need to be combined (joined using spacebar) to make things work properly
} }
/** /**

View File

@ -19,6 +19,8 @@ import org.bukkit.command.CommandSender;
import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.Enchantment;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.Arrays;
import static net.knarcraft.blacksmith.formatting.StringFormatter.displayErrorMessage; import static net.knarcraft.blacksmith.formatting.StringFormatter.displayErrorMessage;
import static net.knarcraft.blacksmith.formatting.StringFormatter.displaySuccessMessage; import static net.knarcraft.blacksmith.formatting.StringFormatter.displaySuccessMessage;
@ -65,50 +67,92 @@ public class BlackSmithConfigCommand implements CommandExecutor {
} }
} }
//Display the current value of a setting
if (args.length == 1) { if (args.length == 1) {
//TODO: See if there's a way to remove this duplication return displayCurrentValue(detectedGlobalSetting, detectedNPCSetting, settings, sender);
String rawValue;
String correctCommandName;
boolean printRawValue = false;
if (detectedGlobalSetting != null) {
rawValue = String.valueOf(settings.getRawValue(detectedGlobalSetting));
correctCommandName = detectedGlobalSetting.getCommandName();
} else if (detectedNPCSetting != null) {
rawValue = String.valueOf(settings.getRawValue(detectedNPCSetting));
correctCommandName = detectedNPCSetting.getCommandName();
if (detectedNPCSetting.getPath().startsWith("defaults.messages")) {
printRawValue = true;
}
} else {
return false;
}
displaySuccessMessage(sender, TranslatableMessage.getCurrentValueMessage(correctCommandName, rawValue));
if (printRawValue) {
sender.sendMessage(TranslatableMessage.getRawValueMessage(
rawValue.replace(ChatColor.COLOR_CHAR, '&')));
}
return true;
} else if (args.length == 2 && isSpecialCase(commandName)) { } else if (args.length == 2 && isSpecialCase(commandName)) {
if (displaySpecialCaseValue(args[1], sender, detectedGlobalSetting, settings)) { if (displaySpecialCaseValue(args[1], sender, detectedGlobalSetting, settings)) {
return true; return true;
} }
} }
//Update the value of a special-case setting
if (args.length == 3 && updateSpecialCase(settings, detectedGlobalSetting, args, sender)) { if (args.length == 3 && updateSpecialCase(settings, detectedGlobalSetting, args, sender)) {
return true; return true;
} }
//Change the value of the specified setting
return changeValue(args, detectedGlobalSetting, detectedNPCSetting, settings, sender);
}
/**
* Changes the value of the setting defined in the user's input
*
* @param args <p>The arguments given by the user</p>
* @param detectedGlobalSetting <p>The global setting recognized from the input, if any</p>
* @param detectedNPCSetting <p>The NPC 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>
* @return <p>True if the value was successfully changed</p>
*/
private boolean changeValue(String[] args, GlobalSetting detectedGlobalSetting, NPCSetting detectedNPCSetting,
GlobalSettings settings, CommandSender sender) {
String newValue = args[1]; String newValue = args[1];
if (detectedGlobalSetting != null) { if (detectedGlobalSetting != null) {
settings.changeValue(detectedGlobalSetting, newValue); settings.changeValue(detectedGlobalSetting, newValue);
displaySuccessMessage(sender, TranslatableMessage.getValueChangedMessage(detectedGlobalSetting.getCommandName(), newValue)); displaySuccessMessage(sender, TranslatableMessage.getValueChangedMessage(detectedGlobalSetting.getCommandName(), newValue));
return true; return true;
} else if (detectedNPCSetting != null) { } else if (detectedNPCSetting != null) {
//This makes sure all arguments are treated as a sentence
if (detectedNPCSetting.getValueType() == SettingValueType.STRING) {
newValue = String.join(" ", Arrays.asList(args).subList(1, args.length));
}
settings.changeValue(detectedNPCSetting, newValue); settings.changeValue(detectedNPCSetting, newValue);
displaySuccessMessage(sender, TranslatableMessage.getValueChangedMessage(detectedNPCSetting.getNodeName(), newValue)); displaySuccessMessage(sender, TranslatableMessage.getValueChangedMessage(detectedNPCSetting.getNodeName(), newValue));
return true; return true;
} else {
return false;
} }
return false; }
/**
* Displays the current value of the selected setting
*
* @param detectedGlobalSetting <p>The global setting recognized from the input, if any</p>
* @param detectedNPCSetting <p>The NPC 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>
* @return <p>True if a settings was successfully displayed</p>
*/
private boolean displayCurrentValue(GlobalSetting detectedGlobalSetting, NPCSetting detectedNPCSetting,
GlobalSettings settings, CommandSender sender) {
String settingValue;
String correctCommandName;
boolean printRawValue = false;
//Find the value of the specified setting
//TODO: See if there's a way to remove this duplication
if (detectedGlobalSetting != null) {
settingValue = String.valueOf(settings.getRawValue(detectedGlobalSetting));
correctCommandName = detectedGlobalSetting.getCommandName();
} else if (detectedNPCSetting != null) {
settingValue = String.valueOf(settings.getRawValue(detectedNPCSetting));
correctCommandName = detectedNPCSetting.getCommandName();
//For messages, print an additional raw value showing which color codes are used
if (detectedNPCSetting.getPath().startsWith("defaults.messages")) {
printRawValue = true;
}
} else {
return false;
}
//Display the current value of the setting
displaySuccessMessage(sender, TranslatableMessage.getCurrentValueMessage(correctCommandName, settingValue));
//Print the value with any colors displayed as &a-f0-9
if (printRawValue) {
sender.sendMessage(TranslatableMessage.getRawValueMessage(
settingValue.replace(ChatColor.COLOR_CHAR, '&')));
}
return true;
} }
/** /**

View File

@ -3,6 +3,7 @@ package net.knarcraft.blacksmith.command;
import net.citizensnpcs.api.CitizensAPI; import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.api.npc.NPC;
import net.knarcraft.blacksmith.config.NPCSetting; import net.knarcraft.blacksmith.config.NPCSetting;
import net.knarcraft.blacksmith.config.SettingValueType;
import net.knarcraft.blacksmith.formatting.StringFormatter; import net.knarcraft.blacksmith.formatting.StringFormatter;
import net.knarcraft.blacksmith.formatting.TranslatableMessage; import net.knarcraft.blacksmith.formatting.TranslatableMessage;
import net.knarcraft.blacksmith.formatting.Translator; import net.knarcraft.blacksmith.formatting.Translator;
@ -14,6 +15,8 @@ import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.Arrays;
import static net.knarcraft.blacksmith.formatting.StringFormatter.displaySuccessMessage; import static net.knarcraft.blacksmith.formatting.StringFormatter.displaySuccessMessage;
/** /**
@ -41,6 +44,10 @@ public class BlackSmithEditCommand implements CommandExecutor {
String commandName = npcSetting.getCommandName(); String commandName = npcSetting.getCommandName();
if (commandName.equalsIgnoreCase(args[0])) { if (commandName.equalsIgnoreCase(args[0])) {
String newValue = args.length < 2 ? null : args[1]; String newValue = args.length < 2 ? null : args[1];
//This makes sure all arguments are treated as a sentence
if (npcSetting.getValueType() == SettingValueType.STRING && args.length > 2) {
newValue = String.join(" ", Arrays.asList(args).subList(1, args.length));
}
return changeNPCSetting(blacksmithTrait, npcSetting, newValue, sender); return changeNPCSetting(blacksmithTrait, npcSetting, newValue, sender);
} }
} }