More getNmsField/Method calls
This commit is contained in:
		| @@ -197,24 +197,14 @@ public class DisguiseUtilities { | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     private static final Field trackerField = ReflectionManager.getNmsField("World", "tracker"); | ||||
|     private static final Field entitiesField = ReflectionManager.getNmsField("EntityTracker", "trackedEntities"); | ||||
|     private static final Method ihmGet = ReflectionManager.getNmsMethod("IntHashMap", "get", int.class); | ||||
|     private static Object getEntityTrackerEntry(Entity target) throws Exception { | ||||
|         Object world = ReflectionManager.getWorld(target.getWorld()); | ||||
|         Object tracker = trackerField.get(world); | ||||
|         Object trackedEntities = entitiesField.get(tracker); | ||||
|         return ihmGet.invoke(trackedEntities, target.getEntityId()); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Sends entity removal packets, as this disguise was removed | ||||
|      */ | ||||
|     public static void destroyEntity(TargetedDisguise disguise) { | ||||
|         try { | ||||
|             Object entityTrackerEntry = getEntityTrackerEntry(disguise.getEntity()); | ||||
|             Object entityTrackerEntry = ReflectionManager.getEntityTrackerEntry(disguise.getEntity()); | ||||
|             if (entityTrackerEntry != null) { | ||||
|                 HashSet trackedPlayers = (HashSet) entityTrackerEntry.getClass().getField("trackedPlayers") | ||||
|                 HashSet trackedPlayers = (HashSet) ReflectionManager.getNmsField("EntityTrackerEntry", "trackedPlayers") | ||||
|                         .get(entityTrackerEntry); | ||||
|                 HashSet cloned = (HashSet) trackedPlayers.clone(); | ||||
|                 PacketContainer destroyPacket = new PacketContainer(PacketType.Play.Server.ENTITY_DESTROY); | ||||
| @@ -331,9 +321,9 @@ public class DisguiseUtilities { | ||||
|     public static ArrayList<Player> getPerverts(Disguise disguise) { | ||||
|         ArrayList<Player> players = new ArrayList<Player>(); | ||||
|         try { | ||||
|             Object entityTrackerEntry = getEntityTrackerEntry(disguise.getEntity()); | ||||
|             Object entityTrackerEntry = ReflectionManager.getEntityTrackerEntry(disguise.getEntity()); | ||||
|             if (entityTrackerEntry != null) { | ||||
|                 HashSet trackedPlayers = (HashSet) ReflectionManager.getNmsField(entityTrackerEntry.getClass(), "trackedPlayers") | ||||
|                 HashSet trackedPlayers = (HashSet) ReflectionManager.getNmsField("EntityTrackerEntry", "trackedPlayers") | ||||
|                         .get(entityTrackerEntry); | ||||
|                 for (Object p : trackedPlayers) { | ||||
|                     Player player = (Player) ReflectionManager.getBukkitEntity(p); | ||||
| @@ -468,13 +458,13 @@ public class DisguiseUtilities { | ||||
|     public static void refreshTracker(TargetedDisguise disguise, String player) { | ||||
|         if (disguise.getEntity() != null && disguise.getEntity().isValid()) { | ||||
|             try { | ||||
|                 Object entityTrackerEntry = getEntityTrackerEntry(disguise.getEntity()); | ||||
|                 Object entityTrackerEntry = ReflectionManager.getEntityTrackerEntry(disguise.getEntity()); | ||||
|                 if (entityTrackerEntry != null) { | ||||
|                     HashSet trackedPlayers = (HashSet) entityTrackerEntry.getClass().getField("trackedPlayers") | ||||
|                     HashSet trackedPlayers = (HashSet) ReflectionManager.getNmsField("EntityTrackerEntry", "trackedPlayers") | ||||
|                             .get(entityTrackerEntry); | ||||
|                     Method clear = entityTrackerEntry.getClass() | ||||
|                             .getMethod("clear", ReflectionManager.getNmsClass("EntityPlayer")); | ||||
|                     Method updatePlayer = entityTrackerEntry.getClass().getMethod("updatePlayer", | ||||
|                     Method clear = ReflectionManager.getNmsMethod("EntityTrackerEntry", "clear", | ||||
|                             ReflectionManager.getNmsClass("EntityPlayer")); | ||||
|                     Method updatePlayer = ReflectionManager.getNmsMethod("EntityTrackerEntry", "updatePlayer", | ||||
|                             ReflectionManager.getNmsClass("EntityPlayer")); | ||||
|                     HashSet cloned = (HashSet) trackedPlayers.clone(); | ||||
|                     for (Object p : cloned) { | ||||
| @@ -497,13 +487,13 @@ public class DisguiseUtilities { | ||||
|     public static void refreshTrackers(Entity entity) { | ||||
|         if (entity.isValid()) { | ||||
|             try { | ||||
|                 Object entityTrackerEntry = getEntityTrackerEntry(entity); | ||||
|                 Object entityTrackerEntry = ReflectionManager.getEntityTrackerEntry(entity); | ||||
|                 if (entityTrackerEntry != null) { | ||||
|                     HashSet trackedPlayers = (HashSet) entityTrackerEntry.getClass().getField("trackedPlayers") | ||||
|                     HashSet trackedPlayers = (HashSet) ReflectionManager.getNmsField("EntityTrackerEntry", "trackedPlayers") | ||||
|                             .get(entityTrackerEntry); | ||||
|                     Method clear = entityTrackerEntry.getClass() | ||||
|                             .getMethod("clear", ReflectionManager.getNmsClass("EntityPlayer")); | ||||
|                     Method updatePlayer = entityTrackerEntry.getClass().getMethod("updatePlayer", | ||||
|                     Method clear = ReflectionManager.getNmsMethod("EntityTrackerEntry", "clear", | ||||
|                             ReflectionManager.getNmsClass("EntityPlayer")); | ||||
|                     Method updatePlayer = ReflectionManager.getNmsMethod("EntityTrackerEntry", "updatePlayer", | ||||
|                             ReflectionManager.getNmsClass("EntityPlayer")); | ||||
|                     HashSet cloned = (HashSet) trackedPlayers.clone(); | ||||
|                     for (Object p : cloned) { | ||||
| @@ -528,12 +518,13 @@ public class DisguiseUtilities { | ||||
|      */ | ||||
|     public static void refreshTrackers(TargetedDisguise disguise) { | ||||
|         try { | ||||
|             Object entityTrackerEntry = getEntityTrackerEntry(disguise.getEntity()); | ||||
|             Object entityTrackerEntry = ReflectionManager.getEntityTrackerEntry(disguise.getEntity()); | ||||
|             if (entityTrackerEntry != null) { | ||||
|                 HashSet trackedPlayers = (HashSet) entityTrackerEntry.getClass().getField("trackedPlayers") | ||||
|                 HashSet trackedPlayers = (HashSet) ReflectionManager.getNmsField("EntityTrackerEntry", "trackedPlayers") | ||||
|                         .get(entityTrackerEntry); | ||||
|                 Method clear = entityTrackerEntry.getClass().getMethod("clear", ReflectionManager.getNmsClass("EntityPlayer")); | ||||
|                 Method updatePlayer = entityTrackerEntry.getClass().getMethod("updatePlayer", | ||||
|                 Method clear = ReflectionManager.getNmsMethod("EntityTrackerEntry", "clear", | ||||
|                         ReflectionManager.getNmsClass("EntityPlayer")); | ||||
|                 Method updatePlayer = ReflectionManager.getNmsMethod("EntityTrackerEntry", "updatePlayer", | ||||
|                         ReflectionManager.getNmsClass("EntityPlayer")); | ||||
|                 HashSet cloned = (HashSet) trackedPlayers.clone(); | ||||
|                 for (Object p : cloned) { | ||||
| @@ -589,9 +580,9 @@ public class DisguiseUtilities { | ||||
|             selfDisguisesIds.remove(player.getUniqueId()); | ||||
|             // Get the entity tracker | ||||
|             try { | ||||
|                 Object entityTrackerEntry = getEntityTrackerEntry(player); | ||||
|                 Object entityTrackerEntry = ReflectionManager.getEntityTrackerEntry(player); | ||||
|                 if (entityTrackerEntry != null) { | ||||
|                     HashSet trackedPlayers = (HashSet) entityTrackerEntry.getClass().getField("trackedPlayers") | ||||
|                     HashSet trackedPlayers = (HashSet) ReflectionManager.getNmsField("EntityTrackerEntry", "trackedPlayers") | ||||
|                             .get(entityTrackerEntry); | ||||
|                     // If the tracker exists. Remove himself from his tracker | ||||
|                     trackedPlayers.remove(ReflectionManager.getNmsEntity(player)); | ||||
| @@ -623,7 +614,7 @@ public class DisguiseUtilities { | ||||
|             if (!player.isValid() || !player.isOnline() || !disguise.isSelfDisguiseVisible()) { | ||||
|                 return; | ||||
|             } | ||||
|             Object entityTrackerEntry = getEntityTrackerEntry(player); | ||||
|             Object entityTrackerEntry = ReflectionManager.getEntityTrackerEntry(player); | ||||
|             if (entityTrackerEntry == null) { | ||||
|                 // A check incase the tracker is null. | ||||
|                 // If it is, then this method will be run again in one tick. Which is when it should be constructed. | ||||
| @@ -640,8 +631,7 @@ public class DisguiseUtilities { | ||||
|             } | ||||
|             int fakeId = selfDisguisesIds.get(player.getUniqueId()); | ||||
|             // Add himself to his own entity tracker | ||||
|             ((HashSet) entityTrackerEntry.getClass().getField("trackedPlayers").get(entityTrackerEntry)).add(ReflectionManager | ||||
|                     .getNmsEntity(player)); | ||||
|             ((HashSet<?>) ReflectionManager.getNmsField("EntityTrackerEntry", "trackedPlayers").get(entityTrackerEntry)).add(ReflectionManager.getNmsEntity(player)); | ||||
|             ProtocolManager manager = ProtocolLibrary.getProtocolManager(); | ||||
|             // Send the player a packet with himself being spawned | ||||
|             manager.sendServerPacket(player, manager.createPacketConstructor(PacketType.Play.Server.NAMED_ENTITY_SPAWN, player) | ||||
|   | ||||
| @@ -544,13 +544,11 @@ public class PacketsManager { | ||||
|                                         boolean hasInvun = false; | ||||
|                                         Object nmsEntity = ReflectionManager.getNmsEntity(entity); | ||||
|                                         try { | ||||
|                                             Class entityClass = ReflectionManager.getNmsClass("Entity"); | ||||
|                                             if (entity instanceof LivingEntity) { | ||||
|                                                 hasInvun = entityClass.getField("noDamageTicks").getInt(nmsEntity) == ReflectionManager | ||||
|                                                         .getNmsClass("EntityLiving").getField("maxNoDamageTicks") | ||||
|                                                         .getInt(nmsEntity); | ||||
|                                                 hasInvun = ReflectionManager.getNmsField("Entity", "noDamageTicks").getInt(nmsEntity) == | ||||
|                                                         ReflectionManager.getNmsField("EntityLiving", "maxNoDamageTicks").getInt(nmsEntity); | ||||
|                                             } else { | ||||
|                                                 hasInvun = (Boolean) entityClass.getMethod("isInvulnerable").invoke(nmsEntity); | ||||
|                                                 hasInvun = (Boolean) ReflectionManager.getNmsMethod("Entity", "isInvulnerable").invoke(nmsEntity); | ||||
|                                             } | ||||
|                                         } catch (Exception ex) { | ||||
|                                             ex.printStackTrace(); | ||||
| @@ -581,18 +579,16 @@ public class PacketsManager { | ||||
|                                         try { | ||||
|                                             int typeId = soundLoc.getWorld().getBlockTypeIdAt(soundLoc.getBlockX(), | ||||
|                                                     soundLoc.getBlockY() - 1, soundLoc.getBlockZ()); | ||||
|                                             Class blockClass = ReflectionManager.getNmsClass("Block"); | ||||
|                                             Object block; | ||||
|                                             if (LibVersion.is1_7()) { | ||||
|                                                 block = ReflectionManager.getNmsClass("RegistryMaterials") | ||||
|                                                         .getMethod("a", int.class) | ||||
|                                                         .invoke(blockClass.getField("REGISTRY").get(null), typeId); | ||||
|                                                 block = ReflectionManager.getNmsMethod("RegistryMaterials", "a", int.class) | ||||
|                                                         .invoke(ReflectionManager.getNmsField("Block", "REGISTRY").get(null), typeId); | ||||
|                                             } else { | ||||
|                                                 block = ((Object[]) blockClass.getField("byId").get(null))[typeId]; | ||||
|                                                 block = ((Object[]) ReflectionManager.getNmsField("Block", "byId").get(null))[typeId]; | ||||
|                                             } | ||||
|                                             if (block != null) { | ||||
|                                                 Object step = blockClass.getField("stepSound").get(block); | ||||
|                                                 mods.write(0, step.getClass().getMethod("getStepSound").invoke(step)); | ||||
|                                                 Object step = ReflectionManager.getNmsField("Block", "stepSound").get(block); | ||||
|                                                 mods.write(0, ReflectionManager.getNmsMethod(step.getClass(), "getStepSound").invoke(step)); | ||||
|                                             } | ||||
|                                         } catch (Exception ex) { | ||||
|                                             ex.printStackTrace(); | ||||
|   | ||||
| @@ -10,7 +10,6 @@ import java.util.UUID; | ||||
| import java.util.regex.Matcher; | ||||
| import java.util.regex.Pattern; | ||||
|  | ||||
| import me.libraryaddict.disguise.disguisetypes.PlayerDisguise; | ||||
| import org.bukkit.Art; | ||||
| import org.bukkit.Bukkit; | ||||
| import org.bukkit.Location; | ||||
| @@ -23,6 +22,7 @@ import org.bukkit.inventory.ItemStack; | ||||
| import com.comphenix.protocol.wrappers.WrappedGameProfile; | ||||
|  | ||||
| public class ReflectionManager { | ||||
|  | ||||
|     public enum LibVersion { | ||||
|         V1_6, V1_7; | ||||
|         private static LibVersion currentVersion = LibVersion.V1_7; | ||||
| @@ -58,11 +58,16 @@ public class ReflectionManager { | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     private static String bukkitVersion = Bukkit.getServer().getClass().getName().split("\\.")[3]; | ||||
|     private static final String bukkitVersion = Bukkit.getServer().getClass().getName().split("\\.")[3]; | ||||
|     private static final Class craftItemClass = getCraftClass("inventory.CraftItemStack"); | ||||
|     private static final Field pingField = getNmsField("EntityPlayer", "ping"); | ||||
|     private static final Field trackerField = getNmsField("World", "tracker"); | ||||
|     private static final Field entitiesField = getNmsField("EntityTracker", "trackedEntities"); | ||||
|     private static final Method ihmGet = getNmsMethod("IntHashMap", "get", int.class); | ||||
|     private static final boolean isForge = Bukkit.getServer().getName().equalsIgnoreCase("Cauldron"); | ||||
|  | ||||
|     private static Method damageAndIdleSoundMethod; | ||||
|     private static Class itemClass; | ||||
|     private static Field pingField; | ||||
|     private static boolean isForge = Bukkit.getServer().getName().equalsIgnoreCase("Cauldron"); | ||||
|  | ||||
|     /** | ||||
|      * Map of mc-dev simple class name to fully qualified Forge class name. | ||||
|      */ | ||||
| @@ -74,7 +79,7 @@ public class ReflectionManager { | ||||
|     /** | ||||
|      * Map of Forge fully qualified class names to a map from mc-dev method names to Forge method names. | ||||
|      */ | ||||
|     private static Map<String, Map<String, String>> ForgeMethodMappings; | ||||
|     private static Map<String, Map<String, Map<Class<?>[], String>>> ForgeMethodMappings; | ||||
|  | ||||
|     private static String dir2fqn(String s) { | ||||
|         return s.replaceAll("/", "."); | ||||
| @@ -85,7 +90,7 @@ public class ReflectionManager { | ||||
|             // Initialize the maps by reading the srg file | ||||
|             ForgeClassMappings = new HashMap<String, String>(); | ||||
|             ForgeFieldMappings = new HashMap<String, Map<String, String>>(); | ||||
|             //ForgeMethodMappings = new HashMap<String, Map<String, String>>(); | ||||
|             ForgeMethodMappings = new HashMap<String, Map<String, Map<Class<?>[], String>>>(); | ||||
|             try { | ||||
|                 InputStream stream = Class.forName("net.minecraftforge.common.MinecraftForge").getClassLoader() | ||||
|                         .getResourceAsStream("mappings/" + getBukkitVersion() + "/cb2numpkg.srg"); | ||||
| @@ -155,21 +160,15 @@ public class ReflectionManager { | ||||
|                 ex.printStackTrace(); | ||||
|             } | ||||
|         } | ||||
|         try { | ||||
|             itemClass = getCraftClass("inventory.CraftItemStack"); | ||||
|             pingField = getNmsField("EntityPlayer", "ping"); | ||||
|         } catch (Exception e) { | ||||
|             e.printStackTrace(); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     public static Object createEntityInstance(String entityName) { | ||||
|         try { | ||||
|             Class entityClass = getNmsClass("Entity" + entityName); | ||||
|             Class<?> entityClass = getNmsClass("Entity" + entityName); | ||||
|             Object entityObject; | ||||
|             Object world = getWorld(Bukkit.getWorlds().get(0)); | ||||
|             if (entityName.equals("Player")) { | ||||
|                 Object minecraftServer = getNmsClass("MinecraftServer").getMethod("getServer").invoke(null); | ||||
|                 Object minecraftServer = getNmsMethod("MinecraftServer", "getServer").invoke(null); | ||||
|                 Object playerinteractmanager = getNmsClass("PlayerInteractManager").getConstructor(getNmsClass("World")) | ||||
|                         .newInstance(world); | ||||
|                 if (LibVersion.is1_7()) { | ||||
| @@ -243,7 +242,7 @@ public class ReflectionManager { | ||||
|  | ||||
|     public static ItemStack getBukkitItem(Object nmsItem) { | ||||
|         try { | ||||
|             return (ItemStack) itemClass.getMethod("asBukkitCopy", getNmsClass("ItemStack")).invoke(null, nmsItem); | ||||
|             return (ItemStack) craftItemClass.getMethod("asBukkitCopy", getNmsClass("ItemStack")).invoke(null, nmsItem); | ||||
|         } catch (Exception e) { | ||||
|             e.printStackTrace(); | ||||
|         } | ||||
| @@ -288,6 +287,13 @@ public class ReflectionManager { | ||||
|         return null; | ||||
|     } | ||||
|  | ||||
|     static Object getEntityTrackerEntry(Entity target) throws Exception { | ||||
|         Object world = getWorld(target.getWorld()); | ||||
|         Object tracker = trackerField.get(world); | ||||
|         Object trackedEntities = entitiesField.get(tracker); | ||||
|         return ihmGet.invoke(trackedEntities, target.getEntityId()); | ||||
|     } | ||||
|  | ||||
|     public static WrappedGameProfile getGameProfile(Player player) { | ||||
|         if (LibVersion.is1_7()) { | ||||
|             return WrappedGameProfile.fromPlayer(player); | ||||
| @@ -343,7 +349,7 @@ public class ReflectionManager { | ||||
|  | ||||
|     public static Object getNmsItem(ItemStack itemstack) { | ||||
|         try { | ||||
|             return itemClass.getMethod("asNMSCopy", ItemStack.class).invoke(null, itemstack); | ||||
|             return craftItemClass.getMethod("asNMSCopy", ItemStack.class).invoke(null, itemstack); | ||||
|         } catch (Exception e) { | ||||
|             e.printStackTrace(); | ||||
|         } | ||||
| @@ -370,10 +376,11 @@ public class ReflectionManager { | ||||
|         return getNmsMethod(getNmsClass(className), methodName, parameters); | ||||
|     } | ||||
|  | ||||
|     public static Method getNmsMethod(Class clazz, String methodName, Class<?>... parameters) { | ||||
|     public static Method getNmsMethod(Class<?> clazz, String methodName, Class<?>... parameters) { | ||||
|         try { | ||||
|             if (isForge) { | ||||
|                 return clazz.getMethod(ForgeMethodMappings.get(clazz.getName()).get(methodName), parameters); | ||||
|                 String trName = ForgeMethodMappings.get(clazz.getName()).get(methodName).get(parameters); | ||||
|                 return clazz.getMethod(trName, parameters); | ||||
|             } | ||||
|             return clazz.getMethod(methodName, parameters); | ||||
|         } catch (NoSuchMethodException e) { | ||||
| @@ -394,8 +401,8 @@ public class ReflectionManager { | ||||
|     public static float[] getSize(Entity entity) { | ||||
|         try { | ||||
|             float length = getNmsField("Entity", "length").getFloat(getNmsEntity(entity)); | ||||
|             float width = getNmsClass("Entity").getField("width").getFloat(getNmsEntity(entity)); | ||||
|             float height = getNmsClass("Entity").getField("height").getFloat(getNmsEntity(entity)); | ||||
|             float width = getNmsField("Entity", "width").getFloat(getNmsEntity(entity)); | ||||
|             float height = getNmsField("Entity", "height").getFloat(getNmsEntity(entity)); | ||||
|             return new float[] { length, width, height }; | ||||
|         } catch (Exception ex) { | ||||
|             ex.printStackTrace(); | ||||
| @@ -405,7 +412,7 @@ public class ReflectionManager { | ||||
|  | ||||
|     public static WrappedGameProfile getSkullBlob(WrappedGameProfile gameProfile) { | ||||
|         try { | ||||
|             Object minecraftServer = getNmsClass("MinecraftServer").getMethod("getServer").invoke(null); | ||||
|             Object minecraftServer = getNmsMethod("MinecraftServer", "getServer").invoke(null); | ||||
|             for (Method method : getNmsClass("MinecraftServer").getMethods()) { | ||||
|                 if (method.getReturnType().getSimpleName().equals("MinecraftSessionService")) { | ||||
|                     Object session = method.invoke(minecraftServer); | ||||
| @@ -467,8 +474,8 @@ public class ReflectionManager { | ||||
|     public static void setAllowSleep(Player player) { | ||||
|         try { | ||||
|             Object nmsEntity = getNmsEntity(player); | ||||
|             Object connection = nmsEntity.getClass().getField("playerConnection").get(nmsEntity); | ||||
|             Field check = connection.getClass().getField("checkMovement"); | ||||
|             Object connection = getNmsField(nmsEntity.getClass(), "playerConnection").get(nmsEntity); | ||||
|             Field check = getNmsField(connection.getClass(), "checkMovement"); | ||||
|             check.setBoolean(connection, true); | ||||
|         } catch (Exception ex) { | ||||
|             ex.printStackTrace(); | ||||
| @@ -477,7 +484,7 @@ public class ReflectionManager { | ||||
|  | ||||
|     public static void setBoundingBox(Entity entity, FakeBoundingBox newBox) { | ||||
|         try { | ||||
|             Object boundingBox = getNmsClass("Entity").getField("boundingBox").get(getNmsEntity(entity)); | ||||
|             Object boundingBox = getNmsField("Entity", "boundingBox").get(getNmsEntity(entity)); | ||||
|             int stage = 0; | ||||
|             Location loc = entity.getLocation(); | ||||
|             for (Field field : boundingBox.getClass().getFields()) { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user