Class CommandTree<C>
- Type Parameters:
C
- Command sender type
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:
- The input queue is empty at a non-leaf node
- The input queue is not empty following a leaf node
- No child node is able to accept the input
- 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
-
Field Summary
Modifier and TypeFieldDescriptionStores the index of the argument that is currently being parsed when parsing aCompoundArgument
-
Method Summary
Modifier and TypeMethodDescription@NonNull CommandManager<C>
Get the command manager@Nullable CommandTree.Node<@Nullable CommandArgument<C,?>>
getNamedNode(@Nullable String name)
Get a named root node, if it exists@NonNull Collection<@NonNull CommandTree.Node<@Nullable CommandArgument<C,?>>>
Get an immutable collection containing all of the root nodes in the treegetSuggestions(@NonNull CommandContext<C> context, @NonNull Queue<@NonNull String> commandQueue)
Get suggestions from the input queuevoid
insertCommand(@NonNull Command<C> command)
Insert a new command into the command treestatic <C> @NonNull CommandTree<C>
newTree(@NonNull CommandManager<C> commandManager)
Create a new command tree instanceparse(@NonNull CommandContext<C> commandContext, @NonNull Queue<@NonNull String> args)
Attempt to parse string input into a commandvoid
Go through all commands and register them, and verify the command tree contracts
-
Field Details
-
PARSING_ARGUMENT_KEY
Stores the index of the argument that is currently being parsed when parsing aCompoundArgument
-
-
Method Details
-
newTree
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 instanceargs
- 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 instancecommandQueue
- Input queue- Returns:
- String suggestions. These should be filtered based on
String.startsWith(String)
-
insertCommand
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
Get the command manager- Returns:
- Command manager
-