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 转义代码来表示
- 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']
- 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: 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 文本点击事件的动作
- run_command: RAction = _RActionImpl[name=run_command]
将给定的文本作为指令运行
(Minecraft <1.19.1) 如果给定的文本不以
"/"
开头,给定的文本将会被当做聊天信息发送至服务端。借此,玩家在点击被修饰的文本后可以自动执行 MCDR 指令注意
在原版 Minecraft >=1.19.1 中,只有以
"/"
开头的字符串,即代表指令的字符串,可以被用作run_command
动作的值对于其他不以
"/"
开头的字符串,客户端将拒接发送对应的聊天消息
文本组件
- class mcdreforged.minecraft.rtext.text.RTextBase[源代码]
Minecraft 文本组件的抽象基类
- to_json_str() str [源代码]
返回一个代表其数据的 json 格式字符串
它可以作为如 Minecraft 命令
/tellraw <target> <message>
中的第二个参数
- set_click_event(action: RAction, value: str) Self [源代码]
设置点击事件
方法
c()
是方法set_click_event()
的简写- 参数:
action – 动作类型
value – 动作的字符串值
- 返回:
自身
- set_hover_text(*args) Self [源代码]
设置悬浮文本
方法
h()
是方法set_hover_text()
的简写
- 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 – 用于格式化的关键字参数
- class mcdreforged.minecraft.rtext.text.RText(text, color: RColor | None = None, styles: RStyle | Iterable[RStyle] | None = None)[源代码]
基类:
RTextBase
常规文本组件类
- class mcdreforged.minecraft.rtext.text.RTextList(*args)[源代码]
基类:
RTextBase
一个储存着若干个
RTextBase
对象的列表,即一个复合文本组件
- 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)
- 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 服务端