DevSrSouza / KotlinBukkitAPI

KotlinBukkitAPI is an API for Bukkit/SpigotAPI using the cool and nifty features Kotlin has to make your life more easier.
MIT License
154 stars 20 forks source link

Support invoke #20

Open smallshen opened 3 years ago

smallshen commented 3 years ago

There is a operator fun called "invoke"

after simply adding

operator fun <T> T.invoke(block : T.() -> Unit) : T {
    return this.apply(block)
}

could achieve

player {
    displayName = "name"
    msg("message")
    etc...
}
sender {
    msg("anything")
}
event<PlayerMoveEvent> {
    isCancelled = true
    player {
        msg("You can move yet")
        etc...
    }
}

makes code cleaner, get rid of with(player) or player.apply { }

it works for anything after that three lines but might effect some classes that already have invoke fun

val component = ComponentBuilder()
component {
    append("wow")
    etc...
}

it looks like the component in net.md_5.bungee.api.chat.ComponentBuilder having a dsl support but actually no

smallshen commented 3 years ago

one thing forget, It doesn't works fine with some class that has val constructer

DevSrSouza commented 3 years ago

I don't think that use a Generic Typed invoke is a good pratice.

About the ComponentBuilder, I will work on it, is something that could be more clean using DSL.

smallshen commented 3 years ago

I think have a dsl support for https://github.com/KyoriPowered/adventure would be great. Since it have Bungeecord, Velocity, bukkit etc support.