2013-11-22 21:10:20 +01:00
|
|
|
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;
|
2013-11-18 20:25:48 +01:00
|
|
|
import java.lang.reflect.Modifier;
|
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;
|
2013-11-18 12:49:04 +01:00
|
|
|
import org.bukkit.Sound;
|
2013-11-18 20:25:48 +01:00
|
|
|
import org.bukkit.World;
|
|
|
|
import org.bukkit.entity.Entity;
|
2013-11-18 04:24:25 +01:00
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
|
|
|
|
public class ReflectionManager {
|
2013-12-07 19:51:37 +01:00
|
|
|
private static boolean after17 = true;
|
2013-11-18 04:24:25 +01:00
|
|
|
private static String bukkitVersion = Bukkit.getServer().getClass().getName().split("\\.")[3];
|
|
|
|
private static Class itemClass;
|
2013-11-18 20:25:48 +01:00
|
|
|
private static Method soundMethod;
|
2013-11-18 04:24:25 +01:00
|
|
|
static {
|
2013-11-18 20:25:48 +01:00
|
|
|
for (Method method : getNmsClass("EntityLiving").getDeclaredMethods()) {
|
|
|
|
try {
|
|
|
|
if (method.getReturnType() == float.class && Modifier.isProtected(method.getModifiers())
|
|
|
|
&& method.getParameterTypes().length == 0) {
|
2013-11-18 20:39:28 +01:00
|
|
|
Object entity = createEntityInstance("Pig");
|
2013-11-18 20:25:48 +01:00
|
|
|
method.setAccessible(true);
|
|
|
|
method.invoke(entity);
|
|
|
|
Field random = getNmsClass("Entity").getDeclaredField("random");
|
|
|
|
random.setAccessible(true);
|
|
|
|
random.set(entity, null);
|
|
|
|
method.setAccessible(true);
|
|
|
|
try {
|
|
|
|
method.invoke(entity);
|
|
|
|
} catch (Exception ex) {
|
|
|
|
soundMethod = method;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Exception ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
2013-11-18 04:24:25 +01:00
|
|
|
try {
|
2013-11-18 20:25:48 +01:00
|
|
|
itemClass = getCraftClass("inventory.CraftItemStack");
|
2013-11-18 04:24:25 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2013-12-07 22:49:41 +01:00
|
|
|
if (bukkitVersion.startsWith("v1_")) {
|
2013-12-06 08:13:45 +01:00
|
|
|
try {
|
2013-12-07 22:49:41 +01:00
|
|
|
if (Integer.parseInt(bukkitVersion.split("_")[1]) < 7) {
|
2013-12-06 08:13:45 +01:00
|
|
|
after17 = false;
|
|
|
|
}
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2013-12-07 22:49:41 +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 (isAfter17()) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-12-22 00:36:06 +01:00
|
|
|
public static FakeBoundingBox getBoundingBox(Entity entity) {
|
|
|
|
try {
|
|
|
|
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()) {
|
2013-12-22 02:30:15 +01:00
|
|
|
if (field.getType().getSimpleName().equals("double")) {
|
2013-12-22 00:36:06 +01:00
|
|
|
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 02:30:15 +01:00
|
|
|
return new FakeBoundingBox(x, y, z);
|
2013-12-22 00:36:06 +01:00
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
|
2013-11-22 20:52:15 +01:00
|
|
|
public static ItemStack getBukkitItem(Object nmsItem) {
|
2013-11-18 04:24:25 +01:00
|
|
|
try {
|
2013-11-22 20:52:15 +01:00
|
|
|
return (ItemStack) itemClass.getMethod("asBukkitCopy", getNmsClass("ItemStack")).invoke(null, nmsItem);
|
2013-11-18 04:24:25 +01:00
|
|
|
} catch (Exception e) {
|
2013-11-22 20:52:15 +01:00
|
|
|
e.printStackTrace();
|
2013-11-18 04:24:25 +01:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-11-18 20:25:48 +01:00
|
|
|
public static Class getCraftClass(String className) {
|
|
|
|
try {
|
|
|
|
return Class.forName("org.bukkit.craftbukkit." + bukkitVersion + "." + className);
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-11-22 20:52:15 +01:00
|
|
|
public static String getCraftSound(Sound sound) {
|
2013-11-18 04:24:25 +01:00
|
|
|
try {
|
2013-11-22 20:52:15 +01:00
|
|
|
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) {
|
2013-11-22 20:52:15 +01:00
|
|
|
ex.printStackTrace();
|
2013-11-18 04:24:25 +01:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-11-22 20:52:15 +01:00
|
|
|
public static String getEnumArt(Art art) {
|
2013-11-18 20:25:48 +01:00
|
|
|
try {
|
2013-11-22 20:52:15 +01:00
|
|
|
Class craftArt = Class.forName("org.bukkit.craftbukkit." + bukkitVersion + ".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;
|
|
|
|
}
|
|
|
|
|
2013-12-05 09:05:58 +01:00
|
|
|
public static Object getGameProfile(String playerName) {
|
|
|
|
try {
|
|
|
|
return Class.forName("net.minecraft.util.com.mojang.authlib.GameProfile").getConstructor(String.class, String.class)
|
|
|
|
.newInstance(playerName, playerName);
|
|
|
|
} catch (Exception ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-11-22 20:52:15 +01:00
|
|
|
public static Class getNmsClass(String className) {
|
|
|
|
try {
|
|
|
|
return Class.forName("net.minecraft.server." + bukkitVersion + "." + className);
|
2013-11-18 04:24:25 +01:00
|
|
|
} catch (Exception e) {
|
2013-11-27 04:38:51 +01:00
|
|
|
// e.printStackTrace();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-11-22 20:52:15 +01:00
|
|
|
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 {
|
2013-11-18 17:50:31 +01:00
|
|
|
return itemClass.getMethod("asNMSCopy", ItemStack.class).invoke(null, itemstack);
|
2013-11-18 04:24:25 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-11-22 20:52:15 +01:00
|
|
|
public static Float getSoundModifier(Object entity) {
|
2013-11-18 04:24:25 +01:00
|
|
|
try {
|
2013-11-22 20:52:15 +01:00
|
|
|
soundMethod.setAccessible(true);
|
|
|
|
return (Float) soundMethod.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;
|
|
|
|
}
|
|
|
|
|
2013-12-07 19:51:37 +01:00
|
|
|
public static boolean isAfter17() {
|
|
|
|
return after17;
|
|
|
|
}
|
|
|
|
|
2013-12-22 02:30:15 +01:00
|
|
|
public static void setBoundingBox(Entity entity, FakeBoundingBox oldBox, FakeBoundingBox newBox) {
|
2013-12-22 01:03:47 +01:00
|
|
|
try {
|
|
|
|
Object boundingBox = getNmsClass("Entity").getField("boundingBox").get(getNmsEntity(entity));
|
|
|
|
int stage = 0;
|
|
|
|
for (Field field : boundingBox.getClass().getFields()) {
|
2013-12-22 02:30:15 +01:00
|
|
|
if (field.getType().getSimpleName().equals("double")) {
|
2013-12-22 01:03:47 +01:00
|
|
|
stage++;
|
|
|
|
switch (stage) {
|
|
|
|
case 1:
|
2013-12-22 02:30:15 +01:00
|
|
|
field.setDouble(boundingBox, field.getDouble(boundingBox) + (oldBox.getX() - newBox.getX()));
|
2013-12-22 01:03:47 +01:00
|
|
|
break;
|
|
|
|
case 2:
|
2013-12-22 02:30:15 +01:00
|
|
|
field.setDouble(boundingBox, field.getDouble(boundingBox) + (oldBox.getY() - newBox.getY()));
|
2013-12-22 01:03:47 +01:00
|
|
|
break;
|
|
|
|
case 3:
|
2013-12-22 02:30:15 +01:00
|
|
|
field.setDouble(boundingBox, field.getDouble(boundingBox) + (oldBox.getZ() - newBox.getZ()));
|
2013-12-22 01:03:47 +01:00
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
case 5:
|
|
|
|
case 6:
|
|
|
|
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
|
|
|
}
|