Patbox / TextPlaceholderAPI

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

About the `nested tags` or `string interpolation`. #56

Closed sakurawald closed 1 month ago

sakurawald commented 1 month ago

I want to write a format so that: one clich the text, it will suggests the /msg <sender_player> command.

Case1

So, i write down the format:

<click:suggest_command:/msg %player:name%>click me</click>

But if corrupted in char : image

Case2

And if you wrap the command_suggest value with ' char. e.g.

<click:suggest_command:'/msg %player:name%'>click me</click>

You will get image

In case2, the parser seems to treat the string literal and skip the placeholder resolve. So i want to know if there is some string interpolation or some escape that can support complex nested tags

sakurawald commented 1 month ago

However, the placeholder can injected into hover tag

<hover:'/msg %player:name%'>hover me</hover>
Patbox commented 1 month ago

Hey! what version and which mods do you have this issues with?

sakurawald commented 1 month ago

Hey! what version and which mods do you have this issues with?

It's

modImplementation(include("eu.pb4:placeholder-api:${project.placeholder_api_version}"))
placeholder_api_version=2.4.0+1.21

The parser and context is

    private static final MergedParser MERGED_PARSER = new MergedParser(
            new NodeParser[]{
                    DEFAULT_PLACEHOLDER_PARSER
                    , TagParser.QUICK_TEXT_WITH_STF
            });

    public static @NotNull Component ofComponent(@Nullable Audience audience, boolean isKey, String keyOrString, Object... args) {
        String string = isKey ? getString(audience, keyOrString, args) : keyOrString;

        PlaceholderContext placeholderContext;
        if (audience instanceof PlayerEntity playerEntity) {
            placeholderContext = PlaceholderContext.of(playerEntity);
        } else {
            placeholderContext = PlaceholderContext.of(Fuji.SERVER);
        }
        ParserContext parserContext = ParserContext.of(PlaceholderContext.KEY, placeholderContext);

        return MERGED_PARSER.parseText(TextNode.of(string), parserContext).asComponent();
    }

The full code is inside my MessageUtil: https://github.com/sakurawald/fuji-fabric/blob/dev/src/main/java/io/github/sakurawald/util/MessageUtil.java

Patbox commented 1 month ago

Oh yeah, you need to swap parsers around, through in this case you should use NodeParser.builder() s it provides you everything and is more stable

sakurawald commented 1 month ago

Oh yeah, you need to swap parsers around, through in this case you should use NodeParser.builder() s it provides you everything and is more stable

I try this

    private static final NodeParser MERGED_PARSER = ParserBuilder.of()
            .add(DEFAULT_PLACEHOLDER_PARSER)
            .add(TagParser.QUICK_TEXT_WITH_STF).build();

and

    private static final NodeParser MERGED_PARSER = NodeParser.builder()
            .add(DEFAULT_PLACEHOLDER_PARSER)
            .add(TagParser.QUICK_TEXT_WITH_STF).build();

It's the same. Do i use the api wrong?

Patbox commented 1 month ago

Yeah both are equal, it doesn't matter

sakurawald commented 1 month ago
    private static final NodeParser MERGED_PARSER = NodeParser.builder()
            .simplifiedTextFormat()
            .globalPlaceholders()
            .build();

I don't understand, the placeholder can injected into the hover tag, but the click:command_suggest not work.

Patbox commented 1 month ago

Do it doesn't work, will check as it might be a bug

sakurawald commented 1 month ago

Do it doesn't work, will check as it might be a bug

Yeah, it still doesn't work.

sakurawald commented 1 month ago

This issue addressed since v2.4.1+1.21, thanks for you great work. <3