Skip to content

TextHelper ​

com.jsmacrosce.jsmacros.client.api.helper.TextHelper

Since1.0.8

Wraps a Minecraft text component, which can contain text with different styles and click or hover behavior.

visit(com.jsmacrosce.jsmacros.core.MethodWrapper) visits each text run with its StyleHelper; getString() returns the flattened text without styling.

Create helpers from strings, translation keys, or component JSON with the corresponding Chat.createTextHelperFrom... methods. replaceFromString(String) replaces this helper's component; it does not update text already displayed elsewhere.

js
// Read the component's flattened text
const line = Chat.createTextHelperFromString("some styled line");  
Chat.log(`flattened: ${line.getString()}`);  

// Inspect each formatted run
line.visit(JavaWrapper.methodToJava(function (style, run) {
  if (style.bold()) {
    Chat.log(`bold run: ${run}`);  
  }
}));  

// Build translated text for display
Chat.actionbar(Chat.createTextHelperFromTranslationKey("block.minecraft.stone"));

Fields ​

STRIP_FORMATTING_PATTERN ​

static STRIP_FORMATTING_PATTERN: PatternSince1.0.8read-only#
Matches a section sign followed by a legacy formatting code (0-9, A-F, K-O, or R), case-insensitively. Used by getStringStripFormatting() and withoutFormatting().

Methods ​

getJson ​

getJson(): StringSince1.2.7#
Returns: the component serialized as JSON using the current player's registry access

getString ​

getString(): StringSince1.2.7#
Returns: the component's flattened text, without style information

getStringStripFormatting ​

getStringStripFormatting(): StringSince1.6.5#
Returns: the flattened text with legacy section-sign formatting codes removed

getWidth ​

getWidth(): intSince1.8.4#
Returns: the rendered width of the component in client font units

toString ​

toString(): StringSince1.0.8#
Returns: a TextHelper:{"text": "..."} representation containing the flattened text

Note: this used to do the same as getString

visit ​

visit(MethodWrapper<StyleHelper, String, Object, ?> visitor): TextHelperSince1.6.5#
Visits each text run with its style and text, then returns this helper.

Parameters:

  • visitor: callback receiving a StyleHelper and the run's text
Returns: self for chaining

withoutFormatting ​

withoutFormatting(): TextHelperSince1.8.4#
Returns: a new helper containing the flattened text with legacy section-sign formatting codes removed

wrap ​

static wrap(Component t): TextHelper#
Wraps a component in a helper, retaining the supplied component as-is.

Parameters:

  • t: the component to wrap, or null
Returns: a new helper retaining t, or null when t is null