Skip to content
This repository was archived by the owner on Dec 8, 2025. It is now read-only.

Commit b3acf4e

Browse files
committed
Swap text component parsers to SNBT
1 parent e42cfe5 commit b3acf4e

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

mecha/parse.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@
184184
from .config import CommandTree
185185
from .error import MechaError
186186
from .spec import CommandSpec, Parser
187-
from .utils import JsonQuoteHelper, QuoteHelper, string_to_number
187+
from .utils import JsonQuoteHelper, NbtQuoteHelper, QuoteHelper, string_to_number
188188

189189
NUMBER_PATTERN: str = r"-?(?:\d+\.?\d*|\.\d+)"
190190

@@ -493,8 +493,8 @@ def get_default_parsers() -> Dict[str, Parser]:
493493
]
494494
),
495495
"command:argument:minecraft:column_pos": delegate("column_pos"),
496-
"command:argument:minecraft:component": MultilineParser(delegate("json")),
497-
"command:argument:minecraft:style": MultilineParser(delegate("json")),
496+
"command:argument:minecraft:component": MultilineParser(delegate("nbt")),
497+
"command:argument:minecraft:style": MultilineParser(delegate("nbt")),
498498
"command:argument:minecraft:dimension": delegate("resource_location"),
499499
"command:argument:minecraft:entity": delegate("entity"),
500500
"command:argument:minecraft:entity_anchor": delegate("entity_anchor"),
@@ -1165,13 +1165,7 @@ class NbtParser:
11651165
}
11661166
)
11671167

1168-
quote_helper: QuoteHelper = field(
1169-
default_factory=lambda: QuoteHelper(
1170-
escape_sequences={
1171-
r"\\": "\\",
1172-
}
1173-
)
1174-
)
1168+
quote_helper: QuoteHelper = field(default_factory=NbtQuoteHelper)
11751169

11761170
def __post_init__(self):
11771171
self.compound_entry_parser = self.parse_compound_entry

mecha/utils.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"QuoteHelper",
33
"QuoteHelperWithUnicode",
44
"JsonQuoteHelper",
5+
"NbtQuoteHelper",
56
"InvalidEscapeSequence",
67
"normalize_whitespace",
78
"string_to_number",
@@ -138,6 +139,23 @@ class JsonQuoteHelper(QuoteHelperWithUnicode):
138139
)
139140

140141

142+
@dataclass
143+
class NbtQuoteHelper(QuoteHelperWithUnicode):
144+
"""Quote helper used for snbt."""
145+
146+
escape_sequences: Dict[str, str] = field(
147+
default_factory=lambda: {
148+
r"\\": "\\",
149+
r"\b": "\b",
150+
r"\f": "\f",
151+
r"\n": "\n",
152+
r"\r": "\r",
153+
r"\s": " ",
154+
r"\t": "\t",
155+
}
156+
)
157+
158+
141159
def underline_code(
142160
source: str,
143161
location: SourceLocation,

0 commit comments

Comments
 (0)