Blacksmith/src/main/java/net/knarcraft/blacksmith/command/BlackSmithEditCommand.java
EpicKnarvik97 cc7d66f270 Fixes several bugs and problems
Fixes getting the name of enchantments
Fixes inconsistencies in material and enchantment name-checking
Allows using "null" or "-1" to unset per-material or per-enchantment configuration options
Fixes a bug where basePrice was set to a material name instead of the price for the material being displayed
Adds missing tab-completion for material/enchantment costs
Prevents inconsistencies in deciding if a value is null
2022-10-24 13:57:58 +02:00

120 lines
5.6 KiB
Java

package net.knarcraft.blacksmith.command;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import net.knarcraft.blacksmith.BlacksmithPlugin;
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.InputParsingHelper;
import net.knarcraft.blacksmith.util.TypeValidationHelper;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
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
*/
public class BlackSmithEditCommand implements CommandExecutor {
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label,
@NotNull String[] args) {
NPC npc = CitizensAPI.getDefaultNPCSelector().getSelected(sender);
if (npc == null || !npc.hasTrait(BlacksmithTrait.class)) {
StringFormatter.displayErrorMessage(sender, TranslatableMessage.NO_NPC_SELECTED);
return true;
}
if (args.length < 1) {
return false;
}
BlacksmithTrait blacksmithTrait = npc.getTraitNullable(BlacksmithTrait.class);
for (NPCSetting npcSetting : NPCSetting.values()) {
String commandName = npcSetting.getCommandName();
if (commandName.equalsIgnoreCase(args[0])) {
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 displayOrChangeNPCSetting(blacksmithTrait, npcSetting, newValue, sender);
}
}
return false;
}
/**
* Changes the given NPC setting, or displays the current value if a new value isn't specified
*
* @param blacksmithTrait <p>The blacksmith trait belonging to the selected NPC</p>
* @param npcSetting <p>The NPC setting to change</p>
* @param newValue <p>The value to change the setting to</p>
* @param sender <p>The command sender to notify about results</p>
* @return <p>True if everything went successfully</p>
*/
private boolean displayOrChangeNPCSetting(BlacksmithTrait blacksmithTrait, NPCSetting npcSetting, String newValue,
CommandSender sender) {
if (newValue == null) {
//Display the current value of the setting
displayNPCSetting(blacksmithTrait, npcSetting, sender);
} else {
//If an empty value or null, clear the value instead of changing it
if (InputParsingHelper.isEmpty(newValue)) {
newValue = null;
} else {
//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.getCommandName(), 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 (InputParsingHelper.isEmpty(rawValue)) {
//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, '&')));
}
}
}