generated from Slimefun/Addon-Template
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
97 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 0 additions & 38 deletions
38
src/main/java/io/github/thebusybiscuit/mobcapturer/adapters/mobs/EndermiteAdapter.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/main/java/io/github/thebusybiscuit/mobcapturer/utils/ReflectionUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/io/github/thebusybiscuit/mobcapturer/utils/compatibility/CatTypeX.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/io/github/thebusybiscuit/mobcapturer/utils/compatibility/FrogVariantX.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
42 changes: 12 additions & 30 deletions
42
...in/java/io/github/thebusybiscuit/mobcapturer/utils/compatibility/VillagerProfessionX.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |