mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2025-01-08 01:17:41 +01:00
Fix to make it work
This commit is contained in:
parent
755452cd3c
commit
f4604b4c27
@ -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<T>) new VectorTypeAdapter();
|
||||
} 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)) {
|
||||
// This covers a lot of Bukkit objects
|
||||
return (TypeAdapter<T>) new BukkitObjectTypeAdapter(gson.getAdapter(Map.class));
|
||||
|
@ -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<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
|
||||
public void write(JsonWriter out, Pair<X, Z> 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<X, Z> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user