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

The blacksmith trait belonging to the selected NPC

* @param npcSetting

The NPC setting to change

* @param newValue

The value to change the setting to

* @param sender

The command sender to notify about results

* @return

True if everything went successfully

*/ 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

The blacksmith trait of the NPC to get the value from

* @param npcSetting

The NPC setting to see the value of

* @param sender

The command sender to display the value to

*/ 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, '&'))); } } }