消息接口

用于可翻译、可配置且带占位符的彩色消息的 Fabric 消息 API

基础库

message-api

一个简单的 Fabric 服务端消息 API。它支持可翻译消息,并使用 PlaceholderAPI 进行文本解析,最终用户可以轻松自定义。

功能

  • 使用 PlaceholderAPI 进行特殊文本解析
  • 易于自定义的游戏内消息
  • 语言本地化支持
  • 易于集成

用法

添加依赖

要在你的模组中使用 message-api,请将以下内容添加到你的 build.gradle 文件中:

message-api 不包含 placeholder-api,你必须同时捆绑一个兼容版本。将 [TAG] 替换为相应的版本。

repositories {
    maven { url "https://maven.nucleoid.xyz/" }
    maven { url "https://api.modrinth.com/maven" }
}

dependencies {
    include(modImplementation("maven.modrinth:message-api:[TAG]"))
    include(modImplementation("eu.pb4:placeholder-api:[TAG]"))
}

添加消息

在你的资源文件夹中创建一个 messages 文件夹。创建一个 JSON 文件,文件名为你想要添加的语言代码,例如 testmod 示例。将你的消息添加到该文件中(确保消息 ID 以你的模组 ID 为前缀,以避免冲突)。

创建消息

使用 LocalizedMessage 中提供的任意方法获取你的文本实例,它的工作方式与任何其他原版文本实例一样!

ServerPlayerEntity player;
player.sendMessageToClient(LocalizedMessage.localized("modid.some.message.id"));
官方 Mojang 映射
ServerPlayer player;
player.sendSystemMessage(LocalizedMessage.localized("modid.some.message.id"));

用户配置

要更改消息,请编辑 ./config/<modid>/<languageid>.json 中的 JSON 文件。这将更改指定 modid 的模组的语言文件。

致谢

特别感谢 PlaceholderAPI 提供解析功能。