Skip to content

Commit

Permalink
minimessage: Add a reset tag via a parser directive
Browse files Browse the repository at this point in the history
  • Loading branch information
kezz committed Feb 13, 2022
1 parent bc2f7b5 commit 5fa909c
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 9 deletions.
Expand Up @@ -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;
Expand All @@ -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 = '>';
Expand Down Expand Up @@ -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)) {
// <reset> tags get special treatment and don't appear in the tree
// instead, they close all currently open tags

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}

/**
Expand Down
@@ -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";
}
};
}
Expand Up @@ -40,11 +40,12 @@
/**
* A tag definition for the MiniMessage language.
*
* <p>All implementations of {@code Tag} must implement one of {@link Inserting}, {@link Modifying}, or {@link PreProcess}.</p>
* <p>All implementations of {@code Tag} must implement one of {@link Inserting}, {@link Modifying},
* {@link ParserDirective} or {@link PreProcess}.</p>
*
* @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.
Expand Down
Expand Up @@ -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;

/**
Expand All @@ -39,6 +40,8 @@
* @since 4.10.0
*/
public final class StandardTags {
private static final String RESET_TAG = "reset";

private StandardTags() {
}

Expand All @@ -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,
Expand All @@ -73,7 +77,8 @@ private StandardTags() {
FONT,
DECORATION,
GRADIENT,
RAINBOW
RAINBOW,
RESET
)
.build();

Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit 5fa909c

Please sign in to comment.