From 56fb8f5a6e11a2260a1a24039184aec070c2d1f3 Mon Sep 17 00:00:00 2001 From: GamerBoss101 Date: Wed, 7 May 2025 21:08:54 -0400 Subject: [PATCH] CommandHandler.kt and Command Template Code --- .idea/.gitignore | 10 ++ .idea/gradle.xml | 16 +++ .idea/misc.xml | 8 ++ .idea/runConfigurations/Minecraft_Client.xml | 16 +++ .idea/runConfigurations/Minecraft_Server.xml | 16 +++ .idea/vcs.xml | 6 ++ gradle.properties | 17 +++ settings.gradle.kts | 2 +- src/main/kotlin/co/sirblob/CobbleSync.kt | 2 - .../co/sirblob/commands/CommandHandler.kt | 23 ++++ .../kotlin/co/sirblob/commands/SaveParty.kt | 100 ++++++++++++++++++ 11 files changed, 213 insertions(+), 3 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/gradle.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/runConfigurations/Minecraft_Client.xml create mode 100644 .idea/runConfigurations/Minecraft_Server.xml create mode 100644 .idea/vcs.xml create mode 100644 src/main/kotlin/co/sirblob/commands/CommandHandler.kt create mode 100644 src/main/kotlin/co/sirblob/commands/SaveParty.kt diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..7bc07ec --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,10 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Environment-dependent path to Maven home directory +/mavenHomeManager.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000..39ec4f5 --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,16 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..f6eb85c --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations/Minecraft_Client.xml b/.idea/runConfigurations/Minecraft_Client.xml new file mode 100644 index 0000000..ba01596 --- /dev/null +++ b/.idea/runConfigurations/Minecraft_Client.xml @@ -0,0 +1,16 @@ + + + + \ No newline at end of file diff --git a/.idea/runConfigurations/Minecraft_Server.xml b/.idea/runConfigurations/Minecraft_Server.xml new file mode 100644 index 0000000..bc90c12 --- /dev/null +++ b/.idea/runConfigurations/Minecraft_Server.xml @@ -0,0 +1,16 @@ + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/gradle.properties b/gradle.properties index e81691d..ccee563 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,2 +1,19 @@ org.gradle.jvmargs=-Xmx4G +org.gradle.parallel=true +# Fabric Properties +# check these on https://fabricmc.net/develop +minecraft_version=1.21.1 +yarn_mappings=1.21.1+build.3 +loader_version=0.16.14 +loom_version=1.10-SNAPSHOT +fabric_kotlin_version=1.13.2+kotlin.2.1.20 + +# Mod Properties +mod_version=1.0.0 +maven_group=co.sirblob +archives_base_name=cobblesync + +# Dependencies +fabric_version=0.115.6+1.21.1 +cobblemon_version=1.6.1+1.21.1 \ No newline at end of file diff --git a/settings.gradle.kts b/settings.gradle.kts index 59bdcda..5a9bc83 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -1,4 +1,4 @@ -rootProject.name = "Fabric-Kotlin" //By default, this is how your built jar is called +rootProject.name = "CobbleSync" //By default, this is how your built jar is called pluginManagement { repositories { diff --git a/src/main/kotlin/co/sirblob/CobbleSync.kt b/src/main/kotlin/co/sirblob/CobbleSync.kt index 97becc4..04c47ed 100644 --- a/src/main/kotlin/co/sirblob/CobbleSync.kt +++ b/src/main/kotlin/co/sirblob/CobbleSync.kt @@ -3,8 +3,6 @@ package co.sirblob import net.fabricmc.api.ModInitializer import org.slf4j.LoggerFactory -import com.cobblemon.mod.common.api.* - object CobbleSync : ModInitializer { private val logger = LoggerFactory.getLogger("cobblesync") diff --git a/src/main/kotlin/co/sirblob/commands/CommandHandler.kt b/src/main/kotlin/co/sirblob/commands/CommandHandler.kt new file mode 100644 index 0000000..d1d3b20 --- /dev/null +++ b/src/main/kotlin/co/sirblob/commands/CommandHandler.kt @@ -0,0 +1,23 @@ +package co.sirblob.commands + +import com.mojang.brigadier.CommandDispatcher +import com.mojang.brigadier.builder.LiteralArgumentBuilder +import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback + +/** + * Handles the registration of all commands for the mod. + */ +object CommandHandler { + + /** + * Initializes and registers all commands. + * This method is typically called from your mod's main initializer. + */ + fun registerCommands() { + CommandRegistrationCallback.EVENT.register { dispatcher, registryAccess, environment -> + // Register your commands here + MyCommand.register(dispatcher) + // Example: AnotherCommand.register(dispatcher) + } + } +} diff --git a/src/main/kotlin/co/sirblob/commands/SaveParty.kt b/src/main/kotlin/co/sirblob/commands/SaveParty.kt new file mode 100644 index 0000000..cf7e819 --- /dev/null +++ b/src/main/kotlin/co/sirblob/commands/SaveParty.kt @@ -0,0 +1,100 @@ +package co.sirblob.commands + +import com.mojang.brigadier.Command +import com.mojang.brigadier.CommandDispatcher +import com.mojang.brigadier.arguments.IntegerArgumentType +import com.mojang.brigadier.builder.LiteralArgumentBuilder +import com.mojang.brigadier.builder.RequiredArgumentBuilder +import com.mojang.brigadier.context.CommandContext +import net.minecraft.server.commands.ServerCommandSource +import net.minecraft.server.commands.CommandManager.* // Required for literal() and argument() +import net.minecraft.text.Text + + +// TEMPLATE CODE DOESN"T WORK BUT MEH + +/** + * A template for a simple command. + * + * This command can be invoked in-game using: + * /mycommand + * /mycommand + */ +object SaveParty { + + private const val COMMAND_NAME = "mycommand" // The base name of the command + + /** + * Registers the command and its subcommands/arguments. + * + * @param dispatcher The command dispatcher used to register commands. + */ + fun register(dispatcher: CommandDispatcher<>) { + // Create the literal argument builder for the base command + // A literal argument is a fixed string that must be typed by the user. + val commandBuilder = literal(COMMAND_NAME) + .executes(::runBaseCommand) // Action to perform if only /mycommand is typed + .then( + // Define an argument that follows the base command. + // In this case, we expect an integer argument named "number". + argument("number", IntegerArgumentType.integer()) + .executes(::runWithNumberArgument) // Action to perform if /mycommand is typed + // You can add more subcommands or arguments here + // .then(literal("subcommand").executes(::runSubCommand)) + ) + + // Register the command + dispatcher.register(commandBuilder) + } + + /** + * Executes when the base command is called (e.g., /mycommand). + * + * @param context The command context, providing information about the command's execution. + * @return An integer indicating command success (typically 1 for success, 0 for failure). + */ + private fun runBaseCommand(context: CommandContext): Int { + val source = context.source + source.sendFeedback({ Text.literal("You ran the base MyCommand!") }, false) // Send feedback to the player + // false means it won't be broadcast to other ops + + // You can add more logic here, like interacting with the game world, player, etc. + // For example, to get the player who executed the command: + // val player = source.player + // player?.sendMessage(Text.literal("Hello from the command!"), false) + + return Command.SINGLE_SUCCESS // Indicates the command was successful + } + + /** + * Executes when the command is called with a number argument (e.g., /mycommand 123). + * + * @param context The command context, providing information about the command's execution. + * @return An integer indicating command success. + */ + private fun runWithNumberArgument(context: CommandContext): Int { + val source = context.source + val number = IntegerArgumentType.getInteger(context, "number") // Get the value of the "number" argument + + source.sendFeedback({ Text.literal("MyCommand received number: $number") }, false) + + // Add logic that uses the number argument + if (number > 100) { + source.sendFeedback({ Text.literal("That's a big number!") }, false) + } else { + source.sendFeedback({ Text.literal("That's a reasonable number.") }, false) + } + + return Command.SINGLE_SUCCESS + } + + /* + // Example of another subcommand function + private fun runSubCommand(context: CommandContext): Int { + val source = context.source + source.sendFeedback({ Text.literal("You ran the 'subcommand' of MyCommand!") }, false) + return Command.SINGLE_SUCCESS + } + */ +} +