CommandAPI 9.3.0
An API for the command UI introduced in Minecraft 1.13
com.mojang.brigadier.context.CommandContextBuilder< S > Class Template Reference

A builder for CommandContext objects. More...

Public Member Functions

 CommandContextBuilder (final CommandDispatcher< S > dispatcher, final S source, final CommandNode< S > rootNode, final int start)
 Creates a new CommandContextBuilder. More...
 
CommandContextBuilder< S > withSource (final S source)
 Sets the source for this command. More...
 
getSource ()
 The command source for the built command context. More...
 
CommandNode< S > getRootNode ()
 Returns the root node in the command tree. More...
 
CommandContextBuilder< S > withArgument (final String name, final ParsedArgument< S, ?> argument)
 Stores an argument in this command, that can later be retrieved. More...
 
Map< String, ParsedArgument< S, ?> > getArguments ()
 Returns all stored arguments. More...
 
CommandContextBuilder< S > withCommand (final Command< S > command)
 Sets the command to execute, i.e. More...
 
CommandContextBuilder< S > withNode (final CommandNode< S > node, final StringRange range)
 Adds the given command node alongside its parsed range to this context. More...
 
CommandContextBuilder< S > copy ()
 Creates a shallow copy of this builder. More...
 
CommandContextBuilder< S > withChild (final CommandContextBuilder< S > child)
 Sets a child context, which is the context for a child command node. More...
 
CommandContextBuilder< S > getChild ()
 Returns the child context, i.e. More...
 
CommandContextBuilder< S > getLastChild ()
 Returns the last child command context in the chain, i.e. More...
 
Command< S > getCommand ()
 Returns the command to execute. More...
 
List< ParsedCommandNode< S > > getNodes ()
 Returns a list with all nodes. More...
 
CommandContext< S > build (final String input)
 Builds the command context. More...
 
CommandDispatcher< S > getDispatcher ()
 Returns the CommandDispatcher set in the constructor. More...
 
StringRange getRange ()
 Returns the range this context spans in the input. More...
 
SuggestionContext< S > findSuggestionContext (final int cursor)
 Finds the SuggestionContext given a cursor value. More...
 

Detailed Description

A builder for CommandContext objects.

Parameters
<S>the type of the command source

Constructor & Destructor Documentation

◆ CommandContextBuilder()

com.mojang.brigadier.context.CommandContextBuilder< S >.CommandContextBuilder ( final CommandDispatcher< S >  dispatcher,
final S  source,
final CommandNode< S >  rootNode,
final int  start 
)

Creates a new CommandContextBuilder.

Parameters
dispatcherthe CommandDispatcher TODO: Why does this exist here?
sourcethe command source
rootNodethe root node of the command tree
startthe start in the input that this context spans

Member Function Documentation

◆ build()

CommandContext< S > com.mojang.brigadier.context.CommandContextBuilder< S >.build ( final String  input)

Builds the command context.

Parameters
inputthe input string
Returns
the built command context

◆ copy()

Creates a shallow copy of this builder.

Returns
a copy of this builder

◆ findSuggestionContext()

SuggestionContext< S > com.mojang.brigadier.context.CommandContextBuilder< S >.findSuggestionContext ( final int  cursor)

Finds the SuggestionContext given a cursor value.

This method attempts to find the correct command node responsible at the given cursor position.

Parameters
cursorthe cursor position to find a suggestion context for
Returns
the SuggestionContext for the given cursor value
Exceptions
IllegalStateExceptionif the cursor position is smaller than the getRange() or not within the bounds of any registered node

◆ getArguments()

Map< String, ParsedArgument< S, ?> > com.mojang.brigadier.context.CommandContextBuilder< S >.getArguments ( )

Returns all stored arguments.

Returns
all stored arguments

◆ getChild()

Returns the child context, i.e.

the context for a child command node.

Returns
the child context, i.e. the context for a child command node

◆ getCommand()

Returns the command to execute.

Returns
the command to execute or null if not set

◆ getDispatcher()

Returns the CommandDispatcher set in the constructor.

TODO: WHY?

Returns
the CommandDispatcher set in the constructor

◆ getLastChild()

Returns the last child command context in the chain, i.e.

the lowest child you can reach from this context.

As each CommandContextBuilder can have a child, you can have a child of a child. This method returns the lowest possible child you can reach, i.e. the last command context that has no children. This can be this command context instance, if it has no child.

Returns
the last child command context builder
See also
CommandContext::getLastChild()

◆ getNodes()

Returns a list with all nodes.

Returns
a list with all nodes
See also
CommandContext::getNodes

◆ getRange()

Returns the range this context spans in the input.

Returns
the range this context spans in the input

◆ getRootNode()

Returns the root node in the command tree.

Returns
the root node in the command tree

◆ getSource()

The command source for the built command context.

Returns
the command source for the built command context

◆ withArgument()

CommandContextBuilder< S > com.mojang.brigadier.context.CommandContextBuilder< S >.withArgument ( final String  name,
final ParsedArgument< S, ?>  argument 
)

Stores an argument in this command, that can later be retrieved.

Parameters
namethe name of the argument
argumentthe argument to store
Returns
this builder
See also
CommandContext::getArgument

◆ withChild()

Sets a child context, which is the context for a child command node.

Parameters
childthe child context
Returns
this builder

◆ withCommand()

Sets the command to execute, i.e.

the one that was matched by this context.

Parameters
commandthe Command that should be executed
Returns
this builder

◆ withNode()

CommandContextBuilder< S > com.mojang.brigadier.context.CommandContextBuilder< S >.withNode ( final CommandNode< S >  node,
final StringRange  range 
)

Adds the given command node alongside its parsed range to this context.

You should probably not call this method in your code, as duplicate invocations could put the builder into an invalid state.

Parameters
nodethe command node to add
rangethe range the node spans in the input
Returns
this builder

◆ withSource()

Sets the source for this command.

Parameters
sourcethr command source
Returns
this builder