| 1 | package com.mojang.realmsclient.dto;
|
| 2 |
|
| 3 | import com.google.gson.JsonArray;
|
| 4 | import com.google.gson.JsonElement;
|
| 5 | import com.google.gson.JsonObject;
|
| 6 | import com.mojang.realmsclient.util.JsonUtils;
|
| 7 | import java.util.Objects;
|
| 8 | import net.fabricmc.api.EnvType;
|
| 9 | import net.fabricmc.api.Environment;
|
| 10 | import net.minecraft.locale.Language;
|
| 11 | import net.minecraft.network.chat.Component;
|
| 12 | import org.jspecify.annotations.Nullable;
|
| 13 |
|
| 14 | @Environment(EnvType.CLIENT)
|
| 15 | public class RealmsText {
|
| 16 | private static final String TRANSLATION_KEY = "translationKey";
|
| 17 | private static final String ARGS = "args";
|
| 18 | private final String translationKey;
|
| 19 | private final String @Nullable [] args;
|
| 20 |
|
| 21 | private RealmsText(final String translationKey, final String @Nullable [] args) {
|
| 22 | this.translationKey = translationKey;
|
| 23 | this.args = args;
|
| 24 | }
|
| 25 |
|
| 26 | public Component createComponent(final Component fallback) {
|
| 27 | return Objects.requireNonNullElse(this.createComponent(), fallback);
|
| 28 | }
|
| 29 |
|
| 30 | public @Nullable Component createComponent() {
|
| 31 | if (!Language.getInstance().has(this.translationKey)) {
|
| 32 | return null;
|
| 33 | } else {
|
| 34 | return this.args == null ? Component.translatable(this.translationKey) : Component.translatable(this.translationKey, this.args);
|
| 35 | }
|
| 36 | }
|
| 37 |
|
| 38 | public static RealmsText parse(final JsonObject jsonObject) {
|
| 39 | String translationKey = JsonUtils.getRequiredString("translationKey", jsonObject);
|
| 40 | JsonElement argsJsonElement = jsonObject.get("args");
|
| 41 | String[] args;
|
| 42 | if (argsJsonElement != null && !argsJsonElement.isJsonNull()) {
|
| 43 | JsonArray argsJsonArray = argsJsonElement.getAsJsonArray();
|
| 44 | args = new String[argsJsonArray.size()];
|
| 45 |
|
| 46 | for (int i = 0; i < argsJsonArray.size(); i++) {
|
| 47 | args[i] = argsJsonArray.get(i).getAsString();
|
| 48 | }
|
| 49 | } else {
|
| 50 | args = null;
|
| 51 | }
|
| 52 |
|
| 53 | return new RealmsText(translationKey, args);
|
| 54 | }
|
| 55 |
|
| 56 | @Override
|
| 57 | public String toString() {
|
| 58 | return this.translationKey;
|
| 59 | }
|
| 60 | }
|
| 61 |
|