Skip to content

How to make a chat command in a Minecraft mod

On 1.13 and newer commands are built with Brigadier, Minecraft’s command library: Commands.literal(“name”) with .executes(context -> …) that returns 1 on success. Register them when the loader asks: Fabric API’s CommandRegistrationCallback.EVENT, RegisterCommandsEvent on Forge and NeoForge. .requires(source -> source.hasPermission(2)) limits a command to operators (permission objects from 1.21.11, see the notes). On 1.12.2 a command is a CommandBase class instead.

Works on Minecraft 1.20.1 to 26.3 on NeoForge; Minecraft 1.16.5 to 26.3 on Fabric; Minecraft 1.16.5 to 1.20.1 on Forge; Minecraft 1.12.2 on Forge. Each example was compiled against its loader and version before it shipped in Linen.

  1. Listen to the command registration event when your mod starts.
  2. Build the command with Commands.literal(…) and .executes(…) (a CommandBase subclass on 1.12.2).
  3. Reply with context.getSource().sendSuccess(…); return 1.
  4. Limit who may use it with .requires(…).

The example uses these values. Change them in the code.

Setting In the example What it does
message Hello! The message it shows.
permission 0 Who may use it: 0 everyone, 2 operators (command blocks too), 4 the server owner.

The example mod is examplemod in the package com.example.examplemod. Use your own mod id and package.

Fabric 1.20 to 1.21.1, Fabric 1.21.2 to 1.21.10

Section titled “Fabric 1.20 to 1.21.1, Fabric 1.21.2 to 1.21.10”

Uses:

  • CommandRegistrationCallback.EVENT
  • Commands.literal
  • CommandSourceStack.sendSuccess
  • CommandSourceStack.hasPermission

src/main/java/com/example/examplemod/command/HelloCommand.java

package com.example.examplemod.command;
import com.mojang.brigadier.CommandDispatcher;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.Component;
// The /hello command, built with Brigadier and registered through Fabric API's CommandRegistrationCallback.
public final class HelloCommand {
private HelloCommand() {
}
// Called from the mod's onInitialize.
public static void register() {
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> build(dispatcher));
}
private static void build(CommandDispatcher<CommandSourceStack> dispatcher) {
dispatcher.register(Commands.literal("hello")
.requires(source -> source.hasPermission(0))
.executes(context -> {
context.getSource().sendSuccess(() -> Component.literal("Hello!"), false);
return 1;
}));
}
}

In your main mod class, inside onInitialize:

HelloCommand.register();

NeoForge 1.20.2 to 1.21.1, NeoForge 1.21.2 to 1.21.10

Section titled “NeoForge 1.20.2 to 1.21.1, NeoForge 1.21.2 to 1.21.10”

Uses:

  • RegisterCommandsEvent
  • Commands.literal
  • CommandSourceStack.sendSuccess
  • CommandSourceStack.hasPermission

src/main/java/com/example/examplemod/command/HelloCommand.java

package com.example.examplemod.command;
import com.mojang.brigadier.CommandDispatcher;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.Component;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.event.RegisterCommandsEvent;
// The /hello command, built with Brigadier and registered on NeoForge's game event bus (RegisterCommandsEvent).
public final class HelloCommand {
private HelloCommand() {
}
// Called from the mod's constructor.
public static void register() {
NeoForge.EVENT_BUS.addListener(HelloCommand::onRegisterCommands);
}
private static void onRegisterCommands(RegisterCommandsEvent event) {
build(event.getDispatcher());
}
private static void build(CommandDispatcher<CommandSourceStack> dispatcher) {
dispatcher.register(Commands.literal("hello")
.requires(source -> source.hasPermission(0))
.executes(context -> {
context.getSource().sendSuccess(() -> Component.literal("Hello!"), false);
return 1;
}));
}
}

In your main mod class, inside <init>:

HelloCommand.register();

Uses:

  • Commands.literal
  • CommandSourceStack.sendSuccess
  • CommandSourceStack.permissions
  • Permission.HasCommandLevel

