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

Posted enaium

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Minecraft Fabric Client 教程 #3 添加自定义Command相关的知识,希望对你有一定的参考价值。

首发于Enaium的个人博客


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[u00a76Excelu00a7c]u00a7r ");
        chatHud.addMessage(prefix.append(component));
    }

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

添加自定义command

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

下载技术图片

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

技术图片

然后再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"
  ],

启动

技术图片

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

完成

以上是关于Minecraft Fabric Client 教程 #3 添加自定义Command的主要内容,如果未能解决你的问题,请参考以下文章

Minecraft Fabric Client 教程 #4 添加Modules

Minecraft Fabric Client 教程 #5 添加EventSprint和ToggleCommand

Minecraft Fabric Client 教程 #2 修改文件和目录名添加run和stop

Minecraft Fabric 教程 #1 开发环境配置

Minecraft Fabric模组开发踩坑:缺失Fabric API

Minecraft Fabric模组开发踩坑:缺失Fabric API