From 563feca4687029633438d65ed01c8418c31bb6d6 Mon Sep 17 00:00:00 2001 From: zml Date: Sun, 27 Feb 2022 19:26:45 -0800 Subject: [PATCH] text-minimessage: Work on interrupting gradients Fixes #509 --- .../standard/AbstractColorChangingTag.java | 27 +++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/tag/standard/AbstractColorChangingTag.java b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/tag/standard/AbstractColorChangingTag.java index 8e15db0b7..ae1beeb77 100644 --- a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/tag/standard/AbstractColorChangingTag.java +++ b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/tag/standard/AbstractColorChangingTag.java @@ -54,7 +54,7 @@ * @since 4.10.0 */ abstract class AbstractColorChangingTag implements Modifying, Examinable { - + private final boolean interrupting = true; private boolean visited; private int size = 0; private int disableApplyingColorDepth = -1; @@ -69,12 +69,20 @@ public final void visit(final @NotNull Node current, final int depth) { throw new IllegalStateException("Color changing tag instances cannot be re-used, return a new one for each resolve"); } + if (this.disableApplyingColorDepth != -1 && depth > this.disableApplyingColorDepth) { + return; + } + if (current instanceof ValueNode) { final String value = ((ValueNode) current).value(); this.size += value.codePointCount(0, value.length()); } else if (current instanceof TagNode) { final TagNode tag = (TagNode) current; - if (tag.tag() instanceof Inserting) { + if (this.interrupting && (tag.tag() == this || (tag.tag() instanceof Inserting && ((Inserting) tag.tag()).value().style().color() != null))) { + if (this.disableApplyingColorDepth == -1 || depth < this.disableApplyingColorDepth) { + this.disableApplyingColorDepth = depth + 1; + } + } else if (tag.tag() instanceof Inserting) { // ComponentTransformation.apply() returns the value of the component placeholder ComponentFlattener.textOnly().flatten(((Inserting) tag.tag()).value(), s -> this.size += s.codePointCount(0, s.length())); } @@ -85,6 +93,7 @@ public final void visit(final @NotNull Node current, final int depth) { public final void postVisit() { // init this.visited = true; + this.disableApplyingColorDepth = -1; this.init(); } @@ -96,12 +105,14 @@ public final Component apply(final @NotNull Component current, final int depth) } // This component has its own color applied, which overrides ours // We still want to keep track of where we are though if this is text - if (current instanceof TextComponent) { - final String content = ((TextComponent) current).content(); - final int len = content.codePointCount(0, content.length()); - for (int i = 0; i < len; i++) { - // increment our color index - this.advanceColor(); + if (!this.interrupting) { + if (current instanceof TextComponent) { + final String content = ((TextComponent) current).content(); + final int len = content.codePointCount(0, content.length()); + for (int i = 0; i < len; i++) { + // increment our color index + this.advanceColor(); + } } } return current.children(Collections.emptyList());