Skip to content

Commit

Permalink
fix: Correctly send argument exception text to executor
Browse files Browse the repository at this point in the history
refactor: Clean up some command context classes
  • Loading branch information
0ffz committed Oct 25, 2024
1 parent 7cf13a6 commit ef2a302
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mineinabyss.idofront.commands.brigadier

import com.mineinabyss.idofront.commands.brigadier.context.IdoCommandContext
import com.mojang.brigadier.arguments.ArgumentType


Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mineinabyss.idofront.commands.brigadier

import com.mineinabyss.idofront.commands.brigadier.context.IdoPlayerCommandContext
import com.mojang.brigadier.arguments.ArgumentType


Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.mineinabyss.idofront.commands.brigadier

import io.papermc.paper.command.brigadier.CommandSourceStack
import com.mineinabyss.idofront.commands.brigadier.context.IdoCommandContext
import kotlin.reflect.KProperty

class IdoArgument<T>(
val name: String,
val resolve: ((CommandSourceStack, Any) -> T)? = null,
val resolve: ((IdoCommandContext, Any) -> T)? = null,
val default: ((IdoCommandContext) -> T)? = null,
) {
operator fun getValue(thisRef: Any?, property: KProperty<*>): IdoArgument<T> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package com.mineinabyss.idofront.commands.brigadier

import com.github.shynixn.mccoroutine.bukkit.asyncDispatcher
import com.github.shynixn.mccoroutine.bukkit.scope
import com.mineinabyss.idofront.commands.brigadier.context.IdoCommandContext
import com.mineinabyss.idofront.commands.brigadier.context.IdoSuggestionsContext
import com.mojang.brigadier.StringReader
import com.mojang.brigadier.arguments.ArgumentType
import com.mojang.brigadier.context.CommandContext
Expand All @@ -16,7 +18,7 @@ import java.util.concurrent.CompletableFuture
data class IdoArgumentType<T>(
val nativeType: ArgumentType<Any>,
val name: String? = null,
val resolve: ((CommandSourceStack, Any) -> T)? = null,
val resolve: ((IdoCommandContext, Any) -> T)? = null,
val suggestions: ((CommandContext<Any>, SuggestionsBuilder) -> CompletableFuture<Suggestions>)? = null,
val commandExamples: MutableCollection<String>,
val default: (IdoCommandContext.() -> T)? = null,
Expand Down Expand Up @@ -49,16 +51,13 @@ data class IdoArgumentType<T>(
fun default(default: IdoCommandContext.() -> T): IdoArgumentType<T> =
copy(default = default)

inline fun <R> map(crossinline transform: IdoCommandParsingContext.(T) -> R): IdoArgumentType<R> =
inline fun <R> map(crossinline transform: IdoCommandContext.(T) -> R): IdoArgumentType<R> =
IdoArgumentType(
nativeType = nativeType,
name = name,
resolve = { stack, value ->
val context = object : IdoCommandParsingContext {
override val stack = stack
}
resolve = { context, value ->
resolve
?.let { transform(context, it(stack, value)) }
?.let { transform(context, it(context, value)) }
?: transform(context, value as T)
},
suggestions = suggestions,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.mineinabyss.idofront.commands.brigadier

import com.mineinabyss.idofront.commands.brigadier.context.IdoCommandContext
import com.mineinabyss.idofront.commands.brigadier.context.IdoPlayerCommandContext
import com.mineinabyss.idofront.commands.brigadier.context.IdoSuggestionsContext
import com.mineinabyss.idofront.commands.execution.CommandExecutionFailedException
import com.mineinabyss.idofront.textcomponents.miniMsg
import com.mojang.brigadier.arguments.ArgumentType
Expand Down Expand Up @@ -189,7 +192,7 @@ open class IdoCommand(
fun <R : ArgumentResolver<T>, T> ArgumentType<R>.resolve(): IdoArgumentType<T> = toIdo().let {
IdoArgumentType(
nativeType = it.nativeType,
resolve = { stack, value -> (value as R).resolve(stack) },
resolve = { context, value -> (value as R).resolve(context.context.source) },
suggestions = it.suggestions,
commandExamples = it.commandExamples
)
Expand All @@ -204,7 +207,7 @@ open class IdoCommand(
fun <T : Any> ArgumentType<T>.suggests(provider: SuggestionProvider<CommandSourceStack>) =
toIdo().suggests(provider)

inline fun <T : Any, R> ArgumentType<T>.map(crossinline transform: IdoCommandParsingContext.(T) -> R) =
inline fun <T : Any, R> ArgumentType<T>.map(crossinline transform: IdoCommandContext.(T) -> R) =
toIdo().map(transform)

fun <T : Any> ArgumentType<T>.named(name: String) = toIdo().copy(name = name)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.mineinabyss.idofront.commands.brigadier
package com.mineinabyss.idofront.commands.brigadier.context

import com.mineinabyss.idofront.commands.brigadier.Annotations
import com.mineinabyss.idofront.commands.brigadier.IdoArgument
import com.mineinabyss.idofront.commands.execution.CommandExecutionFailedException
import com.mineinabyss.idofront.textcomponents.miniMsg
import com.mojang.brigadier.context.CommandContext
Expand All @@ -15,32 +17,32 @@ open class IdoCommandContext(
val context: CommandContext<CommandSourceStack>,
) {
/** Stops the command, sending a [message] formatted with MiniMessage to its [sender]. */
fun fail(message: String): Nothing = throw CommandExecutionFailedException(message.miniMsg())
fun fail(message: String): Nothing = throw CommandExecutionFailedException("<red>$message".miniMsg())

/** Stops the command, sending a [message] to its [sender]. */
fun fail(message: Component): Nothing = throw CommandExecutionFailedException(message)

/** The sender that ran this command. */
val sender: CommandSender = context.source.sender

val source get() = context.source

/** An entity representing the [sender] on the server. */
val executor: Entity? = context.source.executor
val executor: Entity? = source.executor

val location: Location = context.source.location
val location: Location = source.location

@JvmName("invoke2")
inline operator fun <reified T> IdoArgument<T>.invoke(): T {
return getArgumentOrNull<T>(this)
?: fail("<red>Argument $name not found".miniMsg())
return getArgumentOrNull<T>(this) ?: fail("<red>Argument $name not found".miniMsg())
}

@PublishedApi
internal inline fun <reified T> getArgumentOrNull(argument: IdoArgument<T>): T? =
runCatching {
val arg: Any = context.getArgument(argument.name, Any::class.java)
?: return@runCatching null
(argument.resolve?.invoke(context.source, arg) ?: arg) as T
}.getOrNull() ?: argument.default?.invoke(this)
internal inline fun <reified T> getArgumentOrNull(argument: IdoArgument<T>): T? {
val arg: Any = runCatching { context.getArgument(argument.name, Any::class.java) }.getOrNull()
?: return argument.default?.invoke(this)
return (argument.resolve?.invoke(this, arg) ?: arg) as T
}

@PublishedApi
internal inline fun <reified T> arg(argument: IdoArgument<*>): T = (argument as IdoArgument<T>).invoke()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mineinabyss.idofront.commands.brigadier
package com.mineinabyss.idofront.commands.brigadier.context

import com.mineinabyss.idofront.commands.brigadier.Annotations
import com.mojang.brigadier.context.CommandContext
import io.papermc.paper.command.brigadier.CommandSourceStack
import org.bukkit.entity.Player
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package com.mineinabyss.idofront.commands.brigadier
package com.mineinabyss.idofront.commands.brigadier.context

import com.mojang.brigadier.LiteralMessage
import com.mojang.brigadier.Message
import com.mojang.brigadier.context.CommandContext
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType
import com.mojang.brigadier.suggestion.SuggestionsBuilder
import io.papermc.paper.command.brigadier.CommandSourceStack

@Suppress("UnstableApiUsage")
data class IdoSuggestionsContext(
val context: CommandContext<CommandSourceStack>,
class IdoSuggestionsContext(
context: CommandContext<CommandSourceStack>,
val suggestions: SuggestionsBuilder,
): IdoCommandParsingContext {
override val stack get() = context.source
): IdoCommandContext(context) {
/** The argument currently being typed*/
val argument get() = suggestions.remaining

Expand Down

0 comments on commit ef2a302

Please sign in to comment.