Change self disguises to use UUID's instead of integers as the key

This commit is contained in:
libraryaddict 2014-04-03 01:49:48 +13:00
parent 04f6b18fff
commit b6fca92817
4 changed files with 16 additions and 12 deletions

View File

@ -3,6 +3,7 @@ package me.libraryaddict.disguise;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.UUID;
import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.TargetedDisguise; import me.libraryaddict.disguise.disguisetypes.TargetedDisguise;
@ -142,7 +143,7 @@ public class DisguiseAPI {
/** /**
* Get the ID of a fake disguise for a entityplayer * Get the ID of a fake disguise for a entityplayer
*/ */
public static int getFakeDisguise(int entityId) { public static int getFakeDisguise(UUID entityId) {
if (DisguiseUtilities.getSelfDisguisesIds().containsKey(entityId)) if (DisguiseUtilities.getSelfDisguisesIds().containsKey(entityId))
return DisguiseUtilities.getSelfDisguisesIds().get(entityId); return DisguiseUtilities.getSelfDisguisesIds().get(entityId);
return -1; return -1;

View File

@ -4,6 +4,7 @@ import java.lang.reflect.InvocationTargetException;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.UUID;
import me.libraryaddict.disguise.DisguiseAPI; import me.libraryaddict.disguise.DisguiseAPI;
import me.libraryaddict.disguise.DisguiseConfig; import me.libraryaddict.disguise.DisguiseConfig;
@ -33,6 +34,7 @@ public abstract class Disguise {
private static JavaPlugin plugin; private static JavaPlugin plugin;
private DisguiseType disguiseType; private DisguiseType disguiseType;
private Entity entity; private Entity entity;
private UUID disguiseUUID;
private boolean hearSelfDisguise = DisguiseConfig.isSelfDisguisesSoundsReplaced(); private boolean hearSelfDisguise = DisguiseConfig.isSelfDisguisesSoundsReplaced();
private boolean hideArmorFromSelf = DisguiseConfig.isHidingArmorFromSelf(); private boolean hideArmorFromSelf = DisguiseConfig.isHidingArmorFromSelf();
private boolean hideHeldItemFromSelf = DisguiseConfig.isHidingHeldItemFromSelf(); private boolean hideHeldItemFromSelf = DisguiseConfig.isHidingHeldItemFromSelf();
@ -229,7 +231,7 @@ public abstract class Disguise {
(byte) Math.floor(loc.getPitch() * 256.0F / 360.0F))); (byte) Math.floor(loc.getPitch() * 256.0F / 360.0F)));
if (isSelfDisguiseVisible() && getEntity() instanceof Player) { if (isSelfDisguiseVisible() && getEntity() instanceof Player) {
PacketContainer selfLookPacket = lookPacket.shallowClone(); PacketContainer selfLookPacket = lookPacket.shallowClone();
selfLookPacket.getModifier().write(0, DisguiseAPI.getFakeDisguise(getEntity().getEntityId())); selfLookPacket.getModifier().write(0, DisguiseAPI.getFakeDisguise(getEntity().getUniqueId()));
try { try {
ProtocolLibrary.getProtocolManager().sendServerPacket((Player) getEntity(), ProtocolLibrary.getProtocolManager().sendServerPacket((Player) getEntity(),
selfLookPacket, false); selfLookPacket, false);
@ -248,7 +250,7 @@ public abstract class Disguise {
if (!isSelfDisguiseVisible()) { if (!isSelfDisguiseVisible()) {
continue; continue;
} }
mods.write(0, DisguiseAPI.getFakeDisguise(getEntity().getEntityId())); mods.write(0, DisguiseAPI.getFakeDisguise(getEntity().getUniqueId()));
} else { } else {
mods.write(0, getEntity().getEntityId()); mods.write(0, getEntity().getEntityId());
} }
@ -274,7 +276,7 @@ public abstract class Disguise {
ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet, false); ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet, false);
} else if (isSelfDisguiseVisible()) { } else if (isSelfDisguiseVisible()) {
PacketContainer selfPacket = packet.shallowClone(); PacketContainer selfPacket = packet.shallowClone();
selfPacket.getModifier().write(0, DisguiseAPI.getFakeDisguise(getEntity().getEntityId())); selfPacket.getModifier().write(0, DisguiseAPI.getFakeDisguise(getEntity().getUniqueId()));
try { try {
ProtocolLibrary.getProtocolManager().sendServerPacket((Player) getEntity(), ProtocolLibrary.getProtocolManager().sendServerPacket((Player) getEntity(),
selfPacket, false); selfPacket, false);

View File

@ -8,6 +8,7 @@ import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.UUID;
import me.libraryaddict.disguise.DisguiseAPI; import me.libraryaddict.disguise.DisguiseAPI;
import me.libraryaddict.disguise.LibsDisguises; import me.libraryaddict.disguise.LibsDisguises;
@ -39,7 +40,7 @@ public class DisguiseUtilities {
private static LibsDisguises libsDisguises; private static LibsDisguises libsDisguises;
// A internal storage of fake entity ID's I can use. // A internal storage of fake entity ID's I can use.
// Realistically I could probably use a ID like "4" for everyone, seeing as no one shares the ID // Realistically I could probably use a ID like "4" for everyone, seeing as no one shares the ID
private static HashMap<Integer, Integer> selfDisguisesIds = new HashMap<Integer, Integer>(); private static HashMap<UUID, Integer> selfDisguisesIds = new HashMap<UUID, Integer>();
// Store the entity IDs instead of entitys because then I can disguise entitys even before they exist // Store the entity IDs instead of entitys because then I can disguise entitys even before they exist
private static HashMap<Integer, HashSet<TargetedDisguise>> targetedDisguises = new HashMap<Integer, HashSet<TargetedDisguise>>(); private static HashMap<Integer, HashSet<TargetedDisguise>> targetedDisguises = new HashMap<Integer, HashSet<TargetedDisguise>>();
@ -247,7 +248,7 @@ public class DisguiseUtilities {
return dis; return dis;
} }
public static HashMap<Integer, Integer> getSelfDisguisesIds() { public static HashMap<UUID, Integer> getSelfDisguisesIds() {
return selfDisguisesIds; return selfDisguisesIds;
} }
@ -377,17 +378,17 @@ public class DisguiseUtilities {
} }
public static void removeSelfDisguise(Player player) { public static void removeSelfDisguise(Player player) {
if (selfDisguisesIds.containsKey(player.getEntityId())) { if (selfDisguisesIds.containsKey(player.getUniqueId())) {
// Send a packet to destroy the fake entity // Send a packet to destroy the fake entity
PacketContainer packet = new PacketContainer(PacketType.Play.Server.ENTITY_DESTROY); PacketContainer packet = new PacketContainer(PacketType.Play.Server.ENTITY_DESTROY);
packet.getModifier().write(0, new int[] { selfDisguisesIds.get(player.getEntityId()) }); packet.getModifier().write(0, new int[] { selfDisguisesIds.get(player.getUniqueId()) });
try { try {
ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet); ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet);
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
// Remove the fake entity ID from the disguise bin // Remove the fake entity ID from the disguise bin
selfDisguisesIds.remove(player.getEntityId()); selfDisguisesIds.remove(player.getUniqueId());
// Get the entity tracker // Get the entity tracker
try { try {
Object world = ReflectionManager.getWorld(player.getWorld()); Object world = ReflectionManager.getWorld(player.getWorld());
@ -444,7 +445,7 @@ public class DisguiseUtilities {
}); });
return; return;
} }
int fakeId = selfDisguisesIds.get(player.getEntityId()); int fakeId = selfDisguisesIds.get(player.getUniqueId());
// Add himself to his own entity tracker // Add himself to his own entity tracker
((HashSet) entityTrackerEntry.getClass().getField("trackedPlayers").get(entityTrackerEntry)).add(ReflectionManager ((HashSet) entityTrackerEntry.getClass().getField("trackedPlayers").get(entityTrackerEntry)).add(ReflectionManager
.getNmsEntity(player)); .getNmsEntity(player));
@ -572,7 +573,7 @@ public class DisguiseUtilities {
int id = field.getInt(null); int id = field.getInt(null);
// Set the entitycount plus one so we don't have the id being reused // Set the entitycount plus one so we don't have the id being reused
field.set(null, id + 1); field.set(null, id + 1);
selfDisguisesIds.put(player.getEntityId(), id); selfDisguisesIds.put(player.getUniqueId(), id);
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
} }

View File

@ -707,7 +707,7 @@ public class PacketsManager {
public void onPacketSending(PacketEvent event) { public void onPacketSending(PacketEvent event) {
final Player observer = event.getPlayer(); final Player observer = event.getPlayer();
if (event.getPacket().getIntegers().read(0) == observer.getEntityId()) { if (event.getPacket().getIntegers().read(0) == observer.getEntityId()) {
int fakeId = DisguiseAPI.getFakeDisguise(observer.getEntityId()); int fakeId = DisguiseAPI.getFakeDisguise(observer.getUniqueId());
if (fakeId > 0) { if (fakeId > 0) {
// Here I grab the packets to convert them to, So I can display them as if the disguise sent them. // Here I grab the packets to convert them to, So I can display them as if the disguise sent them.
PacketContainer[] packets = transformPacket(event.getPacket(), observer, observer); PacketContainer[] packets = transformPacket(event.getPacket(), observer, observer);