|
| 1 | +package world.bentobox.limits.objects; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.Locale; |
| 6 | +import java.util.Map; |
| 7 | + |
| 8 | +import org.bukkit.NamespacedKey; |
| 9 | + |
| 10 | +import com.google.gson.TypeAdapter; |
| 11 | +import com.google.gson.stream.JsonReader; |
| 12 | +import com.google.gson.stream.JsonToken; |
| 13 | +import com.google.gson.stream.JsonWriter; |
| 14 | + |
| 15 | +/** |
| 16 | + * Backwards-compatible Gson {@link TypeAdapter} for {@code Map<NamespacedKey, Integer>}. |
| 17 | + * |
| 18 | + * <p>Older versions of Limits stored block counts and limits as |
| 19 | + * {@code Map<Material, Integer>}, which Gson serialized as a JSON object whose keys |
| 20 | + * were the Material enum names, e.g.: |
| 21 | + * <pre>{@code |
| 22 | + * "blockCounts": { |
| 23 | + * "POLISHED_DIORITE": 11, |
| 24 | + * "DIRT": 459 |
| 25 | + * } |
| 26 | + * }</pre> |
| 27 | + * |
| 28 | + * <p>The fields were later changed to {@code Map<NamespacedKey, Integer>}. |
| 29 | + * Without an adapter, Gson's reflective handling of NamespacedKey expects an |
| 30 | + * object form ({@code {"namespace":"minecraft","key":"dirt"}}) for each key, so |
| 31 | + * loading any pre-existing database file fails with |
| 32 | + * {@code Expected BEGIN_OBJECT but was STRING ... path $.blockCounts.}. |
| 33 | + * |
| 34 | + * <p>This adapter reads three input shapes and always writes the simple, |
| 35 | + * compact one: |
| 36 | + * <ul> |
| 37 | + * <li>Legacy bare enum names ({@code "POLISHED_DIORITE"}) -- mapped to |
| 38 | + * {@code minecraft:polished_diorite}.</li> |
| 39 | + * <li>Already-namespaced strings ({@code "minecraft:dirt"}).</li> |
| 40 | + * <li>The complex array form Gson produces with |
| 41 | + * {@code enableComplexMapKeySerialization()}: an array of |
| 42 | + * {@code [{"namespace":"...","key":"..."}, count]} pairs. Handled so any |
| 43 | + * file written by the broken intermediate version can still be read.</li> |
| 44 | + * </ul> |
| 45 | + */ |
| 46 | +public class NamespacedKeyMapAdapter extends TypeAdapter<Map<NamespacedKey, Integer>> { |
| 47 | + |
| 48 | + @Override |
| 49 | + public void write(JsonWriter out, Map<NamespacedKey, Integer> value) throws IOException { |
| 50 | + if (value == null) { |
| 51 | + out.nullValue(); |
| 52 | + return; |
| 53 | + } |
| 54 | + out.beginObject(); |
| 55 | + for (Map.Entry<NamespacedKey, Integer> entry : value.entrySet()) { |
| 56 | + if (entry.getKey() == null || entry.getValue() == null) { |
| 57 | + continue; |
| 58 | + } |
| 59 | + // Writes as "minecraft:dirt" |
| 60 | + out.name(entry.getKey().toString()); |
| 61 | + out.value(entry.getValue()); |
| 62 | + } |
| 63 | + out.endObject(); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public Map<NamespacedKey, Integer> read(JsonReader in) throws IOException { |
| 68 | + Map<NamespacedKey, Integer> result = new HashMap<>(); |
| 69 | + JsonToken token = in.peek(); |
| 70 | + if (token == JsonToken.NULL) { |
| 71 | + in.nextNull(); |
| 72 | + return result; |
| 73 | + } |
| 74 | + if (token == JsonToken.BEGIN_OBJECT) { |
| 75 | + // Legacy form (Material enum names) or current clean form (namespaced strings). |
| 76 | + in.beginObject(); |
| 77 | + while (in.hasNext()) { |
| 78 | + String rawKey = in.nextName(); |
| 79 | + Integer count = readIntOrNull(in); |
| 80 | + NamespacedKey key = parseKey(rawKey); |
| 81 | + if (key != null && count != null) { |
| 82 | + result.put(key, count); |
| 83 | + } |
| 84 | + } |
| 85 | + in.endObject(); |
| 86 | + return result; |
| 87 | + } |
| 88 | + if (token == JsonToken.BEGIN_ARRAY) { |
| 89 | + // Complex map form: [[{"namespace":..,"key":..}, count], ...] |
| 90 | + in.beginArray(); |
| 91 | + while (in.hasNext()) { |
| 92 | + in.beginArray(); |
| 93 | + NamespacedKey key = readKeyObject(in); |
| 94 | + Integer count = readIntOrNull(in); |
| 95 | + if (key != null && count != null) { |
| 96 | + result.put(key, count); |
| 97 | + } |
| 98 | + in.endArray(); |
| 99 | + } |
| 100 | + in.endArray(); |
| 101 | + return result; |
| 102 | + } |
| 103 | + // Unknown shape -- skip and return what we have. |
| 104 | + in.skipValue(); |
| 105 | + return result; |
| 106 | + } |
| 107 | + |
| 108 | + private static NamespacedKey parseKey(String raw) { |
| 109 | + if (raw == null || raw.isEmpty()) { |
| 110 | + return null; |
| 111 | + } |
| 112 | + if (raw.indexOf(':') >= 0) { |
| 113 | + return NamespacedKey.fromString(raw.toLowerCase(Locale.ROOT)); |
| 114 | + } |
| 115 | + // Bare legacy enum name like "POLISHED_DIORITE" |
| 116 | + try { |
| 117 | + return new NamespacedKey(NamespacedKey.MINECRAFT, raw.toLowerCase(Locale.ROOT)); |
| 118 | + } catch (IllegalArgumentException e) { |
| 119 | + return null; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + private static Integer readIntOrNull(JsonReader in) throws IOException { |
| 124 | + JsonToken t = in.peek(); |
| 125 | + if (t == JsonToken.NULL) { |
| 126 | + in.nextNull(); |
| 127 | + return null; |
| 128 | + } |
| 129 | + return in.nextInt(); |
| 130 | + } |
| 131 | + |
| 132 | + private static NamespacedKey readKeyObject(JsonReader in) throws IOException { |
| 133 | + JsonToken t = in.peek(); |
| 134 | + if (t == JsonToken.STRING) { |
| 135 | + return parseKey(in.nextString()); |
| 136 | + } |
| 137 | + if (t != JsonToken.BEGIN_OBJECT) { |
| 138 | + in.skipValue(); |
| 139 | + return null; |
| 140 | + } |
| 141 | + String namespace = null; |
| 142 | + String key = null; |
| 143 | + in.beginObject(); |
| 144 | + while (in.hasNext()) { |
| 145 | + String name = in.nextName(); |
| 146 | + if ("namespace".equals(name)) { |
| 147 | + namespace = in.nextString(); |
| 148 | + } else if ("key".equals(name)) { |
| 149 | + key = in.nextString(); |
| 150 | + } else { |
| 151 | + in.skipValue(); |
| 152 | + } |
| 153 | + } |
| 154 | + in.endObject(); |
| 155 | + if (namespace == null || key == null) { |
| 156 | + return null; |
| 157 | + } |
| 158 | + try { |
| 159 | + return new NamespacedKey(namespace.toLowerCase(Locale.ROOT), key.toLowerCase(Locale.ROOT)); |
| 160 | + } catch (IllegalArgumentException e) { |
| 161 | + return null; |
| 162 | + } |
| 163 | + } |
| 164 | +} |
0 commit comments