Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,18 @@ interface Builder extends ComponentBuilder<TranslatableComponent, Builder> {
return this.arguments(args);
}

/**
* Adds a single translation arg.
*
* <p>Non-{@link Component} arguments can be wrapped in {@link TranslationArgument}, or represented with a {@link TranslationArgumentLike}.</p>
*
* @param like the translation arg
* @return this builder
* @since 4.25.0
*/
@Contract("_ -> this")
@NotNull Builder addArgument(final @NotNull ComponentLike like);

/**
* Sets the translation args.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public String toString() {
static final class BuilderImpl extends AbstractComponentBuilder<TranslatableComponent, Builder> implements TranslatableComponent.Builder {
private @Nullable String key;
private @Nullable String fallback;
private List<TranslationArgument> args = Collections.emptyList();
private List<TranslationArgument> args = null;

BuilderImpl() {
}
Expand All @@ -157,12 +157,25 @@ static final class BuilderImpl extends AbstractComponentBuilder<TranslatableComp
this.fallback = component.fallback();
}

private List<TranslationArgument> args() {
if (this.args == null) {
this.args = new ArrayList<>();
}
return this.args;
}

@Override
public @NotNull Builder key(final @NotNull String key) {
this.key = key;
return this;
}

@Override
public @NotNull Builder addArgument(final @NotNull ComponentLike like) {
this.args().add(asArgument(like, -1));
return this;
}

@Override
public @NotNull Builder arguments(final @NotNull ComponentLike@NotNull... args) {
requireNonNull(args, "args");
Expand All @@ -172,7 +185,11 @@ static final class BuilderImpl extends AbstractComponentBuilder<TranslatableComp

@Override
public @NotNull Builder arguments(final @NotNull List<? extends ComponentLike> args) {
this.args = asArguments(requireNonNull(args, "args"));
requireNonNull(args, "args");
this.args = new ArrayList<>(args.size());
for (int i = 0; i < args.size(); i++) {
this.args.add(asArgument(args.get(i), i));
}
Comment on lines +188 to +192
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why the new defensive copy here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean the manual ArrayList? Because it needed to be a mutable list for the builder, so it couldn't use the existing asArguments (and I didn't want to change that method around because it's used in other places)

return this;
}

Expand All @@ -185,7 +202,7 @@ static final class BuilderImpl extends AbstractComponentBuilder<TranslatableComp
@Override
public @NotNull TranslatableComponent build() {
if (this.key == null) throw new IllegalStateException("key must be set");
return create(this.children, this.buildStyle(), this.key, this.fallback, this.args);
return create(this.children, this.buildStyle(), this.key, this.fallback, this.args == null ? Collections.emptyList() : this.args);
}
}

Expand All @@ -196,19 +213,22 @@ static List<TranslationArgument> asArguments(final @NotNull List<? extends Compo

final List<TranslationArgument> ret = new ArrayList<>(likes.size());
for (int i = 0; i < likes.size(); i++) {
final ComponentLike like = likes.get(i);
if (like == null) {
throw new NullPointerException("likes[" + i + "]");
}
if (like instanceof TranslationArgument) {
ret.add((TranslationArgument) like);
} else if (like instanceof TranslationArgumentLike) {
ret.add(requireNonNull(((TranslationArgumentLike) like).asTranslationArgument(), "likes[" + i + "].asTranslationArgument()"));
} else {
ret.add(TranslationArgument.component(like));
}
ret.add(asArgument(likes.get(i), i));
}

return Collections.unmodifiableList(ret);
}

static TranslationArgument asArgument(final ComponentLike like, final int index) {
if (like == null) {
throw new NullPointerException("like" + (index != -1 ? "s[" + index + "]" : ""));
}
if (like instanceof TranslationArgument) {
return (TranslationArgument) like;
} else if (like instanceof TranslationArgumentLike) {
return requireNonNull(((TranslationArgumentLike) like).asTranslationArgument(), "like" + (index != -1 ? "s[" + index + "]" : "") + ".asTranslationArgument()");
} else {
return TranslationArgument.component(like);
}
}
}