💬 text_component#

Converting and scanning Minecraft text components.

convert#

text_component_to_str(
tc: TextComponent,
) → str[source]#

Convert a TextComponent to the plain string it displays.

>>> text_component_to_str("hello")
'hello'
>>> text_component_to_str(["hello", " ", "world"])
'hello world'
>>> text_component_to_str({"text": "Hello"})
'Hello'
>>> text_component_to_str({"text": "Hello", "extra": [{"text": " World"}]})
'Hello World'
>>> text_component_to_str({"text": "A", "extra": ["B", {"text": "C"}]})
'ABC'
>>> text_component_to_str({"color": "red"})
''
item_id_to_text_component(
item_id: str,
use_default: bool = True,
) → TextComponent[source]#

Get the TextComponent from an item id

Parameters:
  • item_id – The item id, ex: “minecraft:stick” or “iyc:adamantium_ingot”

  • use_default – Whether to use the default prettified string if no TextComponent is found

Returns:

The TextComponent of the item, ex: “Stick” or {“text”:”Adamantium Ingot”}

Return type:

str

components_name(
components: JsonDict,
) → TextComponent[source]#

The name an item’s components give it, a music disc’s record name first, “” when they give none.

item_id_to_name(item_id: str) → str[source]#

Get the name from an item id

Parameters:

item_id – The item id, ex: “minecraft:stick” or “iyc:adamantium_ingot”

Returns:

The name of the item, ex: “Stick” or “Adamantium Ingot”

Return type:

str

scan#

Scanning and rewriting text components inside already-generated files.

By the time the finalization plugins run, every text component has been serialised into the text of a datapack file: a tellraw argument, a loot table entry, a dialog body. Those files are not plain JSON (bolt and mecha emit single-quoted strings and unquoted keys), so the passes that need to rewrite them work on fragments of text rather than on parsed objects.

Both stewbeet.plugins.auto.lang_file and stewbeet.plugins.auto.text_renders do the same three things, which is what this module factors out: iterate the datapack’s text files, skip the ones that cannot possibly match, and splice a list of replacements back in one pass.

CLOSERS: dict[str, str] = {'"': '"', "'": "'", '(': ')', '[': ']', '{': '}'}[source]#

Closing character of each opening one, used when walking a fragment of serialized text.

class Replacement(start: int, end: int, fragment: str)[source]#

Bases: NamedTuple

A slice of a file’s text to overwrite.

>>> Replacement(start=1, end=5, fragment='"x"')
Replacement(start=1, end=5, fragment='"x"')
start: int[source]#

Index of the first character to replace.

end: int[source]#

Index just past the last character to replace.

fragment: str[source]#

Text to splice in place of string[start:end].

find_enclosing_object(
string: str,
match_start: int,
match_end: int,
) → tuple[int, int] | None[source]#

Slice bounds of the JSON object enclosing a match, None when there is none.

Walks backwards from match_start to the opening ‘{’, then forwards to its matching ‘}’, nested braces included.

Parameters:

match_end – Unused, kept for API consistency.

>>> find_enclosing_object('{"color":"red","text":"hi"}', 16, 25)
(0, 27)
>>> find_enclosing_object('{"outer":{"text":"inner"}}', 10, 24)
(9, 25)
opening_brace(string: str, start: int) → int | None[source]#

Index of the ‘{’ enclosing start, walking backwards over the objects closed before it.

closing_brace(string: str, opening: int) → int | None[source]#

Index just past the ‘}’ matching the ‘{’ at opening.

apply_replacements(
string: str,
replacements: list[Replacement],
) → str[source]#

Splice every replacement into string in a single pass.

Replacements may come in any order and must not overlap; they are sorted here and applied from the end so earlier indices stay valid.

Returns:

The rewritten text, the input itself when there is nothing to do.

>>> apply_replacements('{"a":1,"b":2}', [Replacement(1, 6, '"x":9')])
'{"x":9,"b":2}'
>>> apply_replacements('abcdef', [Replacement(4, 6, 'Z'), Replacement(0, 2, 'Y')])
'YcdZ'
>>> apply_replacements('unchanged', [])
'unchanged'
iter_data_text_files(
ctx: Context | None = None,
) → Iterator[TextFileBase[str]][source]#

Yield every text file of the datapack, loot tables first.

Loot tables are pulled in explicitly: they are not always reached through ctx.data.all() depending on how they were registered.

Parameters:

ctx – The beet context (None falls back to Mem.ctx).

Yields:

TextFileBase[str] – Each text file, in a stable order so builds stay reproducible.