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

333 lines
12 KiB
Java
Raw Normal View History

package me.libraryaddict.disguise.utilities;
2013-11-18 04:24:25 +01:00
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;
import java.util.UUID;
2013-11-18 04:24:25 +01:00
2013-11-18 12:49:04 +01:00
import org.bukkit.Art;
2013-11-18 04:24:25 +01:00
import org.bukkit.Bukkit;
import org.bukkit.Location;
2013-11-18 12:49:04 +01:00
import org.bukkit.Sound;
import org.bukkit.World;
import org.bukkit.entity.Entity;
2014-01-05 01:13:49 +01:00
import org.bukkit.entity.Player;
2013-11-18 04:24:25 +01:00
import org.bukkit.inventory.ItemStack;
public class ReflectionManager {
public enum LibVersion {
V1_6, V1_7;
private static LibVersion currentVersion;
static {
if (getBukkitVersion().startsWith("v1_")) {
try {
int version = Integer.parseInt(getBukkitVersion().split("_")[1]);
if (version == 7) {
currentVersion = LibVersion.V1_7;
} else {
if (version < 7) {
currentVersion = LibVersion.V1_6;
} else {
currentVersion = LibVersion.V1_7;
}
}
} catch (Exception ex) {
}
}
}
public static LibVersion getGameVersion() {
return currentVersion;
}
public static boolean is1_6() {
return getGameVersion() == V1_6;
}
public static boolean is1_7() {
return getGameVersion() == V1_7;
}
}
2013-11-18 04:24:25 +01:00
private static String bukkitVersion = Bukkit.getServer().getClass().getName().split("\\.")[3];
private static Method damageAndIdleSoundMethod;
private static Class itemClass;
private static Field pingField;
2013-11-18 04:24:25 +01: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();
}
}
2013-11-18 04:24:25 +01:00
try {
itemClass = getCraftClass("inventory.CraftItemStack");
pingField = getNmsClass("EntityPlayer").getField("ping");
2013-11-18 04:24:25 +01:00
} catch (Exception e) {
e.printStackTrace();
}
2014-01-05 01:13:49 +01:00
}
2013-12-22 01:03:47 +01:00
public static Object createEntityInstance(String entityName) {
try {
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 playerinteractmanager = getNmsClass("PlayerInteractManager").getConstructor(getNmsClass("World"))
.newInstance(world);
if (LibVersion.is1_7()) {
2013-12-22 01:03:47 +01:00
Object gameProfile = getGameProfile("LibsDisguises");
entityObject = entityClass.getConstructor(getNmsClass("MinecraftServer"), getNmsClass("WorldServer"),
gameProfile.getClass(), playerinteractmanager.getClass()).newInstance(minecraftServer, world,
gameProfile, playerinteractmanager);
} else {
entityObject = entityClass.getConstructor(getNmsClass("MinecraftServer"), getNmsClass("World"), String.class,
playerinteractmanager.getClass()).newInstance(minecraftServer, world, "LibsDisguises",
playerinteractmanager);
}
} else {
entityObject = entityClass.getConstructor(getNmsClass("World")).newInstance(world);
}
return entityObject;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static FakeBoundingBox getBoundingBox(Entity entity) {
try {
2013-12-22 04:58:49 +01:00
Object boundingBox = getNmsClass("Entity").getField("boundingBox").get(getNmsEntity(entity));
double x = 0, y = 0, z = 0;
int stage = 0;
for (Field field : boundingBox.getClass().getFields()) {
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??");
}
}
}
return new FakeBoundingBox(x, y, z);
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
2013-11-27 04:55:03 +01:00
public static Entity getBukkitEntity(Object nmsEntity) {
try {
Entity bukkitEntity = (Entity) ReflectionManager.getNmsClass("Entity").getMethod("getBukkitEntity").invoke(nmsEntity);
return bukkitEntity;
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
public static ItemStack getBukkitItem(Object nmsItem) {
2013-11-18 04:24:25 +01:00
try {
return (ItemStack) itemClass.getMethod("asBukkitCopy", getNmsClass("ItemStack")).invoke(null, nmsItem);
2013-11-18 04:24:25 +01:00
} catch (Exception e) {
e.printStackTrace();
2013-11-18 04:24:25 +01:00
}
return null;
}
public static String getBukkitVersion() {
return bukkitVersion;
}
public static Class getCraftClass(String className) {
try {
return Class.forName("org.bukkit.craftbukkit." + getBukkitVersion() + "." + className);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String getCraftSound(Sound sound) {
2013-11-18 04:24:25 +01:00
try {
Class c = getCraftClass("CraftSound");
return (String) c.getMethod("getSound", Sound.class).invoke(null, sound);
2013-11-18 04:24:25 +01:00
} catch (Exception ex) {
ex.printStackTrace();
2013-11-18 04:24:25 +01:00
}
return null;
}
public static String getEnumArt(Art art) {
try {
Class craftArt = Class.forName("org.bukkit.craftbukkit." + getBukkitVersion() + ".CraftArt");
Object enumArt = craftArt.getMethod("BukkitToNotch", Art.class).invoke(null, art);
for (Field field : enumArt.getClass().getFields()) {
if (field.getType() == String.class) {
return (String) field.get(enumArt);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
public static Object getGameProfile(String playerName) {
try {
return Class.forName("net.minecraft.util.com.mojang.authlib.GameProfile").getConstructor(String.class, String.class)
2014-01-15 11:14:40 +01:00
.newInstance(UUID.randomUUID().toString(), playerName);
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
public static Class getNmsClass(String className) {
try {
return Class.forName("net.minecraft.server." + getBukkitVersion() + "." + className);
2013-11-18 04:24:25 +01:00
} catch (Exception e) {
// e.printStackTrace();
}
return null;
}
public static Object getNmsEntity(Entity entity) {
try {
return getCraftClass("entity.CraftEntity").getMethod("getHandle").invoke(entity);
} catch (Exception ex) {
ex.printStackTrace();
2013-11-18 04:24:25 +01:00
}
return null;
}
public static Object getNmsItem(ItemStack itemstack) {
try {
return itemClass.getMethod("asNMSCopy", ItemStack.class).invoke(null, itemstack);
2013-11-18 04:24:25 +01:00
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static double getPing(Player player) {
try {
return (double) pingField.getInt(ReflectionManager.getNmsEntity(player));
} catch (Exception ex) {
ex.printStackTrace();
}
return 0D;
}
2013-12-22 05:35:57 +01:00
public static float[] getSize(Entity entity) {
try {
float length = getNmsClass("Entity").getField("length").getFloat(getNmsEntity(entity));
float width = getNmsClass("Entity").getField("width").getFloat(getNmsEntity(entity));
float height = getNmsClass("Entity").getField("height").getFloat(getNmsEntity(entity));
return new float[] { length, width, height };
} catch (Exception ex) {
ex.printStackTrace();
}
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 ex) {
}
return null;
}
public static Object getWorld(World world) {
try {
return getCraftClass("CraftWorld").getMethod("getHandle").invoke(world);
2013-11-18 04:24:25 +01:00
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
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");
check.setBoolean(connection, true);
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void setBoundingBox(Entity entity, FakeBoundingBox newBox, float[] entitySize) {
2013-12-22 01:03:47 +01:00
try {
Object boundingBox = getNmsClass("Entity").getField("boundingBox").get(getNmsEntity(entity));
int stage = 0;
Location loc = entity.getLocation();
2013-12-22 01:03:47 +01:00
for (Field field : boundingBox.getClass().getFields()) {
if (field.getType().getSimpleName().equals("double")) {
2013-12-22 01:03:47 +01:00
stage++;
switch (stage) {
case 1:
field.setDouble(boundingBox, loc.getX() - newBox.getX());
2013-12-22 01:03:47 +01:00
break;
case 2:
2013-12-31 06:37:28 +01:00
// field.setDouble(boundingBox, loc.getY() - newBox.getY());
2013-12-22 01:03:47 +01:00
break;
case 3:
field.setDouble(boundingBox, loc.getZ() - newBox.getZ());
2013-12-22 01:03:47 +01:00
break;
case 4:
field.setDouble(boundingBox, loc.getX() + newBox.getX());
2013-12-22 04:23:55 +01:00
break;
2013-12-22 01:03:47 +01:00
case 5:
2013-12-31 08:08:27 +01:00
field.setDouble(boundingBox, loc.getY() + newBox.getY());
2013-12-22 04:23:55 +01:00
break;
2013-12-22 01:03:47 +01:00
case 6:
field.setDouble(boundingBox, loc.getZ() + newBox.getZ());
2013-12-22 01:03:47 +01:00
break;
default:
throw new Exception("Error while setting the bounding box, more doubles than I thought??");
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
2013-11-18 04:24:25 +01:00
}