From 2019473c50dfe3f1c6fd071d123d49d43bc180c4 Mon Sep 17 00:00:00 2001 From: nea Date: Sat, 27 Aug 2022 02:37:34 +0200 Subject: move commands to its own place --- .../moe/nea/notenoughupdates/commands/dsl.kt | 75 ++++++++++++++++++++++ .../moe/nea/notenoughupdates/commands/rome.kt | 42 ++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 src/main/kotlin/moe/nea/notenoughupdates/commands/dsl.kt create mode 100644 src/main/kotlin/moe/nea/notenoughupdates/commands/rome.kt (limited to 'src/main/kotlin/moe/nea/notenoughupdates/commands') diff --git a/src/main/kotlin/moe/nea/notenoughupdates/commands/dsl.kt b/src/main/kotlin/moe/nea/notenoughupdates/commands/dsl.kt new file mode 100644 index 0000000..c21eab8 --- /dev/null +++ b/src/main/kotlin/moe/nea/notenoughupdates/commands/dsl.kt @@ -0,0 +1,75 @@ +package moe.nea.notenoughupdates.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 moe.nea.notenoughupdates.util.iterate +import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource +import java.lang.reflect.ParameterizedType + + +typealias DefaultSource = FabricClientCommandSource + +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/notenoughupdates/commands/rome.kt b/src/main/kotlin/moe/nea/notenoughupdates/commands/rome.kt new file mode 100644 index 0000000..1e59d78 --- /dev/null +++ b/src/main/kotlin/moe/nea/notenoughupdates/commands/rome.kt @@ -0,0 +1,42 @@ +package moe.nea.notenoughupdates.commands + +import com.mojang.brigadier.CommandDispatcher +import io.github.cottonmc.cotton.gui.client.CottonClientScreen +import moe.nea.notenoughupdates.gui.repoGui +import moe.nea.notenoughupdates.repo.RepoManager +import moe.nea.notenoughupdates.util.ScreenUtil.setScreenLater +import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource +import net.minecraft.text.Text + + +fun neuCommand() = literal("neu") { + thenLiteral("reload") { + thenLiteral("fetch") { + thenExecute { + source.sendFeedback(Text.literal("Trying to redownload the repository")) // TODO better reporting + RepoManager.launchAsyncUpdate() + } + } + thenExecute { + source.sendFeedback(Text.translatable("notenoughupdates.repo.reload.disk")) + RepoManager.reload() + } + } + thenLiteral("repo") { + thenExecute { + setScreenLater(CottonClientScreen(repoGui())) + } + } +} + + +fun registerNeuCommand(dispatcher: CommandDispatcher) { + val neu = dispatcher.register(neuCommand()) + dispatcher.register(literal("alsoneu") { + redirect(neu) + }) +} + + + + -- cgit