Skip to content

Register Commands

Potato Destroyer edited this page Mar 24, 2018 · 5 revisions

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

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<T:CommandSender>

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.

Clone this wiki locally