Delegated properties
The CommandAPI offers an additional way to access arguments when using Kotlin: delegated properties. With delegated properties, there are two possible dependencies you can use:
-
commandapi-core-kotlinSupport for delegated properties has been added to the
commandapi-core-kotlinmodule. If you want to use delegated properties, you need to add this dependency. -
commandapi-bukkit-kotlinIf you are already using the Kotlin DSL to create your commands, you can already use delegated properties.
commandapi-core-kotlinis included incommandapi-bukkit-kotlin.
Access arguments using delegated properties
To be able to access arguments by using delegated properties, your variable name needs to match the node name of the argument. This could look like this:
CommandAPICommand("mycommand")
.withArguments(StringArgument("string"))
.withArguments(PlayerArgument("target"))
.executesPlayer(PlayerCommandExecutor { player, args ->
val string: String by args
val target: Player by args
// Implementation...
})
.register()
commandAPICommand("mycommand") {
stringArgument("string")
playerArgument("target")
playerExecutor { player, args ->
val string: String by args
val target: Player by args
// Implementation...
}
}