Skip to content

How to make a command that runs on the client (Minecraft mod)

A client command runs in the player’s own game, so it works on any server, even one without your mod. Fabric API registers it with ClientCommandRegistrationCallback.EVENT and a literal command node (ClientCommandManager.DISPATCHER on 1.18.2 and older); Forge and NeoForge use RegisterClientCommandsEvent, which fires only in the client. On 1.12.2 a client command is a CommandBase registered with ClientCommandHandler.

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

  1. Register it from client-only code.
  2. Build it like a server command with literal(…) and .executes(…) (on 1.12.2, write a CommandBase subclass).
  3. Reply to the player with the source’s feedback.

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

Setting In the example What it does
message You are here. The message it shows.

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

Fabric 1.19 to 1.21, Fabric 1.21.1, Fabric 1.21.2 to 1.21.11

Section titled “Fabric 1.19 to 1.21, Fabric 1.21.1, Fabric 1.21.2 to 1.21.11”

Uses:

  • ClientCommandRegistrationCallback.EVENT
  • ClientCommandManager.literal
  • FabricClientCommandSource.sendFeedback

src/main/java/com/example/examplemod/client/WhereClientCommand.java

package com.example.examplemod.client;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
import net.minecraft.network.chat.Component;
// /where, a command that runs in the player's own game (Fabric API: ClientCommandRegistrationCallback).
public final class WhereClientCommand {
private WhereClientCommand() {
}
// Called from the mod's client entrypoint.
public static void register() {
ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) -> dispatcher.register(ClientCommandManager.literal("where")
.executes(context -> {
context.getSource().sendFeedback(Component.literal("You are here."));
return 1;
})));
}
}

In your client setup class, inside onInitializeClient:

WhereClientCommand.register();

Uses:

  • RegisterClientCommandsEvent
  • Commands.literal
  • CommandSourceStack.sendSuccess

src/main/java/com/example/examplemod/client/WhereClientCommand.java

package com.example.examplemod.client;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.Component;
import net.minecraftforge.client.event.RegisterClientCommandsEvent;
import net.minecraftforge.common.MinecraftForge;
// /where, a command that runs in the player's own game (Forge: RegisterClientCommandsEvent, client only).
public final class WhereClientCommand {
private WhereClientCommand() {
}
// Called from the mod's constructor, in the client only.
public static void register() {
MinecraftForge.EVENT_BUS.addListener(WhereClientCommand::onRegisterClientCommands);
}
private static void onRegisterClientCommands(RegisterClientCommandsEvent event) {
event.getDispatcher().register(Commands.literal("where")
.executes(context -> {
context.getSource().sendSuccess(new TextComponent("You are here."), false);
return 1;
}));
}
}

In your main mod class, inside <init>:

if (FMLEnvironment.dist == Dist.CLIENT) {
WhereClientCommand.register();
}

Uses:

  • RegisterClientCommandsEvent
  • Commands.literal
  • CommandSourceStack.sendSuccess

src/main/java/com/example/examplemod/client/WhereClientCommand.java

package com.example.examplemod.client;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.Component;
import net.minecraftforge.client.event.RegisterClientCommandsEvent;
import net.minecraftforge.common.MinecraftForge;
// /where, a command that runs in the player's own game (Forge: RegisterClientCommandsEvent, client only).
public final class WhereClientCommand {
private WhereClientCommand() {
}
// Called from the mod's constructor, in the client only.
public static void register() {
MinecraftForge.EVENT_BUS.addListener(WhereClientCommand::onRegisterClientCommands);
}
private static void onRegisterClientCommands(RegisterClientCommandsEvent event) {
event.getDispatcher().register(Commands.literal("where")
.executes(context -> {
context.getSource().sendSuccess(Component.literal("You are here."), false);
return 1;
}));
}
}

In your main mod class, inside <init>:

