Skip to content

Commit

Permalink
feat(virtual-components): fix up MM tests, tweak impl
Browse files Browse the repository at this point in the history
  • Loading branch information
zml2008 committed Dec 10, 2024
1 parent 487718b commit dcba0c6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,38 +28,38 @@
import net.kyori.adventure.text.format.Style;
import org.jetbrains.annotations.NotNull;

final class VirtualComponentImpl extends TextComponentImpl implements VirtualComponent {
static <C> VirtualComponent createVirtual(final @NotNull Class<C> contextType, final @NotNull VirtualComponentRenderer<?> renderer) {
final class VirtualComponentImpl<C> extends TextComponentImpl implements VirtualComponent {
static <C> VirtualComponent createVirtual(final @NotNull Class<C> contextType, final @NotNull VirtualComponentRenderer<C> renderer) {
return createVirtual(contextType, renderer, Collections.emptyList(), Style.empty());
}

static <C> VirtualComponent createVirtual(final @NotNull Class<C> contextType, final @NotNull VirtualComponentRenderer<?> renderer, final List<? extends ComponentLike> children, final Style style) {
static <C> VirtualComponent createVirtual(final @NotNull Class<C> contextType, final @NotNull VirtualComponentRenderer<C> renderer, final List<? extends ComponentLike> children, final Style style) {
final List<Component> filteredChildren = ComponentLike.asComponents(children, IS_NOT_EMPTY);

return new VirtualComponentImpl(filteredChildren, style, "", contextType, renderer);
return new VirtualComponentImpl<>(filteredChildren, style, "", contextType, renderer);
}

private final Class<?> contextType;
private final VirtualComponentRenderer<?> renderer;
private final Class<C> contextType;
private final VirtualComponentRenderer<C> renderer;

private VirtualComponentImpl(final @NotNull List<Component> children, final @NotNull Style style, final @NotNull String content, final @NotNull Class<?> contextType, final @NotNull VirtualComponentRenderer<?> renderer) {
private VirtualComponentImpl(final @NotNull List<Component> children, final @NotNull Style style, final @NotNull String content, final @NotNull Class<C> contextType, final @NotNull VirtualComponentRenderer<C> renderer) {
super(children, style, content);
this.contextType = contextType;
this.renderer = renderer;
}

@Override
VirtualComponent create0(final @NotNull List<? extends ComponentLike> children, final @NotNull Style style, final @NotNull String content) {
return new VirtualComponentImpl(ComponentLike.asComponents(children, IS_NOT_EMPTY), style, content, this.contextType, this.renderer);
return new VirtualComponentImpl<>(ComponentLike.asComponents(children, IS_NOT_EMPTY), style, content, this.contextType, this.renderer);
}

@Override
public @NotNull Class<?> contextType() {
public @NotNull Class<C> contextType() {
return this.contextType;
}

@Override
public @NotNull VirtualComponentRenderer<?> renderer() {
public @NotNull VirtualComponentRenderer<C> renderer() {
return this.renderer;
}

Expand All @@ -70,14 +70,14 @@ VirtualComponent create0(final @NotNull List<? extends ComponentLike> children,

@Override
public @NotNull Builder toBuilder() {
return new BuilderImpl(this);
return new BuilderImpl<>(this);
}

static final class BuilderImpl extends TextComponentImpl.BuilderImpl {
private final Class<?> contextType;
private final VirtualComponentRenderer<?> renderer;
static final class BuilderImpl<C> extends TextComponentImpl.BuilderImpl {
private final Class<C> contextType;
private final VirtualComponentRenderer<C> renderer;

BuilderImpl(final VirtualComponent other) {
BuilderImpl(final VirtualComponentImpl<C> other) {
super(other);
this.contextType = other.contextType();
this.renderer = other.renderer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ protected TextColor color() {
@Override
protected @NotNull Consumer<TokenEmitter> preserveData() {
final boolean reversed = this.reversed;
final int phase = this.phase;
final int phase = (int) Math.round(this.dividedPhase * 10);
return emit -> {
emit.tag(RAINBOW);
if (reversed && phase != 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,17 +370,19 @@ void gh147() {
@Test
void gh1040() {
final String input = "<rainbow:16777215>||||||||||";
final Component expected = textOfChildren(
text("|", color(0x00ffff)),
text("|", color(0x0065ff)),
text("|", color(0x3200ff)),
text("|", color(0xcc00ff)),
text("|", color(0xff0099)),
text("|", color(0xff0000)),
text("|", color(0xff9900)),
text("|", color(0xccff00)),
text("|", color(0x32ff00)),
text("|", color(0x00ff65))
final Component expected = virtualOfChildren(
textOfChildren(
text("|", color(0x00ffff)),
text("|", color(0x0065ff)),
text("|", color(0x3200ff)),
text("|", color(0xcc00ff)),
text("|", color(0xff0099)),
text("|", color(0xff0000)),
text("|", color(0xff9900)),
text("|", color(0xccff00)),
text("|", color(0x32ff00)),
text("|", color(0x00ff65))
)
);

this.assertParsedEquals(expected, input);
Expand Down

0 comments on commit dcba0c6

Please sign in to comment.