Since 1.21.11 permissions are objects: source.permissions().hasPermission(new Permission.HasCommandLevel(PermissionLevel.byId(n))).

src/main/java/com/example/examplemod/command/HelloCommand.java

package com.example.examplemod.command;
import com.mojang.brigadier.CommandDispatcher;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.Component;
import net.minecraft.server.permissions.Permission;
import net.minecraft.server.permissions.PermissionLevel;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.event.RegisterCommandsEvent;
// The /hello command, built with Brigadier and registered on NeoForge's game event bus (RegisterCommandsEvent).
public final class HelloCommand {
private HelloCommand() {
}
// Called from the mod's constructor.
public static void register() {
NeoForge.EVENT_BUS.addListener(HelloCommand::onRegisterCommands);
}
private static void onRegisterCommands(RegisterCommandsEvent event) {
build(event.getDispatcher());
}
private static void build(CommandDispatcher<CommandSourceStack> dispatcher) {
dispatcher.register(Commands.literal("hello")
.requires(source -> source.permissions().hasPermission(new Permission.HasCommandLevel(PermissionLevel.byId(0))))
.executes(context -> {
context.getSource().sendSuccess(() -> Component.literal("Hello!"), false);
return 1;
}));
}
}

In your main mod class, inside <init>:

HelloCommand.register();

Uses:

  • CommandRegistrationCallback.EVENT
  • Commands.literal
  • CommandSourceStack.sendSuccess
  • CommandSourceStack.hasPermission

Since 1.21.11 permissions are objects: source.permissions().hasPermission(new Permission.HasCommandLevel(PermissionLevel.byId(n))).

src/main/java/com/example/examplemod/command/HelloCommand.java

package com.example.examplemod.command;
import com.mojang.brigadier.CommandDispatcher;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.server.permissions.Permission;
import net.minecraft.server.permissions.PermissionLevel;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.Component;
// The /hello command, built with Brigadier and registered through Fabric API's CommandRegistrationCallback.
public final class HelloCommand {
private HelloCommand() {
}
// Called from the mod's onInitialize.
public static void register() {
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> build(dispatcher));
}
private static void build(CommandDispatcher<CommandSourceStack> dispatcher) {
dispatcher.register(Commands.literal("hello")
.requires(source -> source.permissions().hasPermission(new Permission.HasCommandLevel(PermissionLevel.byId(0))))
.executes(context -> {
context.getSource().sendSuccess(() -> Component.literal("Hello!"), false);
return 1;
}));
}
}

In your main mod class, inside onInitialize:

HelloCommand.register();

Uses:

  • RegisterCommandsEvent
  • Commands.literal
  • CommandSourceStack.sendSuccess
  • CommandSourceStack.hasPermission

src/main/java/com/example/examplemod/command/HelloCommand.java

package com.example.examplemod.command;
import net.minecraft.network.chat.TextComponent;
import com.mojang.brigadier.CommandDispatcher;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.Component;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.RegisterCommandsEvent;
// The /hello command, built with Brigadier and registered on Forge's game event bus (RegisterCommandsEvent).
public final class HelloCommand {
private HelloCommand() {
}
// Called from the mod's constructor.
public static void register() {
MinecraftForge.EVENT_BUS.addListener(HelloCommand::onRegisterCommands);
}
private static void onRegisterCommands(RegisterCommandsEvent event) {
build(event.getDispatcher());
}
private static void build(CommandDispatcher<CommandSourceStack> dispatcher) {
dispatcher.register(Commands.literal("hello")
.requires(source -> source.hasPermission(0))
.executes(context -> {
context.getSource().sendSuccess(new TextComponent("Hello!"), false);
return 1;
}));
}
}

In your main mod class, inside <init>:

HelloCommand.register();

Uses:

  • RegisterCommandsEvent
  • Commands.literal
  • CommandSourceStack.sendSuccess
  • CommandSourceStack.hasPermission

src/main/java/com/example/examplemod/command/HelloCommand.java

