From 1cabc45ac58f0fa73aefd349c0d4632e043d469d Mon Sep 17 00:00:00 2001 From: zml Date: Fri, 16 Feb 2024 15:15:57 +0100 Subject: [PATCH] update MM implementation for changes --- .../standard/AbstractColorChangingTag.java | 42 ++++++++----------- .../text/minimessage/AbstractTest.java | 9 +++- 2 files changed, 26 insertions(+), 25 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 a45877ce0..91da16bbd 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 @@ -29,9 +29,10 @@ import java.util.stream.Stream; import net.kyori.adventure.internal.Internals; import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.ComponentLike; import net.kyori.adventure.text.TextComponent; import net.kyori.adventure.text.VirtualComponent; -import net.kyori.adventure.text.VirtualComponentHolder; +import net.kyori.adventure.text.VirtualComponentRenderer; import net.kyori.adventure.text.flattener.ComponentFlattener; import net.kyori.adventure.text.format.TextColor; import net.kyori.adventure.text.minimessage.internal.parser.node.TagNode; @@ -45,6 +46,7 @@ import net.kyori.examination.ExaminableProperty; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.jetbrains.annotations.UnknownNullability; /** * A transformation that applies a colour change. @@ -102,7 +104,7 @@ public final void postVisit() { public final Component apply(final @NotNull Component current, final int depth) { if (depth == 0) { // capture state into a virtual component, no other logic is needed in normal MM handling - return Component.virtual(new TagInfoHolder(new ComponentData(this.preserveData(), current))); + return Component.virtual(Void.class, new TagInfoHolder(this.preserveData(), current)); } if ((this.disableApplyingColorDepth != -1 && depth > this.disableApplyingColorDepth) || current.style().color() != null) { @@ -198,41 +200,33 @@ private void skipColorForLengthOf(final String content) { @Override public abstract int hashCode(); - static final class ComponentData implements Emitable { - final Consumer output; - final Component originalComp; + static final class TagInfoHolder implements VirtualComponentRenderer, Emitable { + private final Consumer output; + private final Component originalComp; - ComponentData(final Consumer output, final Component originalComp) { + TagInfoHolder(final Consumer output, final Component originalComp) { this.output = output; this.originalComp = originalComp; } @Override - public void emit(final @NotNull TokenEmitter emitter) { - this.output.accept(emitter); - } - - @Override - public Component substitute() { + public @UnknownNullability ComponentLike apply(final @NotNull Void context) { return this.originalComp; } - } - static final class TagInfoHolder implements VirtualComponentHolder { - private final ComponentData data; - - TagInfoHolder(final ComponentData data) { - this.data = data; + @Override + public @NotNull String fallbackString() { + return ""; // only holds data for reserialization, not for display } @Override - public @NotNull ComponentData unbox() { - return this.data; + public void emit(final @NotNull TokenEmitter emitter) { + this.output.accept(emitter); } @Override - public @NotNull String fallbackString() { - return ""; // only holds data for reserialization, not for display + public @Nullable Component substitute() { + return this.originalComp; } } @@ -241,11 +235,11 @@ static final class TagInfoHolder implements VirtualComponentHolder holder = ((VirtualComponent) comp).holder(); + final VirtualComponentRenderer holder = ((VirtualComponent) comp).renderer(); if (!(holder instanceof TagInfoHolder)) { return null; } - return ((TagInfoHolder) holder).unbox(); + return (TagInfoHolder) holder; } } diff --git a/text-minimessage/src/test/java/net/kyori/adventure/text/minimessage/AbstractTest.java b/text-minimessage/src/test/java/net/kyori/adventure/text/minimessage/AbstractTest.java index 06451ad1c..ad1e8dd1b 100644 --- a/text-minimessage/src/test/java/net/kyori/adventure/text/minimessage/AbstractTest.java +++ b/text-minimessage/src/test/java/net/kyori/adventure/text/minimessage/AbstractTest.java @@ -29,11 +29,13 @@ import java.util.stream.Collectors; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.ComponentLike; +import net.kyori.adventure.text.VirtualComponentRenderer; import net.kyori.adventure.text.minimessage.tag.Tag; import net.kyori.adventure.text.minimessage.tag.resolver.ArgumentQueue; import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; import net.kyori.examination.string.MultiLineStringExaminer; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.UnknownNullability; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -78,7 +80,12 @@ public static ArgumentQueue emptyArgumentQueue(final Context context) { } public static Component virtualOfChildren(final ComponentLike... children) { - return Component.virtual(() -> "") // not part of equality... should it be? + return Component.virtual(Void.class, new VirtualComponentRenderer() { + @Override + public @UnknownNullability ComponentLike apply(final @NotNull Void context) { + return Component.empty(); + } + }) // not part of equality... should it be? .children(Arrays.asList(children)); } }