From 93d50db77fd4c615a722c0dbb22df0a7894fda0a Mon Sep 17 00:00:00 2001 From: tastybento Date: Tue, 23 Jul 2024 10:37:10 -0700 Subject: [PATCH] #2442 Fixes issue with loading blueprints with villagers on 1.21 --- .../json/BentoboxTypeAdapterFactory.java | 8 +++++ .../json/adapters/ProfessionTypeAdapter.java | 34 +++++++++++++++++++ .../json/adapters/VillagerTypeAdapter.java | 34 +++++++++++++++++++ .../managers/BlueprintClipboardManager.java | 2 ++ 4 files changed, 78 insertions(+) create mode 100644 src/main/java/world/bentobox/bentobox/database/json/adapters/ProfessionTypeAdapter.java create mode 100644 src/main/java/world/bentobox/bentobox/database/json/adapters/VillagerTypeAdapter.java diff --git a/src/main/java/world/bentobox/bentobox/database/json/BentoboxTypeAdapterFactory.java b/src/main/java/world/bentobox/bentobox/database/json/BentoboxTypeAdapterFactory.java index e7aca2f8b..6615ced50 100644 --- a/src/main/java/world/bentobox/bentobox/database/json/BentoboxTypeAdapterFactory.java +++ b/src/main/java/world/bentobox/bentobox/database/json/BentoboxTypeAdapterFactory.java @@ -9,6 +9,8 @@ import org.bukkit.Material; import org.bukkit.World; import org.bukkit.block.Biome; import org.bukkit.configuration.serialization.ConfigurationSerializable; +import org.bukkit.entity.Villager; +import org.bukkit.entity.Villager.Profession; import org.bukkit.inventory.ItemStack; import org.bukkit.potion.PotionEffectType; import org.bukkit.util.Vector; @@ -29,7 +31,9 @@ import world.bentobox.bentobox.database.json.adapters.LocationTypeAdapter; import world.bentobox.bentobox.database.json.adapters.MaterialTypeAdapter; import world.bentobox.bentobox.database.json.adapters.PairTypeAdapter; import world.bentobox.bentobox.database.json.adapters.PotionEffectTypeAdapter; +import world.bentobox.bentobox.database.json.adapters.ProfessionTypeAdapter; import world.bentobox.bentobox.database.json.adapters.VectorTypeAdapter; +import world.bentobox.bentobox.database.json.adapters.VillagerTypeAdapter; import world.bentobox.bentobox.database.json.adapters.WorldTypeAdapter; import world.bentobox.bentobox.util.Pair; @@ -78,6 +82,10 @@ public class BentoboxTypeAdapterFactory implements TypeAdapterFactory { return (TypeAdapter) new WorldTypeAdapter(); } else if (Vector.class.isAssignableFrom(rawType)) { return (TypeAdapter) new VectorTypeAdapter(); + } else if (Profession.class.isAssignableFrom(rawType)) { + return (TypeAdapter) new ProfessionTypeAdapter(); + } else if (Villager.Type.class.isAssignableFrom(rawType)) { + return (TypeAdapter) new VillagerTypeAdapter(); } else if (Pair.class.isAssignableFrom(rawType)) { // Add Pair handling here with type safety Type pairType = type.getType(); diff --git a/src/main/java/world/bentobox/bentobox/database/json/adapters/ProfessionTypeAdapter.java b/src/main/java/world/bentobox/bentobox/database/json/adapters/ProfessionTypeAdapter.java new file mode 100644 index 000000000..bd40494ec --- /dev/null +++ b/src/main/java/world/bentobox/bentobox/database/json/adapters/ProfessionTypeAdapter.java @@ -0,0 +1,34 @@ +package world.bentobox.bentobox.database.json.adapters; + +import java.io.IOException; + +import org.bukkit.entity.Villager.Profession; + +import com.google.gson.TypeAdapter; +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonToken; +import com.google.gson.stream.JsonWriter; + +public class ProfessionTypeAdapter extends TypeAdapter { + + @Override + public void write(JsonWriter out, Profession profession) throws IOException { + out.value(profession.name()); + } + + @Override + public Profession read(JsonReader reader) throws IOException { + if (reader.peek() == JsonToken.NULL) { + reader.nextNull(); + return null; + } + String id = reader.nextString(); + try { + return Profession.valueOf(id); + } catch (Exception e) { + // Do nothing + } + return Profession.NONE; + + } +} \ No newline at end of file diff --git a/src/main/java/world/bentobox/bentobox/database/json/adapters/VillagerTypeAdapter.java b/src/main/java/world/bentobox/bentobox/database/json/adapters/VillagerTypeAdapter.java new file mode 100644 index 000000000..050142ad6 --- /dev/null +++ b/src/main/java/world/bentobox/bentobox/database/json/adapters/VillagerTypeAdapter.java @@ -0,0 +1,34 @@ +package world.bentobox.bentobox.database.json.adapters; + +import java.io.IOException; + +import org.bukkit.entity.Villager; + +import com.google.gson.TypeAdapter; +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonToken; +import com.google.gson.stream.JsonWriter; + +public class VillagerTypeAdapter extends TypeAdapter { + + @Override + public void write(JsonWriter out, Villager.Type type) throws IOException { + out.value(type.name()); + } + + @Override + public Villager.Type read(JsonReader reader) throws IOException { + if (reader.peek() == JsonToken.NULL) { + reader.nextNull(); + return null; + } + String id = reader.nextString(); + try { + return Villager.Type.valueOf(id); + } catch (Exception e) { + // Do nothing + } + return Villager.Type.PLAINS; + + } +} \ No newline at end of file diff --git a/src/main/java/world/bentobox/bentobox/managers/BlueprintClipboardManager.java b/src/main/java/world/bentobox/bentobox/managers/BlueprintClipboardManager.java index 4a5e66c33..fdcbddee3 100644 --- a/src/main/java/world/bentobox/bentobox/managers/BlueprintClipboardManager.java +++ b/src/main/java/world/bentobox/bentobox/managers/BlueprintClipboardManager.java @@ -108,7 +108,9 @@ public class BlueprintClipboardManager { bp = gson.fromJson(fr, Blueprint.class); } catch (Exception e) { plugin.logError("Blueprint has JSON error: " + zipFile.getName()); + plugin.logStacktrace(e); throw new IOException("Blueprint has JSON error: " + zipFile.getName()); + } Files.delete(file.toPath()); // Bedrock check and set