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.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; /** * 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, Translator.getTranslatedMessage(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 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; } 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; } else { return false; } } } }