From 0f9ec29648e1b9840813b24587b8a660f6ab4531 Mon Sep 17 00:00:00 2001 From: nea Date: Wed, 6 Sep 2023 03:10:44 +0200 Subject: Add command for toggling config options --- src/main/kotlin/moe/nea/firmament/commands/dsl.kt | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) (limited to 'src/main/kotlin/moe/nea/firmament/commands/dsl.kt') diff --git a/src/main/kotlin/moe/nea/firmament/commands/dsl.kt b/src/main/kotlin/moe/nea/firmament/commands/dsl.kt index 551f560..60756f4 100644 --- a/src/main/kotlin/moe/nea/firmament/commands/dsl.kt +++ b/src/main/kotlin/moe/nea/firmament/commands/dsl.kt @@ -11,14 +11,14 @@ import com.mojang.brigadier.builder.ArgumentBuilder import com.mojang.brigadier.builder.RequiredArgumentBuilder import com.mojang.brigadier.context.CommandContext import com.mojang.brigadier.suggestion.SuggestionProvider -import java.lang.reflect.ParameterizedType -import java.lang.reflect.Type -import java.lang.reflect.TypeVariable -import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource import kotlinx.coroutines.launch import moe.nea.firmament.Firmament import moe.nea.firmament.util.MinecraftDispatcher import moe.nea.firmament.util.iterate +import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource +import java.lang.reflect.ParameterizedType +import java.lang.reflect.Type +import java.lang.reflect.TypeVariable typealias DefaultSource = FabricClientCommandSource @@ -68,7 +68,14 @@ data class TypeSafeArg(val name: String, val argument: ArgumentType) fun get(ctx: CommandContext): T { - return ctx.getArgument(name, argClass) as T + try { + return ctx.getArgument(name, argClass) as T + } catch (e: Exception) { + if (ctx.child != null) { + return get(ctx.child) + } + throw e + } } } @@ -86,9 +93,9 @@ fun , AT : Any> T.thenArgument( block: RequiredArgumentBuilder.(TypeSafeArg) -> Unit ): T = then(argument(name, argument, block)) -fun > T.suggestsList(provider: () -> Iterable) { +fun > T.suggestsList(provider: CommandContext.() -> Iterable) { suggests(SuggestionProvider { context, builder -> - provider() + provider(context) .asSequence() .filter { it.startsWith(builder.remaining, ignoreCase = true) } .forEach { -- cgit