Minecraft 工具

RText

API 包 路径: mcdreforged.api.rtext

Minecraft 高级文本组件库

感谢 Pandaria98stext API

文本样式

class mcdreforged.minecraft.rtext.style.RItem[源代码]

Minecraft 文本样式

property name: str

其在 Minecraft 文本容器中的 value 值

class mcdreforged.minecraft.rtext.style.RItemClassic(name: str, mc_code: str, console_code: str)[源代码]

基类:RItem, ABC

一种传统的 RItem 对象,可以使用以 “§” 为前缀的传统样式代码,或一个 “\033[31m” 风格的 ANSI 转义代码来表示

property mc_code: str

在 Minecraft 中的样式代码,以 “§” 为前缀

property console_code: str

在控制台中的样式代码,即 ANSI 转义代码

如果没有合适的转义代码,其值可能为空串

class mcdreforged.minecraft.rtext.style.RColor(rgb_code: int)[源代码]

Minecraft 文本颜色

black: RColorClassic = RColorClassic[name=black,mc_code=§0,console_code='\x1b[30m']
dark_blue: RColorClassic = RColorClassic[name=dark_blue,mc_code=§1,console_code='\x1b[34m']
dark_green: RColorClassic = RColorClassic[name=dark_green,mc_code=§2,console_code='\x1b[32m']
dark_aqua: RColorClassic = RColorClassic[name=dark_aqua,mc_code=§3,console_code='\x1b[36m']
dark_red: RColorClassic = RColorClassic[name=dark_red,mc_code=§4,console_code='\x1b[31m']
dark_purple: RColorClassic = RColorClassic[name=dark_purple,mc_code=§5,console_code='\x1b[35m']
gold: RColorClassic = RColorClassic[name=gold,mc_code=§6,console_code='\x1b[33m']
gray: RColorClassic = RColorClassic[name=gray,mc_code=§7,console_code='\x1b[37m\x1b[2m']
dark_gray: RColorClassic = RColorClassic[name=dark_gray,mc_code=§8,console_code='\x1b[37m\x1b[2m']
blue: RColorClassic = RColorClassic[name=blue,mc_code=§9,console_code='\x1b[94m']
green: RColorClassic = RColorClassic[name=green,mc_code=§a,console_code='\x1b[92m']
aqua: RColorClassic = RColorClassic[name=aqua,mc_code=§b,console_code='\x1b[96m']
red: RColorClassic = RColorClassic[name=red,mc_code=§c,console_code='\x1b[91m']
light_purple: RColorClassic = RColorClassic[name=light_purple,mc_code=§d,console_code='\x1b[95m']
yellow: RColorClassic = RColorClassic[name=yellow,mc_code=§e,console_code='\x1b[93m']
white: RColorClassic = RColorClassic[name=white,mc_code=§f,console_code='\x1b[37m']
reset: RColorClassic = RColorClassic[name=reset,mc_code=§r,console_code='\x1b[0m']
classmethod from_mc_value(value: str) RColor[源代码]

一个工厂函数,从给定的 Minecraft 颜色值构造一个 RColor 对象

参数:

value – Minecraft 文本容器 中 color 属性的值,如 "red""blue""#00AAFF"

返回:

与之对应的 RColor 对象

抛出:

ValueError – 若给定的值不是一个合法的 Minecraft 颜色

property r: int

颜色的红色分量,范围为 [0, 255]

property g: int

颜色的绿色分量,范围为 [0, 255]

property b: int

颜色的蓝色分量,范围为 [0, 255]

class mcdreforged.minecraft.rtext.style.RColorClassic(name: str, rgb_code: int, mc_code: str, console_code: str)[源代码]

基类:RItemClassic, RColor

使用颜色名字定义的传统 Minecraft 文本颜色

注意

不要自己构造这个类。如果你想使用传统颜色,请使用 RColor 类的属性

to_rgb() RColorRGB[源代码]

转换为与之对应的,颜色值完全一致的 RColorRGB 对象

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: RStyleClassic = RStyleClassic[name=bold,mc_code=§l,console_code='\x1b[1m']
italic: RStyleClassic = RStyleClassic[name=italic,mc_code=§o,console_code='']
underlined: RStyleClassic = RStyleClassic[name=underlined,mc_code=§n,console_code='']
strikethrough: RStyleClassic = RStyleClassic[name=strikethrough,mc_code=§m,console_code='']
obfuscated: RStyleClassic = RStyleClassic[name=obfuscated,mc_code=§k,console_code='']
class mcdreforged.minecraft.rtext.style.RStyleClassic(name: str, mc_code: str, console_code: str)[源代码]

基类:RItemClassic, RStyle

传统 Minecraft 文本样式,拥有着对应的以 “§” 为前缀的样式代码

class mcdreforged.minecraft.rtext.style.RAction[源代码]

Minecraft 文本点击事件的动作

suggest_command: RAction = _RActionImpl[name=suggest_command]

使用给定的文本填充聊天栏

run_command: RAction = _RActionImpl[name=run_command]

将给定的文本作为指令运行

(Minecraft <1.19.1) 如果给定的文本不以 "/" 开头,给定的文本将会被当做聊天信息发送至服务端。借此,玩家在点击被修饰的文本后可以自动执行 MCDR 指令

注意

在原版 Minecraft >=1.19.1 中,只有以 "/" 开头的字符串,即代表指令的字符串,可以被用作 run_command 动作的值

对于其他不以 "/" 开头的字符串,客户端将拒接发送对应的聊天消息

Issue #203

open_url: RAction = _RActionImpl[name=open_url]

打开给定链接

open_file: RAction = _RActionImpl[name=open_file]

从给定路径打开文件

备注

事实上原版 Minecraft 并不允许指令中的文本包含 open_file 动作,因此当你发现这个 open_file 不起作用时别太惊讶

copy_to_clipboard: RAction = _RActionImpl[name=copy_to_clipboard]

将给定文本复制到剪切板

备注

在 Minecraft 1.15+ 中可用

文本组件

class mcdreforged.minecraft.rtext.text.RTextBase[源代码]

Minecraft 文本组件的抽象基类

to_json_object() dict | list[源代码]

返回一个代表其数据的对象,该对象可以被序列化为 json 字符串

to_json_str() str[源代码]

返回一个代表其数据的 json 格式字符串

它可以作为如 Minecraft 命令 /tellraw <target> <message> 中的第二个参数

to_plain_text() str[源代码]

返回纯文本以供控制台显示

点击事件和悬停事件将被忽略

to_colored_text() str[源代码]

返回由 ANSI 转移代码染色的带颜色文本,以供控制台显示

点击事件和悬停事件将被忽略

to_legacy_text() str[源代码]

返回由以 “§” 为前缀的传统 Minecraft 样式代码染色的带颜色文本

点击事件和悬停事件将被忽略

copy() Self[源代码]

返回自身的一个深拷贝副本

set_color(color: RColor) Self[源代码]

设置文本的颜色,并返回文本组件本身

set_styles(styles: RStyle | Iterable[RStyle]) Self[源代码]

设置文本的样式,并返回文本组件本身

set_click_event(action: RAction, value: str) Self[源代码]

设置点击事件

方法 c() 是方法 set_click_event() 的简写

参数:
  • action – 动作类型

  • value – 动作的字符串值

返回:

自身

set_hover_text(*args) Self[源代码]

设置悬浮文本

方法 h() 是方法 set_hover_text() 的简写

参数:

args – 用于创建 RTextList 实例的元素。RTextList 实例将作为实际的悬浮文本

返回:

自身

c(action: RAction, value: str) Self[源代码]

方法 set_click_event() 的简写

h(*args) Self[源代码]

方法 set_hover_text() 的简写

static from_any(text) RTextBase[源代码]

将任意对象转换为一个 RText 组件

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**kwargsfmt 的基础上进行格式化并生成一个 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) 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, color: RColor | None = None, styles: RStyle | Iterable[RStyle] | None = None)[源代码]

