Minecraft(我的世界)中文论坛
标题: [未知之域]聊天组件API中文翻译——来发送一条更酷炫的消息吧!
作者: 耗子 时间: 2018-8-6 01:19
标题: [未知之域]聊天组件API中文翻译——来发送一条更酷炫的消息吧!
本帖最后由 耗子 于 2018-8-6 01:32 编辑
聊天组件 API
使用 BungeeCord 聊天 API 构建消息
Chat API Javadoc
目录
- 基础
- 颜色与格式
- 事件
- 客户端翻译
- Component Builder(组件建造者)
- 常见的问题
- 动作
- ClickEvent(点击事件)动作
- HoverEvent(悬浮事件)动作
基础
最简单的组件是 TextComponent(文本组件),在 BungeeCord 的使用方法如下:
- player.sendMessage( new TextComponent( "Hello world" ) );
复制代码
或者你想在 Spigot 中使用:
- player.spigot().sendMessage( new TextComponent( "Hello world" ) );
复制代码
这是一个简单的消息,没有格式和颜色。
颜色与格式
每个组件都支持如下功能:
- 颜色(Color)
- 粗体(Bold)
- 斜体(Italic)
- 下划线(Underline)
- 删除线(Strikethrough)
- 乱码(Obfuscate)
- 事件(Events)(在下文中会详细介绍)
举个例子:
- TextComponent message = new TextComponent( "Hello world" );
- message.setColor( ChatColor.RED );
- message.setBold( true );
- player.sendMessage( message );
复制代码
这将显示 "Hello World"(红色,加粗). 任何格式和事件也应用于子组件,除非子组件覆盖设置。例如:
- TextComponent message = new TextComponent( "Hello " );
- message.setColor( ChatColor.RED );
- message.setBold( true );
- TextComponent world = new TextComponent( "world" );
- world.setColor( ChatColor.BLUE );
- message.addExtra( world );
- message.addExtra( "!" );
- player.sendMessage( message );
复制代码
将会显示 "Hello world!"(全文为粗体,"Hello"和"!"为红色,"world"为蓝色)
事件
事件允许当文本被操作(点击或鼠标悬浮)时执行动作。目前 Minecraft 仅支持两个事件,分别是 HoverEvent(鼠标悬浮事件)和 ClickEvent(点击事件)。两个事件都有一个动作(当事件发生之后执行什么)和一个数值(这个动作的参数)。例如,如果一个点击事件的动作是 OPEN_URL(打开URL),那么它的值必须是一个有效的URL,例如"http://spigotmc.org"。
- TextComponent message = new TextComponent( "Click me" );
- message.setClickEvent( new ClickEvent( ClickEvent.Action.OPEN_URL, "http://spigotmc.org" ) );
- message.setHoverEvent( new HoverEvent( HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("Goto the Spigot website!").create() ) );
- player.spigot().sendMessage( message );
复制代码
客户端翻译
TranslatableComponent(可翻译组件)可以用来发送翻译键(key)让客户端翻译,这意味着你只能使用 Minecraft 提供的文本(这里查看),除非使用一个资源包来添加更多文本。一些支持参数的翻译的参数可以是TranslatableComponent(或者只是 TextComponent)。
- TranslatableComponent giveMessage = new TranslatableComponent( "commands.give.success" );
- TranslatableComponent item = new TranslatableComponent( "item.swordDiamond.name" );
- item.setColor( ChatColor.GOLD );
- giveMessage.addWith( item );
- giveMessage.addWith( "32" );
- TextComponent username = new TextComponent( "Thinkofdeath" );
- username.setColor( ChatColor.AQUA );
- giveMessage.addWith( username );
- player.sendMessage( giveMessage );
复制代码
以上的代码在客户端使用的语言为en_US(美式英语)时将会显示"Given Diamond Sword 32 to Thinkofdeath", 但如果客户端使用的语言为zh_CN(简体中文)时将会显示"成功将 钻石剑 32 给予 Thinkofdeath"。
组件建造者(Component Builder)
简单的消息可以使用 ComponentBuilder(组件建造者)节省很多工作。ComponentBuilder 是一个链式结构的对象,允许快速创建消息。例如:
- player.sendMessage( new ComponentBuilder( "Hello " ).color( ChatColor.RED ).bold( true ).append( "world" ).color( ChatColor.BLUE ).append( "!" ).color( ChatColor.RED ).create() );
复制代码
这将显示 "Hello world!"
常见的问题
直接创建 BaseComponent 实例是没用的 (例如 player.sendMessage( new BaseComponent(){} );)。 应使用 TextComponent 或 TranslatableComponent。
使用旧版的颜色代码 (例如 player.sendMessage( new TextComponent( ChatColor.RED + ":-(" ) );) 将会不能很好的显示甚至导致客户端异常。使用 TextComponent.fromLegacyText() 方法将旧格式转换为新格式
动作
所有的 ClickEvent(点击事件) 和 HoverEvent(悬浮事件)的动作如下所示:
点击事件动作
当用户点击消息时,这些动作将会执行:
- Action.OPEN_URL //在用户的浏览器打开指定URL
- Action.OPEN_FILE //在用户的电脑打开指定文件
- Action.RUN_COMMAND //让用户运行指令
- Action.SUGGEST_COMMAND //在用户的输入框设置文字
- Action.CHANGE_PAGE //改变书本的页码
复制代码
悬浮事件动作
当用户鼠标指针悬浮在消息上时,这些动作将会执行:
- Action.SHOW_TEXT //显示一个文本
- Action.SHOW_ACHIEVEMENT //显示一个成就及其介绍
- Action.SHOW_ITEM //显示一个物品的名字和其他信息
- Action.SHOW_ENTITY //显示一个实体的名字,ID和其他信息 。
复制代码
发现很多人并不了解这个API,特此重发到MCBBS上。
欢迎各位支持作者,以编写和翻译更多优秀教程。
[afd]mouse[/afd]
[groupid=1181]Unknown Domain[/groupid]
作者: 肥猫欧嘎桑 时间: 2018-8-6 03:58
滋磁一下
作者: Kyokuki 时间: 2018-8-17 11:10
非常棒的一篇教程,收藏了
作者: Clusters_stars 时间: 2018-8-17 17:45
请问可以使用§吗= =和
message.replace("§","&");
作者: 耗子 时间: 2018-8-17 17:55
在该教程里,不建议使用颜色代码。建议使用
作者: Clusters_stars 时间: 2018-8-17 17:59
个人感觉这样可以自定义emmmm
作者: Clusters_stars 时间: 2018-8-17 18:00
谢谢了 = =
作者: chenmoand 时间: 2018-8-21 17:16
大佬我问一下player.spigot().sendMessage( message );
输出一行,可player.spigot().sendMessage( message + m2);
无法,怎么让json消息能+起来
作者: 耗子 时间: 2018-8-21 17:36
不能+哦,只能使用append
作者: chenmoand 时间: 2018-8-21 19:00
谢谢了 大佬
作者: gooding300 时间: 2018-8-21 20:11
或许可以去申请个高亮?
http://www.mcbbs.net/thread-540567-1-1.html
作者: guo1060924736 时间: 2019-8-18 12:37
如何展示物品呢,用到悬浮事件的Action.SHOW_ITEM类型时,后面new ComponentBuilder()只能加string。如何加入物品的信息?
作者: 血亦寒 时间: 2020-3-2 14:12
看了一会儿,竟然看懂了一些!这么深奥的东西......学到了
作者: 妮玛煤了 时间: 2020-3-3 12:40
提示: 作者被禁止或删除 内容自动屏蔽
作者: Dream守护 时间: 2020-3-3 16:58
支持一下
作者: xiaozhang421 时间: 2020-3-4 11:32
good job !!!
作者: Pointer 时间: 2020-3-11 00:20
Action.OPEN_FILE 没有反应 :C
作者: MappyJ 时间: 2020-5-12 16:49
谢谢楼主的分享!找了很久了,值得学习一下!