Skip to content

Creating a new command

DustinRepo edited this page Jun 24, 2022 · 5 revisions

Jex uses Brigadier for commands.

To create a command instance, create a new class that extends me.dustin.jex.feature.command.core.Command. This should make you create a method called registerCommand, and is where you would build the command.

To mark the command, use the annotation me.dustin.jex.feature.command.core.annotate.Cmd By default, only two values are needed in the annotation.

  1. Name: The name of the command, or what the user would type to call it
  2. Description: The description of the command

There are also optional values you can set

  1. Syntax: How to use the command
  2. Alias: An array of aliases that can also be used to call the command

Here's an example

package me.dustin.example.command;

import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.tree.CommandNode;
import me.dustin.jex.feature.command.core.Command;
import me.dustin.jex.feature.command.core.annotate.Cmd;
import me.dustin.jex.helper.misc.ChatHelper;
import net.fabricmc.fabric.api.client.command.v1.FabricClientCommandSource;

@Cmd(name = "example", description = "Example command", syntax = ".example", alias = "ex")
public class ExampleCommand extends Command {
    @Override
    public void registerCommand(CommandDispatcher<FabricClientCommandSource> dispatcher, CommandRegistryAccess commandRegistryAccess) {
        CommandNode<FabricClientCommandSource> node = dispatcher.register(literal(this.name).executes(this));
        for (String alias : getAlias()) {
            dispatcher.register(literal(alias).redirect(node));
        }
    }

    @Override
    public int run(CommandContext<FabricClientCommandSource> context) throws CommandSyntaxException {
        ChatHelper.INSTANCE.addClientMessage("Hello World!");
        return 1;
    }
}

Clone this wiki locally