AXHud

模组

· 作者 timo2243

我插件的 HUD 模组

0 本站下载 0 关注 更新于 11 个月前

需要前置:Fabric API

最新 1.0.0 · 1.21.7 · Fabric

一键开服(送7天)

AXHud

本模组是我为部分插件开发的 HUD 模组。

功能:

  • 目前,该模组可以在用户屏幕顶部物品的旁边显示文本

命令

  • /axhud test diamond:测试显示效果
  • /axhud clear:清除显示

依赖项:

你可以将此模组用于你自己的模组/插件。

查看方法 [Paper/Spigot 1.21.x]
package <your_package>;

import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public class CommunicationInterface {
    private final JavaPlugin plugin;

    /**
     * 构造函数 - 传入你的插件实例
     *
     * 在你的主插件类中的用法:
     * 
     * public class <your_plugin> extends JavaPlugin {
     *     private CommunicationInterface commInterface;
     *
     *     @Override
     *     public void onEnable() {
     *         this.getServer().getMessenger().registerOutgoingPluginChannel(this, "axhud:display_item");
     *         this.commInterface = new CommunicationInterface(this);
     *     }
     *
     *     public CommunicationInterface getCommInterface() {
     *         return commInterface;
     *     }
     * }
     * 
     */
    public CommunicationInterface(JavaPlugin plugin) {
        this.plugin = plugin;
    }

    /**
     * 向玩家客户端发送物品显示数据
     *
     * @param player 接收数据的玩家
     * @param itemId 物品 ID(例如:"minecraft:diamond"、"minecraft:emerald")
     * @param displayName 在物品旁边显示的名称(支持颜色代码如 §a、§b 等)
     */
    public void sendItemDisplay(Player player, String itemId, String displayName) {
        ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
        DataOutputStream out = new DataOutputStream(byteStream);

        try {
            // 写入物品 ID 字符串
            writeString(out, itemId);

            // 写入显示名称字符串
            writeString(out, displayName);

            // 发送插件消息
            player.sendPluginMessage(plugin, "axhud:display_item", byteStream.toByteArray());
        } catch (IOException e) {
            plugin.getLogger().warning("Failed to send item display to player: " + e.getMessage());
        }
    }

    /**
     * 按照 Minecraft 的格式将字符串写入 DataOutputStream
     * (VarInt 长度 + UTF-8 字节)
     */
    private void writeString(DataOutputStream out, String str) throws IOException {
        byte[] bytes = str.getBytes(java.nio.charset.StandardCharsets.UTF_8);
        writeVarInt(out, bytes.length);
        out.write(bytes);
    }

    /**
     * 向 DataOutputStream 写入 VarInt(Minecraft 协议格式)
     */
    private void writeVarInt(DataOutputStream out, int value) throws IOException {
        while ((value & 0xFFFFFF80) != 0) {
            out.writeByte((value & 0x7F) | 0x80);
            value >>>= 7;
        }
        out.writeByte(value & 0x7F);
    }
}

你是这个资源的作者?登录后可认领此资源。

评论 0

0.0
暂无评分
5 星
0
4 星
0
3 星
0
2 星
0
1 星
0
我的评分

登录后参与评分

还没有评论,来抢沙发吧

评论配图

相似资源