#2442 Fixes issue with loading blueprints with villagers on 1.21

This commit is contained in:
tastybento 2024-07-23 10:37:10 -07:00
parent 9fccb89261
commit 93d50db77f
4 changed files with 78 additions and 0 deletions

View File

@ -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<T>) new WorldTypeAdapter();
} else if (Vector.class.isAssignableFrom(rawType)) {
return (TypeAdapter<T>) new VectorTypeAdapter();
} else if (Profession.class.isAssignableFrom(rawType)) {
return (TypeAdapter<T>) new ProfessionTypeAdapter();
} else if (Villager.Type.class.isAssignableFrom(rawType)) {
return (TypeAdapter<T>) new VillagerTypeAdapter();
} else if (Pair.class.isAssignableFrom(rawType)) {
// Add Pair handling here with type safety
Type pairType = type.getType();

View File

@ -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<Profession> {
@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;
}
}

View File

@ -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<Villager.Type> {
@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;
}
}

View File

@ -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