Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(text-minimessage): Preserve non-text components in color changing tags #834

Merged
merged 1 commit into from
Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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);
}
}