2013-11-22 21:10:20 +01:00
|
|
|
package me.libraryaddict.disguise.utilities;
|
2013-11-18 04:24:25 +01:00
|
|
|
|
2016-03-06 09:38:14 +01:00
|
|
|
import com.comphenix.protocol.wrappers.MinecraftKey;
|
2016-03-05 04:52:52 +01:00
|
|
|
import com.comphenix.protocol.wrappers.WrappedDataWatcher.Registry;
|
|
|
|
import com.comphenix.protocol.wrappers.WrappedDataWatcher.Serializer;
|
|
|
|
import com.comphenix.protocol.wrappers.WrappedDataWatcher.WrappedDataWatcherObject;
|
2016-01-22 06:41:45 +01:00
|
|
|
import com.comphenix.protocol.wrappers.WrappedGameProfile;
|
|
|
|
import com.google.common.collect.ImmutableMap;
|
2016-03-14 05:55:36 +01:00
|
|
|
import com.mojang.authlib.GameProfile;
|
2016-03-14 02:27:46 +01:00
|
|
|
import org.apache.commons.lang3.tuple.ImmutablePair;
|
|
|
|
import org.apache.commons.lang3.tuple.Pair;
|
2016-01-22 06:41:45 +01:00
|
|
|
import org.bukkit.Art;
|
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.Location;
|
2016-03-15 02:53:25 +01:00
|
|
|
import org.bukkit.Material;
|
2016-01-22 06:41:45 +01:00
|
|
|
import org.bukkit.Sound;
|
|
|
|
import org.bukkit.World;
|
|
|
|
import org.bukkit.entity.Entity;
|
2016-03-06 05:14:19 +01:00
|
|
|
import org.bukkit.entity.LivingEntity;
|
2016-01-22 06:41:45 +01:00
|
|
|
import org.bukkit.entity.Player;
|
2016-03-06 05:14:19 +01:00
|
|
|
import org.bukkit.inventory.EntityEquipment;
|
|
|
|
import org.bukkit.inventory.EquipmentSlot;
|
2016-01-22 06:41:45 +01:00
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
import org.bukkit.potion.PotionEffect;
|
|
|
|
|
2015-03-10 01:28:41 +01:00
|
|
|
import java.lang.reflect.Constructor;
|
2013-11-18 12:49:04 +01:00
|
|
|
import java.lang.reflect.Field;
|
2016-03-05 04:52:52 +01:00
|
|
|
import java.lang.reflect.InvocationTargetException;
|
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;
|
2014-06-14 23:58:49 +02:00
|
|
|
import java.util.Map;
|
2014-01-15 11:14:21 +01:00
|
|
|
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;
|
2015-03-10 01:28:41 +01:00
|
|
|
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;
|
2016-03-10 06:44:11 +01:00
|
|
|
public static final Field entityCountField;
|
2014-06-20 16:12:32 +02:00
|
|
|
|
2014-06-16 06:10:30 +02:00
|
|
|
/*
|
|
|
|
* 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 {
|
2015-08-03 00:39:44 +02:00
|
|
|
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 {
|
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) {
|
2014-01-18 20:39:23 +01:00
|
|
|
Object entity = createEntityInstance("Cow");
|
2013-11-18 20:25:48 +01:00
|
|
|
method.setAccessible(true);
|
2014-01-18 20:39:23 +01:00
|
|
|
float value = (Float) method.invoke(entity);
|
|
|
|
if (value == 0.4F) {
|
|
|
|
damageAndIdleSoundMethod = method;
|
2013-11-18 20:25:48 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Exception ex) {
|
2015-03-30 04:47:29 +02:00
|
|
|
ex.printStackTrace(System.out);
|
2013-11-18 20:25:48 +01:00
|
|
|
}
|
|
|
|
}
|
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);
|
2015-08-03 00:39:44 +02:00
|
|
|
boundingBoxConstructor = getNmsConstructor("AxisAlignedBB", double.class, double.class, double.class,
|
2015-03-10 01:28:41 +01:00
|
|
|
double.class, double.class, double.class);
|
|
|
|
setBoundingBoxMethod = getNmsMethod("Entity", "a", getNmsClass("AxisAlignedBB"));
|
2016-03-10 06:44:11 +01:00
|
|
|
entityCountField = getNmsField("Entity", "entityCount");
|
|
|
|
entityCountField.setAccessible(true);
|
2014-06-15 04:30:47 +02:00
|
|
|
}
|
|
|
|
|
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;
|
2016-03-14 05:55:36 +01:00
|
|
|
Object world = getWorldServer(Bukkit.getWorlds().get(0));
|
2016-01-22 06:41:45 +01:00
|
|
|
switch (entityName) {
|
|
|
|
case "Player":
|
|
|
|
Object minecraftServer = getNmsMethod("MinecraftServer", "getServer").invoke(null);
|
2016-03-05 03:02:39 +01:00
|
|
|
Object playerinteractmanager = getNmsClass("PlayerInteractManager").getDeclaredConstructor(getNmsClass("World")).newInstance(world);
|
|
|
|
WrappedGameProfile gameProfile = getGameProfile(null, "Steve");
|
|
|
|
entityObject = entityClass.getDeclaredConstructor(getNmsClass("MinecraftServer"), getNmsClass("WorldServer"),
|
2016-01-22 06:41:45 +01:00
|
|
|
gameProfile.getHandleType(), playerinteractmanager.getClass()).newInstance(minecraftServer, world,
|
|
|
|
gameProfile.getHandle(), playerinteractmanager);
|
|
|
|
break;
|
|
|
|
case "EnderPearl":
|
2016-03-05 03:02:39 +01:00
|
|
|
entityObject = entityClass.getDeclaredConstructor(getNmsClass("World"), getNmsClass("EntityLiving")).newInstance(world, createEntityInstance("Cow"));
|
2016-01-22 06:41:45 +01:00
|
|
|
break;
|
2016-03-15 02:53:25 +01:00
|
|
|
case "Potion":
|
|
|
|
entityObject = entityClass.getDeclaredConstructor(getNmsClass("World"), Double.TYPE, Double.TYPE, Double.TYPE, getNmsClass("ItemStack"))
|
|
|
|
.newInstance(world, 0d, 0d, 0d, getNmsItem(new ItemStack(Material.SPLASH_POTION)));
|
|
|
|
break;
|
2016-01-22 06:41:45 +01:00
|
|
|
default:
|
2016-03-05 03:02:39 +01:00
|
|
|
entityObject = entityClass.getDeclaredConstructor(getNmsClass("World")).newInstance(world);
|
2016-01-22 06:41:45 +01:00
|
|
|
break;
|
2013-12-22 01:03:47 +01:00
|
|
|
}
|
|
|
|
return entityObject;
|
|
|
|
} catch (Exception e) {
|
2015-03-30 04:47:29 +02:00
|
|
|
e.printStackTrace(System.out);
|
2013-12-22 01:03:47 +01:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-03-10 06:44:11 +01:00
|
|
|
public static Object getMobEffectList(int id) {
|
2016-03-14 06:36:25 +01:00
|
|
|
Method nmsMethod = getNmsMethod("MobEffectList", "fromId", Integer.TYPE);
|
2015-03-10 01:28:41 +01:00
|
|
|
try {
|
2016-03-10 06:44:11 +01:00
|
|
|
return nmsMethod.invoke(null, id);
|
|
|
|
} catch (IllegalAccessException | InvocationTargetException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
return null;
|
2015-03-10 01:28:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Object createMobEffect(PotionEffect effect) {
|
|
|
|
return createMobEffect(effect.getType().getId(), effect.getDuration(), effect.getAmplifier(), effect.isAmbient(), effect.hasParticles());
|
|
|
|
}
|
|
|
|
|
2016-03-10 06:44:11 +01:00
|
|
|
public static Object createMobEffect(int id, int duration, int amplification, boolean ambient, boolean particles) {
|
|
|
|
try {
|
|
|
|
return getNmsClass("MobEffect").getDeclaredConstructor(getNmsClass("MobEffectList"), Integer.TYPE, Integer.TYPE, Boolean.TYPE, Boolean.TYPE)
|
|
|
|
.newInstance(getMobEffectList(id), duration, amplification, ambient, particles);
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace(System.out);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-06-15 09:35:47 +02:00
|
|
|
private static String dir2fqn(String s) {
|
|
|
|
return s.replaceAll("/", ".");
|
|
|
|
}
|
|
|
|
|
2013-12-22 00:36:06 +01:00
|
|
|
public static FakeBoundingBox getBoundingBox(Entity entity) {
|
|
|
|
try {
|
2015-03-10 01:28:41 +01:00
|
|
|
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;
|
2016-03-05 03:02:39 +01:00
|
|
|
for (Field field : boundingBox.getClass().getDeclaredFields()) {
|
2013-12-22 04:58:49 +01:00
|
|
|
if (field.getType().getSimpleName().equals("double")) {
|
|
|
|
stage++;
|
|
|
|
switch (stage) {
|
2015-08-03 00:39:44 +02:00
|
|
|
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);
|
|
|
|
|
2013-12-22 00:36:06 +01:00
|
|
|
} catch (Exception ex) {
|
2015-03-30 04:47:29 +02:00
|
|
|
ex.printStackTrace(System.out);
|
2013-12-22 00:36:06 +01:00
|
|
|
}
|
|
|
|
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) {
|
2015-03-30 04:47:29 +02:00
|
|
|
ex.printStackTrace(System.out);
|
2013-11-27 04:55:03 +01:00
|
|
|
}
|
|
|
|
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 {
|
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) {
|
2015-03-30 04:47:29 +02:00
|
|
|
e.printStackTrace(System.out);
|
2013-11-18 04:24:25 +01:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-01-18 20:21:55 +01:00
|
|
|
public static String getBukkitVersion() {
|
|
|
|
return bukkitVersion;
|
|
|
|
}
|
|
|
|
|
2014-06-15 07:38:16 +02:00
|
|
|
public static Class<?> getCraftClass(String className) {
|
2013-11-18 20:25:48 +01:00
|
|
|
try {
|
2014-05-12 03:46:29 +02:00
|
|
|
return Class.forName("org.bukkit.craftbukkit." + getBukkitVersion() + "." + className);
|
2013-11-18 20:25:48 +01:00
|
|
|
} catch (Exception e) {
|
2015-03-30 04:47:29 +02:00
|
|
|
e.printStackTrace(System.out);
|
2013-11-18 20:25:48 +01:00
|
|
|
}
|
|
|
|
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 {
|
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) {
|
2015-03-30 04:47:29 +02:00
|
|
|
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 {
|
2016-03-14 05:55:36 +01:00
|
|
|
Object world = getWorldServer(target.getWorld());
|
2014-06-15 09:35:47 +02:00
|
|
|
Object tracker = trackerField.get(world);
|
|
|
|
Object trackedEntities = entitiesField.get(tracker);
|
|
|
|
return ihmGet.invoke(trackedEntities, target.getEntityId());
|
|
|
|
}
|
|
|
|
|
2016-03-14 05:55:36 +01:00
|
|
|
public static Object getMinecraftServer() {
|
|
|
|
try {
|
|
|
|
return getCraftMethod("CraftServer", "getServer").invoke(Bukkit.getServer());
|
|
|
|
} catch (IllegalAccessException | InvocationTargetException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
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 {
|
2014-06-15 07:38:16 +02:00
|
|
|
Object enumArt = getCraftClass("CraftArt").getMethod("BukkitToNotch", Art.class).invoke(null, art);
|
2016-03-05 03:02:39 +01:00
|
|
|
for (Field field : enumArt.getClass().getDeclaredFields()) {
|
2013-11-22 20:52:15 +01:00
|
|
|
if (field.getType() == String.class) {
|
|
|
|
return (String) field.get(enumArt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Exception ex) {
|
2015-03-30 04:47:29 +02:00
|
|
|
ex.printStackTrace(System.out);
|
2013-11-22 20:52:15 +01:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-03-10 01:28:41 +01:00
|
|
|
public static Object getBlockPosition(int x, int y, int z) {
|
|
|
|
try {
|
2016-03-05 03:02:39 +01:00
|
|
|
return getNmsClass("BlockPosition").getDeclaredConstructor(int.class, int.class, int.class).newInstance(x, y, z);
|
2015-03-10 01:28:41 +01:00
|
|
|
} catch (Exception ex) {
|
2015-03-30 04:47:29 +02:00
|
|
|
ex.printStackTrace(System.out);
|
2015-03-10 01:28:41 +01:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Enum getEnumDirection(int direction) {
|
|
|
|
try {
|
|
|
|
return (Enum) getNmsMethod("EnumDirection", "fromType2", int.class).invoke(null, direction);
|
|
|
|
} catch (Exception ex) {
|
2015-03-30 04:47:29 +02:00
|
|
|
ex.printStackTrace(System.out);
|
2015-03-10 01:28:41 +01:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Enum getEnumPlayerInfoAction(int action) {
|
|
|
|
try {
|
|
|
|
return (Enum) getNmsClass("PacketPlayOutPlayerInfo$EnumPlayerInfoAction").getEnumConstants()[action];
|
|
|
|
} catch (Exception ex) {
|
2015-03-30 04:47:29 +02:00
|
|
|
ex.printStackTrace(System.out);
|
2015-03-10 01:28:41 +01:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Object getPlayerInfoData(Object playerInfoPacket, WrappedGameProfile gameProfile) {
|
|
|
|
try {
|
2016-03-05 03:02:39 +01:00
|
|
|
Object playerListName = getNmsClass("ChatComponentText").getDeclaredConstructor(String.class)
|
2015-03-10 01:28:41 +01:00
|
|
|
.newInstance(gameProfile.getName());
|
2016-03-05 03:02:39 +01:00
|
|
|
return getNmsClass("PacketPlayOutPlayerInfo$PlayerInfoData").getDeclaredConstructor(getNmsClass("PacketPlayOutPlayerInfo"),
|
2015-03-10 01:28:41 +01:00
|
|
|
gameProfile.getHandleType(), int.class, getNmsClass("WorldSettings$EnumGamemode"), getNmsClass("IChatBaseComponent"))
|
|
|
|
.newInstance(playerInfoPacket, gameProfile.getHandle(), 0,
|
|
|
|
getNmsClass("WorldSettings$EnumGamemode").getEnumConstants()[1], playerListName);
|
|
|
|
} catch (Exception ex) {
|
2015-03-30 04:47:29 +02:00
|
|
|
ex.printStackTrace(System.out);
|
2014-04-12 07:07:27 +02:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-08-03 00:39:44 +02:00
|
|
|
public static WrappedGameProfile getGameProfile(Player player) {
|
2015-03-10 01:28:41 +01:00
|
|
|
return WrappedGameProfile.fromPlayer(player);
|
|
|
|
}
|
|
|
|
|
2014-06-04 22:40:15 +02:00
|
|
|
public static WrappedGameProfile getGameProfile(UUID uuid, String playerName) {
|
2013-12-05 09:05:58 +01:00
|
|
|
try {
|
2014-06-04 22:40:15 +02:00
|
|
|
return new WrappedGameProfile(uuid != null ? uuid : UUID.randomUUID(), playerName);
|
2013-12-05 09:05:58 +01:00
|
|
|
} catch (Exception ex) {
|
2015-03-30 04:47:29 +02:00
|
|
|
ex.printStackTrace(System.out);
|
2013-12-05 09:05:58 +01:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-06-04 22:40:15 +02:00
|
|
|
public static WrappedGameProfile getGameProfileWithThisSkin(UUID uuid, String playerName, WrappedGameProfile profileWithSkin) {
|
2014-05-31 20:47:05 +02:00
|
|
|
try {
|
2014-06-04 22:40:15 +02:00
|
|
|
WrappedGameProfile gameProfile = new WrappedGameProfile(uuid != null ? uuid : UUID.randomUUID(), playerName);
|
2015-03-10 01:28:41 +01:00
|
|
|
gameProfile.getProperties().putAll(profileWithSkin.getProperties());
|
2014-05-31 20:47:05 +02:00
|
|
|
return gameProfile;
|
|
|
|
} catch (Exception ex) {
|
2015-03-30 04:47:29 +02:00
|
|
|
ex.printStackTrace(System.out);
|
2014-05-31 20:47:05 +02:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-11-22 20:52:15 +01:00
|
|
|
public static Class getNmsClass(String className) {
|
2014-06-15 07:38:16 +02:00
|
|
|
try {
|
2014-05-12 03:46:29 +02:00
|
|
|
return Class.forName("net.minecraft.server." + getBukkitVersion() + "." + className);
|
2013-11-18 04:24:25 +01:00
|
|
|
} catch (Exception e) {
|
2015-03-30 04:47:29 +02:00
|
|
|
e.printStackTrace(System.out);
|
2013-11-27 04:38:51 +01:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-03-10 01:28:41 +01:00
|
|
|
public static Constructor getNmsConstructor(Class clazz, Class<?>... parameters) {
|
|
|
|
try {
|
2016-03-12 21:19:58 +01:00
|
|
|
Constructor declaredConstructor = clazz.getDeclaredConstructor(parameters);
|
|
|
|
declaredConstructor.setAccessible(true);
|
|
|
|
return declaredConstructor;
|
2015-03-10 01:28:41 +01:00
|
|
|
} catch (NoSuchMethodException e) {
|
2015-03-30 04:47:29 +02:00
|
|
|
e.printStackTrace(System.out);
|
2015-03-10 01:28:41 +01:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Constructor getNmsConstructor(String className, Class<?>... parameters) {
|
|
|
|
return getNmsConstructor(getNmsClass(className), parameters);
|
|
|
|
}
|
|
|
|
|
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) {
|
2015-03-30 04:47:29 +02:00
|
|
|
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 {
|
2016-03-12 21:19:58 +01:00
|
|
|
Field declaredField = clazz.getDeclaredField(fieldName);
|
|
|
|
declaredField.setAccessible(true);
|
|
|
|
return declaredField;
|
2014-06-14 23:58:49 +02:00
|
|
|
} catch (NoSuchFieldException e) {
|
2015-03-30 04:47:29 +02:00
|
|
|
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 {
|
2016-03-05 03:02:39 +01:00
|
|
|
return craftItemClass.getDeclaredMethod("asNMSCopy", ItemStack.class).invoke(null, itemstack);
|
2014-06-15 09:35:47 +02:00
|
|
|
} catch (Exception e) {
|
2015-03-30 04:47:29 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-03-05 03:02:39 +01: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 {
|
2016-03-12 21:19:58 +01:00
|
|
|
Method declaredMethod = clazz.getDeclaredMethod(methodName, parameters);
|
|
|
|
declaredMethod.setAccessible(true);
|
|
|
|
return declaredMethod;
|
2016-03-05 03:02:39 +01:00
|
|
|
} catch (NoSuchMethodException e) {
|
|
|
|
e.printStackTrace(System.out);
|
2014-06-15 07:38:16 +02:00
|
|
|
}
|
2016-03-05 03:02:39 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Method getNmsMethod(Class<?> clazz, String methodName, Class<?>... parameters) {
|
2014-06-14 23:58:49 +02:00
|
|
|
try {
|
2016-03-12 21:19:58 +01:00
|
|
|
Method declaredMethod = clazz.getDeclaredMethod(methodName, parameters);
|
|
|
|
declaredMethod.setAccessible(true);
|
|
|
|
return declaredMethod;
|
2014-06-14 23:58:49 +02:00
|
|
|
} catch (NoSuchMethodException e) {
|
2015-03-30 04:47:29 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-01-20 18:01:49 +01:00
|
|
|
public static double getPing(Player player) {
|
|
|
|
try {
|
|
|
|
return (double) pingField.getInt(ReflectionManager.getNmsEntity(player));
|
|
|
|
} catch (Exception ex) {
|
2015-03-30 04:47:29 +02:00
|
|
|
ex.printStackTrace(System.out);
|
2014-01-20 18:01:49 +01:00
|
|
|
}
|
|
|
|
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));
|
2015-03-10 01:28:41 +01:00
|
|
|
float height = (Float) getNmsMethod("Entity", "getHeadHeight").invoke(getNmsEntity(entity));
|
2015-08-03 00:39:44 +02:00
|
|
|
return new float[]{length, width, height};
|
2013-12-22 05:35:57 +01:00
|
|
|
} catch (Exception ex) {
|
2015-03-30 04:47:29 +02:00
|
|
|
ex.printStackTrace(System.out);
|
2013-12-22 05:35:57 +01:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-06-04 22:40:15 +02:00
|
|
|
public static WrappedGameProfile getSkullBlob(WrappedGameProfile gameProfile) {
|
2014-04-23 01:44:12 +02:00
|
|
|
try {
|
2014-06-15 01:06:02 +02:00
|
|
|
Object minecraftServer = getNmsMethod("MinecraftServer", "getServer").invoke(null);
|
2016-03-05 03:02:39 +01:00
|
|
|
for (Method method : getNmsClass("MinecraftServer").getDeclaredMethods()) {
|
2014-04-23 01:44:12 +02:00
|
|
|
if (method.getReturnType().getSimpleName().equals("MinecraftSessionService")) {
|
|
|
|
Object session = method.invoke(minecraftServer);
|
2014-06-04 22:40:15 +02:00
|
|
|
return WrappedGameProfile.fromHandle(session.getClass()
|
2016-03-05 03:02:39 +01:00
|
|
|
.getDeclaredMethod("fillProfileProperties", gameProfile.getHandleType(), boolean.class)
|
2014-06-04 22:40:15 +02:00
|
|
|
.invoke(session, gameProfile.getHandle(), true));
|
2014-04-23 01:44:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Exception ex) {
|
2015-03-30 04:47:29 +02:00
|
|
|
ex.printStackTrace(System.out);
|
2014-04-23 01:44:12 +02:00
|
|
|
}
|
|
|
|
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 {
|
2014-01-18 20:39:23 +01:00
|
|
|
damageAndIdleSoundMethod.setAccessible(true);
|
|
|
|
return (Float) damageAndIdleSoundMethod.invoke(entity);
|
2016-02-19 21:29:53 +01:00
|
|
|
} catch (Exception ignored) {
|
2013-11-22 20:52:15 +01:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-06-04 22:40:15 +02:00
|
|
|
public static WrappedGameProfile grabProfileAddUUID(String playername) {
|
2014-04-14 16:26:31 +02:00
|
|
|
try {
|
2014-06-15 07:38:16 +02:00
|
|
|
Object minecraftServer = getNmsMethod("MinecraftServer", "getServer").invoke(null);
|
2016-03-05 03:02:39 +01:00
|
|
|
for (Method method : getNmsClass("MinecraftServer").getDeclaredMethods()) {
|
2014-04-18 22:14:02 +02:00
|
|
|
if (method.getReturnType().getSimpleName().equals("GameProfileRepository")) {
|
|
|
|
Object profileRepo = method.invoke(minecraftServer);
|
2016-03-05 03:02:39 +01:00
|
|
|
Object agent = Class.forName("com.mojang.authlib.Agent").getDeclaredField("MINECRAFT").get(null);
|
2014-04-18 22:14:02 +02:00
|
|
|
LibsProfileLookupCaller callback = new LibsProfileLookupCaller();
|
|
|
|
profileRepo
|
|
|
|
.getClass()
|
2016-03-05 03:02:39 +01:00
|
|
|
.getDeclaredMethod("findProfilesByNames", String[].class, agent.getClass(),
|
2015-03-10 01:28:41 +01:00
|
|
|
Class.forName("com.mojang.authlib.ProfileLookupCallback"))
|
2015-08-03 00:39:44 +02:00
|
|
|
.invoke(profileRepo, new String[]{playername}, agent, callback);
|
2014-07-03 11:21:40 +02:00
|
|
|
if (callback.getGameProfile() != null) {
|
|
|
|
return callback.getGameProfile();
|
|
|
|
}
|
|
|
|
return getGameProfile(null, playername);
|
2014-04-14 16:26:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Exception ex) {
|
2015-03-30 04:47:29 +02:00
|
|
|
ex.printStackTrace(System.out);
|
2014-04-14 16:26:31 +02:00
|
|
|
}
|
|
|
|
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) {
|
2016-03-05 04:52:52 +01:00
|
|
|
//Some future remove code if needed
|
2014-04-18 22:14:02 +02:00
|
|
|
}
|
|
|
|
|
2016-03-06 05:14:19 +01:00
|
|
|
public static void setBoundingBox(Entity entity, FakeBoundingBox newBox) {
|
|
|
|
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);
|
|
|
|
} catch (Exception ex) {
|
|
|
|
ex.printStackTrace(System.out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-10 06:44:11 +01:00
|
|
|
public static Enum getSoundCategory(String category) {
|
|
|
|
Method method = getNmsMethod("SoundCategory", "a", String.class);
|
|
|
|
try {
|
|
|
|
Enum invoke = (Enum) method.invoke(null, category.toLowerCase());
|
|
|
|
if (invoke == null) {
|
|
|
|
Class<?> clazz = getNmsClass("SoundCategory");
|
|
|
|
Enum[] enums = clazz != null ? (Enum[]) clazz.getEnumConstants() : null;
|
|
|
|
for (Enum anEnum : enums != null ? enums : new Enum[0]) {
|
|
|
|
if (anEnum.name().equals("MASTER")) return anEnum;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-06 05:14:19 +01:00
|
|
|
/**
|
|
|
|
* Creates the NMS object EnumItemSlot from an EquipmentSlot.
|
2016-03-12 21:19:58 +01:00
|
|
|
*
|
2016-03-06 05:14:19 +01:00
|
|
|
* @param slot
|
|
|
|
* @return null if the equipment slot is null
|
|
|
|
*/
|
|
|
|
public static Enum createEnumItemSlot(EquipmentSlot slot) {
|
|
|
|
Class<?> clazz = getNmsClass("EnumItemSlot");
|
|
|
|
Object[] enums = clazz != null ? clazz.getEnumConstants() : null;
|
|
|
|
if (enums == null) return null;
|
|
|
|
switch (slot) {
|
|
|
|
case HAND:
|
|
|
|
return (Enum) enums[0];
|
|
|
|
case OFF_HAND:
|
|
|
|
return (Enum) enums[1];
|
|
|
|
case FEET:
|
|
|
|
return (Enum) enums[2];
|
|
|
|
case LEGS:
|
|
|
|
return (Enum) enums[3];
|
|
|
|
case CHEST:
|
|
|
|
return (Enum) enums[4];
|
|
|
|
case HEAD:
|
|
|
|
return (Enum) enums[5];
|
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the Bukkit object EquipmentSlot from an EnumItemSlot object.
|
2016-03-12 21:19:58 +01:00
|
|
|
*
|
2016-03-06 05:14:19 +01:00
|
|
|
* @return null if the object isn't an nms EnumItemSlot
|
|
|
|
*/
|
|
|
|
public static EquipmentSlot createEquipmentSlot(Object enumItemSlot) {
|
|
|
|
try {
|
|
|
|
Enum nmsSlot = (Enum) enumItemSlot;
|
|
|
|
switch (nmsSlot.name()) {
|
|
|
|
case "MAINHAND":
|
|
|
|
return EquipmentSlot.HAND;
|
|
|
|
case "OFFHAND":
|
|
|
|
return EquipmentSlot.OFF_HAND;
|
|
|
|
case "FEET":
|
|
|
|
return EquipmentSlot.FEET;
|
|
|
|
case "LEGS":
|
|
|
|
return EquipmentSlot.LEGS;
|
|
|
|
case "CHEST":
|
|
|
|
return EquipmentSlot.CHEST;
|
|
|
|
case "HEAD":
|
|
|
|
return EquipmentSlot.HAND;
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-12 21:19:58 +01:00
|
|
|
* Gets equipment from this entity based on the slot given.
|
|
|
|
*
|
2016-03-06 05:14:19 +01:00
|
|
|
* @param slot
|
|
|
|
* @return null if the disguisedEntity is not an instance of a living entity
|
|
|
|
*/
|
|
|
|
public static ItemStack getEquipment(EquipmentSlot slot, Entity disguisedEntity) {
|
|
|
|
if (!(disguisedEntity instanceof LivingEntity)) return null;
|
|
|
|
switch (slot) {
|
|
|
|
case HAND:
|
|
|
|
return ((LivingEntity) disguisedEntity).getEquipment().getItemInMainHand();
|
|
|
|
case OFF_HAND:
|
|
|
|
return ((LivingEntity) disguisedEntity).getEquipment().getItemInOffHand();
|
|
|
|
case FEET:
|
|
|
|
return ((LivingEntity) disguisedEntity).getEquipment().getBoots();
|
|
|
|
case LEGS:
|
|
|
|
return ((LivingEntity) disguisedEntity).getEquipment().getLeggings();
|
|
|
|
case CHEST:
|
|
|
|
return ((LivingEntity) disguisedEntity).getEquipment().getChestplate();
|
|
|
|
case HEAD:
|
|
|
|
return ((LivingEntity) disguisedEntity).getEquipment().getHelmet();
|
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-06 09:38:14 +01:00
|
|
|
/**
|
|
|
|
* Necessary for 1.9
|
2016-03-12 21:19:58 +01:00
|
|
|
*
|
2016-03-06 09:38:14 +01:00
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public static String convertSoundEffectToString(Object soundEffect) {
|
|
|
|
try {
|
|
|
|
Field f_getMinecraftKey = getNmsField("SoundEffect", "b");
|
|
|
|
f_getMinecraftKey.setAccessible(true);
|
|
|
|
MinecraftKey key = MinecraftKey.fromHandle(f_getMinecraftKey.get(soundEffect));
|
|
|
|
return key.getKey();
|
|
|
|
} catch (IllegalAccessException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Object getCraftSoundEffect(String sound) {
|
|
|
|
try {
|
2016-03-10 06:44:11 +01:00
|
|
|
return getCraftMethod("CraftSound", "getSoundEffect", String.class).invoke(null, sound);
|
2016-03-06 09:38:14 +01:00
|
|
|
} catch (IllegalAccessException | InvocationTargetException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-03-05 04:52:52 +01:00
|
|
|
/**
|
|
|
|
* This creates a DataWatcherItem usable with WrappedWatchableObject
|
2016-03-12 21:19:58 +01:00
|
|
|
*
|
2016-03-05 04:52:52 +01:00
|
|
|
* @param id
|
|
|
|
* @param value
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public static Object createDataWatcherItem(int id, Object value) {
|
|
|
|
if (value == null) return null;
|
|
|
|
Serializer serializer = Registry.get(value.getClass());
|
|
|
|
WrappedDataWatcherObject watcherObject = new WrappedDataWatcherObject(id, serializer);
|
2016-03-06 05:14:19 +01:00
|
|
|
Constructor construct = getNmsConstructor("DataWatcher$Item", getNmsClass("DataWatcherObject"), Object.class);
|
2016-03-05 04:52:52 +01:00
|
|
|
try {
|
|
|
|
return construct.newInstance(watcherObject.getHandle(), value);
|
|
|
|
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-03-06 05:14:19 +01:00
|
|
|
public static EntityEquipment createEntityEquipment(Entity entity) {
|
|
|
|
if (!(entity instanceof LivingEntity)) return null;
|
|
|
|
Constructor construct = getNmsConstructor("CraftEntityEquipment", getNmsClass("CraftLivingEntity"));
|
2013-12-22 01:03:47 +01:00
|
|
|
try {
|
2016-03-06 05:14:19 +01:00
|
|
|
return (EntityEquipment) construct.newInstance((LivingEntity) entity);
|
|
|
|
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | ClassCastException e) {
|
|
|
|
e.printStackTrace();
|
2013-12-22 01:03:47 +01:00
|
|
|
}
|
2016-03-06 05:14:19 +01:00
|
|
|
return null;
|
2013-12-22 01:03:47 +01:00
|
|
|
}
|
2016-03-14 02:27:46 +01:00
|
|
|
|
|
|
|
public static int getCombinedId(int id, int data) {
|
|
|
|
return id + (data << 12);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Pair<Integer, Integer> getFromCombinedId(int combinedId) {
|
|
|
|
int j = combinedId & 4095;
|
|
|
|
int k = combinedId >> 12 & 15;
|
|
|
|
return new ImmutablePair<>(j, k);
|
|
|
|
}
|
2016-03-14 05:55:36 +01:00
|
|
|
|
|
|
|
public static Object getWorldServer(World w) {
|
|
|
|
try {
|
|
|
|
return getCraftMethod("CraftWorld", "getHandle").invoke(w);
|
|
|
|
} catch (IllegalAccessException | InvocationTargetException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Object getPlayerInteractManager(World w) {
|
|
|
|
Object worldServer = getWorldServer(w);
|
|
|
|
try {
|
|
|
|
return getNmsConstructor("PlayerInteractManager", getNmsClass("World")).newInstance(worldServer);
|
|
|
|
} catch (InstantiationException | InvocationTargetException | IllegalAccessException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Object createEntityPlayer(World w, WrappedGameProfile profile) {
|
|
|
|
Object entityPlayer = null;
|
|
|
|
try {
|
|
|
|
entityPlayer = getNmsConstructor("EntityPlayer",
|
|
|
|
getNmsClass("MinecraftServer"),
|
|
|
|
getNmsClass("WorldServer"),
|
|
|
|
GameProfile.class,
|
|
|
|
getNmsClass("PlayerInteractManager"))
|
|
|
|
.newInstance(getMinecraftServer(), getWorldServer(w), profile.getHandle(), getPlayerInteractManager(w));
|
|
|
|
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return entityPlayer;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-18 04:24:25 +01:00
|
|
|
}
|