From 1818f5f1e6980d7f492d980551a21ca8e58bb01b Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Sat, 23 Nov 2013 03:15:07 +1300 Subject: [PATCH] Read desc Convert the disguisetype to use entitytypes dymanically set when the disguisetypes are loaded, so if the entitytype doesn't exist. The plugin will still load. --- .../disguise/BaseDisguiseCommand.java | 3 + .../libraryaddict/disguise/LibsDisguises.java | 21 ++- .../commands/DisguiseHelpCommand.java | 3 + .../disguise/disguisetypes/Disguise.java | 4 + .../disguise/disguisetypes/DisguiseType.java | 168 +++++++++++------- 5 files changed, 123 insertions(+), 76 deletions(-) diff --git a/src/me/libraryaddict/disguise/BaseDisguiseCommand.java b/src/me/libraryaddict/disguise/BaseDisguiseCommand.java index 37ec069c..88773c1a 100644 --- a/src/me/libraryaddict/disguise/BaseDisguiseCommand.java +++ b/src/me/libraryaddict/disguise/BaseDisguiseCommand.java @@ -131,6 +131,9 @@ public abstract class BaseDisguiseCommand implements CommandExecutor { } DisguiseType disguiseType = null; for (DisguiseType type : DisguiseType.values()) { + if (type.getEntityType() == null) { + continue; + } if (args[0].equalsIgnoreCase(type.name()) || type.name().replace("_", "").equalsIgnoreCase(args[0])) { disguiseType = type; break; diff --git a/src/me/libraryaddict/disguise/LibsDisguises.java b/src/me/libraryaddict/disguise/LibsDisguises.java index a685c5d5..d3465f0b 100644 --- a/src/me/libraryaddict/disguise/LibsDisguises.java +++ b/src/me/libraryaddict/disguise/LibsDisguises.java @@ -86,6 +86,9 @@ public class LibsDisguises extends JavaPlugin { private void registerValues() { for (DisguiseType disguiseType : DisguiseType.values()) { + if (disguiseType.getEntityType() == null) { + continue; + } Class watcherClass = null; try { String name; @@ -117,17 +120,13 @@ public class LibsDisguises extends JavaPlugin { watcherClass = Class.forName("me.libraryaddict.disguise.disguisetypes.watchers." + name + "Watcher"); } catch (Exception ex) { // There is no watcher for this entity, or a error was thrown. - try { - Class c = disguiseType.getEntityType().getEntityClass(); - if (Ageable.class.isAssignableFrom(c)) { - watcherClass = AgeableWatcher.class; - } else if (LivingEntity.class.isAssignableFrom(c)) { - watcherClass = LivingWatcher.class; - } else { - watcherClass = FlagWatcher.class; - } - } catch (Exception ex1) { - ex1.printStackTrace(); + Class c = disguiseType.getEntityType().getEntityClass(); + if (Ageable.class.isAssignableFrom(c)) { + watcherClass = AgeableWatcher.class; + } else if (LivingEntity.class.isAssignableFrom(c)) { + watcherClass = LivingWatcher.class; + } else { + watcherClass = FlagWatcher.class; } } disguiseType.setWatcherClass(watcherClass); diff --git a/src/me/libraryaddict/disguise/commands/DisguiseHelpCommand.java b/src/me/libraryaddict/disguise/commands/DisguiseHelpCommand.java index 2cda3db7..b75ab5a9 100644 --- a/src/me/libraryaddict/disguise/commands/DisguiseHelpCommand.java +++ b/src/me/libraryaddict/disguise/commands/DisguiseHelpCommand.java @@ -67,6 +67,9 @@ public class DisguiseHelpCommand extends BaseDisguiseCommand { } DisguiseType type = null; for (DisguiseType disguiseType : DisguiseType.values()) { + if (disguiseType.getEntityType() == null) { + continue; + } if (args[0].equalsIgnoreCase(disguiseType.name()) || disguiseType.name().replace("_", "").equalsIgnoreCase(args[0])) { type = disguiseType; diff --git a/src/me/libraryaddict/disguise/disguisetypes/Disguise.java b/src/me/libraryaddict/disguise/disguisetypes/Disguise.java index 0ad3b743..1b03b6df 100644 --- a/src/me/libraryaddict/disguise/disguisetypes/Disguise.java +++ b/src/me/libraryaddict/disguise/disguisetypes/Disguise.java @@ -52,6 +52,10 @@ public abstract class Disguise { protected void createDisguise(DisguiseType newType, boolean doSounds) { if (getWatcher() != null) return; + if (newType.getEntityType() == null) { + throw new RuntimeException("DisguiseType " + newType + + " was attempted to construct a disguise, but this version of craftbukkit does not have that entity"); + } // Set the disguise type disguiseType = newType; // Set the option to replace the sounds diff --git a/src/me/libraryaddict/disguise/disguisetypes/DisguiseType.java b/src/me/libraryaddict/disguise/disguisetypes/DisguiseType.java index fddee950..c8c2cc9e 100644 --- a/src/me/libraryaddict/disguise/disguisetypes/DisguiseType.java +++ b/src/me/libraryaddict/disguise/disguisetypes/DisguiseType.java @@ -4,131 +4,166 @@ import org.apache.commons.lang.StringUtils; import org.bukkit.entity.EntityType; public enum DisguiseType { - ARROW(EntityType.ARROW, 60), + ARROW(60), - BAT(EntityType.BAT), + BAT(), - BLAZE(EntityType.BLAZE), + BLAZE(), - BOAT(EntityType.BOAT, 1), + BOAT(1), - CAVE_SPIDER(EntityType.CAVE_SPIDER), + CAVE_SPIDER(), - CHICKEN(EntityType.CHICKEN), + CHICKEN(), - COW(EntityType.COW), + COW(), - CREEPER(EntityType.CREEPER), + CREEPER(), - DONKEY(EntityType.HORSE), + DONKEY(), - DROPPED_ITEM(EntityType.DROPPED_ITEM, 2, 1), + DROPPED_ITEM(2, 1), - EGG(EntityType.EGG, 62), + EGG(62), - ENDER_CRYSTAL(EntityType.ENDER_CRYSTAL, 51), + ENDER_CRYSTAL(51), - ENDER_DRAGON(EntityType.ENDER_DRAGON), + ENDER_DRAGON(), - ENDER_PEARL(EntityType.ENDER_PEARL, 65), + ENDER_PEARL(65), - ENDER_SIGNAL(EntityType.ENDER_SIGNAL, 72), + ENDER_SIGNAL(72), - ENDERMAN(EntityType.ENDERMAN), + ENDERMAN(), - EXPERIENCE_ORB(EntityType.EXPERIENCE_ORB), + EXPERIENCE_ORB(), - FALLING_BLOCK(EntityType.FALLING_BLOCK, 70, 1), + FALLING_BLOCK(70, 1), - FIREBALL(EntityType.FIREBALL, 63, 0), + FIREBALL(63, 0), - FIREWORK(EntityType.FIREWORK, 76), + FIREWORK(76), - FISHING_HOOK(EntityType.FISHING_HOOK, 90), + FISHING_HOOK(90), - GHAST(EntityType.GHAST), + GHAST(), - GIANT(EntityType.GIANT), + GIANT(), - HORSE(EntityType.HORSE), + HORSE(), - IRON_GOLEM(EntityType.IRON_GOLEM), + IRON_GOLEM(), - ITEM_FRAME(EntityType.ITEM_FRAME, 71), + ITEM_FRAME(71), - LEASH_HITCH(EntityType.LEASH_HITCH, 77), + LEASH_HITCH(77), - MAGMA_CUBE(EntityType.MAGMA_CUBE), + MAGMA_CUBE(), - MINECART(EntityType.MINECART, 10, 0), + MINECART(10, 0), - MINECART_CHEST(EntityType.MINECART_CHEST, 10, 1), + MINECART_CHEST(10, 1), - MINECART_FURNACE(EntityType.MINECART_FURNACE, 10, 2), + MINECART_FURNACE(10, 2), - MINECART_HOPPER(EntityType.MINECART_HOPPER, 10), + MINECART_HOPPER(10), - MINECART_MOB_SPAWNER(EntityType.MINECART_MOB_SPAWNER, 10, 4), + MINECART_MOB_SPAWNER(10, 4), - MINECART_TNT(EntityType.MINECART_TNT, 10, 3), + MINECART_TNT(10, 3), - MULE(EntityType.HORSE), + MULE(), - MUSHROOM_COW(EntityType.MUSHROOM_COW), + MUSHROOM_COW(), - OCELOT(EntityType.OCELOT), + OCELOT(), - PAINTING(EntityType.PAINTING), + PAINTING(), - PIG(EntityType.PIG), + PIG(), - PIG_ZOMBIE(EntityType.PIG_ZOMBIE), + PIG_ZOMBIE(), - PLAYER(EntityType.PLAYER), + PLAYER(), - PRIMED_TNT(EntityType.PRIMED_TNT, 50), + PRIMED_TNT(50), - SHEEP(EntityType.SHEEP), + SHEEP(), - SILVERFISH(EntityType.SILVERFISH), + SILVERFISH(), - SKELETON(EntityType.SKELETON), + SKELETON(), - SKELETON_HORSE(EntityType.HORSE), + SKELETON_HORSE(), - SLIME(EntityType.SLIME), + SLIME(), - SMALL_FIREBALL(EntityType.SMALL_FIREBALL, 64, 0), + SMALL_FIREBALL(64, 0), - SNOWBALL(EntityType.SNOWBALL, 61), + SNOWBALL(61), - SNOWMAN(EntityType.SNOWMAN), + SNOWMAN(), - SPIDER(EntityType.SPIDER), + SPIDER(), - SPLASH_POTION(EntityType.SPLASH_POTION, 73), + SPLASH_POTION(73), - SQUID(EntityType.SQUID), + SQUID(), - THROWN_EXP_BOTTLE(EntityType.THROWN_EXP_BOTTLE, 75), + THROWN_EXP_BOTTLE(75), - UNDEAD_HORSE(EntityType.HORSE), + UNDEAD_HORSE(), - VILLAGER(EntityType.VILLAGER), + VILLAGER(), - WITCH(EntityType.WITCH), + WITCH(), - WITHER(EntityType.WITHER), + WITHER(), - WITHER_SKELETON(EntityType.SKELETON), + WITHER_SKELETON(), - WITHER_SKULL(EntityType.WITHER_SKULL, 66), + WITHER_SKULL(66), - WOLF(EntityType.WOLF), + WOLF(), - ZOMBIE(EntityType.ZOMBIE), + ZOMBIE(), - ZOMBIE_VILLAGER(EntityType.ZOMBIE); + ZOMBIE_VILLAGER(); + static { + for (DisguiseType type : values()) { + try { + EntityType entityType; + switch (type) { + case DONKEY: + entityType = EntityType.HORSE; + break; + case MULE: + entityType = EntityType.HORSE; + break; + case UNDEAD_HORSE: + entityType = EntityType.HORSE; + break; + case SKELETON_HORSE: + entityType = EntityType.HORSE; + break; + case ZOMBIE_VILLAGER: + entityType = EntityType.ZOMBIE; + break; + case WITHER_SKELETON: + entityType = EntityType.SKELETON; + break; + default: + entityType = EntityType.valueOf(type.name()); + break; + } + if (entityType != null) { + type.setEntityType(entityType); + } + } catch (Exception ex) { + ex.printStackTrace(); + } + } + } public static DisguiseType getType(org.bukkit.entity.EntityType entityType) { return DisguiseType.valueOf(entityType.name()); @@ -140,8 +175,11 @@ public enum DisguiseType { private EntityType entityType; private Class watcherClass; - private DisguiseType(EntityType newType, int... obj) { - entityType = newType; + private void setEntityType(EntityType entityType) { + this.entityType = entityType; + } + + private DisguiseType(int... obj) { for (int i = 0; i < obj.length; i++) { int value = obj[i]; switch (i) {