package fr.xephi.authme.command; import java.util.ArrayList; import java.util.List; import org.bukkit.command.CommandSender; import fr.xephi.authme.util.StringUtils; /** * The AuthMe command handler, responsible for mapping incoming commands to the correct {@link CommandDescription} * or to display help messages for unknown invocations. */ public class CommandHandler { private final CommandService commandService; /** * Create a command handler. * * @param commandService The CommandService instance */ public CommandHandler(CommandService commandService) { this.commandService = commandService; } /** * Map a command that was invoked to the proper {@link CommandDescription} or return a useful error * message upon failure. * * @param sender The command sender. * @param bukkitCommandLabel The command label (Bukkit). * @param bukkitArgs The command arguments (Bukkit). * * @return True if the command was executed, false otherwise. */ public boolean processCommand(CommandSender sender, String bukkitCommandLabel, String[] bukkitArgs) { // Add the Bukkit command label to the front so we get a list like [authme, register, bobby, mysecret] List parts = skipEmptyArguments(bukkitArgs); parts.add(0, bukkitCommandLabel); FoundCommandResult result = commandService.mapPartsToCommand(sender, parts); if (FoundResultStatus.SUCCESS.equals(result.getResultStatus())) { executeCommand(sender, result); } else { commandService.outputMappingError(sender, result); } return !FoundResultStatus.MISSING_BASE_COMMAND.equals(result.getResultStatus()); } /** * Execute the command for the given command sender. * * @param sender The sender which initiated the command * @param result The mapped result */ private void executeCommand(CommandSender sender, FoundCommandResult result) { ExecutableCommand executableCommand = result.getCommandDescription().getExecutableCommand(); List arguments = result.getArguments(); executableCommand.executeCommand(sender, arguments, commandService); } /** * Skip all entries of the given array that are simply whitespace. * * @param args The array to process * @return List of the items that are not empty */ private static List skipEmptyArguments(String[] args) { List cleanArguments = new ArrayList<>(); for (String argument : args) { if (!StringUtils.isEmpty(argument)) { cleanArguments.add(argument); } } return cleanArguments; } }