基类:RTextBase

常规文本组件类

__init__(text, color: RColor | None = None, styles: RStyle | Iterable[RStyle] | None = None)[源代码]

给定文本创建一个 RText 对象,可选文本颜色及样式

参数:
  • text – 文本内容。它将被转换为 str

  • color – 可选参数,文本的颜色

  • styles – 可选参数,文本的样式。它可以是单一一个 RStyle,也可以是 RStyle 的一个可迭代对象

class mcdreforged.minecraft.rtext.text.RTextList(*args)[源代码]

基类:RTextBase

一个储存着若干个 RTextBase 对象的列表,即一个复合文本组件

__init__(*args)[源代码]

使用给定的 *args 创建一个 RTextList

参数:

args – 此 RTextList 中的元素。它们可以是 strRTextBase 或任何实现了 __str__ 方法的类。所有的非 RTextBase 元素都将被转换为 RText

class mcdreforged.minecraft.rtext.text.RTextTranslation(translation_key: str, color: ~mcdreforged.minecraft.rtext.style.RColor = RColorClassic[name=reset,mc_code=§r,console_code='\x1b[0m'], styles: ~mcdreforged.minecraft.rtext.style.RStyle | ~typing.Iterable[~mcdreforged.minecraft.rtext.style.RStyle] | None = None)[源代码]

