Understanding Essential’s automatic argument parsing system
The Command API’s most powerful feature is automatic argument parsing based on function parameters. Simply define parameters with the desired type, and Essential handles the rest.
Marks a parameter as greedy, consuming all remaining arguments:
@DefaultHandlerfun handle(target: String, @Greedy message: String) { // /msg Steve Hello, how are you? // target = "Steve" // message = "Hello, how are you?"}
The @Greedy annotation should only be used on the last parameter, as it consumes all remaining input.
@SubCommand("gamemode")fun gamemode(@Options(["survival", "creative", "adventure", "spectator"]) mode: String) { // Only allows the specified game modes}
This is particularly useful for nested subcommands or enum-like behavior.
When creating custom parsers, you work with ArgumentQueue:
interface ArgumentQueue { /** * Poll the next argument, removing it from the queue. * Throws an exception if no arguments are left. */ fun poll(): String /** * Peek at the next argument without removing it. * Returns null if no arguments are left. */ fun peek(): String? /** * Whether any more arguments have been passed. */ fun isEmpty(): Boolean}
class CoordinateParser : ArgumentParser<Coordinate> { override fun parse(arguments: ArgumentQueue, param: Parameter): Coordinate? { if (arguments.isEmpty()) return null val x = arguments.poll().toIntOrNull() ?: return null val y = arguments.poll().toIntOrNull() ?: return null val z = arguments.poll().toIntOrNull() ?: return null return Coordinate(x, y, z) }}