Skip to content

Commit

Permalink
api: Introduce Component.textOfChildren as a replacement for TextComp…
Browse files Browse the repository at this point in the history
…onent.ofChildren

This indirectly reverts commit 9ffba5b.
  • Loading branch information
kashike committed Dec 19, 2021
1 parent dc7e0f0 commit 08fa6e2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
12 changes: 12 additions & 0 deletions api/src/main/java/net/kyori/adventure/text/Component.java
Expand Up @@ -726,6 +726,18 @@ public interface Component extends ComponentBuilderApplicable, ComponentLike, Ex
return new TextComponentImpl.BuilderImpl();
}

/**
* Creates a text component with {@code components} as the children.
*
* @param components the children
* @return a text component
* @since 4.10.0
*/
static @NotNull TextComponent textOfChildren(final @NotNull ComponentLike@NotNull... components) {
if (components.length == 0) return empty();
return new TextComponentImpl(Arrays.asList(components), Style.empty(), "");
}

/**
* Creates a text component by applying configuration from {@code consumer}.
*
Expand Down
6 changes: 2 additions & 4 deletions api/src/main/java/net/kyori/adventure/text/TextComponent.java
Expand Up @@ -45,14 +45,12 @@ public interface TextComponent extends BuildableComponent<TextComponent, TextCom
* @param components the children
* @return a text component
* @since 4.0.0
* @deprecated For removal since 4.9.0, use {@link Component#join(JoinConfiguration, ComponentLike...)} with {@link JoinConfiguration#noSeparators()}.
* @deprecated for removal since 4.9.0, use {@link Component#textOfChildren(ComponentLike...)} instead
*/
@ApiStatus.ScheduledForRemoval(inVersion = "5.0.0")
@Deprecated
static @NotNull TextComponent ofChildren(final @NotNull ComponentLike@NotNull... components) {
final Component joined = Component.join(JoinConfiguration.noSeparators(), components);
if (joined instanceof TextComponent) return (TextComponent) joined;
else return Component.text().append(joined).build();
return Component.textOfChildren(components);
}

/**
Expand Down
15 changes: 15 additions & 0 deletions api/src/test/java/net/kyori/adventure/text/TextComponentTest.java
Expand Up @@ -60,6 +60,21 @@ private static String legacy(final char character) {
return TextComponentImpl.SECTION_CHAR + String.valueOf(character);
}

@Test
void testOfChildren() {
assertSame(Component.empty(), Component.textOfChildren()); // empty array
assertEquals(
Component.text()
.append(Component.text("a"))
.append(Component.text().content("b"))
.build(),
Component.textOfChildren(
Component.text("a"),
Component.text().content("b")
)
);
}

@Test
void testOf() {
final TextComponent component = Component.text("foo");
Expand Down

0 comments on commit 08fa6e2

Please sign in to comment.