From f794e478425f8f4d93a101b62a666eb424e726ae Mon Sep 17 00:00:00 2001 From: zml Date: Sun, 12 Dec 2021 14:59:37 -0800 Subject: [PATCH 1/2] text-minimessage: Properly handle colour override depth reductions Fixes #510 --- .../inbuild/GradientTransformation.java | 4 +++- .../inbuild/RainbowTransformation.java | 2 ++ .../minimessage/MiniMessageParserTest.java | 20 ++++++++++++++++++- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/transformation/inbuild/GradientTransformation.java b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/transformation/inbuild/GradientTransformation.java index 0a7d8367b..5d01cdcda 100644 --- a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/transformation/inbuild/GradientTransformation.java +++ b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/transformation/inbuild/GradientTransformation.java @@ -137,7 +137,7 @@ public void visit(final ElementNode curr) { } else if (curr instanceof TagNode) { final TagNode tag = (TagNode) curr; if (tag.transformation() instanceof ComponentTransformation) { - // PlaceholderTransformation.apply() returns the value of the component placeholder + // ComponentTransformation.apply() returns the value of the component placeholder ComponentFlattener.textOnly().flatten(tag.transformation().apply(), s -> this.size += s.codePointCount(0, s.length())); } } @@ -159,6 +159,8 @@ public Component apply() { @Override public Component apply(final Component current, final int depth) { + if (this.disableApplyingColorDepth >= depth) this.disableApplyingColorDepth = -1; + if ((this.disableApplyingColorDepth != -1 && depth > this.disableApplyingColorDepth) || current.style().color() != null) { if (this.disableApplyingColorDepth == -1) { this.disableApplyingColorDepth = depth; diff --git a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/transformation/inbuild/RainbowTransformation.java b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/transformation/inbuild/RainbowTransformation.java index d158c3452..9cb1d4373 100644 --- a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/transformation/inbuild/RainbowTransformation.java +++ b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/transformation/inbuild/RainbowTransformation.java @@ -122,6 +122,8 @@ public Component apply() { @Override public Component apply(final Component current, final int depth) { + if (this.disableApplyingColorDepth >= depth) this.disableApplyingColorDepth = -1; + if ((this.disableApplyingColorDepth != -1 && depth > this.disableApplyingColorDepth) || current.style().color() != null) { if (this.disableApplyingColorDepth == -1) { this.disableApplyingColorDepth = depth; diff --git a/text-minimessage/src/test/java/net/kyori/adventure/text/minimessage/MiniMessageParserTest.java b/text-minimessage/src/test/java/net/kyori/adventure/text/minimessage/MiniMessageParserTest.java index 65b38ac3e..f23abf7d3 100644 --- a/text-minimessage/src/test/java/net/kyori/adventure/text/minimessage/MiniMessageParserTest.java +++ b/text-minimessage/src/test/java/net/kyori/adventure/text/minimessage/MiniMessageParserTest.java @@ -29,6 +29,7 @@ import net.kyori.adventure.text.TextComponent; import net.kyori.adventure.text.event.ClickEvent; import net.kyori.adventure.text.event.HoverEvent; +import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.TextColor; import net.kyori.adventure.text.minimessage.placeholder.Placeholder; import net.kyori.adventure.text.minimessage.placeholder.PlaceholderResolver; @@ -1659,11 +1660,28 @@ void testRepeatedResolvingOfStringPlaceholders() { final Component expected = text("cat makes a sound", RED); - assertParsedEquals( + this.assertParsedEquals( expected, input, miniMessage("animal", ""), component("feline", text("cat")) ); } + + // https://github.com/KyoriPowered/adventure/issues/510 + @Test + void testNestedGradientsDontOverrideColors() { + final String input = "ab gray"; + + final Component expected = Component.text() + .append( + text("a", color(0x1985ff)), + text("b", color(0x00fffb)), + text(" ", color(0x1e98ff)), + text("gray", NamedTextColor.GRAY) + ) + .build(); + + this.assertParsedEquals(expected, input); + } } From a79e3adf2d7968ab6671186433d575290da77e5f Mon Sep 17 00:00:00 2001 From: zml Date: Mon, 13 Dec 2021 18:23:10 -0800 Subject: [PATCH 2/2] text-minimessage: There was already handling for this in the rainbow xform use that instead --- .../transformation/inbuild/GradientTransformation.java | 3 +-- .../transformation/inbuild/RainbowTransformation.java | 2 -- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/transformation/inbuild/GradientTransformation.java b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/transformation/inbuild/GradientTransformation.java index 5d01cdcda..b7de71bfb 100644 --- a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/transformation/inbuild/GradientTransformation.java +++ b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/transformation/inbuild/GradientTransformation.java @@ -159,8 +159,6 @@ public Component apply() { @Override public Component apply(final Component current, final int depth) { - if (this.disableApplyingColorDepth >= depth) this.disableApplyingColorDepth = -1; - if ((this.disableApplyingColorDepth != -1 && depth > this.disableApplyingColorDepth) || current.style().color() != null) { if (this.disableApplyingColorDepth == -1) { this.disableApplyingColorDepth = depth; @@ -178,6 +176,7 @@ public Component apply(final Component current, final int depth) { return current.children(Collections.emptyList()); } + this.disableApplyingColorDepth = -1; if (current instanceof TextComponent && ((TextComponent) current).content().length() > 0) { final TextComponent textComponent = (TextComponent) current; final String content = textComponent.content(); diff --git a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/transformation/inbuild/RainbowTransformation.java b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/transformation/inbuild/RainbowTransformation.java index 9cb1d4373..d158c3452 100644 --- a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/transformation/inbuild/RainbowTransformation.java +++ b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/transformation/inbuild/RainbowTransformation.java @@ -122,8 +122,6 @@ public Component apply() { @Override public Component apply(final Component current, final int depth) { - if (this.disableApplyingColorDepth >= depth) this.disableApplyingColorDepth = -1; - if ((this.disableApplyingColorDepth != -1 && depth > this.disableApplyingColorDepth) || current.style().color() != null) { if (this.disableApplyingColorDepth == -1) { this.disableApplyingColorDepth = depth;