diff --git a/core/src/main/java/co/aikar/commands/BaseCommand.java b/core/src/main/java/co/aikar/commands/BaseCommand.java index b60e437d..8ca94121 100644 --- a/core/src/main/java/co/aikar/commands/BaseCommand.java +++ b/core/src/main/java/co/aikar/commands/BaseCommand.java @@ -44,6 +44,7 @@ import java.lang.reflect.Method; import java.lang.reflect.Parameter; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; @@ -59,10 +60,10 @@ import java.util.stream.Stream; /** * A Base command is defined as a command group of related commands. * A BaseCommand does not imply nor enforce that they use the same root command. - * + *

* It is up to the end user how to organize their command. you could use 1 base command per * command in your application. - * + *

* Optionally (and encouraged), you can use the base command to represent a root command, and * then each actionable command is a sub command */ @@ -94,7 +95,8 @@ public abstract class BaseCommand { /** * What method was annoated with {@link PreCommand} to execute before commands. */ - @Nullable private Method preCommandHandler; + @Nullable + private Method preCommandHandler; /** * What root command the user actually entered to access the currently executing command @@ -155,14 +157,17 @@ public abstract class BaseCommand { /** * If a parent exists to this command, and it has a Subcommand annotation, prefix all subcommands in this class with this */ - @Nullable private String parentSubcommand; + @Nullable + private String parentSubcommand; - public BaseCommand() {} + public BaseCommand() { + } /** * Constructor based defining of commands will be removed in the next version bump. - * @deprecated Please switch to {@link CommandAlias} for defining all root commands. + * * @param cmd + * @deprecated Please switch to {@link CommandAlias} for defining all root commands. */ @Deprecated public BaseCommand(@Nullable String cmd) { @@ -171,6 +176,7 @@ public abstract class BaseCommand { /** * Gets the root command name that the user actually typed + * * @return Name */ public String getExecCommandLabel() { @@ -179,6 +185,7 @@ public abstract class BaseCommand { /** * Gets the actual sub command name the user typed + * * @return Name */ public String getExecSubcommand() { @@ -187,6 +194,7 @@ public abstract class BaseCommand { /** * Gets the actual args in string form the user typed + * * @return Args */ public String[] getOrigArgs() { @@ -197,8 +205,7 @@ public abstract class BaseCommand { * This should be called whenever the command gets registered. * It sets all required fields correctly and injects dependencies. * - * @param manager - * The manager to register as this command's owner and handler. + * @param manager The manager to register as this command's owner and handler. */ void onRegister(CommandManager manager) { onRegister(manager, this.commandName); @@ -208,10 +215,8 @@ public abstract class BaseCommand { * This should be called whenever the command gets registered. * It sets all required fields correctly and injects dependencies. * - * @param manager - * The manager to register as this command's owner and handler. - * @param cmd - * The command name to use register with. + * @param manager The manager to register as this command's owner and handler. + * @param cmd The command name to use register with. */ private void onRegister(CommandManager manager, String cmd) { manager.injectDependencies(this); @@ -252,8 +257,7 @@ public abstract class BaseCommand { /** * This recursively registers all subclasses of the command as subcommands, if they are of type {@link BaseCommand}. * - * @param cmd - * The command name of this command. + * @param cmd The command name of this command. */ private void registerSubclasses(String cmd) { for (Class clazz : this.getClass().getDeclaredClasses()) { @@ -358,9 +362,7 @@ public abstract class BaseCommand { /** * Gets the subcommand name of the method given. * - * @param method - * The method to check. - * + * @param method The method to check. * @return The name of the subcommand. It returns null if the input doesn't have {@link Subcommand} attached. */ private String getSubcommandValue(Method method) { @@ -389,10 +391,8 @@ public abstract class BaseCommand { /** * Registers the given {@link BaseCommand cmd} as a child of the {@link RootCommand} linked to the name given. * - * @param name - * Name of the parent to cmd. - * @param cmd - * The {@link BaseCommand} to add as a child to the {@link RootCommand} owned name field. + * @param name Name of the parent to cmd. + * @param cmd The {@link BaseCommand} to add as a child to the {@link RootCommand} owned name field. */ private void register(String name, BaseCommand cmd) { String nameLower = name.toLowerCase(); @@ -405,10 +405,8 @@ public abstract class BaseCommand { /** * Registers the given {@link Method} as a subcommand. * - * @param method - * The method to register as a subcommand. - * @param subCommand - * The subcommand's name(s). + * @param method The method to register as a subcommand. + * @param subCommand The subcommand's name(s). */ private void registerSubcommand(Method method, String subCommand) { subCommand = manager.getCommandReplacements().replace(subCommand.toLowerCase()); @@ -437,7 +435,7 @@ public abstract class BaseCommand { if (aliasNames != null) { for (String name : aliasNames) { - register(name, new ForwardingCommand(this, subCommandParts)); + register(name, new ForwardingCommand(this, cmd, subCommandParts)); } } } @@ -448,7 +446,7 @@ public abstract class BaseCommand { * - foo qux * - bar baz * - bar qux - * + *

* For every possible sub command combination * * @param subCommandParts @@ -512,11 +510,9 @@ public abstract class BaseCommand { /** * Gets the registered command of the given arguments. - * @param args - * The arguments given by the user. * + * @param args The arguments given by the user. * @return The subcommand or null if none were found. - * * @see #findSubCommand(String[]) */ RegisteredCommand getRegisteredCommand(String[] args) { @@ -537,16 +533,11 @@ public abstract class BaseCommand { /** * This is ran before any command operation has been performed. * - * @param issuer - * The user who executed the command. - * @param commandLabel - * The label the user used to execute the command. This is not the command name, but their input. - * When there is multiple aliases, this is which alias was used - * @param args - * The arguments passed to the command when executing it. - * @param isAsync - * Whether the command is executed off of the main thread. - * + * @param issuer The user who executed the command. + * @param commandLabel The label the user used to execute the command. This is not the command name, but their input. + * When there is multiple aliases, this is which alias was used + * @param args The arguments passed to the command when executing it. + * @param isAsync Whether the command is executed off of the main thread. * @return The context which is being registered to the {@link CommandManager}'s {@link * CommandManager#commandOperationContext thread local stack}. */ @@ -582,11 +573,8 @@ public abstract class BaseCommand { /** * Finds a subcommand of the given arguments. * - * @param args - * The arguments the user input. - * + * @param args The arguments the user input. * @return The identified subcommand. - * * @see #findSubCommand(String[], boolean) */ private CommandSearch findSubCommand(String[] args) { @@ -596,11 +584,8 @@ public abstract class BaseCommand { /** * Finds a subcommand of the given arguments. * - * @param args - * The arguments the user input. - * @param completion - * Whether or not completion of arguments should kick in. This may end up with worse than wanted results. - * + * @param args The arguments the user input. + * @param completion Whether or not completion of arguments should kick in. This may end up with worse than wanted results. * @return The identified subcommand. */ private CommandSearch findSubCommand(String[] args, boolean completion) { @@ -655,10 +640,11 @@ public abstract class BaseCommand { /** * Please use command conditions for restricting execution - * @deprecated See {@link CommandConditions} + * * @param issuer * @param cmd * @return + * @deprecated See {@link CommandConditions} */ @SuppressWarnings("DeprecatedIsStillUsed") @Deprecated @@ -669,13 +655,9 @@ public abstract class BaseCommand { /** * Gets tab completed data from the given command from the user. * - * @param issuer - * The user who executed the tabcomplete. - * @param commandLabel - * The label which is being used by the user. - * @param args - * The arguments the user has typed so far. - * + * @param issuer The user who executed the tabcomplete. + * @param commandLabel The label which is being used by the user. + * @param args The arguments the user has typed so far. * @return All possibilities in the tab complete. */ public List tabComplete(CommandIssuer issuer, String commandLabel, String[] args) { @@ -686,20 +668,15 @@ public abstract class BaseCommand { * Gets the tab complete suggestions from a given command. This will automatically find anything * which is valid for the specified command through the command's implementation. * - * @param issuer - * The issuer of the command. - * @param commandLabel - * The command name as entered by the user instead of the ACF registered name. - * @param args - * All arguments entered by the user. - * @param isAsync - * Whether this is run off of the main thread. - * + * @param issuer The issuer of the command. + * @param commandLabel The command name as entered by the user instead of the ACF registered name. + * @param args All arguments entered by the user. + * @param isAsync Whether this is run off of the main thread. * @return The possibilities to tab complete in no particular order. */ @SuppressWarnings("WeakerAccess") public List tabComplete(CommandIssuer issuer, String commandLabel, String[] args, boolean isAsync) - throws IllegalArgumentException { + throws IllegalArgumentException { commandLabel = commandLabel.toLowerCase(); if (args.length == 0) { @@ -730,10 +707,8 @@ public abstract class BaseCommand { /** * Gets all subcommands which are possible to tabcomplete. * - * @param issuer - * The command issuer. + * @param issuer The command issuer. * @param args - * * @return */ List getCommandsForCompletion(CommandIssuer issuer, String[] args) { @@ -758,17 +733,11 @@ public abstract class BaseCommand { /** * Complete a command properly per issuer and input. * - * @param issuer - * The user who executed this. - * @param cmd - * The command to be completed. - * @param args - * All arguments given by the user. - * @param commandLabel - * The command name the user used. - * @param isAsync - * Whether the command was executed async. - * + * @param issuer The user who executed this. + * @param cmd The command to be completed. + * @param args All arguments given by the user. + * @param commandLabel The command name the user used. + * @param isAsync Whether the command was executed async. * @return All results to complete the command. */ private List completeCommand(CommandIssuer issuer, RegisteredCommand cmd, String[] args, String commandLabel, boolean isAsync) { @@ -777,32 +746,28 @@ public abstract class BaseCommand { } List cmds = manager.getCommandCompletions().of(cmd, issuer, args, isAsync); - return filterTabComplete(args[args.length-1], cmds); + return filterTabComplete(args[args.length - 1], cmds); } /** * Gets the actual args in string form the user typed * This returns a list of all tab complete options which are possible with the given argument and commands. - * @param arg - * Argument which was pressed tab on. - * @param cmds - * The possibilities to return. * + * @param arg Argument which was pressed tab on. + * @param cmds The possibilities to return. * @return All possible options. This may be empty. */ private static List filterTabComplete(String arg, List cmds) { return cmds.stream() - .distinct() - .filter(cmd -> cmd != null && (arg.isEmpty() || ApacheCommonsLangUtil.startsWithIgnoreCase(cmd, arg))) - .collect(Collectors.toList()); + .distinct() + .filter(cmd -> cmd != null && (arg.isEmpty() || ApacheCommonsLangUtil.startsWithIgnoreCase(cmd, arg))) + .collect(Collectors.toList()); } /** * Gets a registered command under the given subcommand name. * - * @param subcommand - * The name of the subcommand requested. - * + * @param subcommand The name of the subcommand requested. * @return The subcommand found or null if none. */ private RegisteredCommand getCommandBySubcommand(String subcommand) { @@ -813,11 +778,8 @@ public abstract class BaseCommand { * Gets a registered command under the given name. * If requireOne is true, it won't accept more than a single matching subcommand. * - * @param subcommand - * Name of the subcommand wanted. - * @param requireOne - * Whether to only accept 1 result. - * + * @param subcommand Name of the subcommand wanted. + * @param requireOne Whether to only accept 1 result. * @return The subcommand found, or null if none/too many. */ private RegisteredCommand getCommandBySubcommand(String subcommand, boolean requireOne) { @@ -832,17 +794,11 @@ public abstract class BaseCommand { * Internally calls {@link #executeCommand(CommandOperationContext, CommandIssuer, String[], RegisteredCommand)} * and gets through {@link #getCommandBySubcommand(String)}. * - * @param commandContext - * The command context to use. - * @param subcommand - * The subcommand to find the executor of. - * @param issuer - * The issuer who executed the subcommand. - * @param args - * All arguments given by the issuer. - * + * @param commandContext The command context to use. + * @param subcommand The subcommand to find the executor of. + * @param issuer The issuer who executed the subcommand. + * @param args All arguments given by the issuer. * @return Whether it found a command or not. - * * @see #executeCommand(CommandOperationContext, CommandIssuer, String[], RegisteredCommand) * @see #getCommandBySubcommand(String) * @see RegisteredCommand#invoke(CommandIssuer, List, CommandOperationContext) @@ -860,15 +816,10 @@ public abstract class BaseCommand { /** * Executes the precommand and sees whether something is wrong. Ideally, you get false from this. * - * @param commandOperationContext - * The context to use. - * @param cmd - * The command executed. - * @param issuer - * The issuer who executed the command. - * @param args - * The arguments the issuer provided. - * + * @param commandOperationContext The context to use. + * @param cmd The command executed. + * @param issuer The issuer who executed the command. + * @param args The arguments the issuer provided. * @return Whether something went wrong. */ private boolean checkPrecommand(CommandOperationContext commandOperationContext, RegisteredCommand cmd, CommandIssuer issuer, String[] args) { @@ -901,12 +852,20 @@ public abstract class BaseCommand { return false; } - /** @deprecated Unstable API */ @Deprecated @UnstableAPI + /** + * @deprecated Unstable API + */ + @Deprecated + @UnstableAPI public CommandHelp getCommandHelp() { - return manager.generateCommandHelp(); + return manager.generateCommandHelp(); } - /** @deprecated Unstable API */ @Deprecated @UnstableAPI + /** + * @deprecated Unstable API + */ + @Deprecated + @UnstableAPI public void showCommandHelp() { getCommandHelp().showHelp(); } @@ -914,12 +873,15 @@ public abstract class BaseCommand { public void help(Object issuer, String[] args) { help(manager.getCommandIssuer(issuer), args); } + public void help(CommandIssuer issuer, String[] args) { issuer.sendMessage(MessageType.ERROR, MessageKeys.UNKNOWN_COMMAND); } + public void doHelp(Object issuer, String... args) { doHelp(manager.getCommandIssuer(issuer), args); } + public void doHelp(CommandIssuer issuer, String... args) { help(issuer, args); } @@ -976,7 +938,17 @@ public abstract class BaseCommand { return this.contextFlags.get(cls); } - private static class CommandSearch { RegisteredCommand cmd; int argIndex; String checkSub; + public List getRegisteredCommands() { + List registeredCommands = new ArrayList<>(); + registeredCommands.addAll(this.subCommands.values()); + return registeredCommands; + + } + + private static class CommandSearch { + RegisteredCommand cmd; + int argIndex; + String checkSub; CommandSearch(RegisteredCommand cmd, int argIndex, String checkSub) { this.cmd = cmd; diff --git a/core/src/main/java/co/aikar/commands/CommandCompletionContext.java b/core/src/main/java/co/aikar/commands/CommandCompletionContext.java index bb982c23..3211626e 100644 --- a/core/src/main/java/co/aikar/commands/CommandCompletionContext.java +++ b/core/src/main/java/co/aikar/commands/CommandCompletionContext.java @@ -28,7 +28,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -public class CommandCompletionContext { +public class CommandCompletionContext { private final RegisteredCommand command; protected final I issuer; private final String input; @@ -83,7 +83,7 @@ public class CommandCompletionContext { CommandParameter param = command.parameters[paramIdx]; Class paramType = param.getType(); if (!clazz.isAssignableFrom(paramType)) { - throw new IllegalArgumentException(param.getName() +":" + paramType.getName() + " can not satisfy " + clazz.getName()); + throw new IllegalArgumentException(param.getName() + ":" + paramType.getName() + " can not satisfy " + clazz.getName()); } name = param.getName(); } else { @@ -103,7 +103,7 @@ public class CommandCompletionContext { //noinspection unchecked Map resolved = command.resolveContexts(issuer, args, args.size()); if (resolved == null || paramIdx > resolved.size()) { - this.command.scope.manager.log(LogLevel.ERROR, "resolved: " + resolved + " paramIdx: " + paramIdx + " - size: " + (resolved != null ? resolved.size() : null )); + this.command.scope.manager.log(LogLevel.ERROR, "resolved: " + resolved + " paramIdx: " + paramIdx + " - size: " + (resolved != null ? resolved.size() : null)); ACFUtil.sneaky(new CommandCompletionTextLookupException()); }