Skip to content

Commit

Permalink
Allow strings in extra Motd (#715)
Browse files Browse the repository at this point in the history
* Allow strings in extra Motd

* Correctly resolve type error

If we get list as MOTD, it is actually the same as `{"extra": returned_motd}`,
so we should sync the type change here as well.
  • Loading branch information
PerchunPak authored Dec 31, 2023
1 parent 49e997c commit e693371
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
6 changes: 5 additions & 1 deletion mcstatus/motd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,11 @@ def _parse_as_dict(
auto_add = list(filter(lambda e: type(e) is Formatting and e != Formatting.RESET, parsed_motd))

for element in item["extra"]:
parsed_motd.extend(cls._parse_as_dict(element, auto_add=auto_add.copy()))
parsed_motd.extend(
cls._parse_as_dict(element, auto_add=auto_add.copy())
if isinstance(element, dict)
else auto_add + cls._parse_as_str(element, bedrock=bedrock)
)

return parsed_motd

Expand Down
4 changes: 2 additions & 2 deletions mcstatus/status_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class RawJavaResponseVersion(TypedDict):
class RawJavaResponseMotdWhenDict(TypedDict, total=False):
text: str # only present if `translate` is set
translate: str # same to the above field
extra: list[RawJavaResponseMotdWhenDict]
extra: list[RawJavaResponseMotdWhenDict | str]

color: str
bold: bool
Expand All @@ -34,7 +34,7 @@ class RawJavaResponseMotdWhenDict(TypedDict, total=False):
underlined: bool
obfuscated: bool

RawJavaResponseMotd: TypeAlias = "RawJavaResponseMotdWhenDict | list[RawJavaResponseMotdWhenDict] | str"
RawJavaResponseMotd: TypeAlias = "RawJavaResponseMotdWhenDict | list[RawJavaResponseMotdWhenDict | str] | str"

class RawJavaResponse(TypedDict):
description: RawJavaResponseMotd
Expand Down
13 changes: 13 additions & 0 deletions tests/motd/test_motd.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,19 @@ def test_top_level_formatting_can_be_overwrote(self) -> None:
Formatting.RESET,
]

def test_top_level_formatting_applies_to_string_inside_extra(self) -> None:
"""Although, it is probably a bug in some modded cores, Minecraft supports it, and we should as well.
See `#711 <https://github.com/py-mine/mcstatus/issues/711>`_.
"""
assert Motd.parse({"text": "top", "bold": True, "extra": ["not top"]}).parsed == [
Formatting.BOLD,
"top",
Formatting.RESET,
Formatting.BOLD,
"not top",
]

def test_formatting_key_set_to_false_here_without_it_being_set_to_true_before(self) -> None:
"""Some servers set the formatting keys to false here, even without it ever being set to true before.
Expand Down

0 comments on commit e693371

Please sign in to comment.