Minecraft Fabric Client 教程 #3 添加自定义Command

添加ChatUtils

先在cn.enaium.excel里新建一个包utils

然后创建一个ChatUtils.java

内容

package cn.enaium.excel.utils;

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.hud.ChatHud;
import net.minecraft.text.LiteralText;
import net.minecraft.text.Text;

/**
 * @Author Enaium
 * @Date 2020/1/5 12:54
 */
public class ChatUtils {

    public static void component(Text component)
    {
        ChatHud chatHud = MinecraftClient.getInstance().inGameHud.getChatHud();
        LiteralText prefix = new LiteralText("\u00a7c[\u00a76Excel\u00a7c]\u00a7r ");
        chatHud.addMessage(prefix.append(component));
    }

    public static void message(String message)
    {
        component(new LiteralText(message));
    }
}

添加自定义command

先在cn.enaium.excel里新建一个包command

下载command.zip

将压缩包里面的内容全部放进去

a

然后再Excel.java里添加command

    public CommandManager commandManager;

    public void onEnable() {
        commandManager = new CommandManager();
        commandManager.loadCommands();
    }

注入Mixin

在mixin包里面新建一个ClientPlayerEntityMixin.java

内容

package cn.enaium.excel.mixin;

import cn.enaium.excel.Excel;
import net.minecraft.client.network.ClientPlayerEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

/**
 * @Author Enaium
 * @Date 2020/1/5 13:27
 */
@Mixin(ClientPlayerEntity.class)
public class ClientPlayerEntityMixin {

    @Inject(at = @At("HEAD"),
            method = "sendChatMessage(Ljava/lang/String;)V",
            cancellable = true)
    private void onSendChatMessage(String message, CallbackInfo info)
    {
        if (Excel.INSTANCE.commandManager.processCommand(message))
            info.cancel();
    }

}

添加到mixin.json里面

  "client": [
    "ExampleMixin",
    "MinecraftClientMixin",
    "ClientPlayerEntityMixin"
  ],

启动

a

输入-或者-help 输入返回的信息不会在控制台出现

完成