Class CommandTree<C>

java.lang.Object
cloud.commandframework.CommandTree<C>
Type Parameters:
C - Command sender type

@API(status=INTERNAL, consumers="cloud.commandframework.*") public final class CommandTree<C> extends Object
Tree containing all commands and command paths.

All commands consists of unique paths made out of arguments. These arguments may be literals or variables. Command may either be required or optional, with the requirement that no optional argument precedes a required argument.

The commands are stored in this tree and the nodes of tree consists of the command arguments. Each leaf node of the tree should containing a fully parsed Command. It is thus possible to walk the tree and determine whether or not the supplied input from a command sender constitutes a proper command.

When parsing input, the tree will be walked until one of four scenarios occur:

  1. The input queue is empty at a non-leaf node
  2. The input queue is not empty following a leaf node
  3. No child node is able to accept the input
  4. The input queue is empty following a leaf node

Scenarios one and two would result in a InvalidSyntaxException being thrown, whereas scenario three would result in a NoSuchCommandException if occurring at the root node or a InvalidSyntaxException otherwise. Only the fourth scenario would result in a complete command being parsed.

  • Field Details

    • PARSING_ARGUMENT_KEY

      public static final CloudKey<Integer> PARSING_ARGUMENT_KEY
      Stores the index of the argument that is currently being parsed when parsing a CompoundArgument
  • Method Details

    • newTree

      public static <C> @NonNull CommandTree<C> newTree(@NonNull CommandManager<C> commandManager)
      Create a new command tree instance
      Type Parameters:
      C - Command sender type
      Parameters:
      commandManager - Command manager
      Returns:
      New command tree
    • parse

      public @NonNull Pair<@Nullable Command<C>,@Nullable Exception> parse(@NonNull CommandContext<C> commandContext, @NonNull Queue<@NonNull String> args)
      Attempt to parse string input into a command
      Parameters:
      commandContext - Command context instance
      args - Input
      Returns:
      Parsed command, if one could be found
    • getSuggestions

      public @NonNull List<@NonNull String> getSuggestions(@NonNull CommandContext<C> context, @NonNull Queue<@NonNull String> commandQueue)
      Get suggestions from the input queue
      Parameters:
      context - Context instance
      commandQueue - Input queue
      Returns:
      String suggestions. These should be filtered based on String.startsWith(String)
    • insertCommand

      public void insertCommand(@NonNull Command<C> command)
      Insert a new command into the command tree
      Parameters:
      command - Command to insert
    • verifyAndRegister

      public void verifyAndRegister()
      Go through all commands and register them, and verify the command tree contracts
    • getRootNodes

      public @NonNull Collection<@NonNull CommandTree.Node<@Nullable CommandArgument<C,?>>> getRootNodes()
      Get an immutable collection containing all of the root nodes in the tree
      Returns:
      Root nodes
    • getNamedNode

      public @Nullable CommandTree.Node<@Nullable CommandArgument<C,?>> getNamedNode(@Nullable String name)
      Get a named root node, if it exists
      Parameters:
      name - Root node name
      Returns:
      Root node, or null
    • getCommandManager

      public @NonNull CommandManager<C> getCommandManager()
      Get the command manager
      Returns:
      Command manager