Interface Cursor<S extends CommandSource>
- Type Parameters:
S- the type of the command source (e.g., Player, Console, CommandBlock) that initiated the command execution
The Cursor operates on three distinct levels:
- Character level: Individual character access within the input string
- Raw input level: Space-separated argument strings
- Parameter level: Typed command parameters with associated metadata
The cursor maintains internal cursors for each level, allowing independent navigation through the input data. This design enables complex parsing scenarios such as:
- Lookahead parsing without consuming input
- Backtracking to previous positions
- Conditional input consumption based on validation
- Character-by-character parsing for complex argument formats
Usage Example:
Cursor<CommandSource> cursor = Cursor.of(
ArgumentInput.of("player", "give", "diamond", "64"),
detectedUsage
);
// Navigate through parameters
while (cursor.isCurrentParameterAvailable()) {
Argument<CommandSource> param = cursor.currentParameterIfPresent();
String rawValue = cursor.currentRawIfPresent();
// Process parameter...
cursor.skipParameter();
}
Thread Safety: Implementations of this interface are not guaranteed to be thread-safe. External synchronization is required when accessing the same cursor instance from multiple threads concurrently.
- Since:
- 1.0.0
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptiondefault StringcollectBeforeFirst(char c) Collects and returns all characters before the first occurrence of the specified character.collectRawArguments(int count) Collect a specific number of raw argumentsCollects and returns the remaining raw input strings from the current cursor position to the end of the input queue.copy()Creates a deep copy of this command input cursor.Retrieves the character at the current cursor position wrapped in an Optional container.@Nullable CharacterRetrieves the character at the current cursor position within the current raw input string without advancing the cursor.Retrieves the command parameter at the current cursor position wrapped in an Optional container.Retrieves the command parameter at the current cursor position without advancing the cursor.default intRetrieves the current position index of the parameter cursor.Retrieves the raw input string at the current cursor position wrapped in an Optional container.@Nullable StringRetrieves the raw input string at the current cursor position without advancing the cursor.default intRetrieves the current position index of the raw input cursor.default voidendInput()voidexemptParameter(Argument<S> matchingFlagParameter) Marks the specified parameter as exempt from normal processing.Retrieves the complete list of command parameters associated with this cursor.@NotNull ArgumentInputRetrieves the underlying queue containing all raw input strings.default booleanbooleanChecks if there is another character available for reading in the current input string.booleanChecks if there is a next command parameter available in the cursor.booleanChecks if there is a next raw input string available in the cursor.booleanChecks if there is a previous command parameter available in the cursor.booleanChecks if there is a previous raw input string available in the cursor.booleanChecks if there is a character available at the current cursor position within the current raw input string.booleanChecks if there is a command parameter available at the current cursor position.booleanChecks if there is a raw input string available at the current cursor position.@Nullable StringAdvances the cursor to the next raw input and returns it.Advances the cursor to the next parameter and returns it.static <S extends CommandSource>
Cursor<S>of(ArgumentInput queue, CommandPathway<S> pathway) Creates a newCursorinstance with the specified raw arguments and command pathway information.static <S extends CommandSource>
Cursor<S>ofSingleString(@NotNull Argument<S> parameter, @NotNull String str) Creates a newCursorwith a single string as input.default intRetrieves the total number of command parameters in the cursor.Peeks at the next character without advancing the cursor.Peeks at the next command parameter wrapped in an Optional container.Peeks at the next command parameter without advancing the cursor.peekRaw()Peeks at the next raw input string wrapped in an Optional container.@Nullable StringPeeks at the next raw input string without advancing the cursor.Removes and returns the current character while advancing the cursor.Removes and returns the current command parameter while advancing the cursor.popRaw()Removes and returns the current raw input while advancing the cursor.@NotNull CursorPosition<S>position()Retrieves the current cursor position within the input cursor.Retrieves the previous command parameter without moving the cursor.prevRaw()Retrieves the previous raw input string without moving the cursor.default intRetrieves the total number of raw input strings in the cursor.default StringReads and returns the current raw input without advancing the cursor.voidbooleanskip()Skips the current input element and advances the appropriate cursor.booleanSkips the current character and advances the character cursor.default booleanSkips the current command parameter and advances the parameter cursor.default booleanskipRaw()Skips the current raw input and advances the raw input cursor.default booleanskipTill(char target) Advances the character cursor until the specified target character is encountered.static <S extends CommandSource>
Cursor<S>Creates a subcursor of the specified parent cursor with new input content.
-
Method Details
-
of
Creates a newCursorinstance with the specified raw arguments and command pathway information. The pathway information determines how the raw arguments are mapped to typed command parameters.This factory method is the primary way to create command input cursors from parsed command input and detected command structure.
- Type Parameters:
S- the type of command source- Parameters:
queue- the queue containing all raw argument stringspathway- the command pathway definition that describes expected parameters- Returns:
- a new
Cursorinstance initialized with the provided data - Throws:
IllegalArgumentException- if queue or pathway isnull
-
ofSingleString
static <S extends CommandSource> Cursor<S> ofSingleString(@NotNull @NotNull Argument<S> parameter, @NotNull @NotNull String str) Creates a newCursorwith a single string as input. This factory method is useful for creating cursors for single-argument parsing or testing scenarios.- Type Parameters:
S- the type of command source- Parameters:
parameter- the command parameter associated with the input stringstr- the raw input string to be processed- Returns:
- a new
Cursorinstance containing the single input - Throws:
IllegalArgumentException- if parameter or str isnull
-
subCursor
static <S extends CommandSource> Cursor<S> subCursor(@NotNull @NotNull Cursor<S> cursor, @NotNull @NotNull String input) Creates a subcursor of the specified parent cursor with new input content. The subcursor inherits the current parameter context from the parent cursor but operates on the new input string.This method is useful for recursive parsing scenarios where a parameter value needs to be parsed as a nested command structure.
- Type Parameters:
S- the type of command source- Parameters:
cursor- the parent command input cursor to derive context frominput- the raw input string for the new subcursor- Returns:
- a new
Cursorinstance representing the subcursor - Throws:
NoSuchElementException- if the parent cursor has no current parameter availableIllegalArgumentException- if cursor or input isnull
-
position
Retrieves the current cursor position within the input cursor. The position object contains cursors for all navigation levels (character, raw input, and parameter).- Returns:
- the current cursor position, never
null
-
copy
Creates a deep copy of this command input cursor. The copy maintains independent cursor positions and can be navigated separately from the original cursor.This method is useful for implementing backtracking parsers or for creating savepoints during complex parsing operations.
- Returns:
- a new
Cursorinstance that is an independent copy of this cursor
-
currentParameterIfPresent
Retrieves the command parameter at the current cursor position without advancing the cursor.- Returns:
- the current command parameter, or
nullif the cursor is beyond the end of the parameter list or no parameter exists
-
currentParameter
Retrieves the command parameter at the current cursor position wrapped in an Optional container.- Returns:
- an
Optionalcontaining the current command parameter, orOptional.empty()if none exists
-
currentRawIfPresent
Retrieves the raw input string at the current cursor position without advancing the cursor.- Returns:
- the current raw input string, or
nullif the cursor is beyond the end of the input queue or no input exists
-
currentRaw
Retrieves the raw input string at the current cursor position wrapped in an Optional container.- Returns:
- an
Optionalcontaining the current raw input, orOptional.empty()if none exists
-
currentLetterIfPresent
Retrieves the character at the current cursor position within the current raw input string without advancing the cursor.- Returns:
- the current character, or
nullif the cursor is beyond the end of the current input string or no character exists
-
currentLetter
Retrieves the character at the current cursor position wrapped in an Optional container.- Returns:
- an
Optionalcontaining the current character, orOptional.empty()if none exists
-
peekParameterIfPresent
Peeks at the next command parameter without advancing the cursor. This method allows lookahead parsing to determine the next parameter type without consuming it.- Returns:
- the next command parameter, or
nullif no next parameter exists
-
peekParameter
Peeks at the next command parameter wrapped in an Optional container.- Returns:
- an
Optionalcontaining the next command parameter, orOptional.empty()if none exists
-
peekRawIfPresent
Peeks at the next raw input string without advancing the cursor. This method allows lookahead parsing to examine the next input without consuming it.- Returns:
- the next raw input string, or
nullif no next input exists
-
peekRaw
Peeks at the next raw input string wrapped in an Optional container.- Returns:
- an
Optionalcontaining the next raw input, orOptional.empty()if none exists
-
peekLetter
Peeks at the next character without advancing the cursor. This method allows character-level lookahead parsing.- Returns:
- an
Optionalcontaining the next character, orOptional.empty()if none exists
-
prevParameter
Retrieves the previous command parameter without moving the cursor. This method allows backward navigation through the parameter list.- Returns:
- an
Optionalcontaining the previous command parameter, orOptional.empty()if no previous parameter exists
-
prevRaw
Retrieves the previous raw input string without moving the cursor. This method allows backward navigation through the input queue.- Returns:
- an
Optionalcontaining the previous raw input, orOptional.empty()if no previous input exists
-
nextParameter
Advances the cursor to the next parameter and returns it. This method consumes the parameter and moves the cursor forward.- Returns:
- the next command parameter, or
nullif no more parameters exist
-
nextInput
Advances the cursor to the next raw input and returns it. This method consumes the input and moves the cursor forward.- Returns:
- the next raw input string, or
nullif no more input exists
-
popParameter
Removes and returns the current command parameter while advancing the cursor. This method is equivalent to callingcurrentParameter()followed byskipParameter().- Returns:
- an
Optionalcontaining the popped command parameter, orOptional.empty()if none exists
-
popRaw
Removes and returns the current raw input while advancing the cursor. This method is equivalent to callingcurrentRaw()followed byskipRaw().- Returns:
- an
Optionalcontaining the popped raw input, orOptional.empty()if none exists
-
popLetter
Removes and returns the current character while advancing the cursor. This method is useful for character-by-character parsing of complex argument formats.- Returns:
- an
Optionalcontaining the popped character, orOptional.empty()if none exists
-
isCurrentParameterAvailable
boolean isCurrentParameterAvailable()Checks if there is a command parameter available at the current cursor position.- Returns:
trueif current parameter is available,falseotherwise
-
isCurrentRawInputAvailable
boolean isCurrentRawInputAvailable()Checks if there is a raw input string available at the current cursor position.- Returns:
trueif current raw input is available,falseotherwise
-
isCurrentLetterAvailable
boolean isCurrentLetterAvailable()Checks if there is a character available at the current cursor position within the current raw input string.- Returns:
trueif current character is available,falseotherwise
-
hasNextParameter
boolean hasNextParameter()Checks if there is a next command parameter available in the cursor. This method allows checking for the availability of the next parameter without consuming or peeking at it.- Returns:
trueif a next command parameter exists,falseotherwise
-
hasNextRaw
boolean hasNextRaw()Checks if there is a next raw input string available in the cursor. This method allows checking for the availability of the next input without consuming or peeking at it.- Returns:
trueif a next raw input exists,falseotherwise
-
hasNextLetter
boolean hasNextLetter()Checks if there is another character available for reading in the current input string.- Returns:
trueif a next character exists,falseotherwise
-
hasPreviousParameter
boolean hasPreviousParameter()Checks if there is a previous command parameter available in the cursor.- Returns:
trueif a previous command parameter exists,falseotherwise
-
hasPreviousRaw
boolean hasPreviousRaw()Checks if there is a previous raw input string available in the cursor.- Returns:
trueif a previous raw input exists,falseotherwise
-
skip
boolean skip()Skips the current input element and advances the appropriate cursor. The specific behavior depends on the current cursor position and available input types.- Returns:
trueif the skip operation was successful,falseif no input was available to skip
-
skipParameter
default boolean skipParameter()Skips the current command parameter and advances the parameter cursor. This method moves to the next typed parameter in the parameter list.- Returns:
trueif the skip operation was successful,falseif no parameter was available to skip
-
skipRaw
default boolean skipRaw()Skips the current raw input and advances the raw input cursor. This method moves to the next space-separated argument in the input queue.- Returns:
trueif the skip operation was successful,falseif no raw input was available to skip
-
skipLetter
boolean skipLetter()Skips the current character and advances the character cursor. This method is used for character-level navigation within input strings.- Returns:
trueif the skip operation was successful,falseif no character was available to skip
-
currentParameterPosition
default int currentParameterPosition()Retrieves the current position index of the parameter cursor.- Returns:
- the zero-based index of the current parameter position
-
currentRawPosition
default int currentRawPosition()Retrieves the current position index of the raw input cursor.- Returns:
- the zero-based index of the current raw input position
-
parametersLength
default int parametersLength()Retrieves the total number of command parameters in the cursor.- Returns:
- the total count of command parameters, always non-negative
-
rawsLength
default int rawsLength()Retrieves the total number of raw input strings in the cursor.- Returns:
- the total count of raw inputs, always non-negative
-
getParametersList
Retrieves the complete list of command parameters associated with this cursor. This provides access to all parameter metadata for validation or processing.- Returns:
- an immutable
Listof command parameters, nevernull
-
getRawQueue
Retrieves the underlying queue containing all raw input strings. This provides access to the complete input data for batch operations or cursor analysis.- Returns:
- the
ArgumentInputcontaining all raw inputs, nevernull
-
skipTill
default boolean skipTill(char target) Advances the character cursor until the specified target character is encountered. The target character is also consumed (skipped) by this operation.This method is useful for parsing delimited strings or finding specific separators within command arguments.
- Parameters:
target- the character to search for and skip to- Returns:
trueif the target character was found and reached,falseif the end of input was reached without finding the target
-
collectBeforeFirst
Collects and returns all characters before the first occurrence of the specified character. The character cursor is advanced to the position just before the target character. The target character itself is not consumed or included in the result.This method is useful for extracting string tokens that are delimited by specific characters.
- Parameters:
c- the delimiter character to stop collection at- Returns:
- a string containing all characters collected before the delimiter, or an empty string if the delimiter is immediately encountered or no characters are available
-
readInput
Reads and returns the current raw input without advancing the cursor. This method provides a fail-fast alternative tocurrentRawIfPresent()when the presence of input is guaranteed.- Returns:
- the current raw input string, never
null - Throws:
NoSuchElementException- if no raw input is available at the current position
-
endInput
default void endInput() -
collectRemainingRaw
String collectRemainingRaw()Collects and returns the remaining raw input strings from the current cursor position to the end of the input queue. The raw input cursor is advanced to the end of the queue after this operation.- Returns:
- a string containing all remaining raw inputs concatenated with spaces, or an empty string if no remaining input exists
-
collectRawArguments
Collect a specific number of raw arguments- Parameters:
count- the number of raw input args to collect- Returns:
- A string separating the collected args by space.
-
exemptParameter
Marks the specified parameter as exempt from normal processing. Exempt parameters are typically flag parameters that have been handled separately from the main argument processing flow.This method allows the cursor to track which parameters have been processed outside the normal sequential flow, enabling proper validation and error reporting.
- Parameters:
matchingFlagParameter- the parameter to mark as exempt- Throws:
IllegalArgumentException- if matchingFlagParameter isnull
-
hasFinished
default boolean hasFinished() -
setAt
-