Skip to content

Commit 2f084d0

Browse files
committed
fix: address backward compatibility
1 parent 6d35654 commit 2f084d0

File tree

8 files changed

+97
-75
lines changed

8 files changed

+97
-75
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
import org.bukkit.ChatColor;
1111
import org.bukkit.DyeColor;
1212
import org.bukkit.entity.Cat;
13-
import org.bukkit.entity.Cat.Type;
1413

14+
import io.github.thebusybiscuit.mobcapturer.utils.compatibility.CatTypeX;
1515
import io.github.thebusybiscuit.slimefun4.utils.ChatUtils;
1616

1717
public class CatAdapter extends AbstractTameableAdapter<Cat> {
@@ -40,7 +40,7 @@ public List<String> getLore(@Nonnull JsonObject json) {
4040
public void apply(Cat entity, JsonObject json) {
4141
super.apply(entity, json);
4242

43-
entity.setCatType(Type.valueOf(json.get("catType").getAsString()));
43+
CatTypeX.set(entity, json.get("catType").getAsString());
4444
entity.setSitting(json.get("sitting").getAsBoolean());
4545
entity.setCollarColor(DyeColor.valueOf(json.get("collarColor").getAsString()));
4646
}
@@ -50,7 +50,7 @@ public void apply(Cat entity, JsonObject json) {
5050
public JsonObject saveData(@Nonnull Cat entity) {
5151
JsonObject json = super.saveData(entity);
5252

53-
json.addProperty("catType", entity.getCatType().name());
53+
json.addProperty("catType", CatTypeX.get(entity));
5454
json.addProperty("sitting", entity.isSitting());
5555
json.addProperty("collarColor", entity.getCollarColor().name());
5656

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

Lines changed: 0 additions & 38 deletions
This file was deleted.

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
import com.google.gson.JsonObject;
99

10+
import io.github.thebusybiscuit.mobcapturer.utils.compatibility.FrogVariantX;
11+
1012
import org.bukkit.ChatColor;
1113
import org.bukkit.entity.Frog;
1214
import org.bukkit.entity.Frog.Variant;
@@ -34,15 +36,15 @@ public List<String> getLore(@Nonnull JsonObject json) {
3436
public void apply(Frog entity, JsonObject json) {
3537
super.apply(entity, json);
3638

37-
entity.setVariant(Variant.valueOf(json.get("variant").getAsString()));
39+
FrogVariantX.set(entity, json.get("variant").getAsString());
3840
}
3941

4042
@Nonnull
4143
@Override
4244
public JsonObject saveData(@Nonnull Frog entity) {
4345
JsonObject json = super.saveData(entity);
4446

45-
json.addProperty("variant", entity.getVariant().name());
47+
json.addProperty("variant", FrogVariantX.get(entity));
4648

4749
return json;
4850
}

src/main/java/io/github/thebusybiscuit/mobcapturer/setup/Setup.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.bukkit.entity.Donkey;
1919
import org.bukkit.entity.Drowned;
2020
import org.bukkit.entity.ElderGuardian;
21+
import org.bukkit.entity.Endermite;
2122
import org.bukkit.entity.EntityType;
2223
import org.bukkit.entity.Evoker;
2324
import org.bukkit.entity.Ghast;
@@ -60,7 +61,6 @@
6061
import io.github.thebusybiscuit.mobcapturer.adapters.mobs.ChestedHorseAdapter;
6162
import io.github.thebusybiscuit.mobcapturer.adapters.mobs.CreeperAdapter;
6263
import io.github.thebusybiscuit.mobcapturer.adapters.mobs.EndermanAdapter;
63-
import io.github.thebusybiscuit.mobcapturer.adapters.mobs.EndermiteAdapter;
6464
import io.github.thebusybiscuit.mobcapturer.adapters.mobs.FoxAdapter;
6565
import io.github.thebusybiscuit.mobcapturer.adapters.mobs.FrogAdapter;
6666
import io.github.thebusybiscuit.mobcapturer.adapters.mobs.GlowSquidAdapter;
@@ -275,7 +275,7 @@ private static void setupMobEggs() {
275275
// https://minecraft-heads.com/custom-heads/decoration/23585-spawn-egg-shulker
276276
registerMob(EntityType.SHULKER, new ShulkerAdapter(), "d04252216231b3f744c9ff4ace7084ae9f4164f8b384c65410848a19617af4d");
277277
// https://minecraft-heads.com/custom-heads/decoration/954-spawn-egg-endermite
278-
registerMob(EntityType.ENDERMITE, new EndermiteAdapter(), "3beac501e97db1cc035287d068a8eb538e55ef802f5cca25683933a243136c");
278+
registerMob(EntityType.ENDERMITE, new StandardMobAdapter<>(Endermite.class), "3beac501e97db1cc035287d068a8eb538e55ef802f5cca25683933a243136c");
279279
// </editor-fold>
280280

281281
// <editor-fold desc="Golems">
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package io.github.thebusybiscuit.mobcapturer.utils;
2+
3+
import lombok.experimental.UtilityClass;
4+
5+
/**
6+
* Reflection utilities used for backward compatibility.
7+
* Other plugins should not use this class.
8+
*/
9+
@UtilityClass
10+
@SuppressWarnings("unchecked")
11+
public final class ReflectionUtils {
12+
13+
public static Object invoke(Object instance, String methodName, Object... args) {
14+
Class<?> currentClass = instance.getClass();
15+
while (currentClass != null) {
16+
try {
17+
return currentClass.getDeclaredMethod(methodName).invoke(instance, args);
18+
} catch (Exception x) {
19+
currentClass = currentClass.getSuperclass();
20+
}
21+
}
22+
return null;
23+
}
24+
25+
public static Object valueOf(Class<?> clazz, String fieldName) {
26+
if (clazz.isEnum()) {
27+
return Enum.valueOf(clazz.asSubclass(Enum.class), fieldName);
28+
} else {
29+
try {
30+
return clazz.getField(fieldName).get(null);
31+
} catch (Exception x) {
32+
return null;
33+
}
34+
}
35+
}
36+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package io.github.thebusybiscuit.mobcapturer.utils.compatibility;
2+
3+
import org.bukkit.entity.Cat;
4+
5+
import io.github.thebusybiscuit.mobcapturer.utils.ReflectionUtils;
6+
7+
import lombok.experimental.UtilityClass;
8+
9+
@UtilityClass
10+
public final class CatTypeX {
11+
12+
public static String get(Cat entity) {
13+
Object obj = ReflectionUtils.invoke(entity, "getCatType");
14+
return obj != null ? obj.toString() : "UNKNOWN";
15+
}
16+
17+
public static void set(Cat entity, String obj) {
18+
ReflectionUtils.invoke(entity, "setCatType", ReflectionUtils.valueOf(Cat.Type.class, obj));
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package io.github.thebusybiscuit.mobcapturer.utils.compatibility;
2+
3+
import org.bukkit.entity.Frog;
4+
5+
import io.github.thebusybiscuit.mobcapturer.utils.ReflectionUtils;
6+
7+
import lombok.experimental.UtilityClass;
8+
9+
@UtilityClass
10+
public final class FrogVariantX {
11+
12+
public static String get(Frog entity) {
13+
Object obj = ReflectionUtils.invoke(entity, "getVariant");
14+
return obj != null ? obj.toString() : "UNKNOWN";
15+
}
16+
17+
public static void set(Frog entity, String obj) {
18+
ReflectionUtils.invoke(entity, "setVariant", ReflectionUtils.valueOf(Frog.Variant.class, obj));
19+
}
20+
}
Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,29 @@
11
package io.github.thebusybiscuit.mobcapturer.utils.compatibility;
22

3-
import java.lang.reflect.InvocationTargetException;
43
import java.util.Locale;
54

6-
import javax.annotation.Nonnull;
7-
85
import org.bukkit.NamespacedKey;
6+
import org.bukkit.entity.Villager;
97
import org.bukkit.entity.ZombieVillager;
108

9+
import io.github.thebusybiscuit.mobcapturer.utils.ReflectionUtils;
10+
1111
import lombok.experimental.UtilityClass;
1212

13-
// TODO: This needs to be changed since 1.22 the enum methods will be removed
1413
@UtilityClass
1514
public final class VillagerProfessionX {
1615

17-
@Nonnull
18-
public static String getFromZombieVillager(@Nonnull ZombieVillager entity) {
19-
try {
20-
// get the profession of the zombie villager
21-
var getProfMethod = entity.getClass().getDeclaredMethod("getVillagerProfession");
22-
Object prof = getProfMethod.invoke(entity);
23-
24-
var getKeyMethod = prof.getClass().getDeclaredMethod("getKey");
25-
var nsKey = (NamespacedKey) getKeyMethod.invoke(prof);
26-
return nsKey.getKey().toUpperCase(Locale.ROOT);
27-
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
28-
return "Unknown";
16+
public static String getFromZombieVillager(ZombieVillager entity) {
17+
var profession = ReflectionUtils.invoke(entity, "getVillagerProfession");
18+
if (profession == null) {
19+
return "UNKNOWN";
2920
}
30-
}
3121

32-
public static void setToZombieVillager(@Nonnull ZombieVillager entity, @Nonnull String profession) {
33-
try {
34-
// get the profession of the zombie villager
35-
var getProfMethod = entity.getClass().getDeclaredMethod("getVillagerProfession");
36-
Object prof = getProfMethod.invoke(entity);
37-
38-
var valueOfMethod = prof.getClass().getDeclaredMethod("valueOf", String.class);
39-
Object newProf = valueOfMethod.invoke(prof, profession);
22+
var nsKey = (NamespacedKey) ReflectionUtils.invoke(profession, "getKey");
23+
return nsKey.getKey().toUpperCase(Locale.ROOT);
24+
}
4025

41-
var setProfMethod = entity.getClass().getDeclaredMethod("setVillagerProfession", prof.getClass());
42-
setProfMethod.invoke(entity, newProf);
43-
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
44-
// Ignore
45-
}
26+
public static void setToZombieVillager(ZombieVillager entity, String obj) {
27+
ReflectionUtils.invoke(entity, "setVillagerProfession", ReflectionUtils.valueOf(Villager.Profession.class, obj));
4628
}
4729
}

0 commit comments

Comments
 (0)