CommandHandler.kt and Command Template Code

This commit is contained in:
2025-05-07 21:08:54 -04:00
parent 64c2a95361
commit 56fb8f5a6e
11 changed files with 213 additions and 3 deletions

View File

@@ -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")

View File

@@ -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)
}
}
}

View File

@@ -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 <number>
*/
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<ServerCommandSource>(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 <number> is typed
// You can add more subcommands or arguments here
// .then(literal<ServerCommandSource>("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<ServerCommandSource>): 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<ServerCommandSource>): 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<ServerCommandSource>): Int {
val source = context.source
source.sendFeedback({ Text.literal("You ran the 'subcommand' of MyCommand!") }, false)
return Command.SINGLE_SUCCESS
}
*/
}