ForcePilot/backend/package/yuxi/channel/extensions/minecraft/outbound.py

46 lines
1.3 KiB
Python
Raw Normal View History

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])