fix: handle properly translatable chat msgs

This commit is contained in:
əlemi 2023-02-16 22:07:12 +01:00
parent 28dac56fc7
commit 717ca56f17
Signed by: alemi
GPG key ID: A4895B84D311642C

View file

@ -1,6 +1,6 @@
import json import json
from typing import Union from typing import Union, List, Dict
from termcolor import colored # TODO don't use a lib and put ANSI escaped by hand maybe? from termcolor import colored # TODO don't use a lib and put ANSI escaped by hand maybe?
@ -23,9 +23,8 @@ _EQUIVALENTS = {
"black": "white" "black": "white"
} }
def _parse_formatted_block(msg:dict) -> str: def _parse_formatted_block(text: str, msg:dict) -> str:
attr = [] attr = []
txt = msg["text"] if "text" in msg else msg["translate"] if "translate" in msg else "N/A"
if "bold" in msg and msg["bold"]: if "bold" in msg and msg["bold"]:
attr.append("bold") attr.append("bold")
if "underlined" in msg and msg["underlined"]: if "underlined" in msg and msg["underlined"]:
@ -33,9 +32,23 @@ def _parse_formatted_block(msg:dict) -> str:
if "obfuscated" in msg and msg["obfuscated"]: if "obfuscated" in msg and msg["obfuscated"]:
attr.append("blink") attr.append("blink")
if "color" in msg: if "color" in msg:
return colored(txt, _EQUIVALENTS[msg["color"]], attrs=attr) return colored(text, _EQUIVALENTS[msg["color"]], attrs=attr)
else: else:
return colored(txt, "white", attrs=attr) return colored(text, "white", attrs=attr)
def _parse_translated_block(translate:str, withs: List[dict]) -> str:
out = ""
if translate == "chat.type.text":
out += "<"
out += parse_chat(withs[0])
out += "> "
out += parse_chat(withs[1])
else: # fallback behavior
out += translate + "("
for elem in withs:
out += parse_chat(elem) + ","
out += ")"
return out
def parse_chat(msg:Union[dict,str], ansi_color:bool=False) -> str: def parse_chat(msg:Union[dict,str], ansi_color:bool=False) -> str:
"""Recursive function to parse minecraft chat json, with optional colors""" """Recursive function to parse minecraft chat json, with optional colors"""
@ -48,17 +61,20 @@ def parse_chat(msg:Union[dict,str], ansi_color:bool=False) -> str:
return str(msg) # It's not json, it's already plaintext return str(msg) # It's not json, it's already plaintext
else: else:
return str(msg) return str(msg)
out = "" out = ""
if "text" in data or "translate" in data:
if "translate" in data and "with" in data:
translation = _parse_translated_block(data["translate"], data["with"])
if ansi_color: if ansi_color:
out += _parse_formatted_block(data) out += _parse_formatted_block(translation, data)
elif "text" in data: else:
out += translation
if "text" in data:
if ansi_color:
out += _parse_formatted_block(data["text"], data)
else:
out += data["text"] out += data["text"]
elif "translate" in data:
out += data["translate"]
if "with" in data:
for elem in data["with"]:
out += parse_chat(elem, ansi_color)
if "extra" in data: if "extra" in data:
for elem in data["extra"]: for elem in data["extra"]:
out += parse_chat(elem, ansi_color) out += parse_chat(elem, ansi_color)