码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2020-02-22 12:12:26      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:DDM   icc   博客   back   返回   启动   exce   new   zip   

首发于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[\u00a76Excel\u00a7c]\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

标签:DDM   icc   博客   back   返回   启动   exce   new   zip   

原文地址:https://www.cnblogs.com/Enaium/p/12344576.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!