Skip to content

Commit

Permalink
feat(api): Make virtual components more robust, add fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
zml2008 committed Mar 14, 2023
1 parent 24dab47 commit ff482d5
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 2 deletions.
42 changes: 42 additions & 0 deletions api/src/main/java/net/kyori/adventure/text/Component.java
Original file line number Diff line number Diff line change
Expand Up @@ -1244,6 +1244,48 @@ public interface Component extends ComponentBuilderApplicable, ComponentLike, Ex
return VirtualComponentImpl.createVirtual(virtual);
}

/**
* Creates a virtual component with a value.
*
* @param virtual the value
* @param style the style
* @return a virtual component
* @since 4.13.0
*/
@Contract(value = "_, _ -> new", pure = true)
static @NotNull VirtualComponent virtual(final @NotNull VirtualComponentHolder<?> virtual, final @NotNull Style style) {
requireNonNull(virtual, "virtual");
return VirtualComponentImpl.createVirtual(virtual);
}

/**
* Creates a virtual component with a value.
*
* @param virtual the value
* @param style the style elements
* @return a virtual component
* @since 4.13.0
*/
@Contract(value = "_, _ -> new", pure = true)
static @NotNull VirtualComponent virtual(final @NotNull VirtualComponentHolder<?> virtual, final @NotNull StyleBuilderApplicable... style) {
requireNonNull(virtual, "virtual");
return VirtualComponentImpl.createVirtual(Collections.emptyList(), Style.style(style), virtual);
}

/**
* Creates a virtual component with a value.
*
* @param virtual the value
* @param style the style elements
* @return a virtual component
* @since 4.13.0
*/
@Contract(value = "_, _ -> new", pure = true)
static @NotNull VirtualComponent virtual(final @NotNull VirtualComponentHolder<?> virtual, final @NotNull Iterable<StyleBuilderApplicable> style) {
requireNonNull(virtual, "virtual");
return VirtualComponentImpl.createVirtual(Collections.emptyList(), Style.style(style), virtual);
}

/*
* -------------------------------
* ---- TranslatableComponent ----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public String toString() {
return new BuilderImpl(this);
}

static final class BuilderImpl extends AbstractComponentBuilder<TextComponent, Builder> implements TextComponent.Builder {
static class BuilderImpl extends AbstractComponentBuilder<TextComponent, Builder> implements TextComponent.Builder {
/*
* We default to an empty string to avoid needing to manually set the
* content of a newly-created builder when we only want to append other
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package net.kyori.adventure.text;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.UnknownNullability;

/**
Expand All @@ -39,4 +40,18 @@ public interface VirtualComponentHolder<V> {
* @since 4.13.0
*/
@UnknownNullability V unbox();

/**
* Get a fallback value for when this component has been serialized without being rendered.
*
* <p>By default, this will be the toString{} of {@link #unbox()}.</p>
*
* @return the fallback string
* @since 4.13.0
*/
default @NotNull String fallbackString() {
final Object unboxed = this.unbox();
return unboxed == null ? "" : unboxed.toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ static VirtualComponent createVirtual(final @NotNull VirtualComponentHolder<?> v
return new VirtualComponentImpl(Collections.emptyList(), Style.empty(), "", virtual);
}

static VirtualComponent createVirtual(final List<? extends ComponentLike> children, final Style style, final @NotNull VirtualComponentHolder<?> virtual) {
final List<Component> filteredChildren = ComponentLike.asComponents(children, IS_NOT_EMPTY);

return new VirtualComponentImpl(filteredChildren, style, "", virtual);
}

private final VirtualComponentHolder<?> virtual;

private VirtualComponentImpl(final @NotNull List<Component> children, final @NotNull Style style, final @NotNull String content, final @NotNull VirtualComponentHolder<?> virtual) {
Expand All @@ -49,4 +55,29 @@ VirtualComponent create0(final @NotNull List<? extends ComponentLike> children,
public @NotNull VirtualComponentHolder<?> holder() {
return this.virtual;
}

@Override
public @NotNull String content() {
return this.virtual.fallbackString();
}

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

static final class BuilderImpl extends TextComponentImpl.BuilderImpl {
private final VirtualComponentHolder<?> holder;

BuilderImpl(final VirtualComponent other) {
super(other);

this.holder = other.holder();
}

@Override
public @NotNull TextComponent build() {
return createVirtual(this.children, this.buildStyle(), this.holder);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public abstract class AbstractComponentRenderer<C> implements ComponentRenderer<
* @since 4.13.0
*/
protected @NotNull Component renderVirtual(final @NotNull VirtualComponent component, final @NotNull C context) {
return Component.empty();
return component; // will be processed as a TextComponent instead
}

/**
Expand Down

0 comments on commit ff482d5

Please sign in to comment.