diff --git a/mcdreforged.plugin.json b/mcdreforged.plugin.json new file mode 100644 index 0000000..09f1c0a --- /dev/null +++ b/mcdreforged.plugin.json @@ -0,0 +1,14 @@ +{ + "id": "venture_chat_handler", + "version": "1.0.0", + "name": "VentureChatHandler", + "description": { + "zh_cn": "使MCDReforged能够正常解析由VentureChat修改过后的消息", + "en_us": "Make MCDReforged able to parse VentureChat modified messages" + }, + "dependencies": { + "mcdreforged": ">=2.1.0" + }, + "author": "MC_Nirvana", + "link": "https://github.com/MC-Nirvana/VentureChatHandler" +} diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..5097cb0 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +strip_ansi \ No newline at end of file diff --git a/venture_chat_handler/__init__.py b/venture_chat_handler/__init__.py new file mode 100644 index 0000000..2827d85 --- /dev/null +++ b/venture_chat_handler/__init__.py @@ -0,0 +1,42 @@ +import re + +from strip_ansi import strip_ansi +from typing import List +from typing_extensions import override +from mcdreforged.utils import string_utils +from mcdreforged.info_reactor.info import InfoSource, Info +from mcdreforged.handler.impl import AbstractMinecraftHandler +from venture_chat_handler.config import RegexConfig, load_config + +class VentureChatHandler(AbstractMinecraftHandler): + # 获取处理器的名称 + def get_name(self) -> str: + return 'venturechat_handler' + + # 从服务器标准输出中解析原始结果 + @classmethod + def get_server_stdout_raw_result(cls, text: str) -> Info: + if type(text) is not str: + raise TypeError('The text to parse should be a string') + result = Info(InfoSource.SERVER, text) + result.content = strip_ansi(string_utils.clean_console_color_code(text)) + return result + + # 获取内容解析的正则表达式模式 + @classmethod + @override + def get_content_parsing_formatter(cls) -> re.Pattern: + return re.compile( + r'\[(?P\d+):(?P\d+):(?P\d+) (?P[^]]+)]' + r': (?P.*)' + ) + + # 获取玩家消息解析的正则表达式模式列表 + @classmethod + @override + def get_player_message_parsing_formatter(cls) -> List[re.Pattern]: + regex = RegexConfig.chat_prefix_regex + return [re.compile(regex)] +def on_load(server, prev_module): + load_config(server) + server.register_server_handler(VentureChatHandler()) \ No newline at end of file diff --git a/venture_chat_handler/config.py b/venture_chat_handler/config.py new file mode 100644 index 0000000..4bd54ea --- /dev/null +++ b/venture_chat_handler/config.py @@ -0,0 +1,13 @@ +from mcdreforged.api.all import * + +# 定义默认配置 +class RegexConfig(Serializable): + chat_prefix_regex: str = r'(\[.*] )?(?P[^>]+)>>> (?P.*)' + +# 全局配置对象 +config: RegexConfig + +# 加载配置文件 +def load_config(server): + global config + config = server.load_config_simple(target_class=RegexConfig) \ No newline at end of file