From ead6762eb1c005914b05f9d3c29f334989c67513 Mon Sep 17 00:00:00 2001 From: nea Date: Tue, 16 May 2023 01:23:43 +0200 Subject: Replace references to NEU with Firmament --- src/main/kotlin/moe/nea/firmament/commands/dsl.kt | 81 ++++++++++++++++++++++ src/main/kotlin/moe/nea/firmament/commands/rome.kt | 66 ++++++++++++++++++ 2 files changed, 147 insertions(+) create mode 100644 src/main/kotlin/moe/nea/firmament/commands/dsl.kt create mode 100644 src/main/kotlin/moe/nea/firmament/commands/rome.kt (limited to 'src/main/kotlin/moe/nea/firmament/commands') diff --git a/src/main/kotlin/moe/nea/firmament/commands/dsl.kt b/src/main/kotlin/moe/nea/firmament/commands/dsl.kt new file mode 100644 index 0000000..5ead624 --- /dev/null +++ b/src/main/kotlin/moe/nea/firmament/commands/dsl.kt @@ -0,0 +1,81 @@ +package moe.nea.firmament.commands + +import com.mojang.brigadier.arguments.ArgumentType +import com.mojang.brigadier.builder.ArgumentBuilder +import com.mojang.brigadier.builder.LiteralArgumentBuilder +import com.mojang.brigadier.builder.RequiredArgumentBuilder +import com.mojang.brigadier.context.CommandContext +import java.lang.reflect.ParameterizedType +import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource +import moe.nea.firmament.util.iterate + + +typealias DefaultSource = FabricClientCommandSource + + +inline val > T.context get() = this +operator fun > C.get(arg: TypeSafeArg): T { + return arg.get(this) +} + +fun literal( + name: String, + block: LiteralArgumentBuilder.() -> Unit +): LiteralArgumentBuilder = + LiteralArgumentBuilder.literal(name).also(block) + +data class TypeSafeArg(val name: String, val argument: ArgumentType) { + val argClass by lazy { + argument.javaClass + .iterate>> { + it.superclass + } + .map { + it.genericSuperclass + } + .filterIsInstance() + .find { it.rawType == ArgumentType::class.java }!! + .let { it.actualTypeArguments[0] as Class<*> } + } + + @JvmName("getWithThis") + fun CommandContext.get(): T = + get(this) + + + fun get(ctx: CommandContext): T { + return ctx.getArgument(name, argClass) as T + } +} + + +fun argument( + name: String, + argument: ArgumentType, + block: RequiredArgumentBuilder.(TypeSafeArg) -> Unit +): RequiredArgumentBuilder = + RequiredArgumentBuilder.argument(name, argument).also { block(it, TypeSafeArg(name, argument)) } + +fun , AT : Any> T.thenArgument( + name: String, + argument: ArgumentType, + block: RequiredArgumentBuilder.(TypeSafeArg) -> Unit +): T = then(argument(name, argument, block)) + + +fun > T.thenLiteral( + name: String, + block: LiteralArgumentBuilder.() -> Unit +): T = + then(literal(name, block)) + +fun > T.then(node: ArgumentBuilder, block: T.() -> Unit): T = + then(node).also(block) + +fun > T.thenExecute(block: CommandContext.() -> Unit): T = + executes { + block(it) + 1 + } + + diff --git a/src/main/kotlin/moe/nea/firmament/commands/rome.kt b/src/main/kotlin/moe/nea/firmament/commands/rome.kt new file mode 100644 index 0000000..bdbaa3f --- /dev/null +++ b/src/main/kotlin/moe/nea/firmament/commands/rome.kt @@ -0,0 +1,66 @@ +package moe.nea.firmament.commands + +import com.mojang.brigadier.CommandDispatcher +import io.github.cottonmc.cotton.gui.client.CottonClientScreen +import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource +import net.minecraft.text.Text +import moe.nea.firmament.features.world.FairySouls +import moe.nea.firmament.gui.repoGui +import moe.nea.firmament.repo.RepoManager +import moe.nea.firmament.util.SBData +import moe.nea.firmament.util.ScreenUtil.setScreenLater + + +fun firmamentCommand() = literal("firmament") { + thenLiteral("repo") { + thenLiteral("reload") { + thenLiteral("fetch") { + thenExecute { + source.sendFeedback(Text.translatable("firmament.repo.reload.network")) // TODO better reporting + RepoManager.launchAsyncUpdate() + } + } + thenExecute { + source.sendFeedback(Text.translatable("firmament.repo.reload.disk")) + RepoManager.reload() + } + } + thenExecute { + setScreenLater(CottonClientScreen(repoGui())) + } + } + thenLiteral("dev") { + thenLiteral("config") { + thenExecute { + FairySouls.TConfig.showConfigEditor() + } + } + thenLiteral("sbdata") { + thenExecute { + source.sendFeedback(Text.translatable("firmament.sbinfo.profile", SBData.profileCuteName)) + val locrawInfo = SBData.locraw + if (locrawInfo == null) { + source.sendFeedback(Text.translatable("firmament.sbinfo.nolocraw")) + } else { + source.sendFeedback(Text.translatable("firmament.sbinfo.server", locrawInfo.server)) + source.sendFeedback(Text.translatable("firmament.sbinfo.gametype", locrawInfo.gametype)) + source.sendFeedback(Text.translatable("firmament.sbinfo.mode", locrawInfo.mode)) + source.sendFeedback(Text.translatable("firmament.sbinfo.map", locrawInfo.map)) + } + + } + } + } +} + + +fun registerFirmamentCommand(dispatcher: CommandDispatcher) { + val firmament = dispatcher.register(firmamentCommand()) + dispatcher.register(literal("firm") { + redirect(firmament) + }) +} + + + + -- cgit