| 1 | package net.minecraft.util.datafix;
|
| 2 |
|
| 3 | import com.google.gson.JsonElement;
|
| 4 | import com.google.gson.JsonObject;
|
| 5 | import com.google.gson.JsonParseException;
|
| 6 | import com.mojang.serialization.Dynamic;
|
| 7 | import com.mojang.serialization.DynamicOps;
|
| 8 | import java.util.Optional;
|
| 9 | import net.minecraft.util.GsonHelper;
|
| 10 | import net.minecraft.util.LenientJsonParser;
|
| 11 | import net.minecraft.util.StrictJsonParser;
|
| 12 |
|
| 13 | public class LegacyComponentDataFixUtils {
|
| 14 | private static final String EMPTY_CONTENTS = createTextComponentJson("");
|
| 15 |
|
| 16 | public static <T> Dynamic<T> createPlainTextComponent(final DynamicOps<T> ops, final String text) {
|
| 17 | String stableString = createTextComponentJson(text);
|
| 18 | return new Dynamic<>(ops, ops.createString(stableString));
|
| 19 | }
|
| 20 |
|
| 21 | public static <T> Dynamic<T> createEmptyComponent(final DynamicOps<T> ops) {
|
| 22 | return new Dynamic<>(ops, ops.createString(EMPTY_CONTENTS));
|
| 23 | }
|
| 24 |
|
| 25 | public static String createTextComponentJson(final String text) {
|
| 26 | JsonObject result = new JsonObject();
|
| 27 | result.addProperty("text", text);
|
| 28 | return GsonHelper.toStableString(result);
|
| 29 | }
|
| 30 |
|
| 31 | public static String createTranslatableComponentJson(final String key) {
|
| 32 | JsonObject result = new JsonObject();
|
| 33 | result.addProperty("translate", key);
|
| 34 | return GsonHelper.toStableString(result);
|
| 35 | }
|
| 36 |
|
| 37 | public static <T> Dynamic<T> createTranslatableComponent(final DynamicOps<T> ops, final String key) {
|
| 38 | String stableString = createTranslatableComponentJson(key);
|
| 39 | return new Dynamic<>(ops, ops.createString(stableString));
|
| 40 | }
|
| 41 |
|
| 42 | public static String rewriteFromLenient(final String string) {
|
| 43 | if (!string.isEmpty() && !string.equals("null")) {
|
| 44 | char firstChar = string.charAt(0);
|
| 45 | char lastChar = string.charAt(string.length() - 1);
|
| 46 | if (firstChar == '"' && lastChar == '"' || firstChar == '{' && lastChar == '}' || firstChar == '[' && lastChar == ']') {
|
| 47 | try {
|
| 48 | JsonElement json = LenientJsonParser.parse(string);
|
| 49 | if (json.isJsonPrimitive()) {
|
| 50 | return createTextComponentJson(json.getAsString());
|
| 51 | }
|
| 52 |
|
| 53 | return GsonHelper.toStableString(json);
|
| 54 | } catch (JsonParseException var4) {
|
| 55 | }
|
| 56 | }
|
| 57 |
|
| 58 | return createTextComponentJson(string);
|
| 59 | } else {
|
| 60 | return EMPTY_CONTENTS;
|
| 61 | }
|
| 62 | }
|
| 63 |
|
| 64 | public static boolean isStrictlyValidJson(final Dynamic<?> component) {
|
| 65 | return component.asString().result().filter(string -> {
|
| 66 | try {
|
| 67 | StrictJsonParser.parse(string);
|
| 68 | return true;
|
| 69 | } catch (JsonParseException ignored) {
|
| 70 | return false;
|
| 71 | }
|
| 72 | }).isPresent();
|
| 73 | }
|
| 74 |
|
| 75 | public static Optional<String> extractTranslationString(final String component) {
|
| 76 | try {
|
| 77 | JsonElement parsed = LenientJsonParser.parse(component);
|
| 78 | if (parsed.isJsonObject()) {
|
| 79 | JsonObject parsedObject = parsed.getAsJsonObject();
|
| 80 | JsonElement key = parsedObject.get("translate");
|
| 81 | if (key != null && key.isJsonPrimitive()) {
|
| 82 | return Optional.of(key.getAsString());
|
| 83 | }
|
| 84 | }
|
| 85 | } catch (JsonParseException var4) {
|
| 86 | }
|
| 87 |
|
| 88 | return Optional.empty();
|
| 89 | }
|
| 90 | }
|
| 91 |
|