Search in sources :

Example 1 with PreProcess

use of net.kyori.adventure.text.minimessage.tag.PreProcess in project adventure by KyoriPowered.

the class StringResolvingMatchedTokenConsumer method accept.

@Override
public void accept(final int start, final int end, @NotNull final TokenType tokenType) {
    super.accept(start, end, tokenType);
    if (tokenType != TokenType.OPEN_TAG) {
        // just add it normally, we don't care about other tags
        this.builder.append(this.input, start, end);
    } else {
        // well, now we need to work out if it's a tag or a placeholder!
        final String match = this.input.substring(start, end);
        final String cleanup = this.input.substring(start + 1, end - 1);
        final int index = cleanup.indexOf(SEPARATOR);
        final String tag = index == -1 ? cleanup : cleanup.substring(0, index);
        // we might care if it's a valid tag!
        if (TagInternals.sanitizeAndCheckValidTagName(tag)) {
            final List<Token> tokens = tokenize(match);
            final List<TagPart> parts = new ArrayList<>();
            final List<Token> childs = tokens.isEmpty() ? null : tokens.get(0).childTokens();
            if (childs != null) {
                for (int i = 1; i < childs.size(); i++) {
                    parts.add(new TagPart(match, childs.get(i), this.tagProvider));
                }
            }
            // we might care if it's a pre-process!
            @Nullable final Tag replacement = this.tagProvider.resolve(TokenParser.TagProvider.sanitizePlaceholderName(tag), parts, tokens.get(0));
            if (replacement instanceof PreProcess) {
                this.builder.append(Objects.requireNonNull(((PreProcess) replacement).value(), "PreProcess replacements cannot return null"));
                return;
            }
        }
        // if we get here, the placeholder wasn't found or was null
        this.builder.append(match);
    }
}
Also used : PreProcess(net.kyori.adventure.text.minimessage.tag.PreProcess) ArrayList(java.util.ArrayList) Token(net.kyori.adventure.text.minimessage.internal.parser.Token) Tag(net.kyori.adventure.text.minimessage.tag.Tag) Nullable(org.jetbrains.annotations.Nullable) TagPart(net.kyori.adventure.text.minimessage.internal.parser.node.TagPart)

Aggregations

ArrayList (java.util.ArrayList)1 Token (net.kyori.adventure.text.minimessage.internal.parser.Token)1 TagPart (net.kyori.adventure.text.minimessage.internal.parser.node.TagPart)1 PreProcess (net.kyori.adventure.text.minimessage.tag.PreProcess)1 Tag (net.kyori.adventure.text.minimessage.tag.Tag)1 Nullable (org.jetbrains.annotations.Nullable)1