-
Notifications
You must be signed in to change notification settings - Fork 0
Register Commands
createAndRegisterCommands {
command("test") {
description = "A test command"
addAlias("t")
permission = "test.test"
permissionMessage = "Permission denied!"
action { sender ->
sender sendMessage "Hello!"
CommandResult.Successful
}
subCommand("player") { sender, args ->
whenSenderIs<Player>(sender) {
world.spawnEntity<Pig>(location)
sendMessage(args.joinToString())
CommandResult.Successful
} otherwise {
sendMessage("You are not a player!")
CommandResult.Failed()
}
}
}
command("xxx"){
//...
}
}registerCommands function provides a DSL to register commands. You can register many commands and subcommand. Remember that this function should only be invoke once when your plugin enable.
command function represents that a registered command. It has following arguments:
- description (option)
- permission (option)
- alias (option)
- permission (option)
- permissionMessage (option)
- subCommand (option)
- action (still option XD)
As the code above, super command's action will be invoke only someone sends test or t. Subcommand is attached to super command, like this: test player. The subcommand--player will be invoke via this. Those return value is to tell the server whether this command invoked successfully (sender input proper arguments.. etc.).
whenSenderIs<Player>(sender) {
//action you want to do
CommandResult.Successful
} otherwise {
sendMessage("You are not a Player!")
//action to do if sender's type isn't expected type
CommandResult.Failed()
}This function provides a convenient way to deal with the situation that whether sender is what you wan't do action on. Please note that once you use whenSenderIs<> you should use otherwise to define the action if sender's type isn't expected type.