Class ClientCommandManager

java.lang.Object
net.fabricmc.fabric.api.client.command.v2.ClientCommandManager

public final class ClientCommandManager extends Object
Manages client-sided commands and provides some related helper methods.

Client-sided commands are fully executed on the client, so players can use them in both singleplayer and multiplayer.

Registrations can be done in handlers for ClientCommandRegistrationCallback.EVENT (See example below.)

The commands are run on the client game thread by default. Avoid doing any heavy calculations here as that can freeze the game's rendering. For example, you can move heavy code to another thread.

This class also has alternatives to the server-side helper methods in CommandManager: literal(String) and argument(String, ArgumentType).

The precedence rules of client-sided and server-sided commands with the same name are an implementation detail that is not guaranteed to remain the same in future versions. The aim is to make commands from the server take precedence over client-sided commands in a future version of this API.

Example command

 
 ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) -> {
 		dispatcher.register(
 			ClientCommandManager.literal("hello").executes(context -> {
 				context.getSource().sendFeedback(Text.literal("Hello, world!"));
 				return 0;
 			})
 		);
 });
 
 
  • Method Details

    • getActiveDispatcher

      @Nullable public static @Nullable com.mojang.brigadier.CommandDispatcher<FabricClientCommandSource> getActiveDispatcher()
      Gets the active command dispatcher that handles client command registration and execution.

      May be null when not connected to a server (dedicated or integrated).

      Returns:
      active dispatcher if present
    • literal

      public static com.mojang.brigadier.builder.LiteralArgumentBuilder<FabricClientCommandSource> literal(String name)
      Creates a literal argument builder.
      Parameters:
      name - the literal name
      Returns:
      the created argument builder
    • argument

      public static <T> com.mojang.brigadier.builder.RequiredArgumentBuilder<FabricClientCommandSource,T> argument(String name, com.mojang.brigadier.arguments.ArgumentType<T> type)
      Creates a required argument builder.
      Type Parameters:
      T - the type of the parsed argument value
      Parameters:
      name - the name of the argument
      type - the type of the argument
      Returns:
      the created argument builder