Skip to content

Commit

Permalink
插件初始版本
Browse files Browse the repository at this point in the history
Signed-off-by: 涅槃 <mcnirvana@126.com>
  • Loading branch information
MC-Nirvana committed Nov 3, 2024
1 parent dfa73d7 commit 5ad3e2a
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
14 changes: 14 additions & 0 deletions mcdreforged.plugin.json
Original file line number Diff line number Diff line change
@@ -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"
}
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
strip_ansi
42 changes: 42 additions & 0 deletions venture_chat_handler/__init__.py
Original file line number Diff line number Diff line change
@@ -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<hour>\d+):(?P<min>\d+):(?P<sec>\d+) (?P<logging>[^]]+)]'
r': (?P<content>.*)'
)

# 获取玩家消息解析的正则表达式模式列表
@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())
13 changes: 13 additions & 0 deletions venture_chat_handler/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from mcdreforged.api.all import *

# 定义默认配置
class RegexConfig(Serializable):
chat_prefix_regex: str = r'(\[.*] )?(?P<name>[^>]+)>>> (?P<message>.*)'

# 全局配置对象
config: RegexConfig

# 加载配置文件
def load_config(server):
global config
config = server.load_config_simple(target_class=RegexConfig)

0 comments on commit 5ad3e2a

Please sign in to comment.