mirror of
https://github.com/Melledy/Grasscutter.git
synced 2024-11-23 08:11:12 +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 lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.SneakyThrows;
|
||||
|
||||
@Entity
|
||||
public final class GridPosition implements Serializable {
|
||||
@ -38,8 +39,13 @@ public final class GridPosition implements Serializable {
|
||||
this.x = xzwidth.get(0);
|
||||
}
|
||||
|
||||
public GridPosition(String str) throws IOException {
|
||||
String[] listOfParams = str.replace(" ", "").replace("(", "").replace(")", "").split(",");
|
||||
@SneakyThrows
|
||||
public GridPosition(String str) {
|
||||
var listOfParams = str
|
||||
.replace(" ", "")
|
||||
.replace("(", "")
|
||||
.replace(")", "")
|
||||
.split(",");
|
||||
if (listOfParams.length != 3)
|
||||
throw new IOException("invalid size on GridPosition definition - ");
|
||||
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> {
|
||||
@Override
|
||||
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.reflect.TypeToken;
|
||||
import emu.grasscutter.data.common.DynamicFloat;
|
||||
import emu.grasscutter.utils.JsonAdapters.DynamicFloatAdapter;
|
||||
import emu.grasscutter.utils.JsonAdapters.EnumTypeAdapterFactory;
|
||||
import emu.grasscutter.utils.JsonAdapters.IntListAdapter;
|
||||
import emu.grasscutter.utils.JsonAdapters.PositionAdapter;
|
||||
import emu.grasscutter.utils.JsonAdapters.*;
|
||||
import it.unimi.dsi.fastutil.ints.IntList;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
@ -29,6 +26,7 @@ public final class JsonUtils {
|
||||
.registerTypeAdapter(DynamicFloat.class, new DynamicFloatAdapter())
|
||||
.registerTypeAdapter(IntList.class, new IntListAdapter())
|
||||
.registerTypeAdapter(Position.class, new PositionAdapter())
|
||||
.registerTypeAdapter(GridPosition.class, new GridPositionAdapter())
|
||||
.registerTypeAdapterFactory(new EnumTypeAdapterFactory())
|
||||
.create();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user