Interface IArgConsumer
IArgConsumer is how ICommands read the arguments passed to them. This class has many benefits:
- Mutability. The whole concept of the
IArgConsumer} is to let you gradually consume arguments in any way you'd like. You can change your consumption based on earlier arguments, for subcommands for example. - You don't need to keep track of your consumption. The
IArgConsumer} keeps track of the arguments you consume so that it can throw detailed exceptions whenever something is out of the ordinary. Additionally, if you need to retrieve an argument after you've already consumed it - look no further thanconsumed()! - Easy retrieval of many different types. If you need to retrieve an instance of an int or float for example,
look no further than
getAs(Class). If you need a more powerful way of retrieving data, try out the manygetDatatype...methods. - It's very easy to throw detailed exceptions. The
IArgConsumer} has many different methods that can enforce the number of arguments, the type of arguments, and more, throwing different types ofCommandExceptions if something seems off. You're recommended to do all validation and store all needed data in variables BEFORE logging any data to chat viaHelper.logDirect(String), so that the error handlers can do their job and log the error to chat.
-
Method Summary
Modifier and TypeMethodDescriptionconsumed()copy()get()Gets the next argument and returns it.getArgs()<T> TTries to use a statelessIArgParserto parse the next argument into the specified class<T> TgetAsOrDefault(Class<T> type, T def) Tries to use a statelessIArgParserto parse the next argument into the specified class<T> TgetAsOrNull(Class<T> type) Tries to use a statelessIArgParserto parse the next argument into the specified class<T,D extends IDatatypeFor<T>>
TgetDatatypeFor(D datatype) <T,D extends IDatatypeFor<T>>
TgetDatatypeForOrDefault(D datatype, T def) <T,D extends IDatatypeFor<T>>
TgetDatatypeForOrNull(D datatype) <T,O, D extends IDatatypePost<T, O>>
TgetDatatypePost(D datatype, O original) <T,O, D extends IDatatypePost<T, O>>
TgetDatatypePostOrDefault(D datatype, O original, T _default) <T,O, D extends IDatatypePost<T, O>>
TgetDatatypePostOrNull(D datatype, O original) <E extends Enum<?>>
EGets an enum value from the enum class with the same name as the next argument's value<E extends Enum<?>>
EgetEnumOrDefault(Class<E> enumClass, E def) Gets an enum value from the enum class with the same name as the next argument's value<E extends Enum<?>>
EgetEnumOrNull(Class<E> enumClass) Gets an enum value from the enum class with the same name as the next argument's valueGets the value of the next argument and returns it.booleanhas(int num) booleanhasAny()booleanhasAtMost(int num) booleanbooleanbooleanhasExactly(int num) booleanbooleanbooleanpeek()peek(int index) <T> TTries to use a statelessIArgParserto parse the next argument into the specified class<T> TTries to use a statelessIArgParserto parse the argument at the specified index into the specified class<T> TpeekAsOrDefault(Class<T> type, T def) Tries to use a statelessIArgParserto parse the next argument into the specified class<T> TpeekAsOrDefault(Class<T> type, T def, int index) Tries to use a statelessIArgParserto parse the argument at the specified index into the specified class<T> TpeekAsOrNull(Class<T> type) Tries to use a statelessIArgParserto parse the next argument into the specified class<T> TpeekAsOrNull(Class<T> type, int index) Tries to use a statelessIArgParserto parse the argument at the specified index into the specified class<T> TpeekDatatype(IDatatypeFor<T> datatype) <T,O> T peekDatatype(IDatatypePost<T, O> datatype) <T,O> T peekDatatype(IDatatypePost<T, O> datatype, O original) <T,D extends IDatatypeFor<T>>
TpeekDatatypeFor(Class<D> datatype) Attempts to get the specifiedIDatatypeForfrom this ArgConsumer<T,D extends IDatatypeFor<T>>
TpeekDatatypeForOrDefault(Class<D> datatype, T def) Attempts to get the specifiedIDatatypeForfrom this ArgConsumer<T,D extends IDatatypeFor<T>>
TpeekDatatypeForOrNull(Class<D> datatype) Attempts to get the specifiedIDatatypeForfrom this ArgConsumer<T> TpeekDatatypeOrNull(IDatatypeFor<T> datatype) <T,O> T peekDatatypeOrNull(IDatatypePost<T, O> datatype) <T,O, D extends IDatatypePost<T, O>>
TpeekDatatypePost(D datatype, O original) <T,O, D extends IDatatypePost<T, O>>
TpeekDatatypePostOrDefault(D datatype, O original, T def) <T,O, D extends IDatatypePost<T, O>>
TpeekDatatypePostOrNull(D datatype, O original) <E extends Enum<?>>
E<E extends Enum<?>>
E<E extends Enum<?>>
EpeekEnumOrNull(Class<E> enumClass) <E extends Enum<?>>
EpeekEnumOrNull(Class<E> enumClass, int index) peekString(int index) rawRest()Returns the "raw rest" of the string.voidrequireExactly(int args) voidrequireMax(int max) voidrequireMin(int min) tabCompleteDatatype(T datatype)
-
Method Details
-
getArgs
-
getConsumed
-
has
- Parameters:
num- The number of arguments to check for- Returns:
trueif there are at leastnumarguments left in thisIArgConsumer}- See Also:
-
hasAny
boolean hasAny()- Returns:
trueif there is at least 1 argument left in thisIArgConsumer}- See Also:
-
hasAtMost
- Parameters:
num- The number of arguments to check for- Returns:
trueif there are at mostnumarguments left in thisIArgConsumer}- See Also:
-
hasAtMostOne
boolean hasAtMostOne()- Returns:
trueif there is at most 1 argument left in thisIArgConsumer}- See Also:
-
hasExactly
- Parameters:
num- The number of arguments to check for- Returns:
trueif there are exactlynumarguments left in thisIArgConsumer}- See Also:
-
hasExactlyOne
boolean hasExactlyOne()- Returns:
trueif there is exactly 1 argument left in thisIArgConsumer}- See Also:
-
peek
- Parameters:
index- The index to peek- Returns:
- The argument at index
indexin thisIArgConsumer}, with 0 being the next one. This does not mutate theIArgConsumer} - Throws:
CommandNotEnoughArgumentsException- If there is less thanindex + 1arguments left- See Also:
-
peek
- Returns:
- The next argument in this
IArgConsumer}. This does not mutate theIArgConsumer} - Throws:
CommandNotEnoughArgumentsException- If there is less than one argument left- See Also:
-
is
- Parameters:
type- The type to check forindex- The index to peek- Returns:
- If an ArgParser.Stateless for the specified
typewould succeed in parsing the next argument - Throws:
CommandNotEnoughArgumentsException- If there is less thanindex + 1arguments left- See Also:
-
is
- Parameters:
type- The type to check for- Returns:
- If an ArgParser.Stateless for the specified
typewould succeed in parsing the next argument - Throws:
CommandNotEnoughArgumentsException- If there is less than one argument left- See Also:
-
peekString
- Parameters:
index- The index to peek- Returns:
- The value of the argument at index
indexin thisIArgConsumer}, with 0 being the next one This does not mutate theIArgConsumer} - Throws:
CommandNotEnoughArgumentsException- If there is less thanindex + 1arguments left- See Also:
-
peekString
- Returns:
- The value of the next argument in this
IArgConsumer}. This does not mutate theIArgConsumer} - Throws:
CommandNotEnoughArgumentsException- If there is less than one argument left- See Also:
-
peekEnum
<E extends Enum<?>> E peekEnum(Class<E> enumClass, int index) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException - Parameters:
enumClass- The class to searchindex- The index to peek- Returns:
- From the specified enum class, an enum constant of that class. The enum constant's name will match the next argument's value
- Throws:
NoSuchElementException- If the constant couldn't be foundCommandInvalidTypeExceptionCommandNotEnoughArgumentsException- See Also:
-
peekEnum
<E extends Enum<?>> E peekEnum(Class<E> enumClass) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException - Parameters:
enumClass- The class to search- Returns:
- From the specified enum class, an enum constant of that class. The enum constant's name will match the next argument's value
- Throws:
CommandInvalidTypeException- If the constant couldn't be foundCommandNotEnoughArgumentsException- See Also:
-
peekEnumOrNull
<E extends Enum<?>> E peekEnumOrNull(Class<E> enumClass, int index) throws CommandNotEnoughArgumentsException - Parameters:
enumClass- The class to searchindex- The index to peek- Returns:
- From the specified enum class, an enum constant of that class. The enum constant's name will match the next argument's value. If no constant could be found, null
- Throws:
CommandNotEnoughArgumentsException- See Also:
-
peekEnumOrNull
- Parameters:
enumClass- The class to search- Returns:
- From the specified enum class, an enum constant of that class. The enum constant's name will match the next argument's value. If no constant could be found, null
- Throws:
CommandNotEnoughArgumentsException- See Also:
-
peekAs
<T> T peekAs(Class<T> type, int index) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException Tries to use a statelessIArgParserto parse the argument at the specified index into the specified classA critical difference between
IDatatypes andIArgParsers is how many arguments they can take. WhileIArgParsers always operate on a single argument's value,IDatatypes get access to the entireIArgConsumer}.- Parameters:
type- The type to peek asindex- The index to peek- Returns:
- An instance of the specified type
- Throws:
CommandInvalidTypeException- If the parsing failedCommandNotEnoughArgumentsException- See Also:
-
peekAs
Tries to use a statelessIArgParserto parse the next argument into the specified classA critical difference between
IDatatypes andIArgParsers is how many arguments they can take. WhileIArgParsers always operate on a single argument's value,IDatatypes get access to the entireIArgConsumer}.- Parameters:
type- The type to peek as- Returns:
- An instance of the specified type
- Throws:
CommandInvalidTypeException- If the parsing failedCommandNotEnoughArgumentsException- See Also:
-
peekAsOrDefault
Tries to use a statelessIArgParserto parse the argument at the specified index into the specified classA critical difference between
IDatatypes andIArgParsers is how many arguments they can take. WhileIArgParsers always operate on a single argument's value,IDatatypes get access to the entireIArgConsumer}.- Parameters:
type- The type to peek asdef- The value to return if the argument can't be parsedindex- The index to peek- Returns:
- An instance of the specified type, or
defif it couldn't be parsed - Throws:
CommandNotEnoughArgumentsException- See Also:
-
peekAsOrDefault
Tries to use a statelessIArgParserto parse the next argument into the specified classA critical difference between
IDatatypes andIArgParsers is how many arguments they can take. WhileIArgParsers always operate on a single argument's value,IDatatypes get access to the entireIArgConsumer}.- Parameters:
type- The type to peek asdef- The value to return if the argument can't be parsed- Returns:
- An instance of the specified type, or
defif it couldn't be parsed - Throws:
CommandNotEnoughArgumentsException- See Also:
-
peekAsOrNull
Tries to use a statelessIArgParserto parse the argument at the specified index into the specified classA critical difference between
IDatatypes andIArgParsers is how many arguments they can take. WhileIArgParsers always operate on a single argument's value,IDatatypes get access to the entireIArgConsumer}.- Parameters:
type- The type to peek asindex- The index to peek- Returns:
- An instance of the specified type, or
nullif it couldn't be parsed - Throws:
CommandNotEnoughArgumentsException- See Also:
-
peekAsOrNull
Tries to use a statelessIArgParserto parse the next argument into the specified classA critical difference between
IDatatypes andIArgParsers is how many arguments they can take. WhileIArgParsers always operate on a single argument's value,IDatatypes get access to the entireIArgConsumer}.- Parameters:
type- The type to peek as- Returns:
- An instance of the specified type, or
nullif it couldn't be parsed - Throws:
CommandNotEnoughArgumentsException- See Also:
-
peekDatatype
<T> T peekDatatype(IDatatypeFor<T> datatype) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException -
peekDatatype
<T,O> T peekDatatype(IDatatypePost<T, O> datatype) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException -
peekDatatype
<T,O> T peekDatatype(IDatatypePost<T, O> datatype, O original) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException -
peekDatatypeOrNull
-
peekDatatypeOrNull
-
peekDatatypePost
<T,O, T peekDatatypePostD extends IDatatypePost<T, O>> (D datatype, O original) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException -
peekDatatypePostOrDefault
-
peekDatatypePostOrNull
-
peekDatatypeFor
Attempts to get the specifiedIDatatypeForfrom this ArgConsumerA critical difference between
IDatatypes andIArgParsers is how many arguments they can take. WhileIArgParsers always operate on a single argument's value,IDatatypes get access to the entireIArgConsumer}.Since this is a peek operation, this ArgConsumer will not be mutated by any call to this method.
- Parameters:
datatype- The datatype to get- Returns:
- The datatype instance
- See Also:
-
peekDatatypeForOrDefault
Attempts to get the specifiedIDatatypeForfrom this ArgConsumerA critical difference between
IDatatypes andIArgParsers is how many arguments they can take. WhileIArgParsers always operate on a single argument's value,IDatatypes get access to the entireIArgConsumer}.Since this is a peek operation, this ArgConsumer will not be mutated by any call to this method.
- Parameters:
datatype- The datatype to getdef- The default value- Returns:
- The datatype instance, or
defif it throws an exception - See Also:
-
peekDatatypeForOrNull
Attempts to get the specifiedIDatatypeForfrom this ArgConsumerA critical difference between
IDatatypes andIArgParsers is how many arguments they can take. WhileIArgParsers always operate on a single argument's value,IDatatypes get access to the entireIArgConsumer}.Since this is a peek operation, this ArgConsumer will not be mutated by any call to this method.
- Parameters:
datatype- The datatype to get- Returns:
- The datatype instance, or
nullif it throws an exception - See Also:
-
get
Gets the next argument and returns it. This consumes the first argument so that subsequent calls will return later arguments- Returns:
- The next argument
- Throws:
CommandNotEnoughArgumentsException- If there's less than one argument left
-
getString
Gets the value of the next argument and returns it. This consumes the first argument so that subsequent calls will return later arguments- Returns:
- The value of the next argument
- Throws:
CommandNotEnoughArgumentsException- If there's less than one argument left
-
getEnum
<E extends Enum<?>> E getEnum(Class<E> enumClass) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException Gets an enum value from the enum class with the same name as the next argument's valueFor example if you getEnum as an
Direction, and the next argument's value is "up", this will returnDirection.UP- Parameters:
enumClass- The enum class to search- Returns:
- An enum constant of that class with the same name as the next argument's value
- Throws:
CommandInvalidTypeException- If the constant couldn't be foundCommandNotEnoughArgumentsException- See Also:
-
getEnumOrDefault
<E extends Enum<?>> E getEnumOrDefault(Class<E> enumClass, E def) throws CommandNotEnoughArgumentsException Gets an enum value from the enum class with the same name as the next argument's valueFor example if you getEnum as an
Direction, and the next argument's value is "up", this will returnDirection.UP- Parameters:
enumClass- The enum class to searchdef- The default value- Returns:
- An enum constant of that class with the same name as the next argument's value, or
defif it couldn't be found - Throws:
CommandNotEnoughArgumentsException- See Also:
-
getEnumOrNull
Gets an enum value from the enum class with the same name as the next argument's valueFor example if you getEnum as an
Direction, and the next argument's value is "up", this will returnDirection.UP- Parameters:
enumClass- The enum class to search- Returns:
- An enum constant of that class with the same name as the next argument's value, or
nullif it couldn't be found - Throws:
CommandNotEnoughArgumentsException- See Also:
-
getAs
Tries to use a statelessIArgParserto parse the next argument into the specified classA critical difference between
IDatatypes andIArgParsers is how many arguments they can take. WhileIArgParsers always operate on a single argument's value,IDatatypes get access to the entireIArgConsumer}.- Parameters:
type- The type to peek as- Returns:
- An instance of the specified type
- Throws:
CommandInvalidTypeException- If the parsing failedCommandNotEnoughArgumentsException- See Also:
-
getAsOrDefault
Tries to use a statelessIArgParserto parse the next argument into the specified classA critical difference between
IDatatypes andIArgParsers is how many arguments they can take. WhileIArgParsers always operate on a single argument's value,IDatatypes get access to the entireIArgConsumer}.- Parameters:
type- The type to peek asdef- The default value- Returns:
- An instance of the specified type, or
defif it couldn't be parsed - Throws:
CommandNotEnoughArgumentsException- See Also:
-
getAsOrNull
Tries to use a statelessIArgParserto parse the next argument into the specified classA critical difference between
IDatatypes andIArgParsers is how many arguments they can take. WhileIArgParsers always operate on a single argument's value,IDatatypes get access to the entireIArgConsumer}.- Parameters:
type- The type to peek as- Returns:
- An instance of the specified type, or
nullif it couldn't be parsed - Throws:
CommandNotEnoughArgumentsException- See Also:
-
getDatatypePost
<T,O, T getDatatypePostD extends IDatatypePost<T, O>> (D datatype, O original) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException -
getDatatypePostOrDefault
<T,O, T getDatatypePostOrDefaultD extends IDatatypePost<T, O>> (D datatype, O original, T _default) -
getDatatypePostOrNull
-
getDatatypeFor
<T,D extends IDatatypeFor<T>> T getDatatypeFor(D datatype) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException -
getDatatypeForOrDefault
-
getDatatypeForOrNull
-
tabCompleteDatatype
-
rawRest
Returns the "raw rest" of the string. For example, from a stringarg1 arg2 arg3, split into threeICommandArguments"arg1","arg2", and"arg3":- Returns:
- The "raw rest" of the string.
-
requireMin
- Parameters:
min- The minimum amount of arguments to require.- Throws:
CommandNotEnoughArgumentsException- If there are less thanminarguments left.- See Also:
-
requireMax
- Parameters:
max- The maximum amount of arguments allowed.- Throws:
CommandTooManyArgumentsException- If there are more thanmaxarguments left.- See Also:
-
requireExactly
- Parameters:
args- The exact amount of arguments to require.- Throws:
CommandNotEnoughArgumentsException- If there are less thanargsarguments left.CommandTooManyArgumentsException- If there are more thanargsarguments left.CommandException- See Also:
-
hasConsumed
boolean hasConsumed()- Returns:
- If this
IArgConsumer} has consumed at least one argument. - See Also:
-
consumed
- Returns:
- The last argument this
IArgConsumer} has consumed, or an "unknown" argument, indicated by a comamnd argument index that has a value of-1, if no arguments have been consumed yet. - See Also:
-
consumedString
- Returns:
- The value of thelast argument this
IArgConsumer} has consumed, or an empty string if no arguments have been consumed yet - See Also:
-
copy
- Returns:
- A copy of this
IArgConsumer}. It has the same arguments (both consumed and not), but does not affect or mutate this instance. Useful for the variouspeekfunctions
-