Interface CommandInputStream<S extends Source>
- Type Parameters:
S- the type of the command source (e.g., Player, Console, CommandBlock) that initiated the command execution
The CommandInputStream 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 stream 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:
CommandInputStream<Source> stream = CommandInputStream.of(
ArgumentInput.of("player", "give", "diamond", "64"),
detectedUsage
);
// Navigate through parameters
while (stream.isCurrentParameterAvailable()) {
CommandParameter<Source> param = stream.currentParameterIfPresent();
String rawValue = stream.currentRawIfPresent();
// Process parameter...
stream.skipParameter();
}
Thread Safety: Implementations of this interface are not guaranteed to be thread-safe. External synchronization is required when accessing the same stream 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.copy()Creates a deep copy of this command input stream.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.default @NotNull Optional<CommandParameter<S>>Retrieves the command parameter at the current cursor position wrapped in an Optional container.@Nullable CommandParameter<S>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(CommandParameter<S> matchingFlagParameter) Marks the specified parameter as exempt from normal processing.@NotNull List<CommandParameter<S>>Retrieves the complete list of command parameters associated with this stream.@NotNull ArgumentInputRetrieves the underlying queue containing all raw input strings.booleanChecks if there is another character available for reading in the current input string.booleanChecks if there is a next command parameter available in the stream.booleanChecks if there is a next raw input string available in the stream.booleanChecks if there is a previous command parameter available in the stream.booleanChecks if there is a previous raw input string available in the stream.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.@Nullable CommandParameter<S>Advances the cursor to the next parameter and returns it.static <S extends Source>
CommandInputStream<S>of(ArgumentInput queue, CommandUsage<S> usage) Creates a newCommandInputStreaminstance with the specified raw arguments and command usage information.static <S extends Source>
CommandInputStream<S>ofSingleString(@NotNull CommandParameter<S> parameter, @NotNull String str) Creates a newCommandInputStreamwith a single string as input.default intRetrieves the total number of command parameters in the stream.Peeks at the next character without advancing the cursor.default Optional<CommandParameter<S>>Peeks at the next command parameter wrapped in an Optional container.@Nullable CommandParameter<S>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 StreamPosition<S>position()Retrieves the current cursor position within the input stream.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 stream.default StringReads and returns the current raw input without advancing the cursor.booleanskip()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 Source>
CommandInputStream<S>subStream(@NotNull CommandInputStream<S> stream, @NotNull String input) Creates a substream of the specified parent stream with new input content.
-
Method Details
-
position
Retrieves the current cursor position within the input stream. The position object contains cursors for all navigation levels (character, raw input, and parameter).- Returns:
- the current stream position, never
null
-
copy
CommandInputStream<S> copy()Creates a deep copy of this command input stream. The copy maintains independent cursor positions and can be navigated separately from the original stream.This method is useful for implementing backtracking parsers or for creating savepoints during complex parsing operations.
- Returns:
- a new
CommandInputStreaminstance that is an independent copy of this stream
-
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
Optional<CommandParameter<S>> 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
Optional<CommandParameter<S>> 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 stream. 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 stream. 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 stream.- Returns:
trueif a previous command parameter exists,falseotherwise
-
hasPreviousRaw
boolean hasPreviousRaw()Checks if there is a previous raw input string available in the stream.- 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 stream 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 stream.- Returns:
- the total count of command parameters, always non-negative
-
rawsLength
default int rawsLength()Retrieves the total number of raw input strings in the stream.- Returns:
- the total count of raw inputs, always non-negative
-
getParametersList
Retrieves the complete list of command parameters associated with this stream. 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 stream 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() -
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 stream 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
-
of
Creates a newCommandInputStreaminstance with the specified raw arguments and command usage information. The usage information determines how the raw arguments are mapped to typed command parameters.This factory method is the primary way to create command input streams from parsed command input and detected command structure.
- Type Parameters:
S- the type of command source- Parameters:
queue- the queue containing all raw argument stringsusage- the command usage definition that describes expected parameters- Returns:
- a new
CommandInputStreaminstance initialized with the provided data - Throws:
IllegalArgumentException- if queue or usage isnull
-
ofSingleString
static <S extends Source> CommandInputStream<S> ofSingleString(@NotNull @NotNull CommandParameter<S> parameter, @NotNull @NotNull String str) Creates a newCommandInputStreamwith a single string as input. This factory method is useful for creating streams 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
CommandInputStreaminstance containing the single input - Throws:
IllegalArgumentException- if parameter or str isnull
-
subStream
static <S extends Source> CommandInputStream<S> subStream(@NotNull @NotNull CommandInputStream<S> stream, @NotNull @NotNull String input) Creates a substream of the specified parent stream with new input content. The substream inherits the current parameter context from the parent stream 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:
stream- the parent command input stream to derive context frominput- the raw input string for the new substream- Returns:
- a new
CommandInputStreaminstance representing the substream - Throws:
NoSuchElementException- if the parent stream has no current parameter availableIllegalArgumentException- if stream or input isnull
-