|
| 1 | +package io.github.thebusybiscuit.mobcapturer.utils.compatibility; |
| 2 | + |
| 3 | +import io.github.thebusybiscuit.mobcapturer.MobCapturer; |
| 4 | + |
| 5 | +import lombok.experimental.UtilityClass; |
| 6 | + |
| 7 | +import org.bukkit.NamespacedKey; |
| 8 | +import org.bukkit.entity.ZombieVillager; |
| 9 | + |
| 10 | +import javax.annotation.Nonnull; |
| 11 | + |
| 12 | +import java.lang.reflect.InvocationTargetException; |
| 13 | +import java.util.Locale; |
| 14 | +import java.util.logging.Level; |
| 15 | + |
| 16 | +// TODO: This needs to be changed since 1.22 the enum methods will be removed |
| 17 | +@UtilityClass |
| 18 | +public final class VillagerProfessionX { |
| 19 | + |
| 20 | + @Nonnull |
| 21 | + public static String getFromZombieVillager(@Nonnull ZombieVillager entity) { |
| 22 | + try { |
| 23 | + // get the profession of the zombie villager |
| 24 | + var getProfMethod = entity.getClass().getDeclaredMethod("getVillagerProfession"); |
| 25 | + Object prof = getProfMethod.invoke(entity); |
| 26 | + |
| 27 | + var getKeyMethod = prof.getClass().getDeclaredMethod("getKey"); |
| 28 | + var nsKey = (NamespacedKey) getKeyMethod.invoke(prof); |
| 29 | + return nsKey.getKey().toUpperCase(Locale.ROOT); |
| 30 | + } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { |
| 31 | + MobCapturer.getInstance().getLogger().log(Level.SEVERE, e, () -> "An error occurred while trying to get the profession of a ZombieVillager"); |
| 32 | + return "Unknown"; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + public static void setToZombieVillager(@Nonnull ZombieVillager entity, @Nonnull String profession) { |
| 37 | + try { |
| 38 | + // get the profession of the zombie villager |
| 39 | + var getProfMethod = entity.getClass().getDeclaredMethod("getVillagerProfession"); |
| 40 | + Object prof = getProfMethod.invoke(entity); |
| 41 | + |
| 42 | + var valueOfMethod = prof.getClass().getDeclaredMethod("valueOf", String.class); |
| 43 | + Object newProf = valueOfMethod.invoke(prof, profession); |
| 44 | + |
| 45 | + var setProfMethod = entity.getClass().getDeclaredMethod("setVillagerProfession", prof.getClass()); |
| 46 | + setProfMethod.invoke(entity, newProf); |
| 47 | + } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { |
| 48 | + // Ignore |
| 49 | + } |
| 50 | + } |
| 51 | +} |
0 commit comments