diff --git a/src/main/java/emu/grasscutter/utils/GridPosition.java b/src/main/java/emu/grasscutter/utils/GridPosition.java index 59882d130..1cd1718c8 100644 --- a/src/main/java/emu/grasscutter/utils/GridPosition.java +++ b/src/main/java/emu/grasscutter/utils/GridPosition.java @@ -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 { diff --git a/src/main/java/emu/grasscutter/utils/JsonAdapters.java b/src/main/java/emu/grasscutter/utils/JsonAdapters.java index 091f644b6..e2559265a 100644 --- a/src/main/java/emu/grasscutter/utils/JsonAdapters.java +++ b/src/main/java/emu/grasscutter/utils/JsonAdapters.java @@ -77,6 +77,36 @@ public class JsonAdapters { } } + static class GridPositionAdapter extends TypeAdapter { + @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 { @Override public Position read(JsonReader reader) throws IOException { diff --git a/src/main/java/emu/grasscutter/utils/JsonUtils.java b/src/main/java/emu/grasscutter/utils/JsonUtils.java index 457d89299..84385b9cd 100644 --- a/src/main/java/emu/grasscutter/utils/JsonUtils.java +++ b/src/main/java/emu/grasscutter/utils/JsonUtils.java @@ -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; @@ -25,12 +22,13 @@ import java.util.Map; public final class JsonUtils { static final Gson gson = new GsonBuilder() - .setPrettyPrinting() - .registerTypeAdapter(DynamicFloat.class, new DynamicFloatAdapter()) - .registerTypeAdapter(IntList.class, new IntListAdapter()) - .registerTypeAdapter(Position.class, new PositionAdapter()) - .registerTypeAdapterFactory(new EnumTypeAdapterFactory()) - .create(); + .setPrettyPrinting() + .registerTypeAdapter(DynamicFloat.class, new DynamicFloatAdapter()) + .registerTypeAdapter(IntList.class, new IntListAdapter()) + .registerTypeAdapter(Position.class, new PositionAdapter()) + .registerTypeAdapter(GridPosition.class, new GridPositionAdapter()) + .registerTypeAdapterFactory(new EnumTypeAdapterFactory()) + .create(); /* * Encode an object to a JSON string