Class ClientCommandManager
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 Summary
Modifier and TypeMethodDescriptionstatic <T> com.mojang.brigadier.builder.RequiredArgumentBuilder<FabricClientCommandSource, T> Creates a required argument builder.static @Nullable com.mojang.brigadier.CommandDispatcher<FabricClientCommandSource> Gets the active command dispatcher that handles client command registration and execution.static com.mojang.brigadier.builder.LiteralArgumentBuilder<FabricClientCommandSource> Creates a literal argument builder.static voidRefresh the command completions.
-
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
-
refreshCommandCompletions
public static void refreshCommandCompletions()Refresh the command completions. This is helpful when a condition as defined usingArgumentBuilder.requires(java.util.function.Predicate<S>)changes for a client command. The method uses the last receivedminecraft:commandspacket and calls its handler. This triggers the client command's condition to be reevaluated.- Throws:
IllegalStateException- if not connected to a server (dedicated or integrated) or nominecraft:commandspacket has been received yet
-
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 argumenttype- the type of the argument- Returns:
- the created argument builder
-