if (FMLEnvironment.dist == Dist.CLIENT) {
WhereClientCommand.register();
}

Uses:

  • RegisterClientCommandsEvent
  • Commands.literal
  • CommandSourceStack.sendSuccess

src/main/java/com/example/examplemod/client/WhereClientCommand.java

package com.example.examplemod.client;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.Component;
import net.minecraftforge.client.event.RegisterClientCommandsEvent;
import net.minecraftforge.common.MinecraftForge;
// /where, a command that runs in the player's own game (Forge: RegisterClientCommandsEvent, client only).
public final class WhereClientCommand {
private WhereClientCommand() {
}
// Called from the mod's constructor, in the client only.
public static void register() {
MinecraftForge.EVENT_BUS.addListener(WhereClientCommand::onRegisterClientCommands);
}
private static void onRegisterClientCommands(RegisterClientCommandsEvent event) {
event.getDispatcher().register(Commands.literal("where")
.executes(context -> {
context.getSource().sendSuccess(() -> Component.literal("You are here."), false);
return 1;
}));
}
}

In your main mod class, inside <init>:

if (FMLEnvironment.dist == Dist.CLIENT) {
WhereClientCommand.register();
}

Uses:

  • RegisterClientCommandsEvent
  • Commands.literal
  • CommandSourceStack.sendSuccess

src/main/java/com/example/examplemod/client/WhereClientCommand.java

package com.example.examplemod.client;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.Component;
import net.minecraftforge.client.event.RegisterClientCommandsEvent;
import net.minecraftforge.common.MinecraftForge;
// /where, a command that runs in the player's own game (NeoForge: RegisterClientCommandsEvent, client only).
public final class WhereClientCommand {
private WhereClientCommand() {
}
// Called from the mod's constructor, in the client only.
public static void register() {
MinecraftForge.EVENT_BUS.addListener(WhereClientCommand::onRegisterClientCommands);
}
private static void onRegisterClientCommands(RegisterClientCommandsEvent event) {
event.getDispatcher().register(Commands.literal("where")
.executes(context -> {
context.getSource().sendSuccess(() -> Component.literal("You are here."), false);
return 1;
}));
}
}

In your main mod class, inside <init>:

if (FMLEnvironment.dist == Dist.CLIENT) {
WhereClientCommand.register();
}

NeoForge 1.20.2 to 1.21.1, NeoForge 1.21.2 to 1.21.8

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

Uses:

  • RegisterClientCommandsEvent
  • Commands.literal
  • CommandSourceStack.sendSuccess

src/main/java/com/example/examplemod/client/WhereClientCommand.java

package com.example.examplemod.client;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.Component;
import net.neoforged.neoforge.client.event.RegisterClientCommandsEvent;
import net.neoforged.neoforge.common.NeoForge;
// /where, a command that runs in the player's own game (NeoForge: RegisterClientCommandsEvent, client only).
public final class WhereClientCommand {
private WhereClientCommand() {
}
// Called from the mod's constructor, in the client only.
public static void register() {
NeoForge.EVENT_BUS.addListener(WhereClientCommand::onRegisterClientCommands);
}
private static void onRegisterClientCommands(RegisterClientCommandsEvent event) {
event.getDispatcher().register(Commands.literal("where")
.executes(context -> {
context.getSource().sendSuccess(() -> Component.literal("You are here."), false);
return 1;
}));
}
}

In your main mod class, inside <init>:

if (FMLEnvironment.dist == Dist.CLIENT) {
WhereClientCommand.register();
}

Uses:

  • RegisterClientCommandsEvent
  • Commands.literal
  • CommandSourceStack.sendSuccess

src/main/java/com/example/examplemod/client/WhereClientCommand.java

