Skip to content

Commit

Permalink
Use the cloud command framework instead of creating the commands by t…
Browse files Browse the repository at this point in the history
…hemselves
  • Loading branch information
derNiklaas committed Jul 19, 2024
1 parent 2e35dfa commit d8e3be3
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 113 deletions.
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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(
Expand All @@ -33,8 +40,6 @@ class BuildBugsClientEntrypoint : ClientModInitializer {
)
)

ClientCommandRegistrationCallback.EVENT.register(BuildBugsCommand::register)

ClientTickEvents.END_CLIENT_TICK.register {
if (reportKeybinding.wasPressed()) {
BugCreator.report()
Expand Down
186 changes: 76 additions & 110 deletions src/main/kotlin/de/derniklaas/buildbugs/BuildBugsCommand.kt
Original file line number Diff line number Diff line change
@@ -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<FabricClientCommandSource>, registryAccess: CommandRegistryAccess) {
dispatcher.register(literal<FabricClientCommandSource>("buildbug").then(
literal<FabricClientCommandSource>("config").then(
literal<FabricClientCommandSource>("clipboard").then(argument(
"value", BoolArgumentType.bool()
).executes {
val value = BoolArgumentType.getBool(it, "value")
BuildBugsClientEntrypoint.config.setCopy(value)
if (BuildBugsClientEntrypoint.config.copyToClipboard) {
Utils.sendMiniMessage("<green>Enabled</green> automatic copying to clipboard.")
} else {
Utils.sendMiniMessage("<red>Disabled</red> automatic copying to clipboard.")
}
return@executes Command.SINGLE_SUCCESS
})
).then(
literal<FabricClientCommandSource>("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("<green>Enabled</green> Debug mode.")
BugCreator.printCurrentGameState()
} else {
Utils.sendMiniMessage("<red>Disabled</red> Debug mode.")
}
@Command("buildbug config clipboard <value>")
fun config_clipboard(context: CommandContext<*>, @Argument("value") value: Boolean) {
BuildBugsClientEntrypoint.config.setCopy(value)
if (BuildBugsClientEntrypoint.config.copyToClipboard) {
Utils.sendMiniMessage("<green>Enabled</green> automatic copying to clipboard.")
} else {
Utils.sendMiniMessage("<red>Disabled</red> automatic copying to clipboard.")
}
}

@Command("buildbug config debug <value>")
fun config_debug(context: CommandContext<*>, @Argument("value") value: Boolean) {
BuildBugsClientEntrypoint.config.setDebug(value)
if (BuildBugsClientEntrypoint.config.debugMode) {
Utils.sendMiniMessage("<green>Enabled</green> Debug mode.")
BugCreator.printCurrentGameState()
} else {
Utils.sendMiniMessage("<red>Disabled</red> Debug mode.")
}
}

return@executes Command.SINGLE_SUCCESS
})
).then(
literal<FabricClientCommandSource?>("log_incoming_packets").then(argument(
"value",
BoolArgumentType.bool()
).executes {
val value = BoolArgumentType.getBool(it, "value")
BuildBugsClientEntrypoint.config.setLoggingForIncomingPackets(value)
@Command("buildbug config eventip <address>")
fun config_ip(context: CommandContext<*>, @Argument("address") address: String) {
BuildBugsClientEntrypoint.config.setEventAddress(address)
Utils.sendSuccessMessage("Set event IP to <gold>$address</gold>.")

if (BuildBugsClientEntrypoint.config.logIncomingPackets) {
Utils.sendMiniMessage("<green>Enabled</green> logging of incoming packets.")
} else {
Utils.sendMiniMessage("<red>Disabled</red> logging of incoming packets.")
}
}

return@executes Command.SINGLE_SUCCESS
})
).then(
literal<FabricClientCommandSource?>("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("<green>Enabled</green> logging of outgoing packets.")
} else {
Utils.sendMiniMessage("<red>Disabled</red> 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<FabricClientCommandSource?>("eventip").then(argument(
"value", StringArgumentType.string()
).executes {
val value = StringArgumentType.getString(it, "value")
BuildBugsClientEntrypoint.config.setEventAddress(value)
@Command("buildbug force_state <game> <type> <map>")
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 <game> <type>")
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 <gold>$value</gold>.")

return@executes Command.SINGLE_SUCCESS
})
)
).then(literal<FabricClientCommandSource>("version").executes {
Utils.sendSuccessMessage("Running BuildBugs ${BuildBugsClientEntrypoint.version}!")
return@executes Command.SINGLE_SUCCESS
}).then(literal<FabricClientCommandSource>("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<FabricClientCommandSource>("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<Suggestion> {
return Constants.ALL_TYPES.map { Suggestion.suggestion(it) }
}
}

0 comments on commit d8e3be3

Please sign in to comment.