diff --git a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/parser/TokenParser.java b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/parser/TokenParser.java index 886ccdf1aa..75a2c95066 100644 --- a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/parser/TokenParser.java +++ b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/parser/TokenParser.java @@ -40,6 +40,7 @@ import net.kyori.adventure.text.minimessage.parser.node.TagPart; import net.kyori.adventure.text.minimessage.parser.node.TextNode; import net.kyori.adventure.text.minimessage.tag.Inserting; +import net.kyori.adventure.text.minimessage.tag.ParserDirective; import net.kyori.adventure.text.minimessage.tag.Tag; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; @@ -53,8 +54,6 @@ @ApiStatus.Internal public final class TokenParser { private static final int MAX_DEPTH = 16; - public static final String RESET = "reset"; - public static final String RESET_2 = "r"; // minimessage tags public static final char TAG_START = '<'; public static final char TAG_END = '>'; @@ -364,7 +363,7 @@ private static ElementNode buildTree( case OPEN_TAG: final TagNode tagNode = new TagNode(node, token, message, tagProvider); - if (isReset(tagNode.name())) { + if (isReset(tagNode.name(), tagProvider)) { // tags get special treatment and don't appear in the tree // instead, they close all currently open tags @@ -407,7 +406,7 @@ private static ElementNode buildTree( } final String closeTagName = closeValues.get(0); - if (isReset(closeTagName)) { + if (isReset(closeTagName, tagProvider)) { // This is a synthetic node, closing it means nothing in the context of building a tree continue; } @@ -488,8 +487,8 @@ private static ElementNode buildTree( return root; } - private static boolean isReset(final String input) { - return input.equalsIgnoreCase(RESET) || input.equalsIgnoreCase(RESET_2); + private static boolean isReset(final String input, final TagProvider tagProvider) { + return tagProvider.resolve(input) == ParserDirective.RESET; } /** diff --git a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/tag/ParserDirective.java b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/tag/ParserDirective.java new file mode 100644 index 0000000000..de8ca77294 --- /dev/null +++ b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/tag/ParserDirective.java @@ -0,0 +1,47 @@ +/* + * This file is part of adventure, licensed under the MIT License. + * + * Copyright (c) 2017-2022 KyoriPowered + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package net.kyori.adventure.text.minimessage.tag; + +import org.jetbrains.annotations.ApiStatus; + +/** + * Tags implementing this interface are used to provide directives, or instructions, to the parser directly. + * + * @see #RESET + * @since 4.10.0 + */ +@ApiStatus.NonExtendable +public interface ParserDirective extends Tag { + /** + * Instructs the parser to reset all style, events, insertions, etc. + * + * @since 4.10.0 + */ + Tag RESET = new ParserDirective() { + @Override + public String toString() { + return "ParserDirective.RESET"; + } + }; +} diff --git a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/tag/Tag.java b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/tag/Tag.java index 35f6130ee3..cde2dd5652 100644 --- a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/tag/Tag.java +++ b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/tag/Tag.java @@ -40,11 +40,12 @@ /** * A tag definition for the MiniMessage language. * - *

All implementations of {@code Tag} must implement one of {@link Inserting}, {@link Modifying}, or {@link PreProcess}.

+ *

All implementations of {@code Tag} must implement one of {@link Inserting}, {@link Modifying}, + * {@link ParserDirective} or {@link PreProcess}.

* * @since 4.10.0 */ -public /* sealed */ interface Tag /* permits Inserting, Modifying, PreProcess, /internal/ AbstractTag */ { +public /* sealed */ interface Tag /* permits Inserting, Modifying, ParserDirective, PreProcess, /internal/ AbstractTag */ { /** * Create a tag that inserts the content literally into the parse string. diff --git a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/tag/standard/StandardTags.java b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/tag/standard/StandardTags.java index d03e73cfb8..ff5ccec77f 100644 --- a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/tag/standard/StandardTags.java +++ b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/tag/standard/StandardTags.java @@ -29,6 +29,7 @@ import java.util.stream.Stream; import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.TextDecoration; +import net.kyori.adventure.text.minimessage.tag.ParserDirective; import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; /** @@ -39,6 +40,8 @@ * @since 4.10.0 */ public final class StandardTags { + private static final String RESET_TAG = "reset"; + private StandardTags() { } @@ -62,6 +65,7 @@ private StandardTags() { private static final TagResolver FONT = TagResolver.resolver(FontTag.FONT, FontTag::create); private static final TagResolver GRADIENT = TagResolver.resolver(GradientTag.GRADIENT, GradientTag::create); private static final TagResolver RAINBOW = TagResolver.resolver(RainbowTag.RAINBOW, RainbowTag::create); + private static final TagResolver RESET = TagResolver.resolver(RESET_TAG, ParserDirective.RESET); private static final TagResolver ALL = TagResolver.builder() .resolvers( HOVER_EVENT, @@ -73,7 +77,8 @@ private StandardTags() { FONT, DECORATION, GRADIENT, - RAINBOW + RAINBOW, + RESET ) .build(); @@ -171,6 +176,16 @@ public static TagResolver rainbow() { return RAINBOW; } + /** + * Get a resolver for the {@value RESET_TAG} tag. + * + * @return a resolver for the {@value RESET_TAG} tag. + * @since 4.10.0 + */ + public static TagResolver reset() { + return RESET; + } + /** * Get a resolver that handles all default standard tags. *