fix: correctly (de)serialise ItemType (#2620)

- InternalId changes depending on ~~magic~~ so we should not store this value
 - fixes #2589
This commit is contained in:
Jordan 2024-03-15 18:39:35 +01:00 committed by GitHub
parent df9527b0b7
commit 8363badf80
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 30 additions and 1 deletions

View File

@ -23,6 +23,8 @@ import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import com.sk89q.worldedit.math.BlockVector3; import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.math.Vector3; import com.sk89q.worldedit.math.Vector3;
import com.sk89q.worldedit.world.item.ItemType;
import com.sk89q.worldedit.world.item.ItemTypes;
/** /**
* Utility methods for Google's GSON library. * Utility methods for Google's GSON library.
@ -41,6 +43,7 @@ public final class GsonUtil {
GsonBuilder gsonBuilder = new GsonBuilder(); GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(Vector3.class, new VectorAdapter()); gsonBuilder.registerTypeAdapter(Vector3.class, new VectorAdapter());
gsonBuilder.registerTypeAdapter(BlockVector3.class, new BlockVectorAdapter()); gsonBuilder.registerTypeAdapter(BlockVector3.class, new BlockVectorAdapter());
gsonBuilder.registerTypeAdapter(ItemType.class, new ItemTypeAdapter());
return gsonBuilder; return gsonBuilder;
} }

View File

@ -0,0 +1,26 @@
package com.sk89q.worldedit.util.gson;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.sk89q.worldedit.world.item.ItemType;
import com.sk89q.worldedit.world.item.ItemTypes;
import java.lang.reflect.Type;
public final class ItemTypeAdapter implements JsonDeserializer<ItemType> {
@Override
public ItemType deserialize(JsonElement json, Type type, JsonDeserializationContext cont) throws JsonParseException {
JsonObject jsonObject = json.getAsJsonObject();
String id = jsonObject.get("id").getAsString();
ItemType itemType = ItemTypes.get(id);
if (itemType == null) {
throw new JsonParseException("Could not parse item type `" + id + "`");
}
return itemType;
}
}

View File

@ -79,7 +79,7 @@ public class ItemType implements RegistryItem, Keyed {
} }
//FAWE start //FAWE start
private int internalId; private transient int internalId;
@Override @Override
public void setInternalId(int internalId) { public void setInternalId(int internalId) {