Skip to content

Commands

Gugas749 edited this page Nov 5, 2026 · 1 revision

Commands

Brigadier commands, organized as one class per command group plus one registrar that lists them.

import com.gugas749.abysscore.api.command.AbyssCommand;
import com.gugas749.abysscore.api.command.AbyssCommandRegistrar;

AbyssCommand is a functional interface: void register(CommandDispatcher<CommandSourceStack> dispatcher). A static register method on any class fits it as MyCommand::register.

A command group

public final class HealCommand {
    public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
        dispatcher.register(Commands.literal("mymod")
                .then(Commands.literal("heal")
                        .requires(src -> AbyssPermissionHandler.sourceHas(src, AbyssPermissionLevel.MODERATOR))
                        .then(Commands.argument("target", EntityArgument.player())
                                .executes(ctx -> {
                                    ServerPlayer target = EntityArgument.getPlayer(ctx, "target");
                                    target.setHealth(target.getMaxHealth());
                                    ctx.getSource().sendSuccess(
                                            () -> Component.literal("Healed " + target.getName().getString()), true);
                                    return 1;
                                }))));
    }
}

public final class ManaCommand {
    public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
        dispatcher.register(Commands.literal("mymod")
                .then(Commands.literal("mana")
                        .requires(src -> AbyssPermissionHandler.sourceHas(src, AbyssPermissionLevel.ADMIN))
                        .then(Commands.argument("target", EntityArgument.player())
                                .then(Commands.argument("value", IntegerArgumentType.integer(0, 1000))
                                        .executes(ctx -> {
                                            ServerPlayer p = EntityArgument.getPlayer(ctx, "target");
                                            int v = IntegerArgumentType.getInteger(ctx, "value");
                                            MyData.MANA.set(p, MyData.MANA.get(p).withCurrent(v));
                                            return 1;
                                        })))));
    }
}

The registrar

public class MyCommands extends AbyssCommandRegistrar {
    @Override
    protected List<AbyssCommand> commands() {
        return List.of(
                HealCommand::register,
                ManaCommand::register
        );
    }
}

Hook it up (once per loader)

// NeoForge 1.21.1 — mod constructor
NeoForge.EVENT_BUS.addListener((RegisterCommandsEvent e) -> new MyCommands().registerAll(e.getDispatcher()));

// Forge 1.20.1 — mod constructor
MinecraftForge.EVENT_BUS.addListener((RegisterCommandsEvent e) -> new MyCommands().registerAll(e.getDispatcher()));

Use addListener, not EVENT_BUS.register(new MyCommands()): the registrar has no @SubscribeEvent method, and register throws for classes without one.

The method is named registerAll (not register) so a class can be both a command (static register(...)) and a registrar without a compile error.

⚠ Adding sub-commands under /abysscore

Brigadier merges every literal("abysscore") registration into one node and keeps only the first .requires(). Put your permission check on your own sub-literal:

// ✅ correct
dispatcher.register(Commands.literal("abysscore")
        .then(Commands.literal("mymodstuff")
                .requires(src -> AbyssPermissionHandler.sourceHas(src, AbyssPermissionLevel.MODERATOR))
                .executes(...)));

// ❌ wrong: this requires() is ignored
dispatcher.register(Commands.literal("abysscore")
        .requires(src -> ...)
        .then(Commands.literal("mymodstuff").executes(...)));

The same applies to your own root literal if several of your classes register it, like "mymod" above.

Clone this wiki locally