From 9aea18749dbf62453439582e89e3802d78f53136 Mon Sep 17 00:00:00 2001 From: Intelli Date: Wed, 24 Jul 2024 14:04:46 -0600 Subject: [PATCH] Fixed backwards compatibility with entity serialization --- .../net/coreprotect/bukkit/BukkitAdapter.java | 10 ++++++++++ .../net/coreprotect/bukkit/BukkitInterface.java | 4 ++++ .../net/coreprotect/bukkit/Bukkit_v1_19.java | 7 ++----- .../net/coreprotect/bukkit/Bukkit_v1_21.java | 16 ++++++++++++++++ .../listener/entity/EntityDeathListener.java | 8 ++++---- .../coreprotect/utility/entity/EntityUtil.java | 14 ++++---------- 6 files changed, 40 insertions(+), 19 deletions(-) diff --git a/src/main/java/net/coreprotect/bukkit/BukkitAdapter.java b/src/main/java/net/coreprotect/bukkit/BukkitAdapter.java index db618d5f..8aac8499 100644 --- a/src/main/java/net/coreprotect/bukkit/BukkitAdapter.java +++ b/src/main/java/net/coreprotect/bukkit/BukkitAdapter.java @@ -298,4 +298,14 @@ public EntityType getEntityType(Material material) { } } + @Override + public Object getRegistryKey(Object value) { + return value; + } + + @Override + public Object getRegistryValue(String key, Object tClass) { + return null; + } + } diff --git a/src/main/java/net/coreprotect/bukkit/BukkitInterface.java b/src/main/java/net/coreprotect/bukkit/BukkitInterface.java index f8dbffab..d65a2b02 100644 --- a/src/main/java/net/coreprotect/bukkit/BukkitInterface.java +++ b/src/main/java/net/coreprotect/bukkit/BukkitInterface.java @@ -89,4 +89,8 @@ public interface BukkitInterface { public EntityType getEntityType(Material material); + public Object getRegistryKey(Object value); + + public Object getRegistryValue(String key, Object tClass); + } diff --git a/src/main/java/net/coreprotect/bukkit/Bukkit_v1_19.java b/src/main/java/net/coreprotect/bukkit/Bukkit_v1_19.java index f84f902b..32a21088 100644 --- a/src/main/java/net/coreprotect/bukkit/Bukkit_v1_19.java +++ b/src/main/java/net/coreprotect/bukkit/Bukkit_v1_19.java @@ -4,9 +4,7 @@ import java.util.HashSet; import java.util.List; -import org.bukkit.Bukkit; import org.bukkit.Material; -import org.bukkit.NamespacedKey; import org.bukkit.entity.Entity; import org.bukkit.entity.Frog; import org.bukkit.entity.Goat; @@ -33,7 +31,7 @@ public Bukkit_v1_19() { public boolean getEntityMeta(LivingEntity entity, List info) { if (entity instanceof Frog) { Frog frog = (Frog) entity; - info.add(frog.getVariant().getKey().asString()); + info.add(BukkitAdapter.ADAPTER.getRegistryKey(frog.getVariant())); } else if (entity instanceof Tadpole) { Tadpole tadpole = (Tadpole) entity; @@ -61,8 +59,7 @@ public boolean setEntityMeta(Entity entity, Object value, int count) { Frog frog = (Frog) entity; if (count == 0) { if (value instanceof String) { - NamespacedKey namespacedKey = NamespacedKey.fromString((String) value); - value = Bukkit.getRegistry(Frog.Variant.class).get(namespacedKey); + value = BukkitAdapter.ADAPTER.getRegistryValue((String) value, Frog.Variant.class); } Frog.Variant set = (Frog.Variant) value; frog.setVariant(set); diff --git a/src/main/java/net/coreprotect/bukkit/Bukkit_v1_21.java b/src/main/java/net/coreprotect/bukkit/Bukkit_v1_21.java index 2815f7a3..866d4a92 100644 --- a/src/main/java/net/coreprotect/bukkit/Bukkit_v1_21.java +++ b/src/main/java/net/coreprotect/bukkit/Bukkit_v1_21.java @@ -3,7 +3,10 @@ import java.util.Arrays; import java.util.HashSet; +import org.bukkit.Bukkit; +import org.bukkit.Keyed; import org.bukkit.Material; +import org.bukkit.NamespacedKey; import org.bukkit.Tag; import org.bukkit.entity.EntityType; @@ -40,4 +43,17 @@ public EntityType getEntityType(Material material) { } } + @Override + public Object getRegistryKey(Object value) { + return ((Keyed) value).getKey().asString(); + } + + @Override + public Object getRegistryValue(String key, Object tClass) { + NamespacedKey namespacedKey = NamespacedKey.fromString(key); + // return RegistryAccess.registryAccess().getRegistry(RegistryKey.CAT_VARIANT).get((NamespacedKey)value); + return Bukkit.getRegistry((Class) tClass).get(namespacedKey); + + } + } diff --git a/src/main/java/net/coreprotect/listener/entity/EntityDeathListener.java b/src/main/java/net/coreprotect/listener/entity/EntityDeathListener.java index ed58ab1b..8a9ac74c 100644 --- a/src/main/java/net/coreprotect/listener/entity/EntityDeathListener.java +++ b/src/main/java/net/coreprotect/listener/entity/EntityDeathListener.java @@ -322,7 +322,7 @@ else if (entity instanceof IronGolem) { } else if (entity instanceof Cat) { Cat cat = (Cat) entity; - info.add(cat.getCatType().getKey().asString()); + info.add(BukkitAdapter.ADAPTER.getRegistryKey(cat.getCatType())); info.add(cat.getCollarColor()); } else if (entity instanceof Fox) { @@ -404,8 +404,8 @@ else if (entity instanceof AbstractVillager) { if (abstractVillager instanceof Villager) { Villager villager = (Villager) abstractVillager; - info.add(villager.getProfession().getKey().asString()); - info.add(villager.getVillagerType().getKey().asString()); + info.add(BukkitAdapter.ADAPTER.getRegistryKey(villager.getProfession())); + info.add(BukkitAdapter.ADAPTER.getRegistryKey(villager.getVillagerType())); info.add(recipes); info.add(villager.getVillagerLevel()); info.add(villager.getVillagerExperience()); @@ -433,7 +433,7 @@ else if (entity instanceof Wolf) { else if (entity instanceof ZombieVillager) { ZombieVillager zombieVillager = (ZombieVillager) entity; info.add(zombieVillager.isBaby()); - info.add(zombieVillager.getVillagerProfession().getKey().asString()); + info.add(BukkitAdapter.ADAPTER.getRegistryKey(zombieVillager.getVillagerProfession())); } else if (entity instanceof Zombie) { Zombie zombie = (Zombie) entity; diff --git a/src/main/java/net/coreprotect/utility/entity/EntityUtil.java b/src/main/java/net/coreprotect/utility/entity/EntityUtil.java index 44dbfb3e..dcf43b85 100644 --- a/src/main/java/net/coreprotect/utility/entity/EntityUtil.java +++ b/src/main/java/net/coreprotect/utility/entity/EntityUtil.java @@ -7,7 +7,6 @@ import org.bukkit.Bukkit; import org.bukkit.DyeColor; import org.bukkit.Location; -import org.bukkit.NamespacedKey; import org.bukkit.OfflinePlayer; import org.bukkit.attribute.Attributable; import org.bukkit.attribute.Attribute; @@ -233,9 +232,7 @@ else if (entity instanceof Cat) { Cat cat = (Cat) entity; if (count == 0) { if (value instanceof String) { - NamespacedKey namespacedKey = NamespacedKey.fromString((String) value); - value = Bukkit.getRegistry(Cat.Type.class).get(namespacedKey); - // value = RegistryAccess.registryAccess().getRegistry(RegistryKey.CAT_VARIANT).get((NamespacedKey)value); + value = BukkitAdapter.ADAPTER.getRegistryValue((String) value, Cat.Type.class); } Cat.Type set = (Cat.Type) value; cat.setCatType(set); @@ -334,8 +331,7 @@ else if (entity instanceof AbstractVillager) { if (abstractVillager instanceof Villager) { Villager villager = (Villager) abstractVillager; if (value instanceof String) { - NamespacedKey namespacedKey = NamespacedKey.fromString((String) value); - value = Bukkit.getRegistry(Profession.class).get(namespacedKey); + value = BukkitAdapter.ADAPTER.getRegistryValue((String) value, Profession.class); } Profession set = (Profession) value; villager.setProfession(set); @@ -345,8 +341,7 @@ else if (count == 1) { if (abstractVillager instanceof Villager && value instanceof Villager.Type) { Villager villager = (Villager) abstractVillager; if (value instanceof String) { - NamespacedKey namespacedKey = NamespacedKey.fromString((String) value); - value = Bukkit.getRegistry(Villager.Type.class).get(namespacedKey); + value = BukkitAdapter.ADAPTER.getRegistryValue((String) value, Villager.Type.class); } Villager.Type set = (Villager.Type) value; villager.setVillagerType(set); @@ -442,8 +437,7 @@ else if (entity instanceof ZombieVillager) { } else if (count == 1) { if (value instanceof String) { - NamespacedKey namespacedKey = NamespacedKey.fromString((String) value); - value = Bukkit.getRegistry(Profession.class).get(namespacedKey); + value = BukkitAdapter.ADAPTER.getRegistryValue((String) value, Profession.class); } Profession set = (Profession) value; zombieVillager.setVillagerProfession(set);