Skip to content

Commit

Permalink
update MM implementation for changes
Browse files Browse the repository at this point in the history
  • Loading branch information
zml2008 committed Feb 16, 2024
1 parent 7ee6b1e commit 1cabc45
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -198,41 +200,33 @@ private void skipColorForLengthOf(final String content) {
@Override
public abstract int hashCode();

static final class ComponentData implements Emitable {
final Consumer<TokenEmitter> output;
final Component originalComp;
static final class TagInfoHolder implements VirtualComponentRenderer<Void>, Emitable {
private final Consumer<TokenEmitter> output;
private final Component originalComp;

ComponentData(final Consumer<TokenEmitter> output, final Component originalComp) {
TagInfoHolder(final Consumer<TokenEmitter> 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<ComponentData> {
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;
}
}

Expand All @@ -241,11 +235,11 @@ static final class TagInfoHolder implements VirtualComponentHolder<ComponentData
return null;
}

final VirtualComponentHolder<?> holder = ((VirtualComponent) comp).holder();
final VirtualComponentRenderer<?> holder = ((VirtualComponent) comp).renderer();
if (!(holder instanceof TagInfoHolder)) {
return null;
}

return ((TagInfoHolder) holder).unbox();
return (TagInfoHolder) holder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<Void>() {
@Override
public @UnknownNullability ComponentLike apply(final @NotNull Void context) {
return Component.empty();
}
}) // not part of equality... should it be?
.children(Arrays.asList(children));
}
}

0 comments on commit 1cabc45

Please sign in to comment.