Skip to content

Commit

Permalink
minecraft-next
Browse files Browse the repository at this point in the history
  • Loading branch information
kashike committed Jan 19, 2023
1 parent ce2f9b7 commit 2ed1e7c
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 17 deletions.
8 changes: 4 additions & 4 deletions api/src/main/java/net/kyori/adventure/text/Component.java
Original file line number Diff line number Diff line change
Expand Up @@ -1288,7 +1288,7 @@ public interface Component extends ComponentBuilderApplicable, ComponentLike, Ex
*/
@Contract(value = "_, _ -> new", pure = true)
static @NotNull TranslatableComponent translatable(final @NotNull String key, final @NotNull Style style) {
return TranslatableComponentImpl.create(Collections.emptyList(), requireNonNull(style, "style"), key, Collections.emptyList());
return TranslatableComponentImpl.create(Collections.emptyList(), requireNonNull(style, "style"), key, null, Collections.emptyList());
}

/**
Expand Down Expand Up @@ -1423,7 +1423,7 @@ public interface Component extends ComponentBuilderApplicable, ComponentLike, Ex
*/
@Contract(value = "_, _, _ -> new", pure = true)
static @NotNull TranslatableComponent translatable(final @NotNull String key, final @NotNull Style style, final @NotNull ComponentLike@NotNull... args) {
return TranslatableComponentImpl.create(Collections.emptyList(), requireNonNull(style, "style"), key, requireNonNull(args, "args"));
return TranslatableComponentImpl.create(Collections.emptyList(), requireNonNull(style, "style"), key, null, requireNonNull(args, "args"));
}

/**
Expand Down Expand Up @@ -1508,7 +1508,7 @@ public interface Component extends ComponentBuilderApplicable, ComponentLike, Ex
*/
@Contract(value = "_, _ -> new", pure = true)
static @NotNull TranslatableComponent translatable(final @NotNull String key, final @NotNull List<? extends ComponentLike> args) {
return TranslatableComponentImpl.create(Collections.emptyList(), Style.empty(), key, requireNonNull(args, "args"));
return TranslatableComponentImpl.create(Collections.emptyList(), Style.empty(), key, null, requireNonNull(args, "args"));
}

