CommandAPI 9.3.0
An API for the command UI introduced in Minecraft 1.13
com.mojang.brigadier.arguments.StringArgumentType Class Reference

An ArgumentType that parses strings. More...

+ Inheritance diagram for com.mojang.brigadier.arguments.StringArgumentType:

Classes

enum  StringType
 The type of phrase that is matched by StringArgumentType. More...
 

Public Member Functions

StringType getType ()
 The type of string this argument parses. More...
 
String parse (final StringReader reader) throws CommandSyntaxException
 
String toString ()
 
Collection< String > getExamples ()
 
default< S > CompletableFuture< SuggestionslistSuggestions (final CommandContext< S > context, final SuggestionsBuilder builder)
 Gets suggestions for a parsed input string on what comes next. More...
 

Static Public Member Functions

static StringArgumentType word ()
 A factory method that provides an instance of a word type string argument. More...
 
static StringArgumentType string ()
 A factory method that provides an instance of a string argument. More...
 
static StringArgumentType greedyString ()
 A factory method that provides an instance of a greedy string argument. More...
 
static String getString (final CommandContext<?> context, final String name)
 Retrieves the argument with the given name from the context and casts it to a string. More...
 
static String escapeIfRequired (final String input)
 Converts a string to one that StringType#QUOTABLE_PHRASE would match. More...
 

Detailed Description

An ArgumentType that parses strings.

Member Function Documentation

◆ escapeIfRequired()

static String com.mojang.brigadier.arguments.StringArgumentType.escapeIfRequired ( final String  input)
static

Converts a string to one that StringType#QUOTABLE_PHRASE would match.

If the given string contains any character that does not match StringReader#isAllowedInUnquotedString(char), then it is wrapped in quotation marks and quotation marks and escaping characters within the string are escaped, otherwise the given string is returned unaltered.

Some sample in- and outputs:

  • hey to hey
  • hey you to "hey you"
  • "hello" to "\"hello\""
  • \ to "\\"
Parameters
inputthe input to escape
Returns
the escaped output ready to be parsed by StringType#QUOTABLE_PHRASE

◆ getString()

static String com.mojang.brigadier.arguments.StringArgumentType.getString ( final CommandContext<?>  context,
final String  name 
)
static

Retrieves the argument with the given name from the context and casts it to a string.

Parameters
contextthe context to get the argument from, calls CommandContext#getArgument
namethe name of the argument to retrieve
Returns
the argument as a string
See also
CommandContext::getArgument

◆ getType()

StringType com.mojang.brigadier.arguments.StringArgumentType.getType ( )

The type of string this argument parses.

Returns
the StringType this argument parses

◆ greedyString()

static StringArgumentType com.mojang.brigadier.arguments.StringArgumentType.greedyString ( )
static

A factory method that provides an instance of a greedy string argument.

This will parse the entire remaining input.

Returns
an instance of this argument type parsing everything until the end of the input

◆ listSuggestions()

default< S > CompletableFuture< Suggestions > com.mojang.brigadier.arguments.ArgumentType< T >.listSuggestions ( final CommandContext< S >  context,
final SuggestionsBuilder  builder 
)
inherited

Gets suggestions for a parsed input string on what comes next.

As it is ultimately up to custom argument types to provide suggestions, it may be an asynchronous operation, for example getting in-game data or player names etc. As such, this method returns a future and no guarantees are made to when or how the future completes.

The suggestions provided will be in the context of the end of the parsed input string, but may suggest new or replacement strings for earlier in the input string. For example, if the end of the string was foobar but an argument preferred it to be minecraft:foobar, it will suggest a replacement for that whole segment of the input.

Parameters
contextthe context to get them for
builderthe suggestions builder to add them to
Returns
a future that will eventually resolve into a Suggestions object @implSpec The default implementation simply returns an empty suggestions object

◆ string()

static StringArgumentType com.mojang.brigadier.arguments.StringArgumentType.string ( )
static

A factory method that provides an instance of a string argument.

If the string does not start with a quote, it will be interpreted like a word type string, else it will parse everything up until the next non-escaped quote.

Returns
an instance of this argument type parsing a word or quoted string

◆ word()

static StringArgumentType com.mojang.brigadier.arguments.StringArgumentType.word ( )
static

A factory method that provides an instance of a word type string argument.

Allowed are only characters where StringReader#isAllowedInUnquotedString(char) would return true.
The parser stops parsing at the first character that is not allowed. Typically this needs to be a space.

Returns
an instance of this argument type parsing a single word