From 6e69ae6fe26b6c329d10e93edbbf05d1c9338618 Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Wed, 16 Jun 2021 19:37:52 +1200 Subject: [PATCH] Attempt to fix a tracker exception --- .../disguise/utilities/DisguiseUtilities.java | 11 +++++++++-- .../utilities/reflection/ReflectionManager.java | 17 +++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/main/java/me/libraryaddict/disguise/utilities/DisguiseUtilities.java b/src/main/java/me/libraryaddict/disguise/utilities/DisguiseUtilities.java index 2847c8cd..e6ebdde6 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/DisguiseUtilities.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/DisguiseUtilities.java @@ -982,6 +982,7 @@ public class DisguiseUtilities { return; } + // TODO Store reflection field Set trackedPlayers = (Set) ReflectionManager.getNmsField("EntityTrackerEntry", "trackedPlayers").get(entityTrackerEntry); // If the tracker exists. Remove himself from his tracker @@ -991,7 +992,7 @@ public class DisguiseUtilities { PacketContainer destroyPacket = getDestroyPacket(disguise.getEntity().getEntityId()); for (Object p : trackedPlayers) { - Player player = (Player) ReflectionManager.getBukkitEntity(p); + Player player = (Player) ReflectionManager.getBukkitEntity(ReflectionManager.getTrackerPlayer(p)); if (player == disguise.getEntity() || disguise.canSee(player)) { ProtocolLibrary.getProtocolManager().sendServerPacket(player, destroyPacket); @@ -1188,6 +1189,7 @@ public class DisguiseUtilities { Object entityTrackerEntry = ReflectionManager.getEntityTrackerEntry(disguise.getEntity()); if (entityTrackerEntry != null) { + // TODO Store reflection field Set trackedPlayers = (Set) ReflectionManager.getNmsField("EntityTrackerEntry", "trackedPlayers").get(entityTrackerEntry); trackedPlayers = (Set) new HashSet(trackedPlayers).clone(); // Copy before iterating to prevent // ConcurrentModificationException @@ -1701,6 +1703,7 @@ public class DisguiseUtilities { if (entityTrackerEntry != null) { Set trackedPlayers = (Set) ReflectionManager.getNmsField("EntityTrackerEntry", "trackedPlayers").get(entityTrackerEntry); + // TODO Store the fields final Method clear = ReflectionManager .getNmsMethod("EntityTrackerEntry", NmsVersion.v1_14.isSupported() ? "a" : "clear", ReflectionManager.getNmsClass("EntityPlayer")); @@ -1710,7 +1713,8 @@ public class DisguiseUtilities { trackedPlayers = (Set) new HashSet(trackedPlayers).clone(); PacketContainer destroyPacket = getDestroyPacket(disguise.getEntity().getEntityId()); - for (final Object p : trackedPlayers) { + for (final Object o : trackedPlayers) { + Object p = ReflectionManager.getTrackerPlayer(o); Player player = (Player) ReflectionManager.getBukkitEntity(p); if (disguise.getEntity() != player && disguise.canSee(player)) { @@ -1822,6 +1826,7 @@ public class DisguiseUtilities { if (entityTrackerEntry != null) { + // TODO Store reflection fields // If the tracker exists. Remove himself from his tracker if (!runningPaper) { Object trackedPlayersObj = ReflectionManager.getNmsField("EntityTrackerEntry", "trackedPlayers").get(entityTrackerEntry); @@ -2360,6 +2365,7 @@ public class DisguiseUtilities { return; } + // TODO Store reflection fields // Check for code differences in PaperSpigot vs Spigot if (!runningPaper) { // Add himself to his own entity tracker @@ -2385,6 +2391,7 @@ public class DisguiseUtilities { boolean isMoving = false; try { + // TODO Store the field Field field = ReflectionManager.getNmsClass("EntityTrackerEntry") .getDeclaredField(NmsVersion.v1_17.isSupported() ? "r" : NmsVersion.v1_14.isSupported() ? "q" : "isMoving"); field.setAccessible(true); diff --git a/src/main/java/me/libraryaddict/disguise/utilities/reflection/ReflectionManager.java b/src/main/java/me/libraryaddict/disguise/utilities/reflection/ReflectionManager.java index 55b77e4c..4cbcc096 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/reflection/ReflectionManager.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/reflection/ReflectionManager.java @@ -104,6 +104,7 @@ public class ReflectionManager { private static Constructor mobEffectConstructor; private static Method boundingBoxMethod; private static Method bukkitEntityMethod; + private static Method connectionEntityMethod; private static Field noDamageTicks; private static Method isInvul; private static Object genericDamage; @@ -196,6 +197,7 @@ public class ReflectionManager { villagerProfessionRegistry = getNmsField("IRegistry", "ap").get(null); villagerTypeRegistry = getNmsField("IRegistry", "ao").get(null); playerConnection = getNmsField("EntityPlayer", "b"); + connectionEntityMethod = getNmsMethod("PlayerConnection", "d"); } else { villagerProfessionRegistry = getNmsField("IRegistry", "VILLAGER_PROFESSION").get(null); villagerTypeRegistry = getNmsField("IRegistry", "VILLAGER_TYPE").get(null); @@ -596,6 +598,21 @@ public class ReflectionManager { return null; } + public static Object getTrackerPlayer(Object nmsEntity) { + try { + if (NmsVersion.v1_17.isSupported()) { + // Convert from player connection to EntityPlayer + nmsEntity = connectionEntityMethod.invoke(nmsEntity); + } + + return nmsEntity; + } catch (Exception ex) { + ex.printStackTrace(); + } + + return null; + } + public static Entity getBukkitEntity(Object nmsEntity) { try { return (Entity) bukkitEntityMethod.invoke(nmsEntity);