Skip to content

Commit

Permalink
fix: address backward compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
ybw0014 committed Dec 28, 2024
1 parent 6d35654 commit 2f084d0
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import org.bukkit.ChatColor;
import org.bukkit.DyeColor;
import org.bukkit.entity.Cat;
import org.bukkit.entity.Cat.Type;

import io.github.thebusybiscuit.mobcapturer.utils.compatibility.CatTypeX;
import io.github.thebusybiscuit.slimefun4.utils.ChatUtils;

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

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

json.addProperty("catType", entity.getCatType().name());
json.addProperty("catType", CatTypeX.get(entity));
json.addProperty("sitting", entity.isSitting());
json.addProperty("collarColor", entity.getCollarColor().name());

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import com.google.gson.JsonObject;

import io.github.thebusybiscuit.mobcapturer.utils.compatibility.FrogVariantX;

import org.bukkit.ChatColor;
import org.bukkit.entity.Frog;
import org.bukkit.entity.Frog.Variant;
Expand Down Expand Up @@ -34,15 +36,15 @@ public List<String> getLore(@Nonnull JsonObject json) {
public void apply(Frog entity, JsonObject json) {
super.apply(entity, json);

entity.setVariant(Variant.valueOf(json.get("variant").getAsString()));
FrogVariantX.set(entity, json.get("variant").getAsString());
}

@Nonnull
@Override
public JsonObject saveData(@Nonnull Frog entity) {
JsonObject json = super.saveData(entity);

json.addProperty("variant", entity.getVariant().name());
json.addProperty("variant", FrogVariantX.get(entity));

return json;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.bukkit.entity.Donkey;
import org.bukkit.entity.Drowned;
import org.bukkit.entity.ElderGuardian;
import org.bukkit.entity.Endermite;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Evoker;
import org.bukkit.entity.Ghast;
Expand Down Expand Up @@ -60,7 +61,6 @@
import io.github.thebusybiscuit.mobcapturer.adapters.mobs.ChestedHorseAdapter;
import io.github.thebusybiscuit.mobcapturer.adapters.mobs.CreeperAdapter;
import io.github.thebusybiscuit.mobcapturer.adapters.mobs.EndermanAdapter;
import io.github.thebusybiscuit.mobcapturer.adapters.mobs.EndermiteAdapter;
import io.github.thebusybiscuit.mobcapturer.adapters.mobs.FoxAdapter;
import io.github.thebusybiscuit.mobcapturer.adapters.mobs.FrogAdapter;
import io.github.thebusybiscuit.mobcapturer.adapters.mobs.GlowSquidAdapter;
Expand Down Expand Up @@ -275,7 +275,7 @@ private static void setupMobEggs() {
// https://minecraft-heads.com/custom-heads/decoration/23585-spawn-egg-shulker
registerMob(EntityType.SHULKER, new ShulkerAdapter(), "d04252216231b3f744c9ff4ace7084ae9f4164f8b384c65410848a19617af4d");
// https://minecraft-heads.com/custom-heads/decoration/954-spawn-egg-endermite
registerMob(EntityType.ENDERMITE, new EndermiteAdapter(), "3beac501e97db1cc035287d068a8eb538e55ef802f5cca25683933a243136c");
registerMob(EntityType.ENDERMITE, new StandardMobAdapter<>(Endermite.class), "3beac501e97db1cc035287d068a8eb538e55ef802f5cca25683933a243136c");
// </editor-fold>

// <editor-fold desc="Golems">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.github.thebusybiscuit.mobcapturer.utils;

import lombok.experimental.UtilityClass;

/**
* Reflection utilities used for backward compatibility.
* Other plugins should not use this class.
*/
@UtilityClass
@SuppressWarnings("unchecked")
public final class ReflectionUtils {

public static Object invoke(Object instance, String methodName, Object... args) {
Class<?> currentClass = instance.getClass();
while (currentClass != null) {
try {
return currentClass.getDeclaredMethod(methodName).invoke(instance, args);
} catch (Exception x) {
currentClass = currentClass.getSuperclass();
}
}
return null;
}

public static Object valueOf(Class<?> clazz, String fieldName) {
if (clazz.isEnum()) {
return Enum.valueOf(clazz.asSubclass(Enum.class), fieldName);
} else {
try {
return clazz.getField(fieldName).get(null);
} catch (Exception x) {
return null;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.github.thebusybiscuit.mobcapturer.utils.compatibility;

import org.bukkit.entity.Cat;

import io.github.thebusybiscuit.mobcapturer.utils.ReflectionUtils;

import lombok.experimental.UtilityClass;

@UtilityClass
public final class CatTypeX {

public static String get(Cat entity) {
Object obj = ReflectionUtils.invoke(entity, "getCatType");
return obj != null ? obj.toString() : "UNKNOWN";
}

public static void set(Cat entity, String obj) {
ReflectionUtils.invoke(entity, "setCatType", ReflectionUtils.valueOf(Cat.Type.class, obj));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.github.thebusybiscuit.mobcapturer.utils.compatibility;

import org.bukkit.entity.Frog;

import io.github.thebusybiscuit.mobcapturer.utils.ReflectionUtils;

import lombok.experimental.UtilityClass;

@UtilityClass
public final class FrogVariantX {

public static String get(Frog entity) {
Object obj = ReflectionUtils.invoke(entity, "getVariant");
return obj != null ? obj.toString() : "UNKNOWN";
}

public static void set(Frog entity, String obj) {
ReflectionUtils.invoke(entity, "setVariant", ReflectionUtils.valueOf(Frog.Variant.class, obj));
}
}
Original file line number Diff line number Diff line change
@@ -1,47 +1,29 @@
package io.github.thebusybiscuit.mobcapturer.utils.compatibility;

import java.lang.reflect.InvocationTargetException;
import java.util.Locale;

import javax.annotation.Nonnull;

import org.bukkit.NamespacedKey;
import org.bukkit.entity.Villager;
import org.bukkit.entity.ZombieVillager;

import io.github.thebusybiscuit.mobcapturer.utils.ReflectionUtils;

import lombok.experimental.UtilityClass;

// 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) {
return "Unknown";
public static String getFromZombieVillager(ZombieVillager entity) {
var profession = ReflectionUtils.invoke(entity, "getVillagerProfession");
if (profession == null) {
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 nsKey = (NamespacedKey) ReflectionUtils.invoke(profession, "getKey");
return nsKey.getKey().toUpperCase(Locale.ROOT);
}

var setProfMethod = entity.getClass().getDeclaredMethod("setVillagerProfession", prof.getClass());
setProfMethod.invoke(entity, newProf);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
// Ignore
}
public static void setToZombieVillager(ZombieVillager entity, String obj) {
ReflectionUtils.invoke(entity, "setVillagerProfession", ReflectionUtils.valueOf(Villager.Profession.class, obj));
}
}

0 comments on commit 2f084d0

Please sign in to comment.