Class CommandTree<C>

  • Type Parameters:
    C - Command sender type

    public final class CommandTree<C>
    extends java.lang.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.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  CommandTree.Node<T>
      Very simple tree structure
    • Method Summary

      Modifier and Type Method Description
      @NonNull CommandManager<C> getCommandManager()
      Get the command manager
      @Nullable CommandTree.Node<@Nullable CommandArgument<C,​?>> getNamedNode​(@Nullable java.lang.String name)
      Get a named root node, if it exists
      @NonNull java.util.Collection<@NonNull CommandTree.Node<@Nullable CommandArgument<C,​?>>> getRootNodes()
      Get an immutable collection containing all of the root nodes in the tree
      @NonNull java.util.List<@NonNull java.lang.String> getSuggestions​(@NonNull CommandContext<C> context, @NonNull java.util.Queue<@NonNull java.lang.String> commandQueue)
      Get suggestions from the input queue
      void insertCommand​(@NonNull Command<C> command)
      Insert a new command into the command tree
      static <C> @NonNull CommandTree<C> newTree​(@NonNull CommandManager<C> commandManager)
      Create a new command tree instance
      @NonNull Pair<@Nullable Command<C>,​@Nullable java.lang.Exception> parse​(@NonNull CommandContext<C> commandContext, @NonNull java.util.Queue<@NonNull java.lang.String> args)
      Attempt to parse string input into a command
      void verifyAndRegister()
      Go through all commands and register them, and verify the command tree contracts
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • 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 java.lang.Exception> parse​(@NonNull CommandContext<C> commandContext,
                                                                                             @NonNull java.util.Queue<@NonNull java.lang.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 java.util.List<@NonNull java.lang.String> getSuggestions​(@NonNull CommandContext<C> context,
                                                                                 @NonNull java.util.Queue<@NonNull java.lang.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 java.util.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 java.lang.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