From f4604b4c270e83a2620fc733188127ace7959001 Mon Sep 17 00:00:00 2001 From: tastybento Date: Wed, 3 Jul 2024 16:39:43 -0700 Subject: [PATCH] Fix to make it work --- .../json/BentoboxTypeAdapterFactory.java | 9 +++- .../json/adapters/PairTypeAdapter.java | 45 +++++++++++-------- 2 files changed, 35 insertions(+), 19 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/database/json/BentoboxTypeAdapterFactory.java b/src/main/java/world/bentobox/bentobox/database/json/BentoboxTypeAdapterFactory.java index 9f90a4793..e7aca2f8b 100644 --- a/src/main/java/world/bentobox/bentobox/database/json/BentoboxTypeAdapterFactory.java +++ b/src/main/java/world/bentobox/bentobox/database/json/BentoboxTypeAdapterFactory.java @@ -1,5 +1,7 @@ package world.bentobox.bentobox.database.json; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; import java.util.Map; import org.bukkit.Location; @@ -77,7 +79,12 @@ public class BentoboxTypeAdapterFactory implements TypeAdapterFactory { } else if (Vector.class.isAssignableFrom(rawType)) { return (TypeAdapter) new VectorTypeAdapter(); } else if (Pair.class.isAssignableFrom(rawType)) { - return (TypeAdapter) new PairTypeAdapter<>(); + // Add Pair handling here with type safety + Type pairType = type.getType(); + ParameterizedType parameterizedType = (ParameterizedType) pairType; + Type xType = parameterizedType.getActualTypeArguments()[0]; + Type zType = parameterizedType.getActualTypeArguments()[1]; + return (TypeAdapter) new PairTypeAdapter<>(xType, zType); } else if (ConfigurationSerializable.class.isAssignableFrom(rawType)) { // This covers a lot of Bukkit objects return (TypeAdapter) new BukkitObjectTypeAdapter(gson.getAdapter(Map.class)); diff --git a/src/main/java/world/bentobox/bentobox/database/json/adapters/PairTypeAdapter.java b/src/main/java/world/bentobox/bentobox/database/json/adapters/PairTypeAdapter.java index 20d0fe7cb..ae36aa815 100644 --- a/src/main/java/world/bentobox/bentobox/database/json/adapters/PairTypeAdapter.java +++ b/src/main/java/world/bentobox/bentobox/database/json/adapters/PairTypeAdapter.java @@ -1,7 +1,6 @@ package world.bentobox.bentobox.database.json.adapters; import java.io.IOException; -import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import com.google.gson.Gson; @@ -11,31 +10,41 @@ import com.google.gson.stream.JsonWriter; import world.bentobox.bentobox.util.Pair; -// Custom TypeAdapter for Pair public class PairTypeAdapter extends TypeAdapter> { + private final Type xType; + private final Type zType; + + public PairTypeAdapter(Type xType, Type zType) { + this.xType = xType; + this.zType = zType; + } @Override public void write(JsonWriter out, Pair pair) throws IOException { - if (pair == null || pair.getKey() == null || pair.getValue() == null) { - return; - } - out.beginArray(); - out.value(new Gson().toJson(pair.getKey())); - out.value(new Gson().toJson(pair.getValue())); - out.endArray(); + out.beginObject(); + out.name("x"); + Gson gson = new Gson(); + gson.toJson(pair.getKey(), xType, out); + out.name("z"); + gson.toJson(pair.getValue(), zType, out); + out.endObject(); } @Override public Pair read(JsonReader in) throws IOException { - in.beginArray(); - Type typeX = ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[0]; - X x = new Gson().fromJson(in.nextString(), typeX); - Type typeZ = ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[1]; - Z z = new Gson().fromJson(in.nextString(), typeZ); - in.endArray(); - if (x == null || z == null) { - return null; + X x = null; + Z z = null; + + in.beginObject(); + while (in.hasNext()) { + String name = in.nextName(); + if (name.equals("x")) { + x = new Gson().fromJson(in, xType); + } else if (name.equals("z")) { + z = new Gson().fromJson(in, zType); + } } + in.endObject(); return new Pair<>(x, z); } -} \ No newline at end of file +}