package com.example.examplemod.client;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.Component;
import net.neoforged.neoforge.client.event.RegisterClientCommandsEvent;
import net.neoforged.neoforge.common.NeoForge;
// /where, a command that runs in the player's own game (NeoForge: RegisterClientCommandsEvent, client only).
public final class WhereClientCommand {
private WhereClientCommand() {
}
// Called from the mod's constructor, in the client only.
public static void register() {
NeoForge.EVENT_BUS.addListener(WhereClientCommand::onRegisterClientCommands);
}
private static void onRegisterClientCommands(RegisterClientCommandsEvent event) {
event.getDispatcher().register(Commands.literal("where")
.executes(context -> {
context.getSource().sendSuccess(() -> Component.literal("You are here."), false);
return 1;
}));
}
}

In your main mod class, inside <init>:

if (FMLEnvironment.getDist() == Dist.CLIENT) {
WhereClientCommand.register();
}

Uses:

  • ClientCommandRegistrationCallback.EVENT
  • ClientCommands.literal
  • FabricClientCommandSource.sendFeedback

Since 26.1 the literal node comes from ClientCommands.literal.

src/main/java/com/example/examplemod/client/WhereClientCommand.java

package com.example.examplemod.client;
import net.fabricmc.fabric.api.client.command.v2.ClientCommands;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
import net.minecraft.network.chat.Component;
// /where, a command that runs in the player's own game (Fabric API: ClientCommandRegistrationCallback).
public final class WhereClientCommand {
private WhereClientCommand() {
}
// Called from the mod's client entrypoint.
public static void register() {
ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) -> dispatcher.register(ClientCommands.literal("where")
.executes(context -> {
context.getSource().sendFeedback(Component.literal("You are here."));
return 1;
})));
}
}

In your client setup class, inside onInitializeClient:

WhereClientCommand.register();

Uses:

  • ClientCommandRegistrationCallback.EVENT
  • ClientCommandManager.literal
  • FabricClientCommandSource.sendFeedback

Before 1.19 the Fabric client command API is v1: register on ClientCommandManager.DISPATCHER, and the reply text is a TextComponent.

src/main/java/com/example/examplemod/client/WhereClientCommand.java

package com.example.examplemod.client;
import net.minecraft.network.chat.TextComponent;
import net.fabricmc.fabric.api.client.command.v1.ClientCommandManager;
import net.minecraft.network.chat.Component;
// /where, a command that runs in the player's own game (Fabric API: ClientCommandManager).
public final class WhereClientCommand {
private WhereClientCommand() {
}
// Called from the mod's client entrypoint.
public static void register() {
ClientCommandManager.DISPATCHER.register(ClientCommandManager.literal("where")
.executes(context -> {
context.getSource().sendFeedback(new TextComponent("You are here."));
return 1;
}));
}
}

In your client setup class, inside onInitializeClient:

WhereClientCommand.register();

Uses:

  • ClientCommandHandler.registerCommand
  • CommandBase

In 1.12.2 a client command is a CommandBase subclass registered with ClientCommandHandler.instance.registerCommand.

src/main/java/com/example/examplemod/client/WhereClientCommand.java

package com.example.examplemod.client;
import net.minecraft.command.CommandBase;
import net.minecraft.command.ICommandSender;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.text.TextComponentString;
import net.minecraftforge.client.ClientCommandHandler;
// /where, a command that runs in the player's own game (Forge: ClientCommandHandler, client only).
public class WhereClientCommand extends CommandBase {
// Called from preInit, in the client only.
public static void register() {
ClientCommandHandler.instance.registerCommand(new WhereClientCommand());
}
@Override
public String getName() {
return "where";
}
@Override
public String getUsage(ICommandSender sender) {
return "/where";
}
@Override
public int getRequiredPermissionLevel() {
return 0;
}
@Override
public void execute(MinecraftServer server, ICommandSender sender, String[] args) {
sender.sendMessage(new TextComponentString("You are here."));
}
}

In your main mod class, inside preInit:

if (event.getSide() == Side.CLIENT) {
WhereClientCommand.register();
}

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.