Making Commands

Extend AbstractCommand and type it with a (data?) class that’ll hold on to the sort of data you expect to see during an execution of your command. It’s meant to add a static typing layer over the top of the raw command context’s getting of arguments and their types. Then, register the command using your AbstractMod subclass.

import com.cobblemon.mod.common.api.permission.PermissionLevel
import com.mojang.brigadier.context.CommandContext
import net.minecraft.commands.CommandSourceStack
import net.minecraft.commands.Commands
import net.minecraft.commands.arguments.EntityArgument
import net.minecraft.server.level.ServerPlayer
import us.timinc.mc.cobblemon.timcore.AbstractCommand
import us.timinc.mc.cobblemon.timcore.TimCore

object SillyTestCommand : AbstractCommand<SillyTestCommand.Data>(
    "silly",
    PermissionLevel.ALL_COMMANDS,
    TimCore,
    listOf(
	    Commands.literal("kill"),
	    Commands.argument("player", EntityArgument.player())
	  )
) {
    class Data(
        val player: ServerPlayer
    )

    override fun run(commandContext: Data, rawContext: CommandContext<CommandSourceStack>): Int {
        commandContext.player.kill()
        return 1
    }

    override fun parseContext(ctx: CommandContext<CommandSourceStack>): Data =
        Data(EntityArgument.getPlayer(ctx, "player"))
}

Making Command Argument Types

Create your ArgumentType class.

class DummyArgumentType : ArgumentType<String> {
    override fun parse(reader: StringReader): String {
        return reader.readUnquotedString()
    }

    override fun <S : Any?> listSuggestions(
        context: CommandContext<S>,
        builder: SuggestionsBuilder
    ): CompletableFuture<Suggestions> {
        builder.suggest("dummy")
        return builder.buildFuture()
    }

    override fun getExamples(): Collection<String> {
        return listOf("dummy")
    }
}

In your AbstractMod class, register the command argument type using a CommandArgumentContainer.

class SomeMod : AbstractMod<SomeMod.Config>("some_mod", Config::class.java) {
  class Config : AbstractConfig

  init {
		registerCommandArgument(
      CommandArgumentContainer(
        modResource("dummy_arg"),
        DummyArgumentType::class.java,
        SingletonArgumentInfo.contextFree(::DummyArgumentType)
      )
    )
  }
}