Skip to content

Commit

Permalink
api: 1.17 selector & nbt component separator support
Browse files Browse the repository at this point in the history
  • Loading branch information
kashike committed Jun 5, 2021
1 parent ba6df3b commit a6320c3
Show file tree
Hide file tree
Showing 11 changed files with 238 additions and 44 deletions.
Expand Up @@ -38,21 +38,31 @@
final class BlockNBTComponentImpl extends NBTComponentImpl<BlockNBTComponent, BlockNBTComponent.Builder> implements BlockNBTComponent {
private final Pos pos;

BlockNBTComponentImpl(final @NotNull List<? extends ComponentLike> children, final @NotNull Style style, final String nbtPath, final boolean interpret, final @NotNull Pos pos) {
super(children, style, nbtPath, interpret);
BlockNBTComponentImpl(final @NotNull List<? extends ComponentLike> children, final @NotNull Style style, final String nbtPath, final boolean interpret, final @Nullable ComponentLike separator, final @NotNull Pos pos) {
super(children, style, nbtPath, interpret, separator);
this.pos = pos;
}

@Override
public @NotNull BlockNBTComponent nbtPath(final @NotNull String nbtPath) {
if (Objects.equals(this.nbtPath, nbtPath)) return this;
return new BlockNBTComponentImpl(this.children, this.style, nbtPath, this.interpret, this.pos);
return new BlockNBTComponentImpl(this.children, this.style, nbtPath, this.interpret, this.separator, this.pos);
}

@Override
public @NotNull BlockNBTComponent interpret(final boolean interpret) {
if (this.interpret == interpret) return this;
return new BlockNBTComponentImpl(this.children, this.style, this.nbtPath, interpret, this.pos);
return new BlockNBTComponentImpl(this.children, this.style, this.nbtPath, interpret, this.separator, this.pos);
}

@Override
public @Nullable Component separator() {
return this.separator;
}

@Override
public @NotNull BlockNBTComponent separator(final @Nullable ComponentLike separator) {
return new BlockNBTComponentImpl(this.children, this.style, this.nbtPath, this.interpret, separator, this.pos);
}

@Override
Expand All @@ -62,17 +72,17 @@ final class BlockNBTComponentImpl extends NBTComponentImpl<BlockNBTComponent, Bl

@Override
public @NotNull BlockNBTComponent pos(final @NotNull Pos pos) {
return new BlockNBTComponentImpl(this.children, this.style, this.nbtPath, this.interpret, pos);
return new BlockNBTComponentImpl(this.children, this.style, this.nbtPath, this.interpret, this.separator, pos);
}

@Override
public @NotNull BlockNBTComponent children(final @NotNull List<? extends ComponentLike> children) {
return new BlockNBTComponentImpl(children, this.style, this.nbtPath, this.interpret, this.pos);
return new BlockNBTComponentImpl(children, this.style, this.nbtPath, this.interpret, this.separator, this.pos);
}

@Override
public @NotNull BlockNBTComponent style(final @NotNull Style style) {
return new BlockNBTComponentImpl(this.children, style, this.nbtPath, this.interpret, this.pos);
return new BlockNBTComponentImpl(this.children, style, this.nbtPath, this.interpret, this.separator, this.pos);
}

@Override
Expand Down Expand Up @@ -127,7 +137,7 @@ static final class BuilderImpl extends NBTComponentImpl.BuilderImpl<BlockNBTComp
public @NotNull BlockNBTComponent build() {
if (this.nbtPath == null) throw new IllegalStateException("nbt path must be set");
if (this.pos == null) throw new IllegalStateException("pos must be set");
return new BlockNBTComponentImpl(this.children, this.buildStyle(), this.nbtPath, this.interpret, this.pos);
return new BlockNBTComponentImpl(this.children, this.buildStyle(), this.nbtPath, this.interpret, this.separator, this.pos);
}
}

Expand Down
49 changes: 46 additions & 3 deletions api/src/main/java/net/kyori/adventure/text/Component.java
Expand Up @@ -267,7 +267,22 @@ public interface Component extends ComponentBuilderApplicable, ComponentLike, Ex
*/
@Contract(value = "_, _, _ -> new", pure = true)
static @NotNull BlockNBTComponent blockNBT(final @NotNull String nbtPath, final boolean interpret, final BlockNBTComponent.@NotNull Pos pos) {
return new BlockNBTComponentImpl(Collections.emptyList(), Style.empty(), nbtPath, interpret, pos);
return blockNBT(nbtPath, interpret, null, pos);
}

/**
* Creates a block NBT component with a position.
*
* @param nbtPath the nbt path
* @param interpret whether to interpret
* @param separator the separator
* @param pos the block position
* @return a block NBT component
* @since 4.8.0
*/
@Contract(value = "_, _, _, _ -> new", pure = true)
static @NotNull BlockNBTComponent blockNBT(final @NotNull String nbtPath, final boolean interpret, final @Nullable ComponentLike separator, final BlockNBTComponent.@NotNull Pos pos) {
return new BlockNBTComponentImpl(Collections.emptyList(), Style.empty(), nbtPath, interpret, separator, pos);
}

/*
Expand Down Expand Up @@ -503,7 +518,20 @@ public interface Component extends ComponentBuilderApplicable, ComponentLike, Ex
*/
@Contract(value = "_ -> new", pure = true)
static @NotNull SelectorComponent selector(final @NotNull String pattern) {
return new SelectorComponentImpl(Collections.emptyList(), Style.empty(), pattern);
return selector(pattern, null);
}

/**
* Creates a selector component with a pattern.
*
* @param pattern the selector pattern
* @param separator the separator
* @return a selector component
* @since 4.8.0
*/
@Contract(value = "_, _ -> new", pure = true)
static @NotNull SelectorComponent selector(final @NotNull String pattern, final @Nullable ComponentLike separator) {
return new SelectorComponentImpl(Collections.emptyList(), Style.empty(), pattern, separator);
}

/*
Expand Down Expand Up @@ -559,7 +587,22 @@ public interface Component extends ComponentBuilderApplicable, ComponentLike, Ex
*/
@Contract(value = "_, _, _ -> new", pure = true)
static @NotNull StorageNBTComponent storageNBT(final @NotNull String nbtPath, final boolean interpret, final @NotNull Key storage) {
return new StorageNBTComponentImpl(Collections.emptyList(), Style.empty(), nbtPath, interpret, storage);
return storageNBT(nbtPath, interpret, null, storage);
}

/**
* Creates a storage NBT component with a path and an storage ID.
*
* @param nbtPath the nbt path
* @param interpret whether to interpret
* @param separator the separator
* @param storage the identifier of the storage
* @return a storage NBT component
* @since 4.8.0
*/
@Contract(value = "_, _, _, _ -> new", pure = true)
static @NotNull StorageNBTComponent storageNBT(final @NotNull String nbtPath, final boolean interpret, final @Nullable ComponentLike separator, final @NotNull Key storage) {
return new StorageNBTComponentImpl(Collections.emptyList(), Style.empty(), nbtPath, interpret, separator, storage);
}

/*
Expand Down
11 changes: 11 additions & 0 deletions api/src/main/java/net/kyori/adventure/text/ComponentLike.java
Expand Up @@ -87,6 +87,17 @@ public interface ComponentLike {
return Collections.unmodifiableList(components);
}

/**
* Fetches a {@link Component} from a {@code ComponentLike}.
*
* @param like the component-like
* @return a component, or {@code null}
* @since 4.8.0
*/
static @Nullable Component unbox(final @Nullable ComponentLike like) {
return like != null ? like.asComponent() : null;
}

/**
* Gets a {@link Component} representation.
*
Expand Down
Expand Up @@ -34,21 +34,31 @@
final class EntityNBTComponentImpl extends NBTComponentImpl<EntityNBTComponent, EntityNBTComponent.Builder> implements EntityNBTComponent {
private final String selector;

EntityNBTComponentImpl(final @NotNull List<? extends ComponentLike> children, final @NotNull Style style, final String nbtPath, final boolean interpret, final String selector) {
super(children, style, nbtPath, interpret);
EntityNBTComponentImpl(final @NotNull List<? extends ComponentLike> children, final @NotNull Style style, final String nbtPath, final boolean interpret, final @Nullable ComponentLike separator, final String selector) {
super(children, style, nbtPath, interpret, separator);
this.selector = selector;
}

@Override
public @NotNull EntityNBTComponent nbtPath(final @NotNull String nbtPath) {
if (Objects.equals(this.nbtPath, nbtPath)) return this;
return new EntityNBTComponentImpl(this.children, this.style, nbtPath, this.interpret, this.selector);
return new EntityNBTComponentImpl(this.children, this.style, nbtPath, this.interpret, this.separator, this.selector);
}

@Override
public @NotNull EntityNBTComponent interpret(final boolean interpret) {
if (this.interpret == interpret) return this;
return new EntityNBTComponentImpl(this.children, this.style, this.nbtPath, interpret, this.selector);
return new EntityNBTComponentImpl(this.children, this.style, this.nbtPath, interpret, this.separator, this.selector);
}

@Override
public @Nullable Component separator() {
return this.separator;
}

@Override
public @NotNull EntityNBTComponent separator(final @Nullable ComponentLike separator) {
return new EntityNBTComponentImpl(this.children, this.style, this.nbtPath, this.interpret, separator, this.selector);
}

@Override
Expand All @@ -59,17 +69,17 @@ final class EntityNBTComponentImpl extends NBTComponentImpl<EntityNBTComponent,
@Override
public @NotNull EntityNBTComponent selector(final @NotNull String selector) {
if (Objects.equals(this.selector, selector)) return this;
return new EntityNBTComponentImpl(this.children, this.style, this.nbtPath, this.interpret, selector);
return new EntityNBTComponentImpl(this.children, this.style, this.nbtPath, this.interpret, this.separator, selector);
}

@Override
public @NotNull EntityNBTComponent children(final @NotNull List<? extends ComponentLike> children) {
return new EntityNBTComponentImpl(children, this.style, this.nbtPath, this.interpret, this.selector);
return new EntityNBTComponentImpl(children, this.style, this.nbtPath, this.interpret, this.separator, this.selector);
}

@Override
public @NotNull EntityNBTComponent style(final @NotNull Style style) {
return new EntityNBTComponentImpl(this.children, style, this.nbtPath, this.interpret, this.selector);
return new EntityNBTComponentImpl(this.children, style, this.nbtPath, this.interpret, this.separator, this.selector);
}

@Override
Expand Down Expand Up @@ -124,7 +134,7 @@ static final class BuilderImpl extends NBTComponentImpl.BuilderImpl<EntityNBTCom
public @NotNull EntityNBTComponent build() {
if (this.nbtPath == null) throw new IllegalStateException("nbt path must be set");
if (this.selector == null) throw new IllegalStateException("selector must be set");
return new EntityNBTComponentImpl(this.children, this.buildStyle(), this.nbtPath, this.interpret, this.selector);
return new EntityNBTComponentImpl(this.children, this.buildStyle(), this.nbtPath, this.interpret, this.separator, this.selector);
}
}
}
18 changes: 18 additions & 0 deletions api/src/main/java/net/kyori/adventure/text/NBTComponent.java
Expand Up @@ -25,6 +25,7 @@

import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* A component that can display NBT fetched from different locations, optionally trying to interpret the NBT as JSON
Expand Down Expand Up @@ -82,4 +83,21 @@ public interface NBTComponent<C extends NBTComponent<C, B>, B extends NBTCompone
*/
@Contract(pure = true)
@NotNull C interpret(final boolean interpret);

/**
* Gets the separator.
*
* @return the separator
* @since 4.8.0
*/
@Nullable Component separator();

/**
* Sets the separator.
*
* @param separator the separator
* @return the separator
* @since 4.8.0
*/
@NotNull C separator(final @Nullable ComponentLike separator);
}
Expand Up @@ -25,6 +25,7 @@

