You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Minecraft Fabric模组ClientCommandManager导入错误及跨端适配求助

问题解决指南

一、解决「package net.fabricmc.fabric.api.client.command.v2 does not exist」错误

1. 检查Fabric API依赖

确保你的build.gradle中已正确引入Fabric API,并且版本与你的Minecraft版本匹配:

dependencies {
    // 其他依赖...
    modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"
}

如果是单独引入客户端命令模块,可替换为:

modImplementation "net.fabricmc.fabric-api:fabric-client-commands-v2:${project.fabric_version}"

注意:${project.fabric_version}需在gradle.properties中配置为对应版本(如0.91.1+1.20.1)。

2. 修正代码加载逻辑

ModInitializer是服务端/客户端都会加载的入口类,客户端专属API(如ClientCommandManager)在服务端环境中不存在,因此必须将客户端命令注册移到客户端专属初始化类中:
创建实现ClientModInitializer的类:

import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
import net.minecraft.command.argument.StringArgumentType;

public class CrackTiersClient implements ClientModInitializer {
    @Override
    public void onInitializeClient() {
        ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) -> {
            dispatcher.register(ClientCommandManager.literal("cracktiers")
                    .then(ClientCommandManager.argument("name", StringArgumentType.word())
                            .then(ClientCommandManager.argument("mode", StringArgumentType.word())
                                    .executes(context -> {
                                        // 执行客户端命令逻辑
                                        return 1;
                                    }))));
        });
    }
}

并在fabric.mod.json中配置客户端入口点:

"entrypoints": {
    "main": [
      "你的包名.CrackTiers"
    ],
    "client": [
      "你的包名.CrackTiersClient"
    ]
}

二、实现命令跨客户端/服务端运行

要让命令同时在两端生效,需分别注册客户端和服务端命令,并将公共逻辑抽离到独立类中:

1. 服务端命令注册(原ModInitializer类)

import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.minecraft.command.argument.StringArgumentType;
import static net.fabricmc.fabric.api.command.v2.CommandManager.literal;

public class CrackTiers implements ModInitializer {
    @Override
    public void onInitialize() {
        CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> {
            dispatcher.register(literal("cracktiers")
                    .then(literal("name").then(StringArgumentType.word())
                            .then(literal("mode").then(StringArgumentType.word())
                                    .executes(context -> {
                                        // 调用公共逻辑
                                        CrackTiersCommandLogic.execute(context);
                                        return 1;
                                    }))));
        });
    }
}

2. 公共命令逻辑类

import com.mojang.brigadier.context.CommandContext;
import net.minecraft.command.CommandSource;
import net.minecraft.command.argument.StringArgumentType;

public class CrackTiersCommandLogic {
    public static int execute(CommandContext<? extends CommandSource> context) {
        // 获取命令参数
        String name = StringArgumentType.getString(context, "name");
        String mode = StringArgumentType.getString(context, "mode");
        
        // 区分客户端/服务端逻辑
        if (context.getSource() instanceof net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource) {
            // 客户端专属操作(如渲染提示、修改客户端状态)
        } else if (context.getSource() instanceof net.minecraft.server.command.ServerCommandSource) {
            // 服务端专属操作(如修改世界数据、同步玩家状态)
        }
        
        return 1;
    }
}

3. 客户端命令调用公共逻辑

修改CrackTiersClient中的执行代码,调用公共逻辑:

.executes(context -> {
    CrackTiersCommandLogic.execute(context);
    return 1;
})

内容的提问来源于stack exchange,提问作者Maaz Saeed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 15:44:52