Change self disguises to globally use the entity id -1 for performance and usage in other plugins

This commit is contained in:
libraryaddict 2014-06-15 20:12:07 +12:00
parent 13cf2f5e85
commit 20b3d02f76
4 changed files with 30 additions and 39 deletions

View File

@ -289,9 +289,12 @@ public class DisguiseAPI {
/** /**
* Get the ID of a fake disguise for a entityplayer * Get the ID of a fake disguise for a entityplayer
*/ */
@Deprecated
public static int getFakeDisguise(UUID entityId) { public static int getFakeDisguise(UUID entityId) {
if (DisguiseUtilities.getSelfDisguisesIds().containsKey(entityId)) return -1;
return DisguiseUtilities.getSelfDisguisesIds().get(entityId); }
public static int getSelfDisguiseId() {
return -1; return -1;
} }
@ -313,6 +316,10 @@ public class DisguiseAPI {
return disguise.isDisguiseInUse(); return disguise.isDisguiseInUse();
} }
public static boolean isSelfDisguised(Player player) {
return DisguiseUtilities.getSelfDisguised().contains(player.getUniqueId());
}
/** /**
* Undisguise the entity. This doesn't let you cancel the UndisguiseEvent if the entity is no longer valid. Aka removed from * Undisguise the entity. This doesn't let you cancel the UndisguiseEvent if the entity is no longer valid. Aka removed from
* the world. * the world.

View File

@ -240,7 +240,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().getUniqueId())); selfLookPacket.getModifier().write(0, DisguiseAPI.getSelfDisguiseId());
try { try {
ProtocolLibrary.getProtocolManager().sendServerPacket((Player) getEntity(), ProtocolLibrary.getProtocolManager().sendServerPacket((Player) getEntity(),
selfLookPacket, false); selfLookPacket, false);
@ -259,7 +259,7 @@ public abstract class Disguise {
if (!isSelfDisguiseVisible()) { if (!isSelfDisguiseVisible()) {
continue; continue;
} }
mods.write(0, DisguiseAPI.getFakeDisguise(getEntity().getUniqueId())); mods.write(0, DisguiseAPI.getSelfDisguiseId());
} else { } else {
mods.write(0, getEntity().getEntityId()); mods.write(0, getEntity().getEntityId());
} }
@ -286,7 +286,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().getUniqueId())); selfPacket.getModifier().write(0, DisguiseAPI.getSelfDisguiseId());
try { try {
ProtocolLibrary.getProtocolManager().sendServerPacket((Player) getEntity(), selfPacket, ProtocolLibrary.getProtocolManager().sendServerPacket((Player) getEntity(), selfPacket,
false); false);

View File

@ -62,11 +62,7 @@ public class DisguiseUtilities {
private static HashMap<String, WrappedGameProfile> gameProfiles = new HashMap<String, WrappedGameProfile>(); private static HashMap<String, WrappedGameProfile> gameProfiles = new HashMap<String, WrappedGameProfile>();
private static LibsDisguises libsDisguises; private static LibsDisguises libsDisguises;
private static HashMap<String, ArrayList<Object>> runnables = new HashMap<String, ArrayList<Object>>(); private static HashMap<String, ArrayList<Object>> runnables = new HashMap<String, ArrayList<Object>>();
/** private static HashSet<UUID> selfDisguised = new HashSet<UUID>();
* A internal storage of fake entity ID's each entity has. Realistically I could probably use a ID like "4" for everyone,
* seeing as no one sees each others entity ID
**/
private static HashMap<UUID, Integer> selfDisguisesIds = new HashMap<UUID, Integer>();
public static boolean addClonedDisguise(String key, Disguise disguise) { public static boolean addClonedDisguise(String key, Disguise disguise) {
if (DisguiseConfig.getMaxClonedDisguises() > 0) { if (DisguiseConfig.getMaxClonedDisguises() > 0) {
@ -428,8 +424,8 @@ public class DisguiseUtilities {
return getProfileFromMojang(playerName, (Object) runnableIfCantReturn); return getProfileFromMojang(playerName, (Object) runnableIfCantReturn);
} }
public static HashMap<UUID, Integer> getSelfDisguisesIds() { public static HashSet<UUID> getSelfDisguised() {
return selfDisguisesIds; return selfDisguised;
} }
public static boolean hasGameProfile(String playerName) { public static boolean hasGameProfile(String playerName) {
@ -568,17 +564,17 @@ public class DisguiseUtilities {
} }
public static void removeSelfDisguise(Player player) { public static void removeSelfDisguise(Player player) {
if (selfDisguisesIds.containsKey(player.getUniqueId())) { if (selfDisguised.contains(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.getUniqueId()) }); packet.getModifier().write(0, new int[] { DisguiseAPI.getSelfDisguiseId() });
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.getUniqueId()); selfDisguised.remove(player.getUniqueId());
// Get the entity tracker // Get the entity tracker
try { try {
Object entityTrackerEntry = ReflectionManager.getEntityTrackerEntry(player); Object entityTrackerEntry = ReflectionManager.getEntityTrackerEntry(player);
@ -630,7 +626,6 @@ public class DisguiseUtilities {
}); });
return; return;
} }
int fakeId = selfDisguisesIds.get(player.getUniqueId());
// Add himself to his own entity tracker // Add himself to his own entity tracker
((HashSet<Object>) ReflectionManager.getNmsField("EntityTrackerEntry", "trackedPlayers").get(entityTrackerEntry)) ((HashSet<Object>) ReflectionManager.getNmsField("EntityTrackerEntry", "trackedPlayers").get(entityTrackerEntry))
.add(ReflectionManager.getNmsEntity(player)); .add(ReflectionManager.getNmsEntity(player));
@ -642,7 +637,7 @@ public class DisguiseUtilities {
sendSelfPacket( sendSelfPacket(
player, player,
manager.createPacketConstructor(PacketType.Play.Server.ENTITY_METADATA, player.getEntityId(), dataWatcher, manager.createPacketConstructor(PacketType.Play.Server.ENTITY_METADATA, player.getEntityId(), dataWatcher,
true).createPacket(player.getEntityId(), dataWatcher, true), fakeId); true).createPacket(player.getEntityId(), dataWatcher, true));
boolean isMoving = false; boolean isMoving = false;
try { try {
@ -659,18 +654,18 @@ public class DisguiseUtilities {
player, player,
manager.createPacketConstructor(PacketType.Play.Server.ENTITY_VELOCITY, player.getEntityId(), manager.createPacketConstructor(PacketType.Play.Server.ENTITY_VELOCITY, player.getEntityId(),
velocity.getX(), velocity.getY(), velocity.getZ()).createPacket(player.getEntityId(), velocity.getX(), velocity.getY(), velocity.getZ()).createPacket(player.getEntityId(),
velocity.getX(), velocity.getY(), velocity.getZ()), fakeId); velocity.getX(), velocity.getY(), velocity.getZ()));
} }
// Why the hell would he even need this. Meh. // Why the hell would he even need this. Meh.
if (player.getVehicle() != null && player.getEntityId() > player.getVehicle().getEntityId()) { if (player.getVehicle() != null && player.getEntityId() > player.getVehicle().getEntityId()) {
sendSelfPacket(player, sendSelfPacket(player,
manager.createPacketConstructor(PacketType.Play.Server.ATTACH_ENTITY, 0, player, player.getVehicle()) manager.createPacketConstructor(PacketType.Play.Server.ATTACH_ENTITY, 0, player, player.getVehicle())
.createPacket(0, player, player.getVehicle()), fakeId); .createPacket(0, player, player.getVehicle()));
} else if (player.getPassenger() != null && player.getEntityId() > player.getPassenger().getEntityId()) { } else if (player.getPassenger() != null && player.getEntityId() > player.getPassenger().getEntityId()) {
sendSelfPacket(player, sendSelfPacket(player,
manager.createPacketConstructor(PacketType.Play.Server.ATTACH_ENTITY, 0, player.getPassenger(), player) manager.createPacketConstructor(PacketType.Play.Server.ATTACH_ENTITY, 0, player.getPassenger(), player)
.createPacket(0, player.getPassenger(), player), fakeId); .createPacket(0, player.getPassenger(), player));
} }
// Resend the armor // Resend the armor
@ -686,7 +681,7 @@ public class DisguiseUtilities {
sendSelfPacket( sendSelfPacket(
player, player,
manager.createPacketConstructor(PacketType.Play.Server.ENTITY_EQUIPMENT, player.getEntityId(), i, manager.createPacketConstructor(PacketType.Play.Server.ENTITY_EQUIPMENT, player.getEntityId(), i,
item).createPacket(player.getEntityId(), i, item), fakeId); item).createPacket(player.getEntityId(), i, item));
} }
} }
Location loc = player.getLocation(); Location loc = player.getLocation();
@ -695,14 +690,14 @@ public class DisguiseUtilities {
sendSelfPacket( sendSelfPacket(
player, player,
manager.createPacketConstructor(PacketType.Play.Server.BED, player, loc.getBlockX(), loc.getBlockY(), manager.createPacketConstructor(PacketType.Play.Server.BED, player, loc.getBlockX(), loc.getBlockY(),
loc.getBlockZ()).createPacket(player, loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()), fakeId); loc.getBlockZ()).createPacket(player, loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()));
} }
// Resend any active potion effects // Resend any active potion effects
for (Object potionEffect : player.getActivePotionEffects()) { for (Object potionEffect : player.getActivePotionEffects()) {
sendSelfPacket(player, sendSelfPacket(player,
manager.createPacketConstructor(PacketType.Play.Server.ENTITY_EFFECT, player.getEntityId(), potionEffect) manager.createPacketConstructor(PacketType.Play.Server.ENTITY_EFFECT, player.getEntityId(), potionEffect)
.createPacket(player.getEntityId(), potionEffect), fakeId); .createPacket(player.getEntityId(), potionEffect));
} }
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
@ -712,7 +707,7 @@ public class DisguiseUtilities {
/** /**
* Method to send a packet to the self disguise, translate his entity ID to the fake id. * Method to send a packet to the self disguise, translate his entity ID to the fake id.
*/ */
private static void sendSelfPacket(Player player, PacketContainer packet, int fakeId) { private static void sendSelfPacket(Player player, PacketContainer packet) {
PacketContainer[] packets = PacketsManager.transformPacket(packet, player, player); PacketContainer[] packets = PacketsManager.transformPacket(packet, player, player);
try { try {
if (packets == null) { if (packets == null) {
@ -720,7 +715,7 @@ public class DisguiseUtilities {
} }
for (PacketContainer p : packets) { for (PacketContainer p : packets) {
p = p.deepClone(); p = p.deepClone();
p.getIntegers().write(0, fakeId); p.getIntegers().write(0, DisguiseAPI.getSelfDisguiseId());
ProtocolLibrary.getProtocolManager().sendServerPacket(player, p, false); ProtocolLibrary.getProtocolManager().sendServerPacket(player, p, false);
} }
} catch (InvocationTargetException e) { } catch (InvocationTargetException e) {
@ -749,17 +744,7 @@ public class DisguiseUtilities {
if (!disguise.isSelfDisguiseVisible() || !PacketsManager.isViewDisguisesListenerEnabled() || player.getVehicle() != null) { if (!disguise.isSelfDisguiseVisible() || !PacketsManager.isViewDisguisesListenerEnabled() || player.getVehicle() != null) {
return; return;
} }
try { selfDisguised.add(player.getUniqueId());
// Grab the entity ID the fake disguise will use
Field field = ReflectionManager.getNmsClass("Entity").getDeclaredField("entityCount");
field.setAccessible(true);
int id = field.getInt(null);
// Set the entitycount plus one so we don't have the id being reused
field.set(null, id + 1);
selfDisguisesIds.put(player.getUniqueId(), id);
} catch (Exception ex) {
ex.printStackTrace();
}
sendSelfDisguise(player, disguise); sendSelfDisguise(player, disguise);
if (disguise.isHidingArmorFromSelf() || disguise.isHidingHeldItemFromSelf()) { if (disguise.isHidingArmorFromSelf() || disguise.isHidingHeldItemFromSelf()) {
if (PacketsManager.isInventoryListenerEnabled()) { if (PacketsManager.isInventoryListenerEnabled()) {

View File

@ -741,8 +741,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.getUniqueId()); if (DisguiseAPI.isSelfDisguised(observer)) {
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);
if (packets == null) { if (packets == null) {
@ -752,7 +751,7 @@ public class PacketsManager {
if (packet.equals(event.getPacket())) { if (packet.equals(event.getPacket())) {
packet = packet.deepClone(); packet = packet.deepClone();
} }
packet.getModifier().write(0, fakeId); packet.getModifier().write(0, DisguiseAPI.getSelfDisguiseId());
try { try {
ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet, false); ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet, false);
} catch (InvocationTargetException e) { } catch (InvocationTargetException e) {