import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/*
* This can't be a child of NBTComponent.
Expand Down Expand Up @@ -55,4 +56,14 @@ public interface NBTComponentBuilder<C extends NBTComponent<C, B>, B extends NBT
*/
@Contract("_ -> this")
@NotNull B interpret(final boolean interpret);

/**
* Sets the separator.
*
* @param separator the separator
* @return this builder
* @since 4.8.0
*/
@Contract("_ -> this")
@NotNull B separator(final @Nullable ComponentLike separator);
}
18 changes: 15 additions & 3 deletions api/src/main/java/net/kyori/adventure/text/NBTComponentImpl.java
Expand Up @@ -35,11 +35,13 @@ abstract class NBTComponentImpl<C extends NBTComponent<C, B>, B extends NBTCompo
static final boolean INTERPRET_DEFAULT = false;
final String nbtPath;
final boolean interpret;
final @Nullable Component separator;

NBTComponentImpl(final @NotNull List<? extends ComponentLike> children, final @NotNull Style style, final String nbtPath, final boolean interpret) {
NBTComponentImpl(final @NotNull List<? extends ComponentLike> children, final @NotNull Style style, final String nbtPath, final boolean interpret, final @Nullable ComponentLike separator) {
super(children, style);
this.nbtPath = nbtPath;
this.interpret = interpret;
this.separator = ComponentLike.unbox(separator);
}

@Override
Expand All @@ -58,14 +60,15 @@ public boolean equals(final @Nullable Object other) {
if (!(other instanceof NBTComponent)) return false;
if (!super.equals(other)) return false;
final NBTComponent<?, ?> that = (NBTComponent<?, ?>) other;
return Objects.equals(this.nbtPath, that.nbtPath()) && this.interpret == that.interpret();
return Objects.equals(this.nbtPath, that.nbtPath()) && this.interpret == that.interpret() && Objects.equals(this.separator, that.separator());
}

@Override
public int hashCode() {
int result = super.hashCode();
result = (31 * result) + this.nbtPath.hashCode();
result = (31 * result) + Boolean.hashCode(this.interpret);
result = (31 * result) + Objects.hashCode(this.separator);
return result;
}

Expand All @@ -74,7 +77,8 @@ public int hashCode() {
return Stream.concat(
Stream.of(
ExaminableProperty.of("nbtPath", this.nbtPath),
ExaminableProperty.of("interpret", this.interpret)
ExaminableProperty.of("interpret", this.interpret),
ExaminableProperty.of("separator", this.separator)
),
super.examinablePropertiesWithoutChildren()
);
Expand All @@ -83,6 +87,7 @@ public int hashCode() {
static abstract class BuilderImpl<C extends NBTComponent<C, B>, B extends NBTComponentBuilder<C, B>> extends AbstractComponentBuilder<C, B> implements NBTComponentBuilder<C, B> {
protected @Nullable String nbtPath;
protected boolean interpret = INTERPRET_DEFAULT;
protected @Nullable Component separator;

BuilderImpl() {
}
Expand All @@ -106,5 +111,12 @@ static abstract class BuilderImpl<C extends NBTComponent<C, B>, B extends NBTCom
this.interpret = interpret;
return (B) this;
}

@Override
@SuppressWarnings("unchecked")
public @NotNull B separator(final @Nullable ComponentLike separator) {
this.separator = ComponentLike.unbox(separator);
return (B) this;
}
}
}
28 changes: 28 additions & 0 deletions api/src/main/java/net/kyori/adventure/text/SelectorComponent.java
Expand Up @@ -25,6 +25,7 @@

import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* A component that can display the name of entities found with a given selector.
Expand Down Expand Up @@ -60,6 +61,23 @@ public interface SelectorComponent extends BuildableComponent<SelectorComponent,
@Contract(pure = true)
@NotNull SelectorComponent pattern(final @NotNull String pattern);

/**
* Gets the separator.
*
* @return the separator
* @since 4.8.0
*/
@Nullable Component separator();

/**
* Sets the separator.
*
* @param separator the separator
* @return the separator
* @since 4.8.0
*/
@NotNull SelectorComponent separator(final @Nullable ComponentLike separator);

/**
* A selector component builder.
*
Expand All @@ -75,5 +93,15 @@ interface Builder extends ComponentBuilder<SelectorComponent, Builder> {
*/
@Contract("_ -> this")
@NotNull Builder pattern(final @NotNull String pattern);

/**
* Sets the separator.
*
* @param separator the separator
* @return this builder
* @since 4.8.0
*/
@Contract("_ -> this")
@NotNull Builder separator(final @Nullable ComponentLike separator);
}
}

0 comments on commit a6320c3

Please sign in to comment.