mirror of
https://github.com/Melledy/Grasscutter.git
synced 2024-11-22 08:47:39 +00:00
Add Position JsonAdapter for [x,y,z] format
Also add serializers for existing JsonAdapters
This commit is contained in:
parent
ad502a8568
commit
da3981089d
@ -10,9 +10,11 @@ import com.google.gson.TypeAdapter;
|
||||
import com.google.gson.TypeAdapterFactory;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import com.google.gson.stream.JsonToken;
|
||||
import com.google.gson.stream.JsonWriter;
|
||||
|
||||
import emu.grasscutter.data.common.DynamicFloat;
|
||||
import it.unimi.dsi.fastutil.floats.FloatArrayList;
|
||||
import it.unimi.dsi.fastutil.ints.IntArrayList;
|
||||
import it.unimi.dsi.fastutil.ints.IntList;
|
||||
import lombok.val;
|
||||
@ -65,7 +67,54 @@ public class JsonAdapters {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(JsonWriter writer, IntList i) {};
|
||||
public void write(JsonWriter writer, IntList l) throws IOException {
|
||||
writer.beginArray();
|
||||
for (val i : l) // .forEach() doesn't appreciate exceptions
|
||||
writer.value(i);
|
||||
writer.endArray();
|
||||
};
|
||||
}
|
||||
|
||||
static class PositionAdapter extends TypeAdapter<Position> {
|
||||
@Override
|
||||
public Position read(JsonReader reader) throws IOException {
|
||||
switch (reader.peek()) {
|
||||
case BEGIN_ARRAY: // "pos": [x,y,z]
|
||||
reader.beginArray();
|
||||
val array = new FloatArrayList(3);
|
||||
while (reader.hasNext())
|
||||
array.add(reader.nextInt());
|
||||
reader.endArray();
|
||||
return new Position(array);
|
||||
case BEGIN_OBJECT: // "pos": {"x": x, "y": y, "z": z}
|
||||
float x = 0f;
|
||||
float y = 0f;
|
||||
float z = 0f;
|
||||
reader.beginObject();
|
||||
for (var next = reader.peek(); next != JsonToken.END_OBJECT; next = reader.peek()) {
|
||||
val name = reader.nextName();
|
||||
switch (name) {
|
||||
case "x", "X", "_x" -> x = (float) reader.nextDouble();
|
||||
case "y", "Y", "_y" -> y = (float) reader.nextDouble();
|
||||
case "z", "Z", "_z" -> z = (float) reader.nextDouble();
|
||||
default -> throw new IOException("Invalid field in Position definition - " + name);
|
||||
}
|
||||
}
|
||||
reader.endObject();
|
||||
return new Position(x, y, z);
|
||||
default:
|
||||
throw new IOException("Invalid Position definition - " + reader.peek().name());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(JsonWriter writer, Position i) throws IOException {
|
||||
writer.beginArray();
|
||||
writer.value(i.getX());
|
||||
writer.value(i.getY());
|
||||
writer.value(i.getZ());
|
||||
writer.endArray();
|
||||
};
|
||||
}
|
||||
|
||||
static class EnumTypeAdapterFactory implements TypeAdapterFactory {
|
||||
@ -109,7 +158,9 @@ public class JsonAdapters {
|
||||
throw new IOException("Invalid Enum definition - " + reader.peek().name());
|
||||
}
|
||||
}
|
||||
public void write(JsonWriter writer, T value) {}
|
||||
public void write(JsonWriter writer, T value) throws IOException {
|
||||
writer.value(value.toString());
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -27,6 +27,7 @@ public final class JsonUtils {
|
||||
.setPrettyPrinting()
|
||||
.registerTypeAdapter(DynamicFloat.class, new DynamicFloatAdapter())
|
||||
.registerTypeAdapter(IntList.class, new IntListAdapter())
|
||||
.registerTypeAdapter(Position.class, new PositionAdapter())
|
||||
.registerTypeAdapterFactory(new EnumTypeAdapterFactory())
|
||||
.create();
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
package emu.grasscutter.utils;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
import com.github.davidmoten.rtreemulti.geometry.Point;
|
||||
@ -22,9 +23,7 @@ public class Position implements Serializable {
|
||||
@SerializedName(value="z", alternate={"_z", "Z"})
|
||||
@Getter @Setter private float z;
|
||||
|
||||
public Position() {
|
||||
|
||||
}
|
||||
public Position() {}
|
||||
|
||||
public Position(float x, float y) {
|
||||
set(x, y);
|
||||
@ -34,6 +33,20 @@ public class Position implements Serializable {
|
||||
set(x, y, z);
|
||||
}
|
||||
|
||||
public Position(List<Float> xyz) {
|
||||
switch (xyz.size()) {
|
||||
default: // Might want to error on excess elements, but maybe we want to extend to 3+3 representation later.
|
||||
case 3:
|
||||
this.z = xyz.get(2); // Fall-through
|
||||
case 2:
|
||||
this.y = xyz.get(1); // Fall-through
|
||||
case 1:
|
||||
this.y = xyz.get(0); // pointless fall-through
|
||||
case 0:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public Position(String p) {
|
||||
String[] split = p.split(",");
|
||||
if (split.length >= 2) {
|
||||
|
Loading…
Reference in New Issue
Block a user