/**
Expand All @@ -1535,7 +1535,7 @@ public interface Component extends ComponentBuilderApplicable, ComponentLike, Ex
*/
@Contract(value = "_, _, _ -> new", pure = true)
static @NotNull TranslatableComponent translatable(final @NotNull String key, final @NotNull Style style, final @NotNull List<? extends ComponentLike> args) {
return TranslatableComponentImpl.create(Collections.emptyList(), requireNonNull(style, "style"), key, requireNonNull(args, "args"));
return TranslatableComponentImpl.create(Collections.emptyList(), requireNonNull(style, "style"), key, null, requireNonNull(args, "args"));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import net.kyori.examination.ExaminableProperty;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* A component that can display translated text.
Expand Down Expand Up @@ -91,6 +92,24 @@ public interface TranslatableComponent extends BuildableComponent<TranslatableCo
@Contract(pure = true)
@NotNull TranslatableComponent key(final @NotNull String key);

/**
* Gets the fallback string.
*
* @return the fallback string
* @since 4.13.0
*/
@Nullable String fallback();

/**
* Sets the fallback string.
*
* @param fallback the fallback string
* @return a translatable component
* @since 4.13.0
*/
@Contract(pure = true)
@NotNull TranslatableComponent fallback(final @Nullable String fallback);

/**
* Gets the unmodifiable list of translation arguments.
*
Expand Down Expand Up @@ -158,6 +177,16 @@ interface Builder extends ComponentBuilder<TranslatableComponent, Builder> {
@Contract("_ -> this")
@NotNull Builder key(final @NotNull String key);

/**
* Sets the fallback string.
*
* @param fallback the fallback string
* @return this builder
* @since 4.13.0
*/
@Contract("_ -> this")
@NotNull Builder fallback(final @NotNull String fallback);

/**
* Sets the translation args.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,29 @@
import static java.util.Objects.requireNonNull;

final class TranslatableComponentImpl extends AbstractComponent implements TranslatableComponent {
static TranslatableComponent create(final @NotNull List<Component> children, final @NotNull Style style, final @NotNull String key, final @NotNull ComponentLike@NotNull[] args) {
static TranslatableComponent create(final @NotNull List<Component> children, final @NotNull Style style, final @NotNull String key, final @Nullable String fallback, final @NotNull ComponentLike@NotNull[] args) {
requireNonNull(args, "args");
return create(children, style, key, Arrays.asList(args));
return create(children, style, key, fallback, Arrays.asList(args));
}

static TranslatableComponent create(final @NotNull List<? extends ComponentLike> children, final @NotNull Style style, final @NotNull String key, final @NotNull List<? extends ComponentLike> args) {
static TranslatableComponent create(final @NotNull List<? extends ComponentLike> children, final @NotNull Style style, final @NotNull String key, final @Nullable String fallback, final @NotNull List<? extends ComponentLike> args) {
return new TranslatableComponentImpl(
ComponentLike.asComponents(children, IS_NOT_EMPTY),
requireNonNull(style, "style"),
requireNonNull(key, "key"),
fallback,
ComponentLike.asComponents(args) // Since translation arguments can be indexed, empty components are also included.
);
}

private final String key;
private final @Nullable String fallback;
private final List<Component> args;

TranslatableComponentImpl(final @NotNull List<Component> children, final @NotNull Style style, final @NotNull String key, final @NotNull List<Component> args) {
TranslatableComponentImpl(final @NotNull List<Component> children, final @NotNull Style style, final @NotNull String key, final @Nullable String fallback, final @NotNull List<Component> args) {
super(children, style);
this.key = key;
this.fallback = fallback;
this.args = args;
}

Expand All @@ -68,7 +71,17 @@ static TranslatableComponent create(final @NotNull List<? extends ComponentLike>
@Override
public @NotNull TranslatableComponent key(final @NotNull String key) {
if (Objects.equals(this.key, key)) return this;
return create(this.children, this.style, key, this.args);
return create(this.children, this.style, key, this.fallback, this.args);
}

@Override
public @Nullable String fallback() {
return this.fallback;
}

@Override
public @NotNull TranslatableComponent fallback(final @Nullable String fallback) {
return create(this.children, this.style, this.key, fallback, this.args);
}

@Override
Expand All @@ -78,22 +91,22 @@ static TranslatableComponent create(final @NotNull List<? extends ComponentLike>

@Override
public @NotNull TranslatableComponent args(final @NotNull ComponentLike@NotNull... args) {
return create(this.children, this.style, this.key, args);
return create(this.children, this.style, this.key, this.fallback, args);
}

@Override
public @NotNull TranslatableComponent args(final @NotNull List<? extends ComponentLike> args) {
return create(this.children, this.style, this.key, args);
return create(this.children, this.style, this.key, this.fallback, args);
}

@Override
public @NotNull TranslatableComponent children(final @NotNull List<? extends ComponentLike> children) {
return create(children, this.style, this.key, this.args);
return create(children, this.style, this.key, this.fallback, this.args);
}

@Override
public @NotNull TranslatableComponent style(final @NotNull Style style) {
return create(this.children, style, this.key, this.args);
return create(this.children, style, this.key, this.fallback, this.args);
}

@Override
Expand All @@ -102,13 +115,14 @@ public boolean equals(final @Nullable Object other) {
if (!(other instanceof TranslatableComponent)) return false;
if (!super.equals(other)) return false;
final TranslatableComponent that = (TranslatableComponent) other;
return Objects.equals(this.key, that.key()) && Objects.equals(this.args, that.args());
return Objects.equals(this.key, that.key()) && Objects.equals(this.fallback, that.fallback()) && Objects.equals(this.args, that.args());
}

@Override
public int hashCode() {
int result = super.hashCode();
result = (31 * result) + this.key.hashCode();
result = (31 * result) + Objects.hashCode(this.fallback);
result = (31 * result) + this.args.hashCode();
return result;
}
Expand All @@ -125,6 +139,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<? extends Component> args = Collections.emptyList();

BuilderImpl() {
Expand All @@ -142,6 +157,12 @@ static final class BuilderImpl extends AbstractComponentBuilder<TranslatableComp
return this;
}

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

@Override
public @NotNull Builder args(final @NotNull ComponentBuilder<?, ?> arg) {
return this.args(Collections.singletonList(requireNonNull(arg, "arg").build()));
Expand Down Expand Up @@ -176,7 +197,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.args);
return create(this.children, this.buildStyle(), this.key, this.fallback, this.args);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
final class ComponentSerializerImpl extends TypeAdapter<Component> {
static final String TEXT = "text";
static final String TRANSLATE = "translate";
static final String TRANSLATE_FALLBACK = "fallback";
static final String TRANSLATE_WITH = "with";
static final String SCORE = "score";
static final String SCORE_NAME = "name";
Expand Down Expand Up @@ -115,6 +116,7 @@ private ComponentSerializerImpl(final Gson gson) {
// type specific
String text = null;
String translate = null;
String translateFallback = null;
List<Component> translateWith = null;
String scoreName = null;
String scoreObjective = null;
Expand All @@ -135,6 +137,8 @@ private ComponentSerializerImpl(final Gson gson) {
text = readString(in);
} else if (fieldName.equals(TRANSLATE)) {
translate = in.nextString();
} else if (fieldName.equals(TRANSLATE_FALLBACK)) {
translateFallback = in.nextString();
} else if (fieldName.equals(TRANSLATE_WITH)) {
translateWith = this.gson.fromJson(in, COMPONENT_LIST_TYPE);
} else if (fieldName.equals(SCORE)) {
Expand Down Expand Up @@ -183,9 +187,9 @@ private ComponentSerializerImpl(final Gson gson) {
builder = Component.text().content(text);
} else if (translate != null) {
if (translateWith != null) {
builder = Component.translatable().key(translate).args(translateWith);
builder = Component.translatable().key(translate).fallback(translateFallback).args(translateWith);
} else {
builder = Component.translatable().key(translate);
builder = Component.translatable().key(translate).fallback(translateFallback);
}
} else if (scoreName != null && scoreObjective != null) {
if (scoreValue == null) {
Expand Down Expand Up @@ -261,6 +265,11 @@ public void write(final JsonWriter out, final Component value) throws IOExceptio
final TranslatableComponent translatable = (TranslatableComponent) value;
out.name(TRANSLATE);
out.value(translatable.key());
final @Nullable String fallback = translatable.fallback();
if (fallback != null) {
out.name(TRANSLATE_FALLBACK);
out.value(fallback);
}
if (!translatable.args().isEmpty()) {
out.name(TRANSLATE_WITH);
this.gson.toJson(translatable.args(), COMPONENT_LIST_TYPE, out);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ void testNoArgs() {
this.test(Component.translatable(KEY), object(json -> json.addProperty(ComponentSerializerImpl.TRANSLATE, KEY)));
}

@Test
void testFallback() {
this.test(
Component.translatable()
.key("thisIsA")
.fallback("This is a test.")
.build(),
object(json -> {
json.addProperty(ComponentSerializerImpl.TRANSLATE, "thisIsA");
json.addProperty(ComponentSerializerImpl.TRANSLATE_FALLBACK, "This is a test.");
})
);
}

@Test
void testSingleArgWithEvents() {
final UUID id = UUID.fromString("eb121687-8b1a-4944-bd4d-e0a818d9dfe2");
Expand Down

0 comments on commit 2ed1e7c

Please sign in to comment.