mirror of
https://github.com/Melledy/Grasscutter.git
synced 2024-11-23 12:36:06 +00:00
Fix issue with serialized GridPosition
s
This commit is contained in:
parent
25c72a19e0
commit
66b0e6f3c6
@ -7,6 +7,7 @@ import java.io.Serializable;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
|
import lombok.SneakyThrows;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
public final class GridPosition implements Serializable {
|
public final class GridPosition implements Serializable {
|
||||||
@ -38,8 +39,13 @@ public final class GridPosition implements Serializable {
|
|||||||
this.x = xzwidth.get(0);
|
this.x = xzwidth.get(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public GridPosition(String str) throws IOException {
|
@SneakyThrows
|
||||||
String[] listOfParams = str.replace(" ", "").replace("(", "").replace(")", "").split(",");
|
public GridPosition(String str) {
|
||||||
|
var listOfParams = str
|
||||||
|
.replace(" ", "")
|
||||||
|
.replace("(", "")
|
||||||
|
.replace(")", "")
|
||||||
|
.split(",");
|
||||||
if (listOfParams.length != 3)
|
if (listOfParams.length != 3)
|
||||||
throw new IOException("invalid size on GridPosition definition - ");
|
throw new IOException("invalid size on GridPosition definition - ");
|
||||||
try {
|
try {
|
||||||
|
@ -77,6 +77,36 @@ public class JsonAdapters {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static class GridPositionAdapter extends TypeAdapter<GridPosition> {
|
||||||
|
@Override
|
||||||
|
public void write(JsonWriter out, GridPosition value) throws IOException {
|
||||||
|
out.value("(" + value.getX() + ", " + value.getZ() + ", " + value.getWidth() + ")");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public GridPosition read(JsonReader in) throws IOException {
|
||||||
|
if (in.peek() != JsonToken.STRING)
|
||||||
|
throw new IOException("Invalid GridPosition definition - " + in.peek().name());
|
||||||
|
|
||||||
|
// GridPosition follows the format of: (x, y, z).
|
||||||
|
// Flatten to (x,y,z) for easier parsing.
|
||||||
|
var str = in.nextString()
|
||||||
|
.replace("(", "")
|
||||||
|
.replace(")", "")
|
||||||
|
.replace(" ", "");
|
||||||
|
var split = str.split(",");
|
||||||
|
|
||||||
|
if (split.length != 3)
|
||||||
|
throw new IOException("Invalid GridPosition definition - " + in.peek().name());
|
||||||
|
|
||||||
|
return new GridPosition(
|
||||||
|
Integer.parseInt(split[0]),
|
||||||
|
Integer.parseInt(split[1]),
|
||||||
|
Integer.parseInt(split[2])
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static class PositionAdapter extends TypeAdapter<Position> {
|
static class PositionAdapter extends TypeAdapter<Position> {
|
||||||
@Override
|
@Override
|
||||||
public Position read(JsonReader reader) throws IOException {
|
public Position read(JsonReader reader) throws IOException {
|
||||||
|
@ -6,10 +6,7 @@ import com.google.gson.JsonElement;
|
|||||||
import com.google.gson.JsonSyntaxException;
|
import com.google.gson.JsonSyntaxException;
|
||||||
import com.google.gson.reflect.TypeToken;
|
import com.google.gson.reflect.TypeToken;
|
||||||
import emu.grasscutter.data.common.DynamicFloat;
|
import emu.grasscutter.data.common.DynamicFloat;
|
||||||
import emu.grasscutter.utils.JsonAdapters.DynamicFloatAdapter;
|
import emu.grasscutter.utils.JsonAdapters.*;
|
||||||
import emu.grasscutter.utils.JsonAdapters.EnumTypeAdapterFactory;
|
|
||||||
import emu.grasscutter.utils.JsonAdapters.IntListAdapter;
|
|
||||||
import emu.grasscutter.utils.JsonAdapters.PositionAdapter;
|
|
||||||
import it.unimi.dsi.fastutil.ints.IntList;
|
import it.unimi.dsi.fastutil.ints.IntList;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -25,12 +22,13 @@ import java.util.Map;
|
|||||||
public final class JsonUtils {
|
public final class JsonUtils {
|
||||||
static final Gson gson =
|
static final Gson gson =
|
||||||
new GsonBuilder()
|
new GsonBuilder()
|
||||||
.setPrettyPrinting()
|
.setPrettyPrinting()
|
||||||
.registerTypeAdapter(DynamicFloat.class, new DynamicFloatAdapter())
|
.registerTypeAdapter(DynamicFloat.class, new DynamicFloatAdapter())
|
||||||
.registerTypeAdapter(IntList.class, new IntListAdapter())
|
.registerTypeAdapter(IntList.class, new IntListAdapter())
|
||||||
.registerTypeAdapter(Position.class, new PositionAdapter())
|
.registerTypeAdapter(Position.class, new PositionAdapter())
|
||||||
.registerTypeAdapterFactory(new EnumTypeAdapterFactory())
|
.registerTypeAdapter(GridPosition.class, new GridPositionAdapter())
|
||||||
.create();
|
.registerTypeAdapterFactory(new EnumTypeAdapterFactory())
|
||||||
|
.create();
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Encode an object to a JSON string
|
* Encode an object to a JSON string
|
||||||
|
Loading…
Reference in New Issue
Block a user