diff --git a/example/src/main/kotlin/net/bladehunt/kotstom/example/command/ParticleCommand.kt b/example/src/main/kotlin/net/bladehunt/kotstom/example/command/ParticleCommand.kt index 94d2fd5..83c4b18 100644 --- a/example/src/main/kotlin/net/bladehunt/kotstom/example/command/ParticleCommand.kt +++ b/example/src/main/kotlin/net/bladehunt/kotstom/example/command/ParticleCommand.kt @@ -1,5 +1,6 @@ package net.bladehunt.kotstom.example.command +import net.bladehunt.kotstom.command.invoke import net.bladehunt.kotstom.dsl.kommand.buildSyntax import net.bladehunt.kotstom.dsl.kommand.defaultExecutor import net.bladehunt.kotstom.dsl.kommand.kommand diff --git a/src/main/kotlin/net/bladehunt/kotstom/command/Argument.kt b/src/main/kotlin/net/bladehunt/kotstom/command/Argument.kt new file mode 100644 index 0000000..7dd139a --- /dev/null +++ b/src/main/kotlin/net/bladehunt/kotstom/command/Argument.kt @@ -0,0 +1,12 @@ +package net.bladehunt.kotstom.command + +import net.minestom.server.command.builder.CommandContext +import net.minestom.server.command.builder.arguments.Argument + +/** + * Retrieves the value of an argument from the `CommandContext` + * + * @author oglassdev + */ +context(context: CommandContext) +operator fun Argument.invoke(): T = context[this] diff --git a/src/main/kotlin/net/bladehunt/kotstom/command/KommandExecutorContext.kt b/src/main/kotlin/net/bladehunt/kotstom/command/KommandExecutorContext.kt deleted file mode 100644 index 1753655..0000000 --- a/src/main/kotlin/net/bladehunt/kotstom/command/KommandExecutorContext.kt +++ /dev/null @@ -1,20 +0,0 @@ -package net.bladehunt.kotstom.command - -import net.minestom.server.command.builder.CommandContext -import net.minestom.server.command.builder.arguments.Argument - -/** - * CommandContext wrapper that provides extension functions for Arguments - * - * @property context The command context - * @author oglassdev - */ -@JvmInline -value class KommandExecutorContext(private val context: CommandContext) { - /** - * Retrieves the value of an argument from the `CommandContext` - * - * @author oglassdev - */ - operator fun Argument.invoke(): T = context[this] -} diff --git a/src/main/kotlin/net/bladehunt/kotstom/coroutines/kommand/KommandExt.kt b/src/main/kotlin/net/bladehunt/kotstom/coroutines/kommand/KommandExt.kt index 3279fc8..d5f150c 100644 --- a/src/main/kotlin/net/bladehunt/kotstom/coroutines/kommand/KommandExt.kt +++ b/src/main/kotlin/net/bladehunt/kotstom/coroutines/kommand/KommandExt.kt @@ -5,7 +5,6 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking import net.bladehunt.kotstom.command.Kommand -import net.bladehunt.kotstom.command.KommandExecutorContext import net.bladehunt.kotstom.coroutines.MinestomDispatcher import net.bladehunt.kotstom.dsl.kommand.KommandDSL import net.minestom.server.command.CommandSender @@ -13,22 +12,7 @@ import net.minestom.server.command.builder.CommandContext import net.minestom.server.command.builder.CommandExecutor import kotlin.coroutines.CoroutineContext -internal typealias SuspendSingleKommandExecutor = suspend KommandExecutorContext.(CommandSender) -> Unit - -internal typealias SuspendKommandExecutor = suspend KommandExecutorContext.(CommandSender, CommandContext) -> Unit - -/** - * Sets the async default executor of the Kommand - * - * @param context The `CoroutineContext` to launch the coroutine in - * @param block The suspending code to run - * @author oglassdev - */ -@KommandDSL -inline fun Kommand.defaultExecutorAsync( - scope: CoroutineScope = CoroutineScope(Dispatchers.Default), - crossinline block: @KommandDSL SuspendSingleKommandExecutor -) = defaultExecutorAsync { sender, _ -> block(sender) } +internal typealias SuspendKommandExecutor = suspend context(CommandContext) (CommandSender) -> Unit /** * Sets the async default executor of the Kommand @@ -43,22 +27,10 @@ inline fun Kommand.defaultExecutorAsync( crossinline block: @KommandDSL SuspendKommandExecutor ) = CommandExecutor { sender, ctx -> - CoroutineScope(context).launch { KommandExecutorContext(ctx).block(sender, ctx) } + CoroutineScope(context).launch { block(ctx, sender) } } .also { defaultExecutor = it } -/** - * Sets the blocking default executor of the Kommand - * - * @param block The suspending code to run - * @author oglassdev - */ -@KommandDSL -inline fun Kommand.defaultExecutorBlocking( - coroutineContext: CoroutineContext = Dispatchers.Default, - crossinline block: @KommandDSL SuspendSingleKommandExecutor -) = defaultExecutorBlocking(coroutineContext) { sender, _ -> block(sender) } - /** * Sets the blocking default executor of the Kommand * @@ -71,6 +43,6 @@ inline fun Kommand.defaultExecutorBlocking( crossinline block: @KommandDSL SuspendKommandExecutor ) = CommandExecutor { sender, ctx -> - runBlocking(coroutineContext) { KommandExecutorContext(ctx).block(sender, ctx) } + runBlocking(coroutineContext) { block(ctx, sender) } } - .also { defaultExecutor = it } \ No newline at end of file + .also { defaultExecutor = it } diff --git a/src/main/kotlin/net/bladehunt/kotstom/coroutines/kommand/SyntaxExt.kt b/src/main/kotlin/net/bladehunt/kotstom/coroutines/kommand/SyntaxExt.kt index 666881a..7952207 100644 --- a/src/main/kotlin/net/bladehunt/kotstom/coroutines/kommand/SyntaxExt.kt +++ b/src/main/kotlin/net/bladehunt/kotstom/coroutines/kommand/SyntaxExt.kt @@ -3,26 +3,12 @@ package net.bladehunt.kotstom.coroutines.kommand import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking -import net.bladehunt.kotstom.command.KommandExecutorContext import net.bladehunt.kotstom.coroutines.MinestomDispatcher import net.bladehunt.kotstom.dsl.kommand.KommandDSL import net.bladehunt.kotstom.dsl.kommand.KommandSyntaxBuilder import net.minestom.server.command.builder.CommandExecutor import kotlin.coroutines.CoroutineContext -/** - * Sets the async executor for the command - * - * @param block The executor logic - * @return The command executor - * @author oglassdev - */ -@KommandDSL -inline fun KommandSyntaxBuilder.executorAsync( - context: CoroutineContext = MinestomDispatcher, - crossinline block: @KommandDSL SuspendSingleKommandExecutor -) = executorAsync(context) { sender, _ -> block(sender) } - /** * Sets the async executor for the command * @@ -36,22 +22,10 @@ inline fun KommandSyntaxBuilder.executorAsync( crossinline block: @KommandDSL SuspendKommandExecutor ) = CommandExecutor { sender, ctx -> - CoroutineScope(context).launch { KommandExecutorContext(ctx).block(sender, ctx) } + CoroutineScope(context).launch { block(ctx, sender) } } .also { executor = it } -/** - * Sets the blocking executor for the command - * - * @param block The executor logic - * @return The command executor - * @author oglassdev - */ -@KommandDSL -inline fun KommandSyntaxBuilder.executorBlocking( - crossinline block: @KommandDSL SuspendSingleKommandExecutor -) = executorBlocking { sender, _ -> block(sender) } - /** * Sets the blocking executor for the command * @@ -64,6 +38,6 @@ inline fun KommandSyntaxBuilder.executorBlocking( crossinline block: @KommandDSL SuspendKommandExecutor ) = CommandExecutor { sender, ctx -> - runBlocking { KommandExecutorContext(ctx).block(sender, ctx) } + runBlocking { block(ctx, sender) } } .also { executor = it } diff --git a/src/main/kotlin/net/bladehunt/kotstom/dsl/kommand/Kommand.kt b/src/main/kotlin/net/bladehunt/kotstom/dsl/kommand/Kommand.kt index f4380d8..1dd55d3 100644 --- a/src/main/kotlin/net/bladehunt/kotstom/dsl/kommand/Kommand.kt +++ b/src/main/kotlin/net/bladehunt/kotstom/dsl/kommand/Kommand.kt @@ -1,28 +1,16 @@ package net.bladehunt.kotstom.dsl.kommand import net.bladehunt.kotstom.command.Kommand -import net.bladehunt.kotstom.command.KommandExecutorContext import net.minestom.server.command.CommandSender import net.minestom.server.command.builder.CommandContext import net.minestom.server.command.builder.CommandExecutor -internal typealias SingleKommandExecutor = KommandExecutorContext.(CommandSender) -> Unit - -internal typealias KommandExecutor = KommandExecutorContext.(CommandSender, CommandContext) -> Unit +internal typealias KommandExecutor = context(CommandContext) (CommandSender) -> Unit @DslMarker @Target(AnnotationTarget.FUNCTION, AnnotationTarget.TYPE) annotation class KommandDSL -/** - * Sets the default executor of the Kommand - * - * @author oglassdev - */ -@KommandDSL -inline fun Kommand.defaultExecutor(crossinline block: @KommandDSL SingleKommandExecutor) = - defaultExecutor { sender, _ -> block(sender) } - /** * Sets the default executor of the Kommand * @@ -30,7 +18,7 @@ inline fun Kommand.defaultExecutor(crossinline block: @KommandDSL SingleKommandE */ @KommandDSL inline fun Kommand.defaultExecutor(crossinline block: @KommandDSL KommandExecutor) = - CommandExecutor { sender, context -> KommandExecutorContext(context).block(sender, context) } + CommandExecutor { sender, context -> block(context, sender) } .also { defaultExecutor = it } /** @@ -53,4 +41,4 @@ inline fun Kommand.subkommand(name: String, vararg aliases: String, block: @Komm */ @KommandDSL inline fun kommand(name: String, vararg aliases: String, block: @KommandDSL Kommand.() -> Unit): Kommand = - Kommand(name, *aliases).apply(block) \ No newline at end of file + Kommand(name, *aliases).apply(block) diff --git a/src/main/kotlin/net/bladehunt/kotstom/dsl/kommand/Syntax.kt b/src/main/kotlin/net/bladehunt/kotstom/dsl/kommand/Syntax.kt index 22806e7..3f071cb 100644 --- a/src/main/kotlin/net/bladehunt/kotstom/dsl/kommand/Syntax.kt +++ b/src/main/kotlin/net/bladehunt/kotstom/dsl/kommand/Syntax.kt @@ -1,7 +1,6 @@ package net.bladehunt.kotstom.dsl.kommand import net.bladehunt.kotstom.command.Kommand -import net.bladehunt.kotstom.command.KommandExecutorContext import net.bladehunt.kotstom.command.KommandSyntax import net.minestom.server.command.CommandSender import net.minestom.server.command.builder.CommandExecutor @@ -50,18 +49,6 @@ data class KommandSyntaxBuilder( conditions += condition } - /** - * Sets the executor for the command - * - * @param block The executor logic - * @return The command executor - * @author oglassdev - */ - @KommandDSL - inline fun executor( - crossinline block: @KommandDSL SingleKommandExecutor - ): CommandExecutor = executor { player, _ -> block(player) } - /** * Sets the executor for the command * @@ -73,7 +60,7 @@ data class KommandSyntaxBuilder( inline fun executor( crossinline block: @KommandDSL KommandExecutor ): CommandExecutor = - CommandExecutor { sender, ctx -> KommandExecutorContext(ctx).block(sender, ctx) } + CommandExecutor { sender, ctx -> block(ctx, sender) } .also { executor = it } /** @@ -151,12 +138,12 @@ inline fun Kommand.syntax( true }, defaultValues: Map> = emptyMap(), - crossinline block: @KommandDSL KommandExecutorContext.(CommandSender) -> Unit, + crossinline block: @KommandDSL KommandExecutor, ) = KommandSyntax( commandCondition = { sender, label -> condition(sender, label) }, commandExecutor = { sender, context -> - KommandExecutorContext(context).block(sender) + block(context, sender) }, defaultValues = defaultValues, args = arguments diff --git a/src/test/kotlin/net/bladehunt/kotstom/test/KommandTest.kt b/src/test/kotlin/net/bladehunt/kotstom/test/KommandTest.kt index 6965883..0a90721 100644 --- a/src/test/kotlin/net/bladehunt/kotstom/test/KommandTest.kt +++ b/src/test/kotlin/net/bladehunt/kotstom/test/KommandTest.kt @@ -3,6 +3,7 @@ package net.bladehunt.kotstom.test import io.kotest.core.spec.style.FunSpec import io.kotest.matchers.shouldBe import net.bladehunt.kotstom.CommandManager +import net.bladehunt.kotstom.command.invoke import net.bladehunt.kotstom.dsl.kommand.kommand import net.bladehunt.kotstom.dsl.kommand.subkommand import net.bladehunt.kotstom.dsl.kommand.syntax