Skip to content

Commit 976de0e

Browse files
committed
fix: ZombieVillager's profession
1 parent bb3515e commit 976de0e

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed

src/main/java/io/github/thebusybiscuit/mobcapturer/adapters/mobs/ZombieVillagerAdapter.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111

1212
import org.bukkit.Bukkit;
1313
import org.bukkit.ChatColor;
14-
import org.bukkit.entity.Villager.Profession;
1514
import org.bukkit.entity.ZombieVillager;
1615

16+
import io.github.thebusybiscuit.mobcapturer.utils.compatibility.VillagerProfessionX;
1717
import io.github.thebusybiscuit.slimefun4.utils.ChatUtils;
1818

1919
public class ZombieVillagerAdapter extends ZombieAdapter<ZombieVillager> {
@@ -37,7 +37,7 @@ public List<String> getLore(@Nonnull JsonObject json) {
3737
public JsonObject saveData(@Nonnull ZombieVillager entity) {
3838
JsonObject json = super.saveData(entity);
3939

40-
json.addProperty("profession", entity.getVillagerProfession().name());
40+
json.addProperty("profession", VillagerProfessionX.getFromZombieVillager(entity));
4141
json.addProperty("conversionPlayer", entity.getConversionPlayer() == null ? null : entity.getConversionPlayer().getUniqueId().toString());
4242

4343
return json;
@@ -48,7 +48,11 @@ public JsonObject saveData(@Nonnull ZombieVillager entity) {
4848
public void apply(ZombieVillager entity, JsonObject json) {
4949
super.apply(entity, json);
5050

51-
entity.setVillagerProfession(Profession.valueOf(json.get("profession").getAsString()));
51+
var profession = json.get("profession").getAsString();
52+
53+
if (!profession.equals("Unknown")) {
54+
VillagerProfessionX.setToZombieVillager(entity, profession);
55+
}
5256

5357
JsonElement player = json.get("conversionPlayer");
5458

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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

Comments
 (0)