package com.example.examplemod.command;
import com.mojang.brigadier.CommandDispatcher;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.Component;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.RegisterCommandsEvent;
// The /hello command, built with Brigadier and registered on Forge's game event bus (RegisterCommandsEvent).
public final class HelloCommand {
private HelloCommand() {
}
// Called from the mod's constructor.
public static void register() {
MinecraftForge.EVENT_BUS.addListener(HelloCommand::onRegisterCommands);
}
private static void onRegisterCommands(RegisterCommandsEvent event) {
build(event.getDispatcher());
}
private static void build(CommandDispatcher<CommandSourceStack> dispatcher) {
dispatcher.register(Commands.literal("hello")
.requires(source -> source.hasPermission(0))
.executes(context -> {
context.getSource().sendSuccess(Component.literal("Hello!"), false);
return 1;
}));
}
}

In your main mod class, inside <init>:

HelloCommand.register();

Uses:

  • RegisterCommandsEvent
  • Commands.literal
  • CommandSourceStack.sendSuccess
  • CommandSourceStack.hasPermission

src/main/java/com/example/examplemod/command/HelloCommand.java

package com.example.examplemod.command;
import com.mojang.brigadier.CommandDispatcher;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.Component;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.RegisterCommandsEvent;
// The /hello command, built with Brigadier and registered on Forge's game event bus (RegisterCommandsEvent).
public final class HelloCommand {
private HelloCommand() {
}
// Called from the mod's constructor.
public static void register() {
MinecraftForge.EVENT_BUS.addListener(HelloCommand::onRegisterCommands);
}
private static void onRegisterCommands(RegisterCommandsEvent event) {
build(event.getDispatcher());
}
private static void build(CommandDispatcher<CommandSourceStack> dispatcher) {
dispatcher.register(Commands.literal("hello")
.requires(source -> source.hasPermission(0))
.executes(context -> {
context.getSource().sendSuccess(() -> Component.literal("Hello!"), false);
return 1;
}));
}
}

In your main mod class, inside <init>:

HelloCommand.register();

Uses:

  • RegisterCommandsEvent
  • Commands.literal
  • CommandSourceStack.sendSuccess
  • CommandSourceStack.hasPermission

src/main/java/com/example/examplemod/command/HelloCommand.java

package com.example.examplemod.command;
import com.mojang.brigadier.CommandDispatcher;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.Component;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.RegisterCommandsEvent;
// The /hello command, built with Brigadier and registered on NeoForge's game event bus (RegisterCommandsEvent).
public final class HelloCommand {
private HelloCommand() {
}
// Called from the mod's constructor.
public static void register() {
MinecraftForge.EVENT_BUS.addListener(HelloCommand::onRegisterCommands);
}
private static void onRegisterCommands(RegisterCommandsEvent event) {
build(event.getDispatcher());
}
private static void build(CommandDispatcher<CommandSourceStack> dispatcher) {
dispatcher.register(Commands.literal("hello")
.requires(source -> source.hasPermission(0))
.executes(context -> {
context.getSource().sendSuccess(() -> Component.literal("Hello!"), false);
return 1;
}));
}
}

In your main mod class, inside <init>:

HelloCommand.register();

Uses:

  • CommandRegistrationCallback.EVENT
  • Commands.literal
  • CommandSourceStack.sendSuccess
  • CommandSourceStack.hasPermission

src/main/java/com/example/examplemod/command/HelloCommand.java

package com.example.examplemod.command;
import com.mojang.brigadier.CommandDispatcher;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.Component;
// The /hello command, built with Brigadier and registered through Fabric API's CommandRegistrationCallback.
public final class HelloCommand {
private HelloCommand() {
}
// Called from the mod's onInitialize.
public static void register() {
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> build(dispatcher));
}
private static void build(CommandDispatcher<CommandSourceStack> dispatcher) {
dispatcher.register(Commands.literal("hello")
.requires(source -> source.hasPermission(0))
.executes(context -> {
context.getSource().sendSuccess(Component.literal("Hello!"), false);
return 1;
}));
}
}

In your main mod class, inside onInitialize:

HelloCommand.register();

