Skip to content

Commit

Permalink
fix: ZombieVillager's profession
Browse files Browse the repository at this point in the history
  • Loading branch information
ybw0014 committed Dec 6, 2024
1 parent bb3515e commit 976de0e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<ZombieVillager> {
Expand All @@ -37,7 +37,7 @@ public List<String> 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;
Expand All @@ -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");

Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
}
}

0 comments on commit 976de0e

Please sign in to comment.