From d8e3be34a0dcf57c4804167d10c82c926d91916c Mon Sep 17 00:00:00 2001 From: Niklas Date: Fri, 19 Jul 2024 19:50:48 +0200 Subject: [PATCH] Use the cloud command framework instead of creating the commands by themselves --- .../buildbugs/BuildBugsClientEntrypoint.kt | 11 +- .../derniklaas/buildbugs/BuildBugsCommand.kt | 186 +++++++----------- 2 files changed, 84 insertions(+), 113 deletions(-) diff --git a/src/main/kotlin/de/derniklaas/buildbugs/BuildBugsClientEntrypoint.kt b/src/main/kotlin/de/derniklaas/buildbugs/BuildBugsClientEntrypoint.kt index 22e5d49..a26ab99 100644 --- a/src/main/kotlin/de/derniklaas/buildbugs/BuildBugsClientEntrypoint.kt +++ b/src/main/kotlin/de/derniklaas/buildbugs/BuildBugsClientEntrypoint.kt @@ -1,14 +1,18 @@ package de.derniklaas.buildbugs import de.derniklaas.buildbugs.utils.Utils +import io.leangen.geantyref.TypeToken import net.fabricmc.api.ClientModInitializer -import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback +import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper import net.fabricmc.loader.api.FabricLoader import net.fabricmc.loader.api.Version import net.minecraft.client.option.KeyBinding import net.minecraft.client.util.InputUtil +import org.incendo.cloud.annotations.AnnotationParser +import org.incendo.cloud.execution.ExecutionCoordinator +import org.incendo.cloud.fabric.FabricClientCommandManager import org.lwjgl.glfw.GLFW class BuildBugsClientEntrypoint : ClientModInitializer { @@ -21,6 +25,9 @@ class BuildBugsClientEntrypoint : ClientModInitializer { override fun onInitializeClient() { NoxesiumPacketHandler() + val manager = FabricClientCommandManager.createNative(ExecutionCoordinator.asyncCoordinator()) + val annotationParser = AnnotationParser(manager, TypeToken.get(FabricClientCommandSource::class.java)) + annotationParser.parse(BuildBugsCommand()) BuildBugsConfig.createDefaultConfig() val reportKeybinding = KeyBindingHelper.registerKeyBinding( KeyBinding( @@ -33,8 +40,6 @@ class BuildBugsClientEntrypoint : ClientModInitializer { ) ) - ClientCommandRegistrationCallback.EVENT.register(BuildBugsCommand::register) - ClientTickEvents.END_CLIENT_TICK.register { if (reportKeybinding.wasPressed()) { BugCreator.report() diff --git a/src/main/kotlin/de/derniklaas/buildbugs/BuildBugsCommand.kt b/src/main/kotlin/de/derniklaas/buildbugs/BuildBugsCommand.kt index 7965452..0a508f1 100644 --- a/src/main/kotlin/de/derniklaas/buildbugs/BuildBugsCommand.kt +++ b/src/main/kotlin/de/derniklaas/buildbugs/BuildBugsCommand.kt @@ -1,124 +1,90 @@ package de.derniklaas.buildbugs -import com.mojang.brigadier.Command -import com.mojang.brigadier.CommandDispatcher -import com.mojang.brigadier.arguments.BoolArgumentType -import com.mojang.brigadier.arguments.StringArgumentType -import com.mojang.brigadier.builder.LiteralArgumentBuilder.literal import de.derniklaas.buildbugs.utils.Utils -import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.argument -import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource -import net.minecraft.command.CommandRegistryAccess +import org.incendo.cloud.annotations.Argument +import org.incendo.cloud.annotations.Command +import org.incendo.cloud.annotations.suggestion.Suggestions +import org.incendo.cloud.context.CommandContext +import org.incendo.cloud.suggestion.Suggestion -object BuildBugsCommand { - fun register(dispatcher: CommandDispatcher, registryAccess: CommandRegistryAccess) { - dispatcher.register(literal("buildbug").then( - literal("config").then( - literal("clipboard").then(argument( - "value", BoolArgumentType.bool() - ).executes { - val value = BoolArgumentType.getBool(it, "value") - BuildBugsClientEntrypoint.config.setCopy(value) - if (BuildBugsClientEntrypoint.config.copyToClipboard) { - Utils.sendMiniMessage("Enabled automatic copying to clipboard.") - } else { - Utils.sendMiniMessage("Disabled automatic copying to clipboard.") - } - return@executes Command.SINGLE_SUCCESS - }) - ).then( - literal("debug").then(argument( - "value", BoolArgumentType.bool() - ).executes { - val value = BoolArgumentType.getBool(it, "value") - BuildBugsClientEntrypoint.config.setDebug(value) +class BuildBugsCommand { - if (BuildBugsClientEntrypoint.config.debugMode) { - Utils.sendMiniMessage("Enabled Debug mode.") - BugCreator.printCurrentGameState() - } else { - Utils.sendMiniMessage("Disabled Debug mode.") - } + @Command("buildbug config clipboard ") + fun config_clipboard(context: CommandContext<*>, @Argument("value") value: Boolean) { + BuildBugsClientEntrypoint.config.setCopy(value) + if (BuildBugsClientEntrypoint.config.copyToClipboard) { + Utils.sendMiniMessage("Enabled automatic copying to clipboard.") + } else { + Utils.sendMiniMessage("Disabled automatic copying to clipboard.") + } + } + + @Command("buildbug config debug ") + fun config_debug(context: CommandContext<*>, @Argument("value") value: Boolean) { + BuildBugsClientEntrypoint.config.setDebug(value) + if (BuildBugsClientEntrypoint.config.debugMode) { + Utils.sendMiniMessage("Enabled Debug mode.") + BugCreator.printCurrentGameState() + } else { + Utils.sendMiniMessage("Disabled Debug mode.") + } + } - return@executes Command.SINGLE_SUCCESS - }) - ).then( - literal("log_incoming_packets").then(argument( - "value", - BoolArgumentType.bool() - ).executes { - val value = BoolArgumentType.getBool(it, "value") - BuildBugsClientEntrypoint.config.setLoggingForIncomingPackets(value) + @Command("buildbug config eventip
") + fun config_ip(context: CommandContext<*>, @Argument("address") address: String) { + BuildBugsClientEntrypoint.config.setEventAddress(address) + Utils.sendSuccessMessage("Set event IP to $address.") - if (BuildBugsClientEntrypoint.config.logIncomingPackets) { - Utils.sendMiniMessage("Enabled logging of incoming packets.") - } else { - Utils.sendMiniMessage("Disabled logging of incoming packets.") - } + } - return@executes Command.SINGLE_SUCCESS - }) - ).then( - literal("log_outgoing_packets").then(argument( - "value", - BoolArgumentType.bool() - ).executes { - val value = BoolArgumentType.getBool(it, "value") - BuildBugsClientEntrypoint.config.setLoggingForOutgoingPackets(value) + @Command("buildbug version") + fun version(context: CommandContext<*>) { + Utils.sendSuccessMessage("Running BuildBugs ${BuildBugsClientEntrypoint.version}!") + } - if (BuildBugsClientEntrypoint.config.logOutgoingPackets) { - Utils.sendMiniMessage("Enabled logging of outgoing packets.") - } else { - Utils.sendMiniMessage("Disabled logging of outgoing packets.") - } + @Command("buildbug reset_state") + fun reset_state(context: CommandContext<*>) { + if (BuildBugsClientEntrypoint.config.debugMode) { + BugCreator.resetGameState() + } else { + Utils.sendErrorMessage("You can only reset the game state in debug mode.") + } + } - return@executes Command.SINGLE_SUCCESS - }) - ).then( - literal("eventip").then(argument( - "value", StringArgumentType.string() - ).executes { - val value = StringArgumentType.getString(it, "value") - BuildBugsClientEntrypoint.config.setEventAddress(value) + @Command("buildbug force_state ") + fun force_state_full( + context: CommandContext<*>, + @Argument("game", suggestions = "gameSuggestions") game: String, + @Argument("type") type: String, + @Argument("map") map: String + ) { + if (!BuildBugsClientEntrypoint.config.debugMode) { + Utils.sendErrorMessage("You can only force the game state in debug mode.") + return + } + BugCreator.forceGameState(game, type, map) + } + + @Command("buildbug force_state ") + fun force_state_full( + context: CommandContext<*>, + @Argument("game", suggestions = "gameSuggestions") game: String, + @Argument("type") type: String + ) { + if (!BuildBugsClientEntrypoint.config.debugMode) { + Utils.sendErrorMessage("You can only force the game state in debug mode.") + return + } + BugCreator.forceGameState(game, type, Constants.UNKNOWN) + } + + fun report(context: CommandContext<*>) { + BugCreator.report() + } - Utils.sendSuccessMessage("Set event IP to $value.") - return@executes Command.SINGLE_SUCCESS - }) - ) - ).then(literal("version").executes { - Utils.sendSuccessMessage("Running BuildBugs ${BuildBugsClientEntrypoint.version}!") - return@executes Command.SINGLE_SUCCESS - }).then(literal("reset_state").executes { - if (BuildBugsClientEntrypoint.config.debugMode) { - BugCreator.resetGameState() - } else { - Utils.sendErrorMessage("You can only reset the game state in debug mode.") - } - return@executes Command.SINGLE_SUCCESS - }).then( - literal("force_state").then( - argument( - "type", StringArgumentType.string() - ).then( - argument("subtype", StringArgumentType.string()).then(argument( - "map", StringArgumentType.string() - ).executes { - if (!BuildBugsClientEntrypoint.config.debugMode) { - Utils.sendErrorMessage("You can only force the game state in debug mode.") - return@executes Command.SINGLE_SUCCESS - } - val type = StringArgumentType.getString(it, "type") - val subType = StringArgumentType.getString(it, "subtype") - val map = StringArgumentType.getString(it, "map") - BugCreator.forceGameState(type, subType, map) - return@executes Command.SINGLE_SUCCESS - }) - ) - ) - ).executes { - BugCreator.report() - return@executes Command.SINGLE_SUCCESS - }) + @Suggestions("gameSuggestions") + fun gameSuggestions(commandContext: CommandContext<*>, input: String): List { + return Constants.ALL_TYPES.map { Suggestion.suggestion(it) } } }