Prefer storing the methods instead of getting them every time

This commit is contained in:
riking 2014-06-04 09:43:29 -07:00
parent 983026a898
commit 0d8dce4538

@ -133,6 +133,8 @@ public enum DisguiseType {
ZOMBIE_VILLAGER; ZOMBIE_VILLAGER;
private static Method isVillager, getVariant, getSkeletonType;
static { static {
// We set the entity type in this so that we can safely ignore disguisetypes which don't exist in older versions of MC. // We set the entity type in this so that we can safely ignore disguisetypes which don't exist in older versions of MC.
// Without erroring up everything. // Without erroring up everything.
@ -167,6 +169,20 @@ public enum DisguiseType {
// This version of craftbukkit doesn't have the disguise. // This version of craftbukkit doesn't have the disguise.
} }
} }
try {
isVillager = org.bukkit.entity.Zombie.class.getMethod("isVillager");
} catch (Throwable ignored) {
// This version doesn't have villagers
}
try {
getVariant = org.bukkit.entity.Horse.class.getMethod("getVariant");
} catch (Throwable ignored) {
// This version doesn't have horses
}
try {
getSkeletonType = org.bukkit.entity.Skeleton.class.getMethod("getSkeletonType");
} catch (Throwable ignored) {
}
} }
public static DisguiseType getType(Entity entity) { public static DisguiseType getType(Entity entity) {
@ -174,18 +190,16 @@ public enum DisguiseType {
switch (disguiseType) { switch (disguiseType) {
case ZOMBIE: case ZOMBIE:
try { try {
Method isVillager = entity.getClass().getMethod("isVillager");
if ((Boolean) isVillager.invoke(entity)) { if ((Boolean) isVillager.invoke(entity)) {
disguiseType = DisguiseType.ZOMBIE_VILLAGER; disguiseType = DisguiseType.ZOMBIE_VILLAGER;
} }
} catch (NoSuchMethodException ex) {
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
break; break;
case HORSE: case HORSE:
try { try {
Object variant = entity.getClass().getMethod("getVariant").invoke(entity); Object variant = getVariant.invoke(entity);
disguiseType = DisguiseType.valueOf(((Enum) variant).name()); disguiseType = DisguiseType.valueOf(((Enum) variant).name());
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
@ -193,11 +207,10 @@ public enum DisguiseType {
break; break;
case SKELETON: case SKELETON:
try { try {
Object type = entity.getClass().getMethod("getSkeletonType").invoke(entity); Object type = getSkeletonType.invoke(entity);
if (((Enum) type).name().equals("WITHER")) { if (type == org.bukkit.entity.Skeleton.SkeletonType.WITHER) {
disguiseType = DisguiseType.WITHER_SKELETON; disguiseType = DisguiseType.WITHER_SKELETON;
} }
} catch (NoSuchMethodException ex) {
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
} }