From 976de0e9dbb9ffce16bd1c8f3d8d64e42fe2ce2d Mon Sep 17 00:00:00 2001 From: ybw0014 Date: Fri, 6 Dec 2024 11:18:15 -0800 Subject: [PATCH] fix: ZombieVillager's profession --- .../adapters/mobs/ZombieVillagerAdapter.java | 10 ++-- .../compatibility/VillagerProfessionX.java | 51 +++++++++++++++++++ 2 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 src/main/java/io/github/thebusybiscuit/mobcapturer/utils/compatibility/VillagerProfessionX.java diff --git a/src/main/java/io/github/thebusybiscuit/mobcapturer/adapters/mobs/ZombieVillagerAdapter.java b/src/main/java/io/github/thebusybiscuit/mobcapturer/adapters/mobs/ZombieVillagerAdapter.java index 63c268d..ce0d639 100644 --- a/src/main/java/io/github/thebusybiscuit/mobcapturer/adapters/mobs/ZombieVillagerAdapter.java +++ b/src/main/java/io/github/thebusybiscuit/mobcapturer/adapters/mobs/ZombieVillagerAdapter.java @@ -11,9 +11,9 @@ import org.bukkit.Bukkit; import org.bukkit.ChatColor; -import org.bukkit.entity.Villager.Profession; import org.bukkit.entity.ZombieVillager; +import io.github.thebusybiscuit.mobcapturer.utils.compatibility.VillagerProfessionX; import io.github.thebusybiscuit.slimefun4.utils.ChatUtils; public class ZombieVillagerAdapter extends ZombieAdapter { @@ -37,7 +37,7 @@ public List getLore(@Nonnull JsonObject json) { public JsonObject saveData(@Nonnull ZombieVillager entity) { JsonObject json = super.saveData(entity); - json.addProperty("profession", entity.getVillagerProfession().name()); + json.addProperty("profession", VillagerProfessionX.getFromZombieVillager(entity)); json.addProperty("conversionPlayer", entity.getConversionPlayer() == null ? null : entity.getConversionPlayer().getUniqueId().toString()); return json; @@ -48,7 +48,11 @@ public JsonObject saveData(@Nonnull ZombieVillager entity) { public void apply(ZombieVillager entity, JsonObject json) { super.apply(entity, json); - entity.setVillagerProfession(Profession.valueOf(json.get("profession").getAsString())); + var profession = json.get("profession").getAsString(); + + if (!profession.equals("Unknown")) { + VillagerProfessionX.setToZombieVillager(entity, profession); + } JsonElement player = json.get("conversionPlayer"); diff --git a/src/main/java/io/github/thebusybiscuit/mobcapturer/utils/compatibility/VillagerProfessionX.java b/src/main/java/io/github/thebusybiscuit/mobcapturer/utils/compatibility/VillagerProfessionX.java new file mode 100644 index 0000000..76edb74 --- /dev/null +++ b/src/main/java/io/github/thebusybiscuit/mobcapturer/utils/compatibility/VillagerProfessionX.java @@ -0,0 +1,51 @@ +package io.github.thebusybiscuit.mobcapturer.utils.compatibility; + +import io.github.thebusybiscuit.mobcapturer.MobCapturer; + +import lombok.experimental.UtilityClass; + +import org.bukkit.NamespacedKey; +import org.bukkit.entity.ZombieVillager; + +import javax.annotation.Nonnull; + +import java.lang.reflect.InvocationTargetException; +import java.util.Locale; +import java.util.logging.Level; + +// TODO: This needs to be changed since 1.22 the enum methods will be removed +@UtilityClass +public final class VillagerProfessionX { + + @Nonnull + public static String getFromZombieVillager(@Nonnull ZombieVillager entity) { + try { + // get the profession of the zombie villager + var getProfMethod = entity.getClass().getDeclaredMethod("getVillagerProfession"); + Object prof = getProfMethod.invoke(entity); + + var getKeyMethod = prof.getClass().getDeclaredMethod("getKey"); + var nsKey = (NamespacedKey) getKeyMethod.invoke(prof); + return nsKey.getKey().toUpperCase(Locale.ROOT); + } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { + MobCapturer.getInstance().getLogger().log(Level.SEVERE, e, () -> "An error occurred while trying to get the profession of a ZombieVillager"); + return "Unknown"; + } + } + + public static void setToZombieVillager(@Nonnull ZombieVillager entity, @Nonnull String profession) { + try { + // get the profession of the zombie villager + var getProfMethod = entity.getClass().getDeclaredMethod("getVillagerProfession"); + Object prof = getProfMethod.invoke(entity); + + var valueOfMethod = prof.getClass().getDeclaredMethod("valueOf", String.class); + Object newProf = valueOfMethod.invoke(prof, profession); + + var setProfMethod = entity.getClass().getDeclaredMethod("setVillagerProfession", prof.getClass()); + setProfMethod.invoke(entity, newProf); + } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { + // Ignore + } + } +}