基类:RText

翻译文本组件类。几乎和 RText 相同

__init__(translation_key: str, color: ~mcdreforged.minecraft.rtext.style.RColor = RColorClassic[name=reset,mc_code=§r,console_code='\x1b[0m'], styles: ~mcdreforged.minecraft.rtext.style.RStyle | ~typing.Iterable[~mcdreforged.minecraft.rtext.style.RStyle] | None = None)[源代码]

使用特定的翻译键创建一个 RTextTranslation 对象。其余参数与 RText 相同

如果这个翻译需要一些参数,你可以使用方法 arg() 来设置

例子:

RTextTranslation('advancements.nether.root.title', color=RColor.red)
参数:
  • translation_key – 翻译键

  • color – 可选参数,文本的颜色

  • styles – 可选参数,文本的样式。它可以是单一一个 RStyle,也可以是 RStyle 的一个可迭代对象

arg(*args: Any) Self[源代码]

设置翻译参数

参数:

args – 翻译的参数

fallback(fallback: str) Self[源代码]

设置翻译缺省文本

注意

仅适用于 Minecraft >= 1.19.4

参数:

fallback – 用于在翻译键未被识别时显示的缺省文本

class mcdreforged.translation.translation_text.RTextMCDRTranslation(translation_key: str, *args, **kwargs)[源代码]

基类:RTextBase

MCDR 翻译文本组件类

当 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 – 在上下文中进行翻译时所用的语言

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 服务端

__init__(address: str, port: int, password: str, *, logger: Logger | None = None)[源代码]

创建一个 rcon 客户端实例

参数:
  • address – rcon 服务器的地址

  • port – rcon 服务器的端口

  • password – rcon 服务器的密码

关键字参数:

logger – 可选参数,一个 logging.Logger 实例。它用于输出一些警告信息,例如接受数据包失败

connect() bool[源代码]

启动与 rcon 服务器的连接并尝试登录

返回:

连接与登录是否成功

disconnect()[源代码]

断开与服务器的连接

send_command(command: str, max_retry_time: int = 3) str | None[源代码]

将命令发送到 rcon 服务器

参数:
  • command – 要发送到服务器的命令

  • max_retry_time – 操作的最大重试次数

返回:

服务器的命令执行结果。如果重试次数超过 max_retry_time,则将返回 None