CommandAPI
CommandAPI copied to clipboard
Type-safe Kotlin DSL
Description
The current Kotlin DSL requires casts, so it is not type-safe. We can't get the most out of Kotlin.
commandTree("sendmessageto") { playerArgument("player") { greedyStringArgument("msg") { anyExecutor { _, args -> val player: Player = args["player"] as Player val message: String = args["msg"] as String player.sendMessage(message) } } } }
commandAPICommand("sendmessageto") { playerArgument("player") greedyStringArgument("msg") anyExecutor { _, args -> val player: Player = args["player"] as Player val message: String = args["msg"] as String player.sendMessage(message) } }
Expected code
commandTree("sendmessageto") {
playerArgument("player") { getPlayer ->
greedyStringArgument("msg") { getMessage ->
anyExecutor { _, args ->
val player = getPlayer(args) // Returns Player
val message = getMessage(args) // Returns String
player.sendMessage(message)
}
}
}
}
I have confirmed that this code can actually be rewritten. You can try it out using this library: https://github.com/sya-ri/commandapi-kotlin-improved
In my library, the Kotlin DSL for CommandAPICommand is not supported, but we should be able to rewrite it similarly.
Extra details
If necessary, I would like to submit a PR for the changes made by my library 😉