CommandAPI 9.7.0
An API for the command UI introduced in Minecraft 1.13
com.mojang.brigadier.StringReader Class Reference

A mutable implementation of an ImmutableStringReader, that allows moving the cursor. More...

+ Inheritance diagram for com.mojang.brigadier.StringReader:

Public Member Functions

 StringReader (final StringReader other)
 
 StringReader (final String string)
 
String getString ()
 Returns the full input string. More...
 
void setCursor (final int cursor)
 Sets the cursor index position. More...
 
int getRemainingLength ()
 Returns the remaining length, so the distance from the cursor to the end of the input. More...
 
int getTotalLength ()
 Returns the total length of the input. More...
 
int getCursor ()
 Returns the current cursor position. More...
 
String getRead ()
 Returns the part of the input that was already read. More...
 
String getRemaining ()
 Returns the part of the input that was not yet read. More...
 
boolean canRead (final int length)
 Checks if the reader has enough input to read length more characters. More...
 
boolean canRead ()
 Checks if the reader can read at least one more character. More...
 
char peek ()
 Returns the next character but without consuming it (so the cursor stays at its current position). More...
 
char peek (final int offset)
 Returns the character offset places from the cursor position without consuming it (so the cursor stays at its current position). More...
 
char read ()
 Reads the next character. More...
 
void skip ()
 Skips a single character.
 
void skipWhitespace ()
 Skips all following whitespace characters as determined by Character#isWhitespace(char).
 
int readInt () throws CommandSyntaxException
 Reads an integer. More...
 
long readLong () throws CommandSyntaxException
 Reads a long. More...
 
double readDouble () throws CommandSyntaxException
 Reads a double. More...
 
float readFloat () throws CommandSyntaxException
 Reads a float. More...
 
String readUnquotedString ()
 Reads an unquoted string, i.e. for as long as isAllowedInUnquotedString(char) returns true. More...
 
String readQuotedString () throws CommandSyntaxException
 Returns a quoted string. More...
 
String readStringUntil (char terminator) throws CommandSyntaxException
 
String readString () throws CommandSyntaxException
 Reads a string, deciding between readQuotedString and readUnquotedString based on whether the next character is a quote. More...
 
boolean readBoolean () throws CommandSyntaxException
 Reads a single boolean. More...
 
void expect (final char c) throws CommandSyntaxException
 Peeks at the next char and consumes it, if it is the expected character. If not, it throws an exception. More...
 

Static Public Member Functions

static boolean isAllowedNumber (final char c)
 Checks if the character is allowed in a number. More...
 
static boolean isQuotedStringStart (char c)
 Checks if the character is a allowed as the start of a quoted string. More...
 
static boolean isAllowedInUnquotedString (final char c)
 Checks if a character is allowed in an unquoted string or needs to be escaped. More...
 

Detailed Description

A mutable implementation of an ImmutableStringReader, that allows moving the cursor.

This is done by calling some of the methods that consume some part of the input string (like readInt), as they advance the cursor behind the input they have read.

This class also provides methods to freely set the cursor.

Member Function Documentation

◆ canRead() [1/2]

boolean com.mojang.brigadier.StringReader.canRead ( )

Checks if the reader can read at least one more character.

Returns
true if the reader can read at least one more character
See also
canRead(int)

Implements com.mojang.brigadier.ImmutableStringReader.

◆ canRead() [2/2]

boolean com.mojang.brigadier.StringReader.canRead ( final int  length)

Checks if the reader has enough input to read length more characters.

Parameters
lengththe amount of characters to read
Returns
true if the reader has enough input to read length more characters

Implements com.mojang.brigadier.ImmutableStringReader.

◆ expect()

void com.mojang.brigadier.StringReader.expect ( final char  c) throws CommandSyntaxException

Peeks at the next char and consumes it, if it is the expected character. If not, it throws an exception.

Parameters
cthe character that is expected to occur next
Exceptions
CommandSyntaxExceptionif the character was not the expected character or the end of the input was reached

◆ getCursor()

int com.mojang.brigadier.StringReader.getCursor ( )

Returns the current cursor position.

Returns
the current cursor position

Implements com.mojang.brigadier.ImmutableStringReader.

◆ getRead()

String com.mojang.brigadier.StringReader.getRead ( )

Returns the part of the input that was already read.

Returns
the part of the input that was already read

Implements com.mojang.brigadier.ImmutableStringReader.

◆ getRemaining()

String com.mojang.brigadier.StringReader.getRemaining ( )

Returns the part of the input that was not yet read.

Returns
the part of the input that was not yet read

Implements com.mojang.brigadier.ImmutableStringReader.

◆ getRemainingLength()

int com.mojang.brigadier.StringReader.getRemainingLength ( )

Returns the remaining length, so the distance from the cursor to the end of the input.

