Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 12 additions & 0 deletions src/main/kotlin/net/bladehunt/kotstom/command/Argument.kt
Original file line number Diff line number Diff line change
@@ -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 <T> Argument<T>.invoke(): T = context[this]

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,14 @@ 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
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
Expand All @@ -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
*
Expand All @@ -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 }
.also { defaultExecutor = it }
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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
*
Expand All @@ -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 }
18 changes: 3 additions & 15 deletions src/main/kotlin/net/bladehunt/kotstom/dsl/kommand/Kommand.kt
Original file line number Diff line number Diff line change
@@ -1,36 +1,24 @@
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
*
* @author oglassdev
*/
@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 }

/**
Expand All @@ -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)
Kommand(name, *aliases).apply(block)
19 changes: 3 additions & 16 deletions src/main/kotlin/net/bladehunt/kotstom/dsl/kommand/Syntax.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
*
Expand All @@ -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 }

/**
Expand Down Expand Up @@ -151,12 +138,12 @@ inline fun Kommand.syntax(
true
},
defaultValues: Map<String, Function<CommandSender, Any>> = 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
Expand Down
1 change: 1 addition & 0 deletions src/test/kotlin/net/bladehunt/kotstom/test/KommandTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading