Class Command

Direct Known Subclasses:

public class Command extends Object
Represents a command which has suggestion/auto-completion.

The command works using a list of valid syntaxes. For instance we could build the command "/health set Notch 50" into multiple argument types "/health [set/add/remove] [username] [integer]"

All the default argument types can be found in ArgumentType and the syntax be created/registered using addSyntax(CommandExecutor, Argument[]).

If the command is executed with an incorrect syntax or without any argument, the default CommandExecutor will be called, you can set it using setDefaultExecutor(CommandExecutor).

Before any syntax to be successfully executed the CommandSender needs to validated the CommandCondition sets with setCondition(CommandCondition) (ignored if null).

Some Argument could also require additional condition (eg: a number which need to be between 2 values), in this case, if the whole syntax is correct but not the argument condition, you can listen to its error code using setArgumentCallback(ArgumentCallback, Argument) or Argument.setCallback(ArgumentCallback).

  • Field Details

    • LOGGER

      public static final org.slf4j.Logger LOGGER
  • Constructor Details

    • Command

      public Command(@NotNull @NotNull String name, @Nullable @Nullable String... aliases)
      Creates a Command with a name and one or multiple aliases.
      name - the name of the command
      aliases - the command aliases
      See Also:
    • Command

      public Command(@NotNull @NotNull String name)
      Creates a Command with a name and no alias.
      name - the name of the command
      See Also:
  • Method Details

    • getCondition

      @Nullable public @Nullable CommandCondition getCondition()
      Gets the CommandCondition.

      It is called after the parsing and just before the execution no matter the syntax used and can be used to check permissions or the CommandSender type.

      Worth mentioning that the condition is also used to know if the command known from a player (at connection).

      the command condition, null if not any
    • setCondition

      public void setCondition(@Nullable @Nullable CommandCondition commandCondition)
      commandCondition - the new command condition, null to do not call anything
      See Also:
    • setArgumentCallback

      public void setArgumentCallback(@NotNull @NotNull ArgumentCallback callback, @NotNull @NotNull Argument<?> argument)
      Sets an ArgumentCallback.

      The argument callback is called when there's an error in the argument.

      callback - the callback for the argument
      argument - the argument which get the callback
    • addSubcommand

      public void addSubcommand(@NotNull @NotNull Command command)
    • getSubcommands

      @NotNull public @NotNull List<Command> getSubcommands()
    • addConditionalSyntax

      @NotNull public @NotNull Collection<CommandSyntax> addConditionalSyntax(@Nullable @Nullable CommandCondition commandCondition, @NotNull @NotNull CommandExecutor executor, @NotNull @NotNull Argument<?>... args)
      Adds a new syntax in the command.

      A syntax is simply a list of arguments and an executor called when successfully parsed.

      commandCondition - the condition to use the syntax
      executor - the executor to call when the syntax is successfully received
      args - all the arguments of the syntax, the length needs to be higher than 0
      the created syntaxes, there can be multiple of them when optional arguments are used
    • addSyntax

      @NotNull public @NotNull Collection<CommandSyntax> addSyntax(@NotNull @NotNull CommandExecutor executor, @NotNull @NotNull Argument<?>... args)
      Adds a new syntax without condition.
      See Also:
    • addSyntax

      @Experimental @NotNull public @NotNull Collection<CommandSyntax> addSyntax(@NotNull @NotNull CommandExecutor executor, @NotNull @NotNull String format)
      Creates a syntax from a formatted string.

      Currently in beta as the format is not final.

      executor - the syntax executor
      format - the syntax format
      the newly created syntaxes.
    • getName

      @NotNull public @NotNull String getName()
      Gets the main command's name.
      the main command's name
    • getAliases

      @Nullable public @Nullable String[] getAliases()
      Gets the command's aliases.
      the command aliases, can be null or empty
    • getNames

      @NotNull public @NotNull String[] getNames()
      Gets all the possible names for this command.

      Include getName() and getAliases().

      this command names
    • getDefaultExecutor

      @Nullable public @Nullable CommandExecutor getDefaultExecutor()
      Gets the default CommandExecutor which is called when there is no argument or if no corresponding syntax has been found.
      the default executor, null if not any
      See Also:
    • setDefaultExecutor

      public void setDefaultExecutor(@Nullable @Nullable CommandExecutor executor)
      Sets the default CommandExecutor.
      executor - the new default executor, null to remove it
      See Also:
    • getSyntaxes

      @NotNull public @NotNull Collection<CommandSyntax> getSyntaxes()
      Gets all the syntaxes of this command.
      a collection containing all this command syntaxes
      See Also:
    • globalListener

      public void globalListener(@NotNull @NotNull CommandSender sender, @NotNull @NotNull CommandContext context, @NotNull @NotNull String command)
      Called when a CommandSender executes this command before any syntax callback.

      WARNING: the CommandCondition is not executed, and all the CommandSyntax are not checked, this is called every time a CommandSender send a command which start by getName() or getAliases().

      Can be used if you wish to still suggest the player syntaxes but want to parse things mostly by yourself.

      sender - the CommandSender
      context - the UNCHECKED context of the command, some can be null even when unexpected
      command - the raw UNCHECKED received command
    • getSyntaxesStrings

      @Experimental @NotNull public @NotNull Set<String> getSyntaxesStrings()
    • getSyntaxesTree

      @Experimental @NotNull public @NotNull String getSyntaxesTree()
    • isValidName

      public static boolean isValidName(@NotNull @NotNull Command command, @NotNull @NotNull String name)