Patbox / TextPlaceholderAPI

Placeholder API for Fabric
GNU Lesser General Public License v3.0
38 stars 17 forks source link

Difficulty post-processing PlaceholderAPI-emitted text #20

Closed John-Paul-R closed 1 year ago

John-Paul-R commented 2 years ago

This set of changes to ParentNode.toText has made post-processing text emitted by PlaceholderAPI far more complex that it was in previous versions.

Consider the scenario:

var defaultStyle = Style.EMPTY.withFormatting(Formatting.GRAY, Formatting.ITALIC);
var playerNameText = Text.empty()
    .append(Text.literal("[UnstyledPrefix] "))
    .append(Text.literal("Steve").formatted(Formatting.AQUA));
var expectedMessage =
    Text.empty()
        .append(playerNameText)
        .append(Text.literal(" is now AFK.").setStyle(defaultStyle));

var out = TextNode.convert(expectedMessage).toText(ParserContext.of(), true);
var stringVal = out.toString();

Which yields:

literal{[UnstyledPrefix] }[siblings=[
    literal{Steve}[style={color=aqua}],
    literal{ is now AFK.}[style={color=gray,italic}]
]]

Before this change, the output would have been:

empty[siblings=[
    empty[siblings=[
        literal{[UnstyledPrefix] },
        literal{Steve}[style={color=aqua}]
    ]],
    literal{ is now AFK.}[style={color=gray,italic}]
]]

While more verbose, this older layout is far simpler to manipulate, as inserted placeholders will always be one self-contained node, in the same shape they were provided. Right now, I'm having trouble doing things like applying styling to all text nodes except for the inserted node.

Patbox commented 2 years ago

Well, now with 1.19.1 "flattening" these text is kinda required if you don't want to get constant "server modified" in case of formatting player ones (see Styled Chat). If you want to manipulate them, using TextNodes to do that was intended solution