Skip to content

Type-safe Kotlin DSL #544

Open
Open
@sya-ri

Description

@sya-ri

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)
    }
}

from: Defining a simple message command

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 😉

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions