The @DefaultHandler annotation marks a function to be invoked when:
The command has no subcommands, OR
The user didn’t specify a subcommand
If no @DefaultHandler is specified and no subcommand is matched, the command’s usage will be printed instead.
object MyCommand : Command("mycommand") { @DefaultHandler fun handle() { // Called when user types: /mycommand } @SubCommand("test") fun test() { // Called when user types: /mycommand test }}
Whether to automatically generate a help subcommand (i.e., /$name help) that shows all available subcommands, their usages, and descriptions.Default: true
object MyCommand : Command( name = "mycommand", autoHelpSubcommand = true)
Define global aliases using the commandAliases property:
object MyCommand : Command("mycommand") { override val commandAliases = setOf( Alias("mc"), Alias("mycmd", hideFromAutocomplete = true) ) @DefaultHandler fun handle() { // Can be invoked with /mycommand, /mc, or /mycmd }}
Each Alias can optionally be hidden from autocomplete:
data class Alias( val alias: String, val hideFromAutocomplete: Boolean = false)
import gg.essential.api.commands.*object TeleportCommand : Command( name = "tp", autoHelpSubcommand = true) { override val commandAliases = setOf( Alias("teleport") ) @DefaultHandler fun handle(@DisplayName("player") target: String) { // Teleport to player println("Teleporting to $target") } @SubCommand("coords", description = "Teleport to coordinates") fun coords(x: Int, y: Int, z: Int) { println("Teleporting to $x, $y, $z") } @SubCommand("random", description = "Teleport to random location") fun random(radius: Int?) { val r = radius ?: 1000 println("Teleporting to random location within $r blocks") }}// Register the commandfun init() { TeleportCommand.register()}