Improves un-setting of NPC values #10
This commit is contained in:
@ -7,6 +7,7 @@ import net.knarcraft.blacksmith.config.NPCSetting;
|
||||
import net.knarcraft.blacksmith.config.SettingValueType;
|
||||
import net.knarcraft.blacksmith.formatting.StringFormatter;
|
||||
import net.knarcraft.blacksmith.formatting.TranslatableMessage;
|
||||
import net.knarcraft.blacksmith.formatting.Translator;
|
||||
import net.knarcraft.blacksmith.trait.BlacksmithTrait;
|
||||
import net.knarcraft.blacksmith.util.TypeValidationHelper;
|
||||
import net.md_5.bungee.api.ChatColor;
|
||||
@ -18,6 +19,8 @@ import org.jetbrains.annotations.NotNull;
|
||||
import java.util.Arrays;
|
||||
|
||||
import static net.knarcraft.blacksmith.formatting.StringFormatter.displaySuccessMessage;
|
||||
import static net.knarcraft.blacksmith.formatting.TranslatableMessage.getCurrentValueMessage;
|
||||
import static net.knarcraft.blacksmith.formatting.TranslatableMessage.getValueChangedMessage;
|
||||
|
||||
/**
|
||||
* The main command used for blacksmith editing
|
||||
@ -66,31 +69,49 @@ public class BlackSmithEditCommand implements CommandExecutor {
|
||||
CommandSender sender) {
|
||||
if (newValue == null) {
|
||||
//Display the current value of the setting
|
||||
String rawValue = String.valueOf(blacksmithTrait.getSettings().getRawValue(npcSetting));
|
||||
//Displays the default value, if no custom value has been specified
|
||||
if (rawValue == null || rawValue.equalsIgnoreCase("null")) {
|
||||
rawValue = String.valueOf(BlacksmithPlugin.getInstance().getSettings().getRawValue(npcSetting));
|
||||
}
|
||||
displaySuccessMessage(sender, TranslatableMessage.getCurrentValueMessage(npcSetting.getCommandName(), rawValue));
|
||||
if (npcSetting.getPath().startsWith("defaults.messages")) {
|
||||
sender.sendMessage(TranslatableMessage.getRawValueMessage(
|
||||
rawValue.replace(ChatColor.COLOR_CHAR, '&')));
|
||||
}
|
||||
return true;
|
||||
displayNPCSetting(blacksmithTrait, npcSetting, sender);
|
||||
} else {
|
||||
boolean isValidType = TypeValidationHelper.isValid(npcSetting.getValueType(), newValue, sender);
|
||||
if (isValidType) {
|
||||
//Change the setting
|
||||
Object nullCheckedValue = newValue.equalsIgnoreCase("null") ? null :
|
||||
ChatColor.translateAlternateColorCodes('&', newValue);
|
||||
blacksmithTrait.getSettings().changeSetting(npcSetting, nullCheckedValue);
|
||||
displaySuccessMessage(sender, TranslatableMessage.getValueChangedMessage(npcSetting.getNodeName(), newValue));
|
||||
//Save the changes immediately to prevent data loss on server crash
|
||||
CitizensAPI.getNPCRegistry().saveToStore();
|
||||
return true;
|
||||
//If an empty value or null, clear the value instead of changing it
|
||||
if (newValue.equalsIgnoreCase("null") || newValue.equals("\"\"") || newValue.trim().isEmpty()) {
|
||||
newValue = null;
|
||||
} else {
|
||||
return false;
|
||||
//Abort if an invalid value is given
|
||||
boolean isValidType = TypeValidationHelper.isValid(npcSetting.getValueType(), newValue, sender);
|
||||
if (!isValidType) {
|
||||
return false;
|
||||
}
|
||||
newValue = ChatColor.translateAlternateColorCodes('&', newValue);
|
||||
}
|
||||
|
||||
//Change the setting
|
||||
blacksmithTrait.getSettings().changeSetting(npcSetting, newValue);
|
||||
displaySuccessMessage(sender, getValueChangedMessage(npcSetting.getNodeName(), String.valueOf(newValue)));
|
||||
//Save the changes immediately to prevent data loss on server crash
|
||||
CitizensAPI.getNPCRegistry().saveToStore();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays the current value of the given NPC setting
|
||||
*
|
||||
* @param blacksmithTrait <p>The blacksmith trait of the NPC to get the value from</p>
|
||||
* @param npcSetting <p>The NPC setting to see the value of</p>
|
||||
* @param sender <p>The command sender to display the value to</p>
|
||||
*/
|
||||
private void displayNPCSetting(BlacksmithTrait blacksmithTrait, NPCSetting npcSetting, CommandSender sender) {
|
||||
String rawValue = String.valueOf(blacksmithTrait.getSettings().getRawValue(npcSetting));
|
||||
if (rawValue == null || rawValue.trim().isEmpty() || rawValue.equalsIgnoreCase("null")) {
|
||||
//Display the default value, if no custom value has been specified
|
||||
rawValue = String.valueOf(BlacksmithPlugin.getInstance().getSettings().getRawValue(npcSetting));
|
||||
displaySuccessMessage(sender, getCurrentValueMessage(npcSetting.getCommandName(), rawValue));
|
||||
} else {
|
||||
//Add a marker if the value has been customized
|
||||
String marker = Translator.getTranslatedMessage(TranslatableMessage.SETTING_OVERRIDDEN_MARKER);
|
||||
displaySuccessMessage(sender, getCurrentValueMessage(npcSetting.getCommandName(), rawValue) + marker);
|
||||
}
|
||||
if (npcSetting.getPath().startsWith("defaults.messages")) {
|
||||
sender.sendMessage(TranslatableMessage.getRawValueMessage(rawValue.replace(ChatColor.COLOR_CHAR, '&')));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -361,7 +361,7 @@ public class NPCSettings {
|
||||
newReforgeAbleItems = (String) replaceReforgeAblePresets(newReforgeAbleItems);
|
||||
|
||||
for (String item : newReforgeAbleItems.split(",")) {
|
||||
if (item == null || item.equalsIgnoreCase("null")) {
|
||||
if (item == null || item.trim().isEmpty() || item.equalsIgnoreCase("null")) {
|
||||
continue;
|
||||
}
|
||||
Material material = Material.matchMaterial(item.replace('-', '_'));
|
||||
|
@ -158,7 +158,12 @@ public enum TranslatableMessage {
|
||||
/**
|
||||
* The text to display when describing more than 5 minutes remaining
|
||||
*/
|
||||
INTERVAL_MORE_THAN_5_MINUTES;
|
||||
INTERVAL_MORE_THAN_5_MINUTES,
|
||||
|
||||
/**
|
||||
* The marker used for displaying that a given setting has been overridden for the selected NPC
|
||||
*/
|
||||
SETTING_OVERRIDDEN_MARKER;
|
||||
|
||||
/**
|
||||
* Gets the message to display when displaying the raw value of messages
|
||||
|
Reference in New Issue
Block a user