Uses:

  • CommandRegistrationCallback.EVENT
  • Commands.literal
  • CommandSourceStack.sendSuccess
  • CommandSourceStack.hasPermission

src/main/java/com/example/examplemod/command/HelloCommand.java

package com.example.examplemod.command;
import net.minecraft.network.chat.TextComponent;
import com.mojang.brigadier.CommandDispatcher;
import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.Component;
// The /hello command, built with Brigadier and registered through Fabric API's CommandRegistrationCallback.
public final class HelloCommand {
private HelloCommand() {
}
// Called from the mod's onInitialize.
public static void register() {
CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> build(dispatcher));
}
private static void build(CommandDispatcher<CommandSourceStack> dispatcher) {
dispatcher.register(Commands.literal("hello")
.requires(source -> source.hasPermission(0))
.executes(context -> {
context.getSource().sendSuccess(new TextComponent("Hello!"), false);
return 1;
}));
}
}

In your main mod class, inside onInitialize:

HelloCommand.register();

Uses:

  • RegisterCommandsEvent
  • Commands.literal
  • CommandSource.sendSuccess
  • CommandSource.hasPermission

src/main/java/com/example/examplemod/command/HelloCommand.java

package com.example.examplemod.command;
import net.minecraft.util.text.StringTextComponent;
import com.mojang.brigadier.CommandDispatcher;
import net.minecraft.command.CommandSource;
import net.minecraft.command.Commands;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.RegisterCommandsEvent;
// The /hello command, built with Brigadier and registered on Forge's game event bus (RegisterCommandsEvent).
public final class HelloCommand {
private HelloCommand() {
}
// Called from the mod's constructor.
public static void register() {
MinecraftForge.EVENT_BUS.addListener(HelloCommand::onRegisterCommands);
}
private static void onRegisterCommands(RegisterCommandsEvent event) {
build(event.getDispatcher());
}
private static void build(CommandDispatcher<CommandSource> dispatcher) {
dispatcher.register(Commands.literal("hello")
.requires(source -> source.hasPermission(0))
.executes(context -> {
context.getSource().sendSuccess(new StringTextComponent("Hello!"), false);
return 1;
}));
}
}

In your main mod class, inside <init>:

HelloCommand.register();

Uses:

  • CommandBase
  • CommandHandler.registerCommand
  • WorldEvent.Load

1.12.2 commands are classes that extend CommandBase, not Brigadier trees. FMLServerStartingEvent only reaches the @Mod class, so the template registers the command from a WorldEvent.Load handler.

src/main/java/com/example/examplemod/command/HelloCommand.java

package com.example.examplemod.command;
import com.example.examplemod.ExampleMod;
import net.minecraft.command.CommandBase;
import net.minecraft.command.CommandHandler;
import net.minecraft.command.ICommandSender;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.text.TextComponentString;
import net.minecraftforge.event.world.WorldEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
// The /hello command: a 1.12.2 command extends CommandBase. The class also registers itself, when the first world of a server loads.
@Mod.EventBusSubscriber(modid = ExampleMod.MOD_ID)
public class HelloCommand extends CommandBase {
@Override
public String getName() {
return "hello";
}
@Override
public String getUsage(ICommandSender sender) {
return "/hello";
}
// 0 is everyone, 2 is operators.
@Override
public int getRequiredPermissionLevel() {
return 0;
}
@Override
public void execute(MinecraftServer server, ICommandSender sender, String[] args) {
sender.sendMessage(new TextComponentString("Hello!"));
}
// FMLServerStartingEvent only reaches the @Mod class, so this handler registers the command when the overworld loads.
@SubscribeEvent
public static void onWorldLoad(WorldEvent.Load event) {
if (!event.getWorld().isRemote && event.getWorld().provider.getDimension() == 0) {
((CommandHandler) event.getWorld().getMinecraftServer().getCommandManager()).registerCommand(new HelloCommand());
}
}
}

Linen’s How-tos library has this page too, with the example for your own mod’s loader and Minecraft version. Hover a name in your code and Linen points to the how-tos that explain it. Both are free. Download Linen.