LibsDisguises/src/main/java/me/libraryaddict/disguise/utilities/ReflectionManager.java

486 lines
19 KiB
Java
Raw Normal View History

package me.libraryaddict.disguise.utilities;
2013-11-18 04:24:25 +01:00
import com.comphenix.protocol.wrappers.WrappedGameProfile;
import com.google.common.collect.ImmutableMap;
import org.bukkit.Art;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Sound;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;
import java.lang.reflect.Constructor;
2013-11-18 12:49:04 +01:00
import java.lang.reflect.Field;
2013-11-18 04:24:25 +01:00
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
2014-06-14 23:58:49 +02:00
import java.util.Map;
import java.util.UUID;
2013-11-18 04:24:25 +01:00
public class ReflectionManager {
2014-06-15 01:06:02 +02:00
private static final String bukkitVersion = Bukkit.getServer().getClass().getName().split("\\.")[3];
2014-06-15 09:35:47 +02:00
private static final Class<?> craftItemClass;
private static Method damageAndIdleSoundMethod;
private static final Field entitiesField;
private static final Constructor<?> boundingBoxConstructor;
private static final Method setBoundingBoxMethod;
2014-06-15 09:35:47 +02:00
private static final Method ihmGet;
private static final Field pingField;
private static Map<Class<?>, String> primitiveTypes;
private static final Field trackerField;
/*
* This portion of code is originally Copyright (C) 2014-2014 Kane York.
*
* In addition to the implicit license granted to libraryaddict to redistribuite the code, the
* code is also licensed to the public under the BSD 2-clause license.
*
* The publicly licensed version may be viewed here: https://gist.github.com/riking/2f330f831c30e2276df7
*/
2013-11-18 04:24:25 +01:00
static {
primitiveTypes = ImmutableMap.<Class<?>, String>builder().put(boolean.class, "Z").put(byte.class, "B")
2014-06-15 09:35:47 +02:00
.put(char.class, "C").put(short.class, "S").put(int.class, "I").put(long.class, "J").put(float.class, "F")
.put(double.class, "D").put(void.class, "V").build();
2014-06-15 05:19:56 +02:00
}
2014-06-14 23:58:49 +02:00
2014-06-15 05:19:56 +02:00
static {
for (Method method : getNmsClass("EntityLiving").getDeclaredMethods()) {
try {
if (method.getReturnType() == float.class && Modifier.isProtected(method.getModifiers())
&& method.getParameterTypes().length == 0) {
Object entity = createEntityInstance("Cow");
method.setAccessible(true);
float value = (Float) method.invoke(entity);
if (value == 0.4F) {
damageAndIdleSoundMethod = method;
break;
}
}
} catch (Exception ex) {
ex.printStackTrace(System.out);
}
}
2014-06-15 05:19:56 +02:00
craftItemClass = getCraftClass("inventory.CraftItemStack");
pingField = getNmsField("EntityPlayer", "ping");
trackerField = getNmsField("WorldServer", "tracker");
entitiesField = getNmsField("EntityTracker", "trackedEntities");
ihmGet = getNmsMethod("IntHashMap", "get", int.class);
boundingBoxConstructor = getNmsConstructor("AxisAlignedBB", double.class, double.class, double.class,
double.class, double.class, double.class);
setBoundingBoxMethod = getNmsMethod("Entity", "a", getNmsClass("AxisAlignedBB"));
}
2013-12-22 01:03:47 +01:00
public static Object createEntityInstance(String entityName) {
try {
2014-06-15 01:06:02 +02:00
Class<?> entityClass = getNmsClass("Entity" + entityName);
2013-12-22 01:03:47 +01:00
Object entityObject;
Object world = getWorld(Bukkit.getWorlds().get(0));
switch (entityName) {
case "Player":
Object minecraftServer = getNmsMethod("MinecraftServer", "getServer").invoke(null);
Object playerinteractmanager = getNmsClass("PlayerInteractManager").getDeclaredConstructor(getNmsClass("World")).newInstance(world);
WrappedGameProfile gameProfile = getGameProfile(null, "Steve");
entityObject = entityClass.getDeclaredConstructor(getNmsClass("MinecraftServer"), getNmsClass("WorldServer"),
gameProfile.getHandleType(), playerinteractmanager.getClass()).newInstance(minecraftServer, world,
gameProfile.getHandle(), playerinteractmanager);
break;
case "EnderPearl":
entityObject = entityClass.getDeclaredConstructor(getNmsClass("World"), getNmsClass("EntityLiving")).newInstance(world, createEntityInstance("Cow"));
break;
default:
entityObject = entityClass.getDeclaredConstructor(getNmsClass("World")).newInstance(world);
break;
2013-12-22 01:03:47 +01:00
}
return entityObject;
} catch (Exception e) {
e.printStackTrace(System.out);
2013-12-22 01:03:47 +01:00
}
return null;
}
public static Object createMobEffect(int id, int duration, int amplification, boolean ambient, boolean particles) {
try {
return getNmsClass("MobEffect").getDeclaredConstructor(int.class, int.class, int.class, boolean.class, boolean.class)
.newInstance(id, duration, amplification, ambient, particles);
} catch (Exception e) {
e.printStackTrace(System.out);
}
return null;
}
public static Object createMobEffect(PotionEffect effect) {
return createMobEffect(effect.getType().getId(), effect.getDuration(), effect.getAmplifier(), effect.isAmbient(), effect.hasParticles());
}
2014-06-15 09:35:47 +02:00
private static String dir2fqn(String s) {
return s.replaceAll("/", ".");
}
public static FakeBoundingBox getBoundingBox(Entity entity) {
try {
Object boundingBox = getNmsMethod("Entity", "getBoundingBox").invoke(getNmsEntity(entity));
2013-12-22 04:58:49 +01:00
double x = 0, y = 0, z = 0;
int stage = 0;
for (Field field : boundingBox.getClass().getDeclaredFields()) {
2013-12-22 04:58:49 +01:00
if (field.getType().getSimpleName().equals("double")) {
stage++;
switch (stage) {
case 1:
x -= field.getDouble(boundingBox);
break;
case 2:
y -= field.getDouble(boundingBox);
break;
case 3:
z -= field.getDouble(boundingBox);
break;
case 4:
x += field.getDouble(boundingBox);
break;
case 5:
y += field.getDouble(boundingBox);
break;
case 6:
z += field.getDouble(boundingBox);
break;
default:
throw new Exception("Error while setting the bounding box, more doubles than I thought??");
2013-12-22 04:58:49 +01:00
}
}
}
return new FakeBoundingBox(x, y, z);
} catch (Exception ex) {
ex.printStackTrace(System.out);
}
return null;
}
2013-11-27 04:55:03 +01:00
public static Entity getBukkitEntity(Object nmsEntity) {
try {
2014-06-15 07:38:16 +02:00
return (Entity) getNmsMethod("Entity", "getBukkitEntity").invoke(nmsEntity);
2013-11-27 04:55:03 +01:00
} catch (Exception ex) {
ex.printStackTrace(System.out);
2013-11-27 04:55:03 +01:00
}
return null;
}
public static ItemStack getBukkitItem(Object nmsItem) {
2013-11-18 04:24:25 +01:00
try {
2014-06-15 01:06:02 +02:00
return (ItemStack) craftItemClass.getMethod("asBukkitCopy", getNmsClass("ItemStack")).invoke(null, nmsItem);
2013-11-18 04:24:25 +01:00
} catch (Exception e) {
e.printStackTrace(System.out);
2013-11-18 04:24:25 +01:00
}
return null;
}
public static String getBukkitVersion() {
return bukkitVersion;
}
2014-06-15 07:38:16 +02:00
public static Class<?> getCraftClass(String className) {
try {
return Class.forName("org.bukkit.craftbukkit." + getBukkitVersion() + "." + className);
} catch (Exception e) {
e.printStackTrace(System.out);
}
return null;
}
public static String getCraftSound(Sound sound) {
2013-11-18 04:24:25 +01:00
try {
2014-06-15 07:38:16 +02:00
return (String) getCraftClass("CraftSound").getMethod("getSound", Sound.class).invoke(null, sound);
2013-11-18 04:24:25 +01:00
} catch (Exception ex) {
ex.printStackTrace(System.out);
2013-11-18 04:24:25 +01:00
}
return null;
}
2014-06-15 09:40:10 +02:00
public static Object getEntityTrackerEntry(Entity target) throws Exception {
2014-06-15 09:35:47 +02:00
Object world = getWorld(target.getWorld());
Object tracker = trackerField.get(world);
Object trackedEntities = entitiesField.get(tracker);
return ihmGet.invoke(trackedEntities, target.getEntityId());
}
public static String getEnumArt(Art art) {
try {
2014-06-15 07:38:16 +02:00
Object enumArt = getCraftClass("CraftArt").getMethod("BukkitToNotch", Art.class).invoke(null, art);
for (Field field : enumArt.getClass().getDeclaredFields()) {
if (field.getType() == String.class) {
return (String) field.get(enumArt);
}
}
} catch (Exception ex) {
ex.printStackTrace(System.out);
}
return null;
}
public static Object getBlockPosition(int x, int y, int z) {
try {
return getNmsClass("BlockPosition").getDeclaredConstructor(int.class, int.class, int.class).newInstance(x, y, z);
} catch (Exception ex) {
ex.printStackTrace(System.out);
}
return null;
}
public static Enum getEnumDirection(int direction) {
try {
return (Enum) getNmsMethod("EnumDirection", "fromType2", int.class).invoke(null, direction);
} catch (Exception ex) {
ex.printStackTrace(System.out);
}
return null;
}
public static Enum getEnumPlayerInfoAction(int action) {
try {
return (Enum) getNmsClass("PacketPlayOutPlayerInfo$EnumPlayerInfoAction").getEnumConstants()[action];
} catch (Exception ex) {
ex.printStackTrace(System.out);
}
return null;
}
public static Object getPlayerInfoData(Object playerInfoPacket, WrappedGameProfile gameProfile) {
try {
Object playerListName = getNmsClass("ChatComponentText").getDeclaredConstructor(String.class)
.newInstance(gameProfile.getName());
return getNmsClass("PacketPlayOutPlayerInfo$PlayerInfoData").getDeclaredConstructor(getNmsClass("PacketPlayOutPlayerInfo"),
gameProfile.getHandleType(), int.class, getNmsClass("WorldSettings$EnumGamemode"), getNmsClass("IChatBaseComponent"))
.newInstance(playerInfoPacket, gameProfile.getHandle(), 0,
getNmsClass("WorldSettings$EnumGamemode").getEnumConstants()[1], playerListName);
} catch (Exception ex) {
ex.printStackTrace(System.out);
}
return null;
}
public static WrappedGameProfile getGameProfile(Player player) {
return WrappedGameProfile.fromPlayer(player);
}
public static WrappedGameProfile getGameProfile(UUID uuid, String playerName) {
try {
return new WrappedGameProfile(uuid != null ? uuid : UUID.randomUUID(), playerName);
} catch (Exception ex) {
ex.printStackTrace(System.out);
}
return null;
}
public static WrappedGameProfile getGameProfileWithThisSkin(UUID uuid, String playerName, WrappedGameProfile profileWithSkin) {
try {
WrappedGameProfile gameProfile = new WrappedGameProfile(uuid != null ? uuid : UUID.randomUUID(), playerName);
gameProfile.getProperties().putAll(profileWithSkin.getProperties());
return gameProfile;
} catch (Exception ex) {
ex.printStackTrace(System.out);
}
return null;
}
public static Class getNmsClass(String className) {
2014-06-15 07:38:16 +02:00
try {
return Class.forName("net.minecraft.server." + getBukkitVersion() + "." + className);
2013-11-18 04:24:25 +01:00
} catch (Exception e) {
e.printStackTrace(System.out);
}
return null;
}
public static Constructor getNmsConstructor(Class clazz, Class<?>... parameters) {
try {
return clazz.getDeclaredConstructor(parameters);
} catch (NoSuchMethodException e) {
e.printStackTrace(System.out);
}
return null;
}
public static Constructor getNmsConstructor(String className, Class<?>... parameters) {
return getNmsConstructor(getNmsClass(className), parameters);
}
public static Object getNmsEntity(Entity entity) {
try {
return getCraftClass("entity.CraftEntity").getMethod("getHandle").invoke(entity);
} catch (Exception ex) {
ex.printStackTrace(System.out);
2013-11-18 04:24:25 +01:00
}
return null;
}
2014-06-14 23:58:49 +02:00
public static Field getNmsField(Class clazz, String fieldName) {
2014-06-15 07:38:16 +02:00
try {
return clazz.getDeclaredField(fieldName);
2014-06-14 23:58:49 +02:00
} catch (NoSuchFieldException e) {
e.printStackTrace(System.out);
2014-06-14 23:58:49 +02:00
}
return null;
}
2014-06-15 09:35:47 +02:00
public static Field getNmsField(String className, String fieldName) {
return getNmsField(getNmsClass(className), fieldName);
2014-06-14 23:58:49 +02:00
}
2014-06-15 09:35:47 +02:00
public static Object getNmsItem(ItemStack itemstack) {
try {
return craftItemClass.getDeclaredMethod("asNMSCopy", ItemStack.class).invoke(null, itemstack);
2014-06-15 09:35:47 +02:00
} catch (Exception e) {
e.printStackTrace(System.out);
2014-06-15 07:38:16 +02:00
}
2014-06-15 09:35:47 +02:00
return null;
2014-06-15 07:38:16 +02:00
}
public static Method getCraftMethod(String className, String methodName, Class<?>... parameters) {
return getCraftMethod(getCraftClass(className), methodName, parameters);
}
public static Method getCraftMethod(Class<?> clazz, String methodName, Class<?>... parameters) {
try {
return clazz.getDeclaredMethod(methodName, parameters);
} catch (NoSuchMethodException e) {
e.printStackTrace(System.out);
2014-06-15 07:38:16 +02:00
}
return null;
}
public static Method getNmsMethod(Class<?> clazz, String methodName, Class<?>... parameters) {
2014-06-14 23:58:49 +02:00
try {
return clazz.getDeclaredMethod(methodName, parameters);
2014-06-14 23:58:49 +02:00
} catch (NoSuchMethodException e) {
e.printStackTrace(System.out);
2014-06-14 23:58:49 +02:00
}
return null;
}
2014-06-15 09:35:47 +02:00
public static Method getNmsMethod(String className, String methodName, Class<?>... parameters) {
return getNmsMethod(getNmsClass(className), methodName, parameters);
}
public static double getPing(Player player) {
try {
return (double) pingField.getInt(ReflectionManager.getNmsEntity(player));
} catch (Exception ex) {
ex.printStackTrace(System.out);
}
return 0D;
}
2013-12-22 05:35:57 +01:00
public static float[] getSize(Entity entity) {
try {
2014-06-14 23:58:49 +02:00
float length = getNmsField("Entity", "length").getFloat(getNmsEntity(entity));
2014-06-15 01:06:02 +02:00
float width = getNmsField("Entity", "width").getFloat(getNmsEntity(entity));
float height = (Float) getNmsMethod("Entity", "getHeadHeight").invoke(getNmsEntity(entity));
return new float[]{length, width, height};
2013-12-22 05:35:57 +01:00
} catch (Exception ex) {
ex.printStackTrace(System.out);
2013-12-22 05:35:57 +01:00
}
return null;
}
public static WrappedGameProfile getSkullBlob(WrappedGameProfile gameProfile) {
try {
2014-06-15 01:06:02 +02:00
Object minecraftServer = getNmsMethod("MinecraftServer", "getServer").invoke(null);
for (Method method : getNmsClass("MinecraftServer").getDeclaredMethods()) {
if (method.getReturnType().getSimpleName().equals("MinecraftSessionService")) {
Object session = method.invoke(minecraftServer);
return WrappedGameProfile.fromHandle(session.getClass()
.getDeclaredMethod("fillProfileProperties", gameProfile.getHandleType(), boolean.class)
.invoke(session, gameProfile.getHandle(), true));
}
}
} catch (Exception ex) {
ex.printStackTrace(System.out);
}
return null;
}
public static Float getSoundModifier(Object entity) {
2013-11-18 04:24:25 +01:00
try {
damageAndIdleSoundMethod.setAccessible(true);
return (Float) damageAndIdleSoundMethod.invoke(entity);
} catch (Exception ignored) {
}
return null;
}
public static Object getWorld(World world) {
try {
return getCraftClass("CraftWorld").getDeclaredMethod("getHandle").invoke(world);
2013-11-18 04:24:25 +01:00
} catch (Exception e) {
e.printStackTrace(System.out);
2013-11-18 04:24:25 +01:00
}
return null;
}
public static WrappedGameProfile grabProfileAddUUID(String playername) {
try {
2014-06-15 07:38:16 +02:00
Object minecraftServer = getNmsMethod("MinecraftServer", "getServer").invoke(null);
for (Method method : getNmsClass("MinecraftServer").getDeclaredMethods()) {
if (method.getReturnType().getSimpleName().equals("GameProfileRepository")) {
Object profileRepo = method.invoke(minecraftServer);
Object agent = Class.forName("com.mojang.authlib.Agent").getDeclaredField("MINECRAFT").get(null);
LibsProfileLookupCaller callback = new LibsProfileLookupCaller();
profileRepo
.getClass()
.getDeclaredMethod("findProfilesByNames", String[].class, agent.getClass(),
Class.forName("com.mojang.authlib.ProfileLookupCallback"))
.invoke(profileRepo, new String[]{playername}, agent, callback);
if (callback.getGameProfile() != null) {
return callback.getGameProfile();
}
return getGameProfile(null, playername);
}
}
} catch (Exception ex) {
ex.printStackTrace(System.out);
}
return null;
}
2014-06-15 09:35:47 +02:00
private static String methodSignaturePart(Class<?> param) {
if (param.isArray()) {
return "[" + methodSignaturePart(param.getComponentType());
} else if (param.isPrimitive()) {
return primitiveTypes.get(param);
} else {
return "L" + param.getName().replaceAll("\\.", "/") + ";";
}
2014-09-26 06:50:57 +02:00
}
public static void removePlayer(Player player) {
}
public static void setAllowSleep(Player player) {
try {
//TODO: Fix this!
/**
* Object nmsEntity = getNmsEntity(player); Object connection = getNmsField(nmsEntity.getClass(), "playerConnection").get(nmsEntity); Field check = getNmsField(connection.getClass(), "checkMovement"); check.setBoolean(connection, true); *
*/
} catch (Exception ex) {
ex.printStackTrace(System.out);
}
}
public static void setBoundingBox(Entity entity, FakeBoundingBox newBox) {
2013-12-22 01:03:47 +01:00
try {
Location loc = entity.getLocation();
Object boundingBox = boundingBoxConstructor.newInstance(loc.getX() - newBox.getX(), loc.getY() - newBox.getY(),
loc.getZ() - newBox.getZ(), loc.getX() + newBox.getX(), loc.getY() + newBox.getY(), loc.getZ() + newBox.getZ());
setBoundingBoxMethod.invoke(getNmsEntity(entity), boundingBox);
2013-12-22 01:03:47 +01:00
} catch (Exception ex) {
ex.printStackTrace(System.out);
2013-12-22 01:03:47 +01:00
}
}
2013-11-18 04:24:25 +01:00
}