Skip to content

Commit

Permalink
fix(text-minimessage): Preserve non-text components in color changing…
Browse files Browse the repository at this point in the history
… tags

Fixes GH-827
  • Loading branch information
zml2008 committed Nov 9, 2022
1 parent 59100a9 commit 4a073fb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
Expand Up @@ -55,6 +55,11 @@
*/
abstract class AbstractColorChangingTag implements Modifying, Examinable {

private static final ComponentFlattener LENGTH_CALCULATOR = ComponentFlattener.builder()
.mapper(TextComponent.class, TextComponent::content)
.unknownMapper(x -> "_") // every unknown component gets a single colour
.build();

private boolean visited;
private int size = 0;
private int disableApplyingColorDepth = -1;
Expand All @@ -76,7 +81,7 @@ public final void visit(final @NotNull Node current, final int depth) {
final TagNode tag = (TagNode) current;
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()));
LENGTH_CALCULATOR.flatten(((Inserting) tag.tag()).value(), s -> this.size += s.codePointCount(0, s.length()));
}
}
}
Expand Down Expand Up @@ -124,6 +129,10 @@ public final Component apply(final @NotNull Component current, final int depth)
}

return parent.build();
} else if (!(current instanceof TextComponent)) {
final Component ret = current.children(Collections.emptyList()).colorIfAbsent(this.color());
this.advanceColor();
return ret;
}

return Component.empty().mergeStyle(current);
Expand Down
Expand Up @@ -32,6 +32,7 @@

import static net.kyori.adventure.text.Component.empty;
import static net.kyori.adventure.text.Component.text;
import static net.kyori.adventure.text.Component.translatable;
import static net.kyori.adventure.text.event.HoverEvent.showText;
import static net.kyori.adventure.text.format.NamedTextColor.BLACK;
import static net.kyori.adventure.text.format.NamedTextColor.BLUE;
Expand Down Expand Up @@ -542,4 +543,18 @@ void testDecorationsPreserved() {

this.assertParsedEquals(expected, input, Placeholder.component("placeholder", placeholder));
}

// https://github.com/KyoriPowered/adventure/issues/827
@Test
void testLangTagInGradient() {
final String input = "<gradient:red:blue>ab<lang:block.minecraft.diamond_block>!</gradient>";
final Component expected = Component.textOfChildren(
text("a", RED),
text("b", color(0xd55580)),
translatable("block.minecraft.diamond_block", color(0xaa55aa))
.append(text("!", color(0x8055d5)))
);

this.assertParsedEquals(expected, input);
}
}

0 comments on commit 4a073fb

Please sign in to comment.