Returns
the remaining length, so the distance from the cursor to the end of the input

Implements com.mojang.brigadier.ImmutableStringReader.

◆ getString()

String com.mojang.brigadier.StringReader.getString ( )

Returns the full input string.

Returns
the full input string

Implements com.mojang.brigadier.ImmutableStringReader.

◆ getTotalLength()

int com.mojang.brigadier.StringReader.getTotalLength ( )

Returns the total length of the input.

Returns
the total length of the input

Implements com.mojang.brigadier.ImmutableStringReader.

◆ isAllowedInUnquotedString()

static boolean com.mojang.brigadier.StringReader.isAllowedInUnquotedString ( final char  c)
static

Checks if a character is allowed in an unquoted string or needs to be escaped.

Currently allowed characters are:
[0-9A-Za-z_\-.+] (as a regular expression set)

Parameters
cthe character to check
Returns
true if the character is allowed to appear in an unquoted string

◆ isAllowedNumber()

static boolean com.mojang.brigadier.StringReader.isAllowedNumber ( final char  c)
static

Checks if the character is allowed in a number.

Parameters
cthe character to check
Returns
true if the character is allowed to appear in a number

◆ isQuotedStringStart()

static boolean com.mojang.brigadier.StringReader.isQuotedStringStart ( char  c)
static

Checks if the character is a allowed as the start of a quoted string.

Currently allowed characters are:
["'"] (as a regular expression set)

Parameters
cthe character to check
Returns
true if the character is allowed as the start of a quoted string

◆ peek() [1/2]

char com.mojang.brigadier.StringReader.peek ( )

Returns the next character but without consuming it (so the cursor stays at its current position).

Returns
the next character
See also
peek(int)

Implements com.mojang.brigadier.ImmutableStringReader.

◆ peek() [2/2]

char com.mojang.brigadier.StringReader.peek ( final int  offset)

Returns the character offset places from the cursor position without consuming it (so the cursor stays at its current position).

Parameters
offsetthe offset of the character to peek at
Returns
the character offset places from the current cursor position

Implements com.mojang.brigadier.ImmutableStringReader.

◆ read()

char com.mojang.brigadier.StringReader.read ( )

Reads the next character.

Same as peek(), but also consumes the character.

Returns
the read character

◆ readBoolean()

boolean com.mojang.brigadier.StringReader.readBoolean ( ) throws CommandSyntaxException

Reads a single boolean.

A boolean can be either true or false and is case sensitive.

Returns
the read boolean
Exceptions
CommandSyntaxExceptionif the input was empty or the read string is not a valid boolean

◆ readDouble()

double com.mojang.brigadier.StringReader.readDouble ( ) throws CommandSyntaxException

Reads a double.

The double may only contain characters that match isAllowedNumber(char).

Returns
the read double
Exceptions
CommandSyntaxExceptionif the command is no proper double

◆ readFloat()

float com.mojang.brigadier.StringReader.readFloat ( ) throws CommandSyntaxException

Reads a float.

The float may only contain characters that match isAllowedNumber(char).

Returns
the read float
Exceptions
CommandSyntaxExceptionif the command is no proper float

◆ readInt()

int com.mojang.brigadier.StringReader.readInt ( ) throws CommandSyntaxException

Reads an integer.

The integer may only contain characters that match isAllowedNumber(char).

Returns
the read integer
Exceptions
CommandSyntaxExceptionif the command is no proper int

◆ readLong()

long com.mojang.brigadier.StringReader.readLong ( ) throws CommandSyntaxException

Reads a long.

The long may only contain characters that match isAllowedNumber(char).

Returns
the read long
Exceptions
CommandSyntaxExceptionif the command is no proper long

◆ readQuotedString()

String com.mojang.brigadier.StringReader.readQuotedString ( ) throws CommandSyntaxException

Returns a quoted string.

The format of a quoted string is as follows:

  • Starts and ends with " (quotation mark)
  • Escape character \ (backslash) has to be used for quotation marks and literal escape characters within the string
Returns
a quoted string or an empty string, if canRead is false
Exceptions
CommandSyntaxExceptionif the next character is not a quote, an invalid escape character is encountered or the closing quote was not found

◆ readString()

String com.mojang.brigadier.StringReader.readString ( ) throws CommandSyntaxException

Reads a string, deciding between readQuotedString and readUnquotedString based on whether the next character is a quote.

Returns
the read string
Exceptions
CommandSyntaxExceptionif an error occurs parsing the string

◆ readUnquotedString()

String com.mojang.brigadier.StringReader.readUnquotedString ( )

Reads an unquoted string, i.e. for as long as isAllowedInUnquotedString(char) returns true.

Returns
the read string

◆ setCursor()

void com.mojang.brigadier.StringReader.setCursor ( final int  cursor)

Sets the cursor index position.

Parameters
cursorthe new cursor position