新增 Minecraft 渠道扩展,支持在 Yuxi 平台中集成 Minecraft 游戏服务器渠道。 包含以下功能模块: - client: Minecraft 客户端封装 - config: 渠道配置管理 - gateway: SSE/WebSocket 网关接入 - outbound: 外发消息管理 - streaming: 流式消息处理 - pairing: 用户配对与绑定 - security: 安全校验 - auth: 认证管理 - accounts: 账户管理 - dedupe: 消息去重 - monitor: 渠道状态监控 - status: 会话状态管理 - protocol: Minecraft 协议处理 - rcon: RCON 远程控制 - keepalive: 连接保活 - version_adapter: 版本适配 - setup: 初始化设置 - types: 类型定义
46 lines
1.3 KiB
Python
46 lines
1.3 KiB
Python
import logging
|
|
|
|
from yuxi.channel.extensions.minecraft.protocol import write_string
|
|
from yuxi.channel.extensions.minecraft.format import strip_mc_format_codes
|
|
from yuxi.channel.extensions.minecraft.streaming import (
|
|
stream_block_minecraft,
|
|
MC_CHAT_MAX_CHARS,
|
|
)
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
async def send_mc_chat(gateway, content: str) -> None:
|
|
clean = strip_mc_format_codes(content)
|
|
|
|
if len(clean) <= MC_CHAT_MAX_CHARS:
|
|
await _send_single_chat(gateway, clean)
|
|
return
|
|
|
|
async def send_one(chunk: str):
|
|
await _send_single_chat(gateway, chunk)
|
|
|
|
await stream_block_minecraft(send_one, clean)
|
|
|
|
|
|
async def _send_single_chat(gateway, text: str) -> None:
|
|
if not gateway or not gateway.client:
|
|
logger.error("Gateway not available for chat send")
|
|
return
|
|
|
|
stripped = strip_mc_format_codes(text)
|
|
if not stripped:
|
|
return
|
|
|
|
adapter = gateway.adapter
|
|
if stripped.startswith("/"):
|
|
packet_id = adapter.sb("chat_command") if adapter else 0x04
|
|
else:
|
|
packet_id = adapter.sb("chat_message") if adapter else 0x05
|
|
if packet_id is None:
|
|
packet_id = 0x05
|
|
|
|
data = write_string(stripped)
|
|
await gateway.client.send_packet(packet_id, data)
|
|
logger.debug("MC chat sent: %s", text[:50])
|