Skip to content

Register Commands

Potato Destroyer / liuyipeng001 edited this page Feb 16, 2018 · 5 revisions

Register Commands

registerCommands {
    command("test") {
		description = "A test command"
		addAlias("t")
		permission = "test.test"
		permissionMessage = "Permission denied!"
		action { sender ->
			sender sendMessage "Hello!"
			true
		}
		subCommand("player") { sender, args ->
			sender.whenSenderIs<Player> {
				world.spawnEntity<Pig>(location)
				sendMessage(args.joinToString())
				true
			} otherwise {
				sendMessage("You are not a player!")
				false
			}
		}
	}
	command("xxx"){
		//...
	}
}

registerCommands function provides a DSL to register permission. 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 ot 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>

sender.whenSenderIs<Player> {
	//action you want to do
	true
} otherwise {
	sendMessage("You are not a Player!")
	//action to do if sender's type isn't expected type
	false
}

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