From 27cc9cd70f1284b27b38edcf776cec4fa2c1c8f9 Mon Sep 17 00:00:00 2001 From: NavidK0 Date: Tue, 7 Jul 2015 11:30:12 -0400 Subject: [PATCH] Added rudimentary support for disguising Custom Entities and Modded Entities Added the DisguiseType 'Unknown', obviously can't be used by players Cleaned up code --- build.gradle | 2 +- .../libraryaddict/disguise/DisguiseAPI.java | 8 +++- .../disguise/DisguiseListener.java | 2 +- .../libraryaddict/disguise/LibsDisguises.java | 39 +++++++++++++------ .../commands/DisguiseEntityCommand.java | 3 +- .../disguise/disguisetypes/DisguiseType.java | 14 +++++-- .../utilities/BaseDisguiseCommand.java | 3 ++ .../disguise/utilities/DisguiseUtilities.java | 1 + .../disguise/utilities/DisguiseValues.java | 1 + src/main/resources/plugin.yml | 2 +- 10 files changed, 53 insertions(+), 22 deletions(-) diff --git a/build.gradle b/build.gradle index ddf639cd..adaf0b57 100644 --- a/build.gradle +++ b/build.gradle @@ -13,7 +13,7 @@ println 'Compiling LibsDisguises via Gradle ver. ' + gradle.gradleVersion sourceCompatibility = '1.7' ext.spigotVersion = '1.8.7-R0.1-SNAPSHOT' -ext.disguisesVersion = '8.5.1' +ext.disguisesVersion = '8.5.2' [compileJava, compileTestJava]*.options*.encoding = 'UTF-8' diff --git a/src/main/java/me/libraryaddict/disguise/DisguiseAPI.java b/src/main/java/me/libraryaddict/disguise/DisguiseAPI.java index 92456031..62d01799 100644 --- a/src/main/java/me/libraryaddict/disguise/DisguiseAPI.java +++ b/src/main/java/me/libraryaddict/disguise/DisguiseAPI.java @@ -189,6 +189,8 @@ public class DisguiseAPI { /** * Disguise the next entity to spawn with this disguise. This may not work however if the entity doesn't actually spawn. + * @param disguise + * @return */ public static int disguiseNextEntity(Disguise disguise) { if (disguise == null) @@ -202,7 +204,7 @@ public class DisguiseAPI { int id = field.getInt(null); DisguiseUtilities.addFutureDisguise(id, (TargetedDisguise) disguise); return id; - } catch (Exception ex) { + } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException ex) { ex.printStackTrace(System.out); } return -1; @@ -210,6 +212,8 @@ public class DisguiseAPI { /** * Disguise this entity with this disguise + * @param entity + * @param disguise */ public static void disguiseToAll(Entity entity, Disguise disguise) { if (disguise.getEntity() != null) { @@ -333,4 +337,4 @@ public class DisguiseAPI { private DisguiseAPI() { } -} \ No newline at end of file +} diff --git a/src/main/java/me/libraryaddict/disguise/DisguiseListener.java b/src/main/java/me/libraryaddict/disguise/DisguiseListener.java index 815ea212..e1dda602 100644 --- a/src/main/java/me/libraryaddict/disguise/DisguiseListener.java +++ b/src/main/java/me/libraryaddict/disguise/DisguiseListener.java @@ -197,7 +197,7 @@ public class DisguiseListener implements Listener { event.setCancelled(true); disguiseRunnable.remove(p.getName()).cancel(); Entity entity = event.getRightClicked(); - String entityName = ""; + String entityName; if (entity instanceof Player && !disguiseClone.containsKey(p.getName())) { entityName = ((Player) entity).getName(); } else { diff --git a/src/main/java/me/libraryaddict/disguise/LibsDisguises.java b/src/main/java/me/libraryaddict/disguise/LibsDisguises.java index af36fc4b..965276c8 100644 --- a/src/main/java/me/libraryaddict/disguise/LibsDisguises.java +++ b/src/main/java/me/libraryaddict/disguise/LibsDisguises.java @@ -1,5 +1,6 @@ package me.libraryaddict.disguise; +import com.comphenix.protocol.reflect.FieldAccessException; import java.io.IOException; import java.lang.reflect.Field; @@ -31,6 +32,7 @@ import org.bukkit.plugin.java.JavaPlugin; import com.comphenix.protocol.wrappers.WrappedDataWatcher; import com.comphenix.protocol.wrappers.WrappedWatchableObject; +import me.libraryaddict.disguise.utilities.FakeBoundingBox; import me.libraryaddict.disguise.utilities.Metrics; import org.bukkit.event.HandlerList; @@ -87,7 +89,7 @@ public class LibsDisguises extends JavaPlugin { if (disguiseType.getEntityType() == null) { continue; } - Class watcherClass = null; + Class watcherClass; try { switch (disguiseType) { case MINECART_CHEST: @@ -125,14 +127,18 @@ public class LibsDisguises extends JavaPlugin { } catch (ClassNotFoundException ex) { // There is no explicit watcher for this entity. Class entityClass = disguiseType.getEntityType().getEntityClass(); - if (Tameable.class.isAssignableFrom(entityClass)) { - watcherClass = TameableWatcher.class; - } else if (Ageable.class.isAssignableFrom(entityClass)) { - watcherClass = AgeableWatcher.class; - } else if (LivingEntity.class.isAssignableFrom(entityClass)) { - watcherClass = LivingWatcher.class; + if (entityClass != null) { + if (Tameable.class.isAssignableFrom(entityClass)) { + watcherClass = TameableWatcher.class; + } else if (Ageable.class.isAssignableFrom(entityClass)) { + watcherClass = AgeableWatcher.class; + } else if (LivingEntity.class.isAssignableFrom(entityClass)) { + watcherClass = LivingWatcher.class; + } else { + watcherClass = FlagWatcher.class; + } } else { - watcherClass = FlagWatcher.class; + watcherClass = FlagWatcher.class; //Disguise is unknown type } } disguiseType.setWatcherClass(watcherClass); @@ -182,6 +188,15 @@ public class LibsDisguises extends JavaPlugin { break; } try { + if (nmsEntityName.equalsIgnoreCase("Unknown")) { + DisguiseValues disguiseValues = new DisguiseValues(disguiseType, null, 0, 0); + disguiseValues.setAdultBox(new FakeBoundingBox(0, 0, 0)); + DisguiseSound sound = DisguiseSound.getType(disguiseType.name()); + if (sound != null) { + sound.setDamageAndIdleSoundVolume(1f); + } + continue; + } Object nmsEntity = ReflectionManager.createEntityInstance(nmsEntityName); if (nmsEntity == null) { continue; @@ -201,14 +216,14 @@ public class LibsDisguises extends JavaPlugin { disguiseValues.setMetaValue(watch.getIndex(), watch.getValue()); // Uncomment when I need to find the new datawatcher values for a class.. - // System.out.print("Disguise: " + disguiseType + ", ID: " + watch.getIndex() + ", Class: " - // + (watch.getValue() == null ? "null" : watch.getValue().getClass()) + ", Value: " + watch.getValue()); +// System.out.print("Disguise: " + disguiseType + ", ID: " + watch.getIndex() + ", Class: " +// + (watch.getValue() == null ? "null" : watch.getValue().getClass()) + ", Value: " + watch.getValue()); } DisguiseSound sound = DisguiseSound.getType(disguiseType.name()); if (sound != null) { Float soundStrength = ReflectionManager.getSoundModifier(nmsEntity); if (soundStrength != null) { - sound.setDamageAndIdleSoundVolume((Float) soundStrength); + sound.setDamageAndIdleSoundVolume(soundStrength); } } @@ -222,7 +237,7 @@ public class LibsDisguises extends JavaPlugin { disguiseValues.setBabyBox(ReflectionManager.getBoundingBox(bukkitEntity)); } disguiseValues.setEntitySize(ReflectionManager.getSize(bukkitEntity)); - } catch (Exception ex) { + } catch (SecurityException | IllegalArgumentException | IllegalAccessException | FieldAccessException ex) { System.out.print("[LibsDisguises] Uh oh! Trouble while making values for the disguise " + disguiseType.name() + "!"); System.out.print("[LibsDisguises] Before reporting this error, " diff --git a/src/main/java/me/libraryaddict/disguise/commands/DisguiseEntityCommand.java b/src/main/java/me/libraryaddict/disguise/commands/DisguiseEntityCommand.java index cd284159..f225200c 100644 --- a/src/main/java/me/libraryaddict/disguise/commands/DisguiseEntityCommand.java +++ b/src/main/java/me/libraryaddict/disguise/commands/DisguiseEntityCommand.java @@ -1,5 +1,6 @@ package me.libraryaddict.disguise.commands; +import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.HashMap; @@ -29,7 +30,7 @@ public class DisguiseEntityCommand extends BaseDisguiseCommand { sender.sendMessage(ex.getMessage()); } return true; - } catch (Exception ex) { + } catch (IllegalAccessException | InvocationTargetException ex) { ex.printStackTrace(System.out); return true; } diff --git a/src/main/java/me/libraryaddict/disguise/disguisetypes/DisguiseType.java b/src/main/java/me/libraryaddict/disguise/disguisetypes/DisguiseType.java index 43407153..df695ae7 100644 --- a/src/main/java/me/libraryaddict/disguise/disguisetypes/DisguiseType.java +++ b/src/main/java/me/libraryaddict/disguise/disguisetypes/DisguiseType.java @@ -147,8 +147,10 @@ public enum DisguiseType { ZOMBIE, - ZOMBIE_VILLAGER; - + ZOMBIE_VILLAGER, + + UNKNOWN; + private static Method isVillager, getVariant, getSkeletonType, isElder; static { @@ -253,7 +255,7 @@ public enum DisguiseType { try { return valueOf(entityType.name().toUpperCase()); } catch (Throwable ex) { - return null; + return DisguiseType.UNKNOWN; } } @@ -315,6 +317,10 @@ public enum DisguiseType { public boolean isPlayer() { return this == DisguiseType.PLAYER; } + + public boolean isUnknown() { + return this == DisguiseType.UNKNOWN; + } private void setEntityType(EntityType entityType) { this.entityType = entityType; @@ -330,4 +336,4 @@ public enum DisguiseType { split[i] = split[i].substring(0, 1) + split[i].substring(1).toLowerCase(); return StringUtils.join(split, " "); } -} \ No newline at end of file +} diff --git a/src/main/java/me/libraryaddict/disguise/utilities/BaseDisguiseCommand.java b/src/main/java/me/libraryaddict/disguise/utilities/BaseDisguiseCommand.java index 7c700179..d89ba749 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/BaseDisguiseCommand.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/BaseDisguiseCommand.java @@ -345,6 +345,9 @@ public abstract class BaseDisguiseCommand implements CommandExecutor { throw new DisguiseParseException(ChatColor.RED + "Error! The disguise " + ChatColor.GREEN + args[0] + ChatColor.RED + " doesn't exist!"); } + if (disguiseType.isUnknown()) { + throw new DisguiseParseException(ChatColor.RED + "Error! You cannot disguise as " + ChatColor.GREEN + "Unknown!"); + } if (disguiseType.getEntityType() == null) { throw new DisguiseParseException(ChatColor.RED + "Error! This version of minecraft does not have that disguise!"); } diff --git a/src/main/java/me/libraryaddict/disguise/utilities/DisguiseUtilities.java b/src/main/java/me/libraryaddict/disguise/utilities/DisguiseUtilities.java index e6c1e623..8d21981b 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/DisguiseUtilities.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/DisguiseUtilities.java @@ -970,6 +970,7 @@ public class DisguiseUtilities { /** * Setup it so he can see himself when disguised + * @param disguise */ public static void setupFakeDisguise(final Disguise disguise) { Entity e = disguise.getEntity(); diff --git a/src/main/java/me/libraryaddict/disguise/utilities/DisguiseValues.java b/src/main/java/me/libraryaddict/disguise/utilities/DisguiseValues.java index ea02dbf1..bcee1f1d 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/DisguiseValues.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/DisguiseValues.java @@ -52,6 +52,7 @@ public class DisguiseValues { private HashMap metaValues = new HashMap<>(); private Class nmsEntityClass; + @SuppressWarnings("LeakingThisInConstructor") public DisguiseValues(DisguiseType type, Class classType, int entitySize, double maxHealth) { values.put(type, this); enumEntitySize = entitySize; diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 50a11241..da754a0e 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,6 +1,6 @@ name: LibsDisguises main: me.libraryaddict.disguise.LibsDisguises -version: 8.5.1 +version: 8.5.2 author: libraryaddict authors: [Byteflux, Navid K.] depend: [ProtocolLib]