More getNmsField/Method calls

This commit is contained in:
riking 2014-06-14 16:06:02 -07:00
parent 2566d8824e
commit 48056c735f
3 changed files with 63 additions and 70 deletions

View File

@ -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 * Sends entity removal packets, as this disguise was removed
*/ */
public static void destroyEntity(TargetedDisguise disguise) { public static void destroyEntity(TargetedDisguise disguise) {
try { try {
Object entityTrackerEntry = getEntityTrackerEntry(disguise.getEntity()); Object entityTrackerEntry = ReflectionManager.getEntityTrackerEntry(disguise.getEntity());
if (entityTrackerEntry != null) { if (entityTrackerEntry != null) {
HashSet trackedPlayers = (HashSet) entityTrackerEntry.getClass().getField("trackedPlayers") HashSet trackedPlayers = (HashSet) ReflectionManager.getNmsField("EntityTrackerEntry", "trackedPlayers")
.get(entityTrackerEntry); .get(entityTrackerEntry);
HashSet cloned = (HashSet) trackedPlayers.clone(); HashSet cloned = (HashSet) trackedPlayers.clone();
PacketContainer destroyPacket = new PacketContainer(PacketType.Play.Server.ENTITY_DESTROY); PacketContainer destroyPacket = new PacketContainer(PacketType.Play.Server.ENTITY_DESTROY);
@ -331,9 +321,9 @@ public class DisguiseUtilities {
public static ArrayList<Player> getPerverts(Disguise disguise) { public static ArrayList<Player> getPerverts(Disguise disguise) {
ArrayList<Player> players = new ArrayList<Player>(); ArrayList<Player> players = new ArrayList<Player>();
try { try {
Object entityTrackerEntry = getEntityTrackerEntry(disguise.getEntity()); Object entityTrackerEntry = ReflectionManager.getEntityTrackerEntry(disguise.getEntity());
if (entityTrackerEntry != null) { if (entityTrackerEntry != null) {
HashSet trackedPlayers = (HashSet) ReflectionManager.getNmsField(entityTrackerEntry.getClass(), "trackedPlayers") HashSet trackedPlayers = (HashSet) ReflectionManager.getNmsField("EntityTrackerEntry", "trackedPlayers")
.get(entityTrackerEntry); .get(entityTrackerEntry);
for (Object p : trackedPlayers) { for (Object p : trackedPlayers) {
Player player = (Player) ReflectionManager.getBukkitEntity(p); Player player = (Player) ReflectionManager.getBukkitEntity(p);
@ -468,13 +458,13 @@ public class DisguiseUtilities {
public static void refreshTracker(TargetedDisguise disguise, String player) { public static void refreshTracker(TargetedDisguise disguise, String player) {
if (disguise.getEntity() != null && disguise.getEntity().isValid()) { if (disguise.getEntity() != null && disguise.getEntity().isValid()) {
try { try {
Object entityTrackerEntry = getEntityTrackerEntry(disguise.getEntity()); Object entityTrackerEntry = ReflectionManager.getEntityTrackerEntry(disguise.getEntity());
if (entityTrackerEntry != null) { if (entityTrackerEntry != null) {
HashSet trackedPlayers = (HashSet) entityTrackerEntry.getClass().getField("trackedPlayers") HashSet trackedPlayers = (HashSet) ReflectionManager.getNmsField("EntityTrackerEntry", "trackedPlayers")
.get(entityTrackerEntry); .get(entityTrackerEntry);
Method clear = entityTrackerEntry.getClass() Method clear = ReflectionManager.getNmsMethod("EntityTrackerEntry", "clear",
.getMethod("clear", ReflectionManager.getNmsClass("EntityPlayer")); ReflectionManager.getNmsClass("EntityPlayer"));
Method updatePlayer = entityTrackerEntry.getClass().getMethod("updatePlayer", Method updatePlayer = ReflectionManager.getNmsMethod("EntityTrackerEntry", "updatePlayer",
ReflectionManager.getNmsClass("EntityPlayer")); ReflectionManager.getNmsClass("EntityPlayer"));
HashSet cloned = (HashSet) trackedPlayers.clone(); HashSet cloned = (HashSet) trackedPlayers.clone();
for (Object p : cloned) { for (Object p : cloned) {
@ -497,13 +487,13 @@ public class DisguiseUtilities {
public static void refreshTrackers(Entity entity) { public static void refreshTrackers(Entity entity) {
if (entity.isValid()) { if (entity.isValid()) {
try { try {
Object entityTrackerEntry = getEntityTrackerEntry(entity); Object entityTrackerEntry = ReflectionManager.getEntityTrackerEntry(entity);
if (entityTrackerEntry != null) { if (entityTrackerEntry != null) {
HashSet trackedPlayers = (HashSet) entityTrackerEntry.getClass().getField("trackedPlayers") HashSet trackedPlayers = (HashSet) ReflectionManager.getNmsField("EntityTrackerEntry", "trackedPlayers")
.get(entityTrackerEntry); .get(entityTrackerEntry);
Method clear = entityTrackerEntry.getClass() Method clear = ReflectionManager.getNmsMethod("EntityTrackerEntry", "clear",
.getMethod("clear", ReflectionManager.getNmsClass("EntityPlayer")); ReflectionManager.getNmsClass("EntityPlayer"));
Method updatePlayer = entityTrackerEntry.getClass().getMethod("updatePlayer", Method updatePlayer = ReflectionManager.getNmsMethod("EntityTrackerEntry", "updatePlayer",
ReflectionManager.getNmsClass("EntityPlayer")); ReflectionManager.getNmsClass("EntityPlayer"));
HashSet cloned = (HashSet) trackedPlayers.clone(); HashSet cloned = (HashSet) trackedPlayers.clone();
for (Object p : cloned) { for (Object p : cloned) {
@ -528,12 +518,13 @@ public class DisguiseUtilities {
*/ */
public static void refreshTrackers(TargetedDisguise disguise) { public static void refreshTrackers(TargetedDisguise disguise) {
try { try {
Object entityTrackerEntry = getEntityTrackerEntry(disguise.getEntity()); Object entityTrackerEntry = ReflectionManager.getEntityTrackerEntry(disguise.getEntity());
if (entityTrackerEntry != null) { if (entityTrackerEntry != null) {
HashSet trackedPlayers = (HashSet) entityTrackerEntry.getClass().getField("trackedPlayers") HashSet trackedPlayers = (HashSet) ReflectionManager.getNmsField("EntityTrackerEntry", "trackedPlayers")
.get(entityTrackerEntry); .get(entityTrackerEntry);
Method clear = entityTrackerEntry.getClass().getMethod("clear", ReflectionManager.getNmsClass("EntityPlayer")); Method clear = ReflectionManager.getNmsMethod("EntityTrackerEntry", "clear",
Method updatePlayer = entityTrackerEntry.getClass().getMethod("updatePlayer", ReflectionManager.getNmsClass("EntityPlayer"));
Method updatePlayer = ReflectionManager.getNmsMethod("EntityTrackerEntry", "updatePlayer",
ReflectionManager.getNmsClass("EntityPlayer")); ReflectionManager.getNmsClass("EntityPlayer"));
HashSet cloned = (HashSet) trackedPlayers.clone(); HashSet cloned = (HashSet) trackedPlayers.clone();
for (Object p : cloned) { for (Object p : cloned) {
@ -589,9 +580,9 @@ public class DisguiseUtilities {
selfDisguisesIds.remove(player.getUniqueId()); selfDisguisesIds.remove(player.getUniqueId());
// Get the entity tracker // Get the entity tracker
try { try {
Object entityTrackerEntry = getEntityTrackerEntry(player); Object entityTrackerEntry = ReflectionManager.getEntityTrackerEntry(player);
if (entityTrackerEntry != null) { if (entityTrackerEntry != null) {
HashSet trackedPlayers = (HashSet) entityTrackerEntry.getClass().getField("trackedPlayers") HashSet trackedPlayers = (HashSet) ReflectionManager.getNmsField("EntityTrackerEntry", "trackedPlayers")
.get(entityTrackerEntry); .get(entityTrackerEntry);
// If the tracker exists. Remove himself from his tracker // If the tracker exists. Remove himself from his tracker
trackedPlayers.remove(ReflectionManager.getNmsEntity(player)); trackedPlayers.remove(ReflectionManager.getNmsEntity(player));
@ -623,7 +614,7 @@ public class DisguiseUtilities {
if (!player.isValid() || !player.isOnline() || !disguise.isSelfDisguiseVisible()) { if (!player.isValid() || !player.isOnline() || !disguise.isSelfDisguiseVisible()) {
return; return;
} }
Object entityTrackerEntry = getEntityTrackerEntry(player); Object entityTrackerEntry = ReflectionManager.getEntityTrackerEntry(player);
if (entityTrackerEntry == null) { if (entityTrackerEntry == null) {
// A check incase the tracker is 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. // 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()); 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<?>) ReflectionManager.getNmsField("EntityTrackerEntry", "trackedPlayers").get(entityTrackerEntry)).add(ReflectionManager.getNmsEntity(player));
.getNmsEntity(player));
ProtocolManager manager = ProtocolLibrary.getProtocolManager(); ProtocolManager manager = ProtocolLibrary.getProtocolManager();
// Send the player a packet with himself being spawned // Send the player a packet with himself being spawned
manager.sendServerPacket(player, manager.createPacketConstructor(PacketType.Play.Server.NAMED_ENTITY_SPAWN, player) manager.sendServerPacket(player, manager.createPacketConstructor(PacketType.Play.Server.NAMED_ENTITY_SPAWN, player)

View File

@ -544,13 +544,11 @@ public class PacketsManager {
boolean hasInvun = false; boolean hasInvun = false;
Object nmsEntity = ReflectionManager.getNmsEntity(entity); Object nmsEntity = ReflectionManager.getNmsEntity(entity);
try { try {
Class entityClass = ReflectionManager.getNmsClass("Entity");
if (entity instanceof LivingEntity) { if (entity instanceof LivingEntity) {
hasInvun = entityClass.getField("noDamageTicks").getInt(nmsEntity) == ReflectionManager hasInvun = ReflectionManager.getNmsField("Entity", "noDamageTicks").getInt(nmsEntity) ==
.getNmsClass("EntityLiving").getField("maxNoDamageTicks") ReflectionManager.getNmsField("EntityLiving", "maxNoDamageTicks").getInt(nmsEntity);
.getInt(nmsEntity);
} else { } else {
hasInvun = (Boolean) entityClass.getMethod("isInvulnerable").invoke(nmsEntity); hasInvun = (Boolean) ReflectionManager.getNmsMethod("Entity", "isInvulnerable").invoke(nmsEntity);
} }
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
@ -581,18 +579,16 @@ public class PacketsManager {
try { try {
int typeId = soundLoc.getWorld().getBlockTypeIdAt(soundLoc.getBlockX(), int typeId = soundLoc.getWorld().getBlockTypeIdAt(soundLoc.getBlockX(),
soundLoc.getBlockY() - 1, soundLoc.getBlockZ()); soundLoc.getBlockY() - 1, soundLoc.getBlockZ());
Class blockClass = ReflectionManager.getNmsClass("Block");
Object block; Object block;
if (LibVersion.is1_7()) { if (LibVersion.is1_7()) {
block = ReflectionManager.getNmsClass("RegistryMaterials") block = ReflectionManager.getNmsMethod("RegistryMaterials", "a", int.class)
.getMethod("a", int.class) .invoke(ReflectionManager.getNmsField("Block", "REGISTRY").get(null), typeId);
.invoke(blockClass.getField("REGISTRY").get(null), typeId);
} else { } else {
block = ((Object[]) blockClass.getField("byId").get(null))[typeId]; block = ((Object[]) ReflectionManager.getNmsField("Block", "byId").get(null))[typeId];
} }
if (block != null) { if (block != null) {
Object step = blockClass.getField("stepSound").get(block); Object step = ReflectionManager.getNmsField("Block", "stepSound").get(block);
mods.write(0, step.getClass().getMethod("getStepSound").invoke(step)); mods.write(0, ReflectionManager.getNmsMethod(step.getClass(), "getStepSound").invoke(step));
} }
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();

View File

@ -10,7 +10,6 @@ import java.util.UUID;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import me.libraryaddict.disguise.disguisetypes.PlayerDisguise;
import org.bukkit.Art; import org.bukkit.Art;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
@ -23,6 +22,7 @@ import org.bukkit.inventory.ItemStack;
import com.comphenix.protocol.wrappers.WrappedGameProfile; import com.comphenix.protocol.wrappers.WrappedGameProfile;
public class ReflectionManager { public class ReflectionManager {
public enum LibVersion { public enum LibVersion {
V1_6, V1_7; V1_6, V1_7;
private static LibVersion currentVersion = LibVersion.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 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. * 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. * 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) { private static String dir2fqn(String s) {
return s.replaceAll("/", "."); return s.replaceAll("/", ".");
@ -85,7 +90,7 @@ public class ReflectionManager {
// Initialize the maps by reading the srg file // Initialize the maps by reading the srg file
ForgeClassMappings = new HashMap<String, String>(); ForgeClassMappings = new HashMap<String, String>();
ForgeFieldMappings = new HashMap<String, Map<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 { try {
InputStream stream = Class.forName("net.minecraftforge.common.MinecraftForge").getClassLoader() InputStream stream = Class.forName("net.minecraftforge.common.MinecraftForge").getClassLoader()
.getResourceAsStream("mappings/" + getBukkitVersion() + "/cb2numpkg.srg"); .getResourceAsStream("mappings/" + getBukkitVersion() + "/cb2numpkg.srg");
@ -155,21 +160,15 @@ public class ReflectionManager {
ex.printStackTrace(); ex.printStackTrace();
} }
} }
try {
itemClass = getCraftClass("inventory.CraftItemStack");
pingField = getNmsField("EntityPlayer", "ping");
} catch (Exception e) {
e.printStackTrace();
}
} }
public static Object createEntityInstance(String entityName) { public static Object createEntityInstance(String entityName) {
try { try {
Class entityClass = getNmsClass("Entity" + entityName); Class<?> entityClass = getNmsClass("Entity" + entityName);
Object entityObject; Object entityObject;
Object world = getWorld(Bukkit.getWorlds().get(0)); Object world = getWorld(Bukkit.getWorlds().get(0));
if (entityName.equals("Player")) { 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")) Object playerinteractmanager = getNmsClass("PlayerInteractManager").getConstructor(getNmsClass("World"))
.newInstance(world); .newInstance(world);
if (LibVersion.is1_7()) { if (LibVersion.is1_7()) {
@ -243,7 +242,7 @@ public class ReflectionManager {
public static ItemStack getBukkitItem(Object nmsItem) { public static ItemStack getBukkitItem(Object nmsItem) {
try { try {
return (ItemStack) itemClass.getMethod("asBukkitCopy", getNmsClass("ItemStack")).invoke(null, nmsItem); return (ItemStack) craftItemClass.getMethod("asBukkitCopy", getNmsClass("ItemStack")).invoke(null, nmsItem);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -288,6 +287,13 @@ public class ReflectionManager {
return null; 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) { public static WrappedGameProfile getGameProfile(Player player) {
if (LibVersion.is1_7()) { if (LibVersion.is1_7()) {
return WrappedGameProfile.fromPlayer(player); return WrappedGameProfile.fromPlayer(player);
@ -343,7 +349,7 @@ public class ReflectionManager {
public static Object getNmsItem(ItemStack itemstack) { public static Object getNmsItem(ItemStack itemstack) {
try { try {
return itemClass.getMethod("asNMSCopy", ItemStack.class).invoke(null, itemstack); return craftItemClass.getMethod("asNMSCopy", ItemStack.class).invoke(null, itemstack);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -370,10 +376,11 @@ public class ReflectionManager {
return getNmsMethod(getNmsClass(className), methodName, parameters); 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 { try {
if (isForge) { 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); return clazz.getMethod(methodName, parameters);
} catch (NoSuchMethodException e) { } catch (NoSuchMethodException e) {
@ -394,8 +401,8 @@ public class ReflectionManager {
public static float[] getSize(Entity entity) { public static float[] getSize(Entity entity) {
try { try {
float length = getNmsField("Entity", "length").getFloat(getNmsEntity(entity)); float length = getNmsField("Entity", "length").getFloat(getNmsEntity(entity));
float width = getNmsClass("Entity").getField("width").getFloat(getNmsEntity(entity)); float width = getNmsField("Entity", "width").getFloat(getNmsEntity(entity));
float height = getNmsClass("Entity").getField("height").getFloat(getNmsEntity(entity)); float height = getNmsField("Entity", "height").getFloat(getNmsEntity(entity));
return new float[] { length, width, height }; return new float[] { length, width, height };
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
@ -405,7 +412,7 @@ public class ReflectionManager {
public static WrappedGameProfile getSkullBlob(WrappedGameProfile gameProfile) { public static WrappedGameProfile getSkullBlob(WrappedGameProfile gameProfile) {
try { try {
Object minecraftServer = getNmsClass("MinecraftServer").getMethod("getServer").invoke(null); Object minecraftServer = getNmsMethod("MinecraftServer", "getServer").invoke(null);
for (Method method : getNmsClass("MinecraftServer").getMethods()) { for (Method method : getNmsClass("MinecraftServer").getMethods()) {
if (method.getReturnType().getSimpleName().equals("MinecraftSessionService")) { if (method.getReturnType().getSimpleName().equals("MinecraftSessionService")) {
Object session = method.invoke(minecraftServer); Object session = method.invoke(minecraftServer);
@ -467,8 +474,8 @@ public class ReflectionManager {
public static void setAllowSleep(Player player) { public static void setAllowSleep(Player player) {
try { try {
Object nmsEntity = getNmsEntity(player); Object nmsEntity = getNmsEntity(player);
Object connection = nmsEntity.getClass().getField("playerConnection").get(nmsEntity); Object connection = getNmsField(nmsEntity.getClass(), "playerConnection").get(nmsEntity);
Field check = connection.getClass().getField("checkMovement"); Field check = getNmsField(connection.getClass(), "checkMovement");
check.setBoolean(connection, true); check.setBoolean(connection, true);
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
@ -477,7 +484,7 @@ public class ReflectionManager {
public static void setBoundingBox(Entity entity, FakeBoundingBox newBox) { public static void setBoundingBox(Entity entity, FakeBoundingBox newBox) {
try { try {
Object boundingBox = getNmsClass("Entity").getField("boundingBox").get(getNmsEntity(entity)); Object boundingBox = getNmsField("Entity", "boundingBox").get(getNmsEntity(entity));
int stage = 0; int stage = 0;
Location loc = entity.getLocation(); Location loc = entity.getLocation();
for (Field field : boundingBox.getClass().getFields()) { for (Field field : boundingBox.getClass().getFields()) {