Interface ArgumentReader.Mutable
-
- All Superinterfaces:
ArgumentReader
- Enclosing interface:
- ArgumentReader
public static interface ArgumentReader.Mutable extends ArgumentReader
Represents aArgumentReader
where the cursor may move.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.spongepowered.api.command.parameter.ArgumentReader
ArgumentReader.Immutable, ArgumentReader.Mutable
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description ArgumentReader.Immutable
immutable()
Returns a immutable copy of theArgumentReader
.boolean
parseBoolean()
Parses "true" or "false", else throws an exception.char
parseChar()
Reads a character and advances the cursor by oneDataContainer
parseDataContainer()
Parses aDataContainer
, from a SNBT string, else throws an exception.double
parseDouble()
Attempts to read adouble
from the input starting at the cursor position.float
parseFloat()
Attempts to read afloat
from the input starting at the cursor position.int
parseInt()
Attempts to read anint
from the input starting at the cursor position.String
parseNBTString()
Parses a SNBT string, returning that string.ResourceKey
parseResourceKey()
Attempts to read a string that is in aResourceKey
format, which isnamespace:identifier
.ResourceKey
parseResourceKey(String defaultNamespace)
Attempts to read a string that is in aResourceKey
format, which isnamespace:identifier
.String
parseString()
Gets aString
from the position of the cursor.String
parseUnquotedString()
Gets the next word in the input from the position of the cursor.String
peekString()
Returns the nextString
but does not advance the reader.void
setState(ArgumentReader state)
Attempts to reset the state of thisArgumentReader.Mutable
to the state that the providedArgumentReader
is in.void
skipWhitespace()
Moves the cursor to the next non-whitespace character.-
Methods inherited from interface org.spongepowered.api.command.parameter.ArgumentReader
canRead, createException, cursor, input, parsed, peekCharacter, remaining, remainingLength, totalLength
-
-
-
-
Method Detail
-
skipWhitespace
void skipWhitespace()
Moves the cursor to the next non-whitespace character. The cursor will not advance if it already refers to a non-whitespace character.
-
parseChar
char parseChar()
Reads a character and advances the cursor by one- Returns:
- The character
-
parseInt
int parseInt() throws ArgumentParseException
Attempts to read anint
from the input starting at the cursor position. The cursor will advance until it finds a non-number and will return anint
based on the consumed string.Numbers may begin with "-" to indicate a negative number
When using this in your parser, you should set
ValueParser.clientCompletionType()
toClientCompletionTypes.WHOLE_NUMBER
to tell the client that the client completion should be an integer.- Returns:
- The integer
- Throws:
ArgumentParseException
- if the cursor is not at a number character
-
parseDouble
double parseDouble() throws ArgumentParseException
Attempts to read adouble
from the input starting at the cursor position. The cursor will advance until it finds a non-number and will return adouble
based on the consumed string.Numbers may begin with "-" to indicate a negative number, and one period (".") may be present in the string.
When using this in your parser, you should set
ValueParser.clientCompletionType()
toClientCompletionTypes.DECIMAL_NUMBER
to tell the client that the client completion should be a floating point number.- Returns:
- The double
- Throws:
ArgumentParseException
- if the cursor is not at a number character
-
parseFloat
float parseFloat() throws ArgumentParseException
Attempts to read afloat
from the input starting at the cursor position. The cursor will advance until it finds a non-number and will return afloat
based on the consumed string.Numbers may begin with "-" to indicate a negative number, and one period (".") may be present in the string.
When using this in your parser, you should set
ValueParser.clientCompletionType()
toClientCompletionTypes.DECIMAL_NUMBER
to tell the client that the client completion should be a floating point number.- Returns:
- The double
- Throws:
ArgumentParseException
- if the cursor is not at a number character
-
parseResourceKey
ResourceKey parseResourceKey() throws ArgumentParseException
Attempts to read a string that is in aResourceKey
format, which isnamespace:identifier
.This parser is a strict parser. If the input is not of the format specified above, this will fail. If you are potentially expecting a non-namespaced key, but would like to accept such strings with a default namespace, use
parseResourceKey(String)
When using this in your parser, you should set
ValueParser.clientCompletionType()
toClientCompletionTypes.RESOURCE_KEY
to tell the client that the client completion should be aResourceKey
, so that your users will not be told to put their argument in quotation marks.- Returns:
- The
ResourceKey
- Throws:
ArgumentParseException
- if a key could not be parsed
-
parseResourceKey
ResourceKey parseResourceKey(String defaultNamespace) throws ArgumentParseException
Attempts to read a string that is in aResourceKey
format, which isnamespace:identifier
.If no colon is encountered, the default namespace defined below will be used as the namespace.
When using this in your parser, you should set
ValueParser.clientCompletionType()
toClientCompletionTypes.RESOURCE_KEY
to tell the client that the client completion should be aResourceKey
, so that your users will not be told to put their argument in quotation marks.- Parameters:
defaultNamespace
- The default namespace to use when parsing a string that does not contain a colon.- Returns:
- The
ResourceKey
- Throws:
ArgumentParseException
- if a key could not be parsed
-
parseUnquotedString
String parseUnquotedString() throws ArgumentParseException
Gets the next word in the input from the position of the cursor. This will return a string that contains the characters up to, but excluding, whitespace. The cursor will then be moved to the whitespace after the word.As an example, if the input string is
eggs bacon spam
and the cursor is at position zero (at the beginning), after this operation, the cursor will be at the whitespace between "eggs" and "bacon".In general, you will likely wish to use
parseString()
instead unless you are expecting a double quote mark at the beginning of your string and this is part of the argument you wish to return.The following characters will be parsed as part of a valid string without the need for quotation marks.
- Alphanumeric symbols
- Underscores (_)
- Hyphens (-)
- Periods (.)
- Plus signs (+)
If you require other symbols, use
parseString()
and ensure that users are aware they need to surround their inputs with double quotation marks. Alternatively, consider whether other parse types are suitable (for example, if you are expecting aResourceKey
, useparseResourceKey()
orparseResourceKey(String)
.- Returns:
- The parsed
String
- Throws:
ArgumentParseException
- if aString
could not be read
-
parseString
String parseString() throws ArgumentParseException
Gets aString
from the position of the cursor. The parsing of the argument depends on whether a double quote mark is the next character to be consumed.- If a double quote is at the beginning of the string , the parser will read characters until a second quotation mark is found and return the string between the two marks. The cursor will then be set to the position after the second mark.
- If the first character is not a double quote
,
parseUnquotedString()
will parse the string
If part of the string is quoted, such as
"eggs bacon" spam
, this method will returneggs bacon
on first invocation, then (after runningskipWhitespace()
,spam
on the second.- Returns:
- The parsed
String
- Throws:
ArgumentParseException
- if aString
could not be read
-
peekString
String peekString() throws ArgumentParseException
Returns the nextString
but does not advance the reader.This call will return the same result as
parseString()
. Calling this multiple times in succession will return the same result each time until anotherparse*
method orsetState(ArgumentReader)
is called.- Returns:
- The next string to be read
- Throws:
ArgumentParseException
- if aString
could not be read
-
parseBoolean
boolean parseBoolean() throws ArgumentParseException
Parses "true" or "false", else throws an exception.- Returns:
- A
boolean
- Throws:
ArgumentParseException
- if aboolean
could not be read
-
parseNBTString
String parseNBTString() throws ArgumentParseException
Parses a SNBT string, returning that string.When using this in your parser, you should set
ValueParser.clientCompletionType()
toClientCompletionTypes.SNBT
to tell the client that the client completion should be a SNBT string.- Returns:
- The string
- Throws:
ArgumentParseException
- if a SNBT string could not be read
-
parseDataContainer
DataContainer parseDataContainer() throws ArgumentParseException
Parses aDataContainer
, from a SNBT string, else throws an exception.When using this in your parser, you should set
ValueParser.clientCompletionType()
toClientCompletionTypes.SNBT
to tell the client that the client completion should be a string in Mojang's SNBT format.- Returns:
- A
JsonObject
- Throws:
ArgumentParseException
- if aJsonObject
could not be read
-
immutable
ArgumentReader.Immutable immutable()
Returns a immutable copy of theArgumentReader
. This can be used to get and restore the state of this reader when coupled withsetState(ArgumentReader)
.- Returns:
- An
ArgumentReader.Immutable
-
setState
void setState(ArgumentReader state) throws IllegalArgumentException
Attempts to reset the state of thisArgumentReader.Mutable
to the state that the providedArgumentReader
is in. This is generally used withimmutable()
.If the provided
state
does not have the sameArgumentReader.input()
, this will throw aIllegalArgumentException
- Parameters:
state
- The state to restore this to- Throws:
IllegalArgumentException
-
-