Fix to make it work

This commit is contained in:
tastybento 2024-07-03 16:39:43 -07:00
parent 755452cd3c
commit f4604b4c27
2 changed files with 35 additions and 19 deletions

View File

@ -1,5 +1,7 @@
package world.bentobox.bentobox.database.json; package world.bentobox.bentobox.database.json;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Map; import java.util.Map;
import org.bukkit.Location; import org.bukkit.Location;
@ -77,7 +79,12 @@ public class BentoboxTypeAdapterFactory implements TypeAdapterFactory {
} else if (Vector.class.isAssignableFrom(rawType)) { } else if (Vector.class.isAssignableFrom(rawType)) {
return (TypeAdapter<T>) new VectorTypeAdapter(); return (TypeAdapter<T>) new VectorTypeAdapter();
} else if (Pair.class.isAssignableFrom(rawType)) { } else if (Pair.class.isAssignableFrom(rawType)) {
return (TypeAdapter<T>) 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<T>) new PairTypeAdapter<>(xType, zType);
} else if (ConfigurationSerializable.class.isAssignableFrom(rawType)) { } else if (ConfigurationSerializable.class.isAssignableFrom(rawType)) {
// This covers a lot of Bukkit objects // This covers a lot of Bukkit objects
return (TypeAdapter<T>) new BukkitObjectTypeAdapter(gson.getAdapter(Map.class)); return (TypeAdapter<T>) new BukkitObjectTypeAdapter(gson.getAdapter(Map.class));

View File

@ -1,7 +1,6 @@
package world.bentobox.bentobox.database.json.adapters; package world.bentobox.bentobox.database.json.adapters;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import com.google.gson.Gson; import com.google.gson.Gson;
@ -11,31 +10,41 @@ import com.google.gson.stream.JsonWriter;
import world.bentobox.bentobox.util.Pair; import world.bentobox.bentobox.util.Pair;
// Custom TypeAdapter for Pair<X, Z>
public class PairTypeAdapter<X, Z> extends TypeAdapter<Pair<X, Z>> { public class PairTypeAdapter<X, Z> extends TypeAdapter<Pair<X, Z>> {
private final Type xType;
private final Type zType;
public PairTypeAdapter(Type xType, Type zType) {
this.xType = xType;
this.zType = zType;
}
@Override @Override
public void write(JsonWriter out, Pair<X, Z> pair) throws IOException { public void write(JsonWriter out, Pair<X, Z> pair) throws IOException {
if (pair == null || pair.getKey() == null || pair.getValue() == null) { out.beginObject();
return; out.name("x");
} Gson gson = new Gson();
out.beginArray(); gson.toJson(pair.getKey(), xType, out);
out.value(new Gson().toJson(pair.getKey())); out.name("z");
out.value(new Gson().toJson(pair.getValue())); gson.toJson(pair.getValue(), zType, out);
out.endArray(); out.endObject();
} }
@Override @Override
public Pair<X, Z> read(JsonReader in) throws IOException { public Pair<X, Z> read(JsonReader in) throws IOException {
in.beginArray(); X x = null;
Type typeX = ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[0]; Z z = null;
X x = new Gson().fromJson(in.nextString(), typeX);
Type typeZ = ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[1]; in.beginObject();
Z z = new Gson().fromJson(in.nextString(), typeZ); while (in.hasNext()) {
in.endArray(); String name = in.nextName();
if (x == null || z == null) { if (name.equals("x")) {
return null; x = new Gson().fromJson(in, xType);
} else if (name.equals("z")) {
z = new Gson().fromJson(in, zType);
}
} }
in.endObject();
return new Pair<>(x, z); return new Pair<>(x, z);
} }
} }