Skip to content

Commit 57a529b

Browse files
committed
feature: state and reply functions now inline
for the use with coroutines
1 parent 1a2f0b0 commit 57a529b

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

src/main/kotlin/dev/fruxz/brigadikt/CommandFactory.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ object CommandFactory {
5757
raw = context,
5858
path = branch.buildNamePath(),
5959
replyRenderer = branch.chatRenderer,
60-
state = { state, process -> resultState = state; process() }
60+
state = { state -> resultState = state; }
6161
)
6262
)
6363

src/main/kotlin/dev/fruxz/brigadikt/Execution.kt

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ package dev.fruxz.brigadikt
44

55
import com.mojang.brigadier.Command
66
import com.mojang.brigadier.context.CommandContext
7-
import dev.fruxz.ascend.extension.forceCastOrNull
87
import dev.fruxz.ascend.extension.isNotNull
98
import dev.fruxz.ascend.extension.logging.getItsLogger
109
import dev.fruxz.ascend.extension.switch
@@ -20,7 +19,6 @@ import net.kyori.adventure.text.Component
2019
import net.kyori.adventure.text.ComponentLike
2120
import org.bukkit.command.CommandSender
2221
import org.bukkit.entity.Entity
23-
import org.bukkit.entity.Player
2422

2523
interface CommandAccess {
2624

@@ -42,7 +40,7 @@ data class CommandContext(
4240
val raw: CommandContext<CommandSourceStack>,
4341
val replyRenderer: ReplyChatRenderer?,
4442
override val path: List<String>,
45-
val state: (state: Int, process: () -> Unit) -> Unit,
43+
val state: (state: Int) -> Unit,
4644
): CommandAccess {
4745

4846
override val sender = raw.source.sender
@@ -74,14 +72,23 @@ data class CommandContext(
7472
}
7573

7674
// state modification
77-
@BrigadiKtDSL fun state(state: Int, message: ComponentLike) = this.state.invoke(state) { reply(message) }
75+
@BrigadiKtDSL fun state(state: Int, message: ComponentLike) {
76+
this.state(state)
77+
reply(message)
78+
}
7879
@BrigadiKtDSL fun state(state: Int, @StyledString message: String) = state(state, message.asStyledComponent)
7980

80-
@BrigadiKtDSL fun fail(process: () -> Unit = { }) = state(0, process)
81+
@BrigadiKtDSL inline fun fail(process: () -> Unit = { }) {
82+
state(0)
83+
process()
84+
}
8185
@BrigadiKtDSL fun fail(message: ComponentLike) = state(0, message)
8286
@BrigadiKtDSL fun fail(@StyledString message: String) = state(0, message)
8387

84-
@BrigadiKtDSL fun success(process: () -> Unit = { }) = state(Command.SINGLE_SUCCESS, process)
88+
@BrigadiKtDSL inline fun success(process: () -> Unit = { }) {
89+
state(Command.SINGLE_SUCCESS)
90+
process()
91+
}
8592
@BrigadiKtDSL fun success(message: ComponentLike) = state(Command.SINGLE_SUCCESS, message)
8693
@BrigadiKtDSL fun success(@StyledString message: String) = state(Command.SINGLE_SUCCESS, message)
8794

@@ -98,7 +105,7 @@ data class CommandContext(
98105
this.reply(component = message.asStyledComponent, sound = sound)
99106

100107
@BrigadiKtDSL
101-
fun reply(sound: Sound? = null, messageBuilder: StackedBuilder.() -> Unit) =
108+
inline fun reply(sound: Sound? = null, messageBuilder: StackedBuilder.() -> Unit) =
102109
this.reply(Component.empty().toStackedBuilder().apply(messageBuilder), sound)
103110

104111
override fun CommandSender.hasPathPermission(logResult: Boolean): Boolean =

src/main/kotlin/dev/fruxz/brigadikt/structure/ArgumentProvider.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ open class ArgumentProvider<I : Any, O>(
5353

5454
operator fun provideDelegate(thisRef: Any?, property: KProperty<*>): ArgumentProvider<I, O> {
5555
if (name == null) name = property.name
56-
argumentStorage.setter.call(argumentStorage.getter.call() + lazyArgument(name!!))
56+
argumentStorage.setter.call(argumentStorage.getter.call() + lazyArgument(name!!)) // TODO rewrite to avoid reflect calls which are notfound in classpath
5757

5858
return this
5959
}

0 commit comments

Comments
 (0)