Skip to content

Commit

Permalink
text-minimessage: Properly handle colour override depth reductions
Browse files Browse the repository at this point in the history
Fixes #510
  • Loading branch information
zml2008 committed Dec 14, 2021
1 parent 04e84f1 commit f794e47
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
}
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1659,11 +1660,28 @@ void testRepeatedResolvingOfStringPlaceholders() {

final Component expected = text("cat makes a sound", RED);

assertParsedEquals(
this.assertParsedEquals(
expected,
input,
miniMessage("animal", "<red><feline>"),
component("feline", text("cat"))
);
}

// https://github.com/KyoriPowered/adventure/issues/510
@Test
void testNestedGradientsDontOverrideColors() {
final String input = "<gradient:#1985ff:#2bc7ff>a<gradient:#00fffb:#00ffc3>b</gradient> <gray>gray</gray></gradient>";

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);
}
}

0 comments on commit f794e47

Please sign in to comment.