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.
This commit is contained in:
Andrew 2013-07-21 15:17:21 +12:00
parent a1ff8e6db9
commit 59588f5270
3 changed files with 46 additions and 29 deletions

View File

@ -6,6 +6,7 @@ import org.bukkit.ChatColor;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
public class UndisguiseCommand implements CommandExecutor { public class UndisguiseCommand implements CommandExecutor {
@ -17,7 +18,7 @@ public class UndisguiseCommand implements CommandExecutor {
return true; return true;
} }
if (sender.hasPermission("libsdisguises.undisguise")) { if (sender.hasPermission("libsdisguises.undisguise")) {
if (DisguiseAPI.isDisguised(sender.getName())) { if (DisguiseAPI.isDisguised((Entity) sender)) {
DisguiseAPI.undisguiseToAll((Player) sender); DisguiseAPI.undisguiseToAll((Player) sender);
sender.sendMessage(ChatColor.RED + "You are no longer disguised"); sender.sendMessage(ChatColor.RED + "You are no longer disguised");
} else } else

View File

@ -17,7 +17,7 @@ public class UndisguisePlayerCommand implements CommandExecutor {
if (args.length > 0) { if (args.length > 0) {
Player p = Bukkit.getPlayer(args[0]); Player p = Bukkit.getPlayer(args[0]);
if (p != null) { if (p != null) {
if (DisguiseAPI.isDisguised(p.getName())) { if (DisguiseAPI.isDisguised(p)) {
DisguiseAPI.undisguiseToAll(p); DisguiseAPI.undisguiseToAll(p);
sender.sendMessage(ChatColor.RED + "He is no longer disguised"); sender.sendMessage(ChatColor.RED + "He is no longer disguised");
} else } else

View File

@ -31,18 +31,19 @@ import com.comphenix.protocol.reflect.StructureModifier;
public class DisguiseAPI { public class DisguiseAPI {
private static HashMap<Object, Disguise> disguises = new HashMap<Object, Disguise>(); private static HashMap<Entity, Disguise> disguises = new HashMap<Entity, Disguise>();
private static PacketListener packetListener; private static PacketListener packetListener;
private static JavaPlugin plugin; private static JavaPlugin plugin;
private static boolean sendVelocity;
private static boolean soundsEnabled; private static boolean soundsEnabled;
private synchronized static Disguise access(Object obj, Disguise... object) { private synchronized static Disguise access(Entity entity, Disguise... args) {
if (object.length == 0) if (args.length == 0)
return disguises.get(obj); return disguises.get(entity);
if (object[0] == null) if (args[0] == null)
disguises.remove(obj); disguises.remove(entity);
else else
disguises.put(obj, object[0]); disguises.put(entity, args[0]);
return null; return null;
} }
@ -57,8 +58,11 @@ public class DisguiseAPI {
return; return;
if (disguise.getWatcher() != null) if (disguise.getWatcher() != null)
disguise = disguise.clone(); disguise = disguise.clone();
put(entity instanceof Player ? ((Player) entity).getName() : entity.getUniqueId(), disguise); Disguise oldDisguise = getDisguise(entity);
disguise.constructWatcher(entity.getType(), entity.getEntityId()); if (oldDisguise != null)
oldDisguise.getScheduler().cancel();
put(entity, disguise);
disguise.constructWatcher(plugin, entity);
refresh(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); return access(obj);
} }
@ -81,16 +85,15 @@ public class DisguiseAPI {
* @param Disguiser * @param Disguiser
* @return Disguise * @return Disguise
*/ */
public static Disguise getDisguise(Object disguiser) { public static Disguise getDisguise(Entity disguiser) {
if (disguiser instanceof Entity) {
if (disguiser instanceof Player)
return get(((Player) disguiser).getName());
else
return get(((Entity) disguiser).getUniqueId());
}
return get(disguiser); return get(disguiser);
} }
@Deprecated
public static Disguise getDisguise(Object disguiser) {
return get((Entity) disguiser);
}
protected static void init(JavaPlugin mainPlugin) { protected static void init(JavaPlugin mainPlugin) {
plugin = mainPlugin; plugin = mainPlugin;
packetListener = new PacketAdapter(plugin, ConnectionSide.SERVER_SIDE, ListenerPriority.NORMAL, packetListener = new PacketAdapter(plugin, ConnectionSide.SERVER_SIDE, ListenerPriority.NORMAL,
@ -172,17 +175,20 @@ public class DisguiseAPI {
* @param Disguiser * @param Disguiser
* @return boolean - If the disguiser is disguised * @return boolean - If the disguiser is disguised
*/ */
public static boolean isDisguised(Object disguiser) { public static boolean isDisguised(Entity disguiser) {
if (disguiser instanceof Entity) {
if (disguiser instanceof Player)
return get(((Player) disguiser).getName()) != null;
else
return get(((Entity) disguiser).getUniqueId()) != null;
}
return get(disguiser) != null; 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); access(obj, disguise);
} }
@ -204,12 +210,22 @@ public class DisguiseAPI {
} }
} }
public static void setVelocitySent(boolean sendVelocityPackets) {
sendVelocity = sendVelocityPackets;
}
/** /**
* @param Disguiser * @param Disguiser
* - Undisguises him * - Undisguises him
*/ */
public static void undisguiseToAll(Entity entity) { public static void undisguiseToAll(Entity entity) {
put(entity instanceof Player ? ((Player) entity).getName() : entity.getUniqueId(), null); Disguise disguise = getDisguise(entity);
refresh(entity); if (disguise == null)
return;
disguise.getScheduler().cancel();
put(entity, null);
if (entity.isValid()) {
refresh(entity);
}
} }
} }