Minecraft 工具
RText
API 包 路径: mcdreforged.api.rtext
Minecraft 高级文本组件库
感谢 Pandaria98 的 stext API 库,这同时是此库的灵感来源
文本样式
- class mcdreforged.minecraft.rtext.style.RItemClassic(name: str, mc_code: str, console_code: str)[源代码]
-
一种传统的
RItem对象,可以使用以 "§" 为前缀的传统样式代码,或一个 "\033[31m" 风格的 ANSI 转义代码来表示参见
Minecraft Wiki 格式化代码 页面
- class mcdreforged.minecraft.rtext.style.RColor(rgb_code: int)[源代码]
Minecraft 文本颜色
- black: ClassVar[RColorClassic] = RColorClassic(name='black', mc_code='§0', console_code='\x1b[30m')
- dark_blue: ClassVar[RColorClassic] = RColorClassic(name='dark_blue', mc_code='§1', console_code='\x1b[34m')
- dark_green: ClassVar[RColorClassic] = RColorClassic(name='dark_green', mc_code='§2', console_code='\x1b[32m')
- dark_aqua: ClassVar[RColorClassic] = RColorClassic(name='dark_aqua', mc_code='§3', console_code='\x1b[36m')
- dark_red: ClassVar[RColorClassic] = RColorClassic(name='dark_red', mc_code='§4', console_code='\x1b[31m')
- dark_purple: ClassVar[RColorClassic] = RColorClassic(name='dark_purple', mc_code='§5', console_code='\x1b[35m')
- gold: ClassVar[RColorClassic] = RColorClassic(name='gold', mc_code='§6', console_code='\x1b[33m')
- gray: ClassVar[RColorClassic] = RColorClassic(name='gray', mc_code='§7', console_code='\x1b[37m\x1b[2m')
- dark_gray: ClassVar[RColorClassic] = RColorClassic(name='dark_gray', mc_code='§8', console_code='\x1b[37m\x1b[2m')
- blue: ClassVar[RColorClassic] = RColorClassic(name='blue', mc_code='§9', console_code='\x1b[94m')
- green: ClassVar[RColorClassic] = RColorClassic(name='green', mc_code='§a', console_code='\x1b[92m')
- aqua: ClassVar[RColorClassic] = RColorClassic(name='aqua', mc_code='§b', console_code='\x1b[96m')
- red: ClassVar[RColorClassic] = RColorClassic(name='red', mc_code='§c', console_code='\x1b[91m')
- light_purple: ClassVar[RColorClassic] = RColorClassic(name='light_purple', mc_code='§d', console_code='\x1b[95m')
- yellow: ClassVar[RColorClassic] = RColorClassic(name='yellow', mc_code='§e', console_code='\x1b[93m')
- white: ClassVar[RColorClassic] = RColorClassic(name='white', mc_code='§f', console_code='\x1b[37m')
- reset: ClassVar[RColorClassic] = RColorClassic(name='reset', mc_code='§r', console_code='\x1b[0m')
- class mcdreforged.minecraft.rtext.style.RColorClassic(name: str, rgb_code: int, mc_code: str, console_code: str)[源代码]
基类:
RItemClassic,RColor使用颜色名字定义的传统 Minecraft 文本颜色
注意
不要自己构造这个类。如果你想使用传统颜色,请使用
RColor类的属性
- class mcdreforged.minecraft.rtext.style.RColorRGB(rgb_code: int)[源代码]
使用十六进制 RGB 颜色格式定义的 Minecraft 文本颜色。借助它你可以精准地指定颜色的 RGB 值
备注
在 Minecraft 1.16+ 中可用
- __init__(rgb_code: int)[源代码]
- 参数:
rgb_code -- 一个 int,其值在十六进制下为 0xRRGGBB 格式,或者一个形如
"RRGGBB"、"0xRRGGBB"或"#RRGGBB"的字符串
- classmethod from_code(rgb_code: str | int) RColorRGB[源代码]
一个工厂函数,使用 RGB 颜色代码构造一个
RColorRGB对象- 参数:
rgb_code -- 一个 int,其值在十六进制下为 0xRRGGBB 格式,或者一个形如
"RRGGBB"、"0xRRGGBB"或"#RRGGBB"的字符串
- classmethod from_rgb(red: int, green: int, blue: int) RColorRGB[源代码]
一个工厂函数,使用 3 个 RGB 颜色值构造一个
RColorRGB对象- 参数:
red -- 颜色的红色分量
green -- 颜色的绿色分量
blue -- 颜色的蓝色分量
- to_classic() RColorClassic[源代码]
转换为在 RGB 颜色空间中与之欧氏距离最近的
RColorClassic对象
- class mcdreforged.minecraft.rtext.style.RStyle[源代码]
Minecraft 文本样式
- bold: ClassVar[RStyleClassic] = RStyleClassic(name='bold', mc_code='§l', console_code='\x1b[1m')
- italic: ClassVar[RStyleClassic] = RStyleClassic(name='italic', mc_code='§o', console_code='')
- underlined: ClassVar[RStyleClassic] = RStyleClassic(name='underlined', mc_code='§n', console_code='')
- strikethrough: ClassVar[RStyleClassic] = RStyleClassic(name='strikethrough', mc_code='§m', console_code='')
- obfuscated: ClassVar[RStyleClassic] = RStyleClassic(name='obfuscated', mc_code='§k', console_code='')
文本点击事件
- class mcdreforged.minecraft.rtext.click_event.RClickAction[源代码]
Minecraft 文本点击事件行为
在 v2.15.0 版本加入.
- suggest_command: ClassVar[RClickAction[RClickSuggestCommand]] = _RClickActionImpl(action='suggest_command', class=<class 'mcdreforged.minecraft.rtext.click_event.RClickSuggestCommand'>)
使用给定的文本填充聊天栏
- run_command: ClassVar[RClickAction[RClickRunCommand]] = _RClickActionImpl(action='run_command', class=<class 'mcdreforged.minecraft.rtext.click_event.RClickRunCommand'>)
将给定的文本作为命令运行
(Minecraft <1.19.1) 如果给定的文本不以
"/"开头,给定的文本将会被当做聊天信息发送至服务端。借此,玩家在点击被修饰的文本后可以自动执行 MCDR 命令注意
在原版 Minecraft >=1.19.1 中,只有以
"/"开头的字符串,即代表命令的字符串,可以被用作run_command动作的值对于其他不以
"/"开头的字符串,客户端将拒接发送对应的聊天消息
- open_url: ClassVar[RClickAction[RClickOpenUrl]] = _RClickActionImpl(action='open_url', class=<class 'mcdreforged.minecraft.rtext.click_event.RClickOpenUrl'>)
打开给定链接
- open_file: ClassVar[RClickAction[RClickOpenFile]] = _RClickActionImpl(action='open_file', class=<class 'mcdreforged.minecraft.rtext.click_event.RClickOpenFile'>)
从给定路径打开文件
- copy_to_clipboard: ClassVar[RClickAction[RClickCopyToClipboard]] = _RClickActionImpl(action='copy_to_clipboard', class=<class 'mcdreforged.minecraft.rtext.click_event.RClickCopyToClipboard'>)
将给定文本复制到剪切板
备注
在 Minecraft 1.15+ 中可用
- change_page: ClassVar[RClickAction[RClickChangePage]] = _RClickActionImpl(action='change_page', class=<class 'mcdreforged.minecraft.rtext.click_event.RClickChangePage'>)
要跳转到当前打开的书本页数
备注
仅在成书中的有效
- show_dialog: ClassVar[RClickAction[RClickShowDialog]] = _RClickActionImpl(action='show_dialog', class=<class 'mcdreforged.minecraft.rtext.click_event.RClickShowDialog'>)
打开一个对话框
备注
在 Minecraft 1.21.6+ 中可用
在 v2.15.0 版本加入.
- custom: ClassVar[RClickAction[RClickCustom]] = _RClickActionImpl(action='custom', class=<class 'mcdreforged.minecraft.rtext.click_event.RClickCustom'>)
在服务端触发一个自定义文本事件,后续表现取决于服务端的 mod / 插件,原版服务端对此不会有任何响应
备注
在 Minecraft 1.21.6+ 中可用
在 v2.15.0 版本加入.
- mcdreforged.minecraft.rtext.click_event.RAction
RClickAction的别名在 v1.0.0 版本加入.
- class mcdreforged.minecraft.rtext.click_event.RClickEvent[源代码]
Minecraft 文本点击事件组件的抽象基类
在 v2.15.0 版本加入.
- abstract property action: RClickAction
返回该组件的点击事件行为
- final to_json_object(json_format: RTextJsonFormat) dict[源代码]
将自身序列化成一个 json dict
- 参数:
json_format -- 目标 json 格式
- final classmethod from_json_object(click_event: dict, json_format: RTextJsonFormat) RClickEvent[源代码]
将一个 json dict 反序列化回一个点击事件组件
- 参数:
click_event -- 要反序列化的 json dict
json_format -- 源 json 格式
- class mcdreforged.minecraft.rtext.click_event.RClickEventSingleValue[源代码]
那些只包含 1 个值的点击事件组件的抽象基类
在 v2.15.0 版本加入.
- class mcdreforged.minecraft.rtext.click_event.RClickSuggestCommand(command: str)[源代码]
表示
RClickAction.suggest_command的点击事件组件在 v2.15.0 版本加入.
- class mcdreforged.minecraft.rtext.click_event.RClickRunCommand(command: str)[源代码]
表示
RClickAction.run_command的点击事件组件在 v2.15.0 版本加入.
- class mcdreforged.minecraft.rtext.click_event.RClickOpenUrl(url: str)[源代码]
表示
RClickAction.open_url的点击事件组件在 v2.15.0 版本加入.
- class mcdreforged.minecraft.rtext.click_event.RClickOpenFile(path: str)[源代码]
表示
RClickAction.open_file的点击事件组件在 v2.15.0 版本加入.
- class mcdreforged.minecraft.rtext.click_event.RClickCopyToClipboard(value: str)[源代码]
表示
RClickAction.copy_to_clipboard的点击事件组件在 v2.15.0 版本加入.
- class mcdreforged.minecraft.rtext.click_event.RClickShowDialog(dialog: str | dict)[源代码]
表示
RClickAction.show_dialog的点击事件组件备注
在 Minecraft 1.21.6+ 中可用
在 v2.15.0 版本加入.
- class mcdreforged.minecraft.rtext.click_event.RClickChangePage(page: int)[源代码]
表示
RClickAction.change_page的点击事件组件在 v2.15.0 版本加入.
文本悬浮事件
- class mcdreforged.minecraft.rtext.hover_event.RHoverAction[源代码]
Minecraft 文本悬浮事件行为
在 v2.15.0 版本加入.
- show_text: ClassVar[RHoverAction[RHoverText]] = _RHoverActionImpl(action='show_text', class=<class 'mcdreforged.minecraft.rtext.hover_event.RHoverText'>)
展示悬浮文本
- show_entity: ClassVar[RHoverAction[RHoverEntity]] = _RHoverActionImpl(action='show_entity', class=<class 'mcdreforged.minecraft.rtext.hover_event.RHoverEntity'>)
展示实体信息
- show_item: ClassVar[RHoverAction[RHoverItem]] = _RHoverActionImpl(action='show_item', class=<class 'mcdreforged.minecraft.rtext.hover_event.RHoverItem'>)
展示物品信息
- class mcdreforged.minecraft.rtext.hover_event.RHoverEvent[源代码]
Minecraft 文本悬浮事件组件的抽象基类
在 v2.15.0 版本加入.
- abstract property action: RHoverAction
返回该组件的悬浮事件行为
- final to_json_object(json_format: RTextJsonFormat) dict[源代码]
将自身序列化成一个 json dict
- 参数:
json_format -- 目标 json 格式
- final classmethod from_json_object(hover_event: dict, json_format: RTextJsonFormat) RHoverEvent[源代码]
将一个 json dict 反序列化回一个悬浮事件组件
- 参数:
hover_event -- 要反序列化的 json dict
json_format -- 源 json 格式
- class mcdreforged.minecraft.rtext.hover_event.RHoverText(text: RTextBase)[源代码]
表示
RHoverAction.show_text的悬浮事件组件在 v2.15.0 版本加入.
- class mcdreforged.minecraft.rtext.hover_event.RHoverEntity(id: str, uuid: UUID, name: str | RTextBase | None = None)[源代码]
表示
RHoverAction.show_entity的悬浮事件组件在 v2.15.0 版本加入.
文本组件
- class mcdreforged.minecraft.rtext.text.RTextBase[源代码]
Minecraft 文本组件的抽象基类
- abstractmethod to_json_object(**kwargs: Unpack[ToJsonKwargs]) dict | list[源代码]
返回一个代表其数据的对象,该对象可以被序列化为 json 字符串
- to_json_str(**kwargs: Unpack[ToJsonKwargs]) str[源代码]
返回一个代表其数据的 json 格式字符串
它可以作为如 Minecraft 命令
/tellraw <target> <message>中的第二个参数
- final set_click_event(event: RClickEvent, /) Self[源代码]
- final set_click_event(*, event: RClickEvent) Self
- final set_click_event(action: RClickAction[RClickEventSingleValue[_T]], value: _T, /) Self
- final set_click_event(*, action: RClickAction[RClickEventSingleValue[_T]], value: _T) Self
设置点击事件
你既可以直接提供一个点击事件组件,也可以提供点击事件组件类型和组件的值
text = RText('example') text.set_click_event(RClickSuggestCommand('/say something')) # directly text.set_click_event(event=RClickSuggestCommand('/say something')) # directly (kwarg) text.set_click_event(RClickAction.suggest_command, '/say something') # action + value text.set_click_event(action=RClickAction.suggest_command, value='/say something') # action + value (kwarg)
方法
c()是方法set_click_event()的简写
- abstractmethod set_hover_event(hover_event: RHoverEvent) Self[源代码]
设置悬浮事件
- set_hover_text(*args) Self[源代码]
设置悬浮文本
方法
h()是方法set_hover_text()的简写
- c(arg1: RClickEvent | RClickAction[RClickEventSingleValue[_T]] = <unset>, arg2: _T = <unset>, /, *, event: RClickEvent = <unset>, action: RClickAction[RClickEventSingleValue[_T]] = <unset>, value: _T = <unset>) Self
方法
set_click_event()的别名
- h(*args) Self
方法
set_hover_text()的别名
- static join(divider: Any, iterable: Iterable[Any]) RTextBase[源代码]
就像
str.join()方法一样,它使用 divider 连接任意数量的文本例子:
>>> text = RTextBase.join(',', [RText('1'), '2', 3]) >>> text.to_plain_text() '1,2,3'
- 参数:
divider -- 元素间的分隔符。这个分隔符对象将被复用
iterable -- 将被 join 的元素
- static format(fmt: str, *args, **kwargs) RTextBase[源代码]
就像
str.format()方法一样,它使用 *args 及 **kwargs 在 fmt 的基础上进行格式化并生成一个 RText 组件例子:
>>> text = RTextBase.format('a={},b={},c={c}', RText('1', color=RColor.blue), '2', c=3) >>> text.to_plain_text() 'a=1,b=2,c=3'
- 参数:
fmt -- 格式字符串
args -- 用于格式化的参数
kwargs -- 用于格式化的关键字参数
- classmethod from_json_object(data: str | list | dict, **kwargs: Unpack[FromJsonKwargs]) RTextBase[源代码]
将一个 json 对象转换为一个
RText组件例子:
>>> text = RTextBase.from_json_object({'text': 'my text', 'color': 'red'}) >>> text.to_plain_text() 'my text' >>> text.to_json_object()['color'] 'red'
- 参数:
data -- 一个 json 对象
在 v2.4.0 版本加入.
- class mcdreforged.minecraft.rtext.text.RText(text: Any, color: RColor | None = None, styles: RStyle | Iterable[RStyle] | None = None)[源代码]
基类:
RTextBase常规文本组件类
- class mcdreforged.minecraft.rtext.text.RTextList(*args: Any)[源代码]
基类:
RTextBase一个储存着若干个
RTextBase对象的列表,即一个复合文本组件
- class mcdreforged.minecraft.rtext.text.RTextTranslation(translation_key: str, color: RColor = RColorClassic(name='reset', mc_code='§r', console_code='\x1b[0m'), styles: RStyle | Iterable[RStyle] | None = None)[源代码]
基类:
RText翻译文本组件类。几乎和
RText相同- __init__(translation_key: str, color: RColor = RColorClassic(name='reset', mc_code='§r', console_code='\x1b[0m'), styles: RStyle | Iterable[RStyle] | None = None)[源代码]
使用特定的翻译键创建一个
RTextTranslation对象。其余参数与RText相同如果这个翻译需要一些参数,你可以使用方法
arg()来设置例子:
RTextTranslation('advancements.nether.root.title', color=RColor.red)
- class mcdreforged.translation.translation_text.RTextMCDRTranslation(translation_key: str, *args, **kwargs)[源代码]
基类:
RTextBaseMCDR 翻译文本组件类
当 MCDR 正在运行时,它将使用
ServerInterface类中的tr()方法作为翻译的方法,并使用 MCDR 的语言作为默认语言在 v2.1.0 版本加入.
- __init__(translation_key: str, *args, **kwargs)[源代码]
使用与翻译相关的必要参数创建一个
RTextMCDRTranslation组件- 参数:
translation_key -- 翻译键
args -- 翻译的参数
kwargs -- 翻译的关键字参数
- classmethod language_context(language: str)[源代码]
创建一个上下文,在其中所有的
RTextMCDRTranslation将会使用给定的语言来进行翻译它主要在你想要在特定语言下,获得该文本组件对应的翻译后的字符串或翻译后的 Minecraft json 文本时使用
MCDR 将自动地于发送信息至玩家或控制台前,使用 用户的偏好语言 应用该上下文管理器
例子:
def log_message_line_by_line(server: ServerInterface): with RTextMCDRTranslation.language_context('en_us'): text: RTextMCDRTranslation = server.rtr('my_plugin.some_message') text_as_str: str = text.to_plain_text() # The translation operation happens here server.logger.info('Lines of my translation') for line in text_as_str.splitlines(): server.logger.info('- {}'.format(line))
- 参数:
language -- 在上下文中进行翻译时所用的语言
杂项
- class mcdreforged.minecraft.rtext.schema.RTextJsonFormat(*values)[源代码]
为不同的 Minecraft 版本定义的
RText组件的序列化格式- V_1_7 = RTextJsonFormatItem(click_event_key='clickEvent', hover_event_key='hoverEvent')
适用于 Minecraft
[1.7, 1.21.5)
- V_1_21_5 = RTextJsonFormatItem(click_event_key='click_event', hover_event_key='hover_event')
适用于 Minecraft
[1.21.5, ~)
Rcon
API 包 路径: mcdreforged.api.rcon
- class mcdreforged.minecraft.rcon.rcon_connection.RconConnection(address: str, port: int, password: str, *, logger: Logger | None = None)[源代码]
一个简单的 rcon 客户端,用于连接到任何支持 rcon 协议的 Minecraft 服务端