From 59588f527078eb9476a9bc68cf1d9a3dda471895 Mon Sep 17 00:00:00 2001 From: Andrew Date: Sun, 21 Jul 2013 15:17:21 +1200 Subject: [PATCH] Read desc Now use Entitys completely for the disguising. Now removes the disguise when the entity isn't valid. Deprecated the methods using a object. Added methods to set and get velocity - If packets should be sent. --- .../disguise/Commands/UndisguiseCommand.java | 3 +- .../Commands/UndisguisePlayerCommand.java | 2 +- .../libraryaddict/disguise/DisguiseAPI.java | 70 ++++++++++++------- 3 files changed, 46 insertions(+), 29 deletions(-) diff --git a/src/me/libraryaddict/disguise/Commands/UndisguiseCommand.java b/src/me/libraryaddict/disguise/Commands/UndisguiseCommand.java index 7038a408..caf4dbec 100644 --- a/src/me/libraryaddict/disguise/Commands/UndisguiseCommand.java +++ b/src/me/libraryaddict/disguise/Commands/UndisguiseCommand.java @@ -6,6 +6,7 @@ import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; +import org.bukkit.entity.Entity; import org.bukkit.entity.Player; public class UndisguiseCommand implements CommandExecutor { @@ -17,7 +18,7 @@ public class UndisguiseCommand implements CommandExecutor { return true; } if (sender.hasPermission("libsdisguises.undisguise")) { - if (DisguiseAPI.isDisguised(sender.getName())) { + if (DisguiseAPI.isDisguised((Entity) sender)) { DisguiseAPI.undisguiseToAll((Player) sender); sender.sendMessage(ChatColor.RED + "You are no longer disguised"); } else diff --git a/src/me/libraryaddict/disguise/Commands/UndisguisePlayerCommand.java b/src/me/libraryaddict/disguise/Commands/UndisguisePlayerCommand.java index d410fcd7..99ab5cec 100644 --- a/src/me/libraryaddict/disguise/Commands/UndisguisePlayerCommand.java +++ b/src/me/libraryaddict/disguise/Commands/UndisguisePlayerCommand.java @@ -17,7 +17,7 @@ public class UndisguisePlayerCommand implements CommandExecutor { if (args.length > 0) { Player p = Bukkit.getPlayer(args[0]); if (p != null) { - if (DisguiseAPI.isDisguised(p.getName())) { + if (DisguiseAPI.isDisguised(p)) { DisguiseAPI.undisguiseToAll(p); sender.sendMessage(ChatColor.RED + "He is no longer disguised"); } else diff --git a/src/me/libraryaddict/disguise/DisguiseAPI.java b/src/me/libraryaddict/disguise/DisguiseAPI.java index 6f906f16..cf24ebbd 100644 --- a/src/me/libraryaddict/disguise/DisguiseAPI.java +++ b/src/me/libraryaddict/disguise/DisguiseAPI.java @@ -31,18 +31,19 @@ import com.comphenix.protocol.reflect.StructureModifier; public class DisguiseAPI { - private static HashMap disguises = new HashMap(); + private static HashMap disguises = new HashMap(); private static PacketListener packetListener; private static JavaPlugin plugin; + private static boolean sendVelocity; private static boolean soundsEnabled; - private synchronized static Disguise access(Object obj, Disguise... object) { - if (object.length == 0) - return disguises.get(obj); - if (object[0] == null) - disguises.remove(obj); + private synchronized static Disguise access(Entity entity, Disguise... args) { + if (args.length == 0) + return disguises.get(entity); + if (args[0] == null) + disguises.remove(entity); else - disguises.put(obj, object[0]); + disguises.put(entity, args[0]); return null; } @@ -57,8 +58,11 @@ public class DisguiseAPI { return; if (disguise.getWatcher() != null) disguise = disguise.clone(); - put(entity instanceof Player ? ((Player) entity).getName() : entity.getUniqueId(), disguise); - disguise.constructWatcher(entity.getType(), entity.getEntityId()); + Disguise oldDisguise = getDisguise(entity); + if (oldDisguise != null) + oldDisguise.getScheduler().cancel(); + put(entity, disguise); + disguise.constructWatcher(plugin, entity); refresh(entity); } @@ -73,7 +77,7 @@ public class DisguiseAPI { } } - private static Disguise get(Object obj) { + private static Disguise get(Entity obj) { return access(obj); } @@ -81,16 +85,15 @@ public class DisguiseAPI { * @param Disguiser * @return Disguise */ - public static Disguise getDisguise(Object disguiser) { - if (disguiser instanceof Entity) { - if (disguiser instanceof Player) - return get(((Player) disguiser).getName()); - else - return get(((Entity) disguiser).getUniqueId()); - } + public static Disguise getDisguise(Entity disguiser) { return get(disguiser); } + @Deprecated + public static Disguise getDisguise(Object disguiser) { + return get((Entity) disguiser); + } + protected static void init(JavaPlugin mainPlugin) { plugin = mainPlugin; packetListener = new PacketAdapter(plugin, ConnectionSide.SERVER_SIDE, ListenerPriority.NORMAL, @@ -172,17 +175,20 @@ public class DisguiseAPI { * @param Disguiser * @return boolean - If the disguiser is disguised */ - public static boolean isDisguised(Object disguiser) { - if (disguiser instanceof Entity) { - if (disguiser instanceof Player) - return get(((Player) disguiser).getName()) != null; - else - return get(((Entity) disguiser).getUniqueId()) != null; - } + public static boolean isDisguised(Entity disguiser) { return get(disguiser) != null; } - private static void put(Object obj, Disguise disguise) { + @Deprecated + public static boolean isDisguised(Object disguiser) { + return get((Entity) disguiser) != null; + } + + public static boolean isVelocitySent() { + return sendVelocity; + } + + private static void put(Entity obj, Disguise disguise) { access(obj, disguise); } @@ -204,12 +210,22 @@ public class DisguiseAPI { } } + public static void setVelocitySent(boolean sendVelocityPackets) { + sendVelocity = sendVelocityPackets; + } + /** * @param Disguiser * - Undisguises him */ public static void undisguiseToAll(Entity entity) { - put(entity instanceof Player ? ((Player) entity).getName() : entity.getUniqueId(), null); - refresh(entity); + Disguise disguise = getDisguise(entity); + if (disguise == null) + return; + disguise.getScheduler().cancel(); + put(entity, null); + if (entity.isValid()) { + refresh(entity); + } } } \ No newline at end of file