2016-05-12 13:30:22 +02:00
|
|
|
package me.libraryaddict.disguise.utilities;
|
|
|
|
|
2017-06-08 17:06:58 +02:00
|
|
|
import com.comphenix.protocol.wrappers.*;
|
2016-07-08 07:59:30 +02:00
|
|
|
import com.comphenix.protocol.wrappers.EnumWrappers.Direction;
|
2016-05-12 13:30:22 +02:00
|
|
|
import com.comphenix.protocol.wrappers.WrappedDataWatcher.Registry;
|
|
|
|
import com.comphenix.protocol.wrappers.WrappedDataWatcher.Serializer;
|
|
|
|
import com.comphenix.protocol.wrappers.WrappedDataWatcher.WrappedDataWatcherObject;
|
2017-06-08 17:06:58 +02:00
|
|
|
import com.comphenix.protocol.wrappers.nbt.NbtWrapper;
|
2018-11-08 09:09:47 +01:00
|
|
|
import me.libraryaddict.disguise.DisguiseConfig;
|
2016-05-12 13:30:22 +02:00
|
|
|
import me.libraryaddict.disguise.disguisetypes.DisguiseType;
|
2018-12-16 02:47:42 +01:00
|
|
|
import org.apache.commons.io.IOUtils;
|
2017-06-08 17:06:58 +02:00
|
|
|
import org.bukkit.*;
|
2018-12-16 02:47:42 +01:00
|
|
|
import org.bukkit.configuration.InvalidConfigurationException;
|
|
|
|
import org.bukkit.configuration.file.YamlConfiguration;
|
2017-06-08 17:06:58 +02:00
|
|
|
import org.bukkit.entity.*;
|
|
|
|
import org.bukkit.inventory.EquipmentSlot;
|
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
import org.bukkit.potion.PotionEffect;
|
|
|
|
|
2018-12-16 02:47:42 +01:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
2017-06-08 17:06:58 +02:00
|
|
|
import java.lang.reflect.*;
|
2018-11-08 09:09:47 +01:00
|
|
|
import java.nio.ByteBuffer;
|
2018-08-05 10:34:02 +02:00
|
|
|
import java.util.Optional;
|
2017-06-08 17:06:58 +02:00
|
|
|
import java.util.UUID;
|
2016-05-12 13:30:22 +02:00
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
public class ReflectionManager {
|
2016-05-12 13:30:22 +02:00
|
|
|
private static final String bukkitVersion = Bukkit.getServer().getClass().getName().split("\\.")[3];
|
|
|
|
private static final Class<?> craftItemClass;
|
|
|
|
private static Method damageAndIdleSoundMethod;
|
|
|
|
private static final Field entitiesField;
|
|
|
|
private static final Constructor<?> boundingBoxConstructor;
|
|
|
|
private static final Method setBoundingBoxMethod;
|
|
|
|
private static final Method ihmGet;
|
|
|
|
private static final Field pingField;
|
|
|
|
private static final Field trackerField;
|
|
|
|
public static final Field entityCountField;
|
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
static {
|
2018-02-14 07:14:49 +01:00
|
|
|
try {
|
|
|
|
Object entity = createEntityInstance("Cow");
|
|
|
|
|
|
|
|
for (Method method : getNmsClass("EntityLiving").getDeclaredMethods()) {
|
|
|
|
if (method.getReturnType() != float.class)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!Modifier.isProtected(method.getModifiers()))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (method.getParameterTypes().length != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
method.setAccessible(true);
|
|
|
|
|
|
|
|
float value = (Float) method.invoke(entity);
|
|
|
|
|
|
|
|
if ((float) method.invoke(entity) != 0.4f)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
damageAndIdleSoundMethod = method;
|
|
|
|
break;
|
2016-05-12 13:30:22 +02:00
|
|
|
}
|
|
|
|
}
|
2018-02-14 07:14:49 +01:00
|
|
|
catch (Exception ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
2016-05-12 13:30:22 +02:00
|
|
|
|
|
|
|
craftItemClass = getCraftClass("inventory.CraftItemStack");
|
|
|
|
|
|
|
|
pingField = getNmsField("EntityPlayer", "ping");
|
|
|
|
|
|
|
|
trackerField = getNmsField("WorldServer", "tracker");
|
|
|
|
|
|
|
|
entitiesField = getNmsField("EntityTracker", "trackedEntities");
|
|
|
|
|
|
|
|
ihmGet = getNmsMethod("IntHashMap", "get", int.class);
|
|
|
|
|
2017-03-19 13:14:45 +01:00
|
|
|
boundingBoxConstructor = getNmsConstructor("AxisAlignedBB", double.class, double.class, double.class,
|
|
|
|
double.class, double.class, double.class);
|
2016-05-12 13:30:22 +02:00
|
|
|
|
|
|
|
setBoundingBoxMethod = getNmsMethod("Entity", "a", getNmsClass("AxisAlignedBB"));
|
|
|
|
|
|
|
|
entityCountField = getNmsField("Entity", "entityCount");
|
|
|
|
|
|
|
|
entityCountField.setAccessible(true);
|
|
|
|
}
|
|
|
|
|
2018-12-16 02:47:42 +01:00
|
|
|
public static YamlConfiguration getPluginYaml(ClassLoader loader) {
|
|
|
|
try (InputStream stream = loader.getResourceAsStream("plugin.yml")) {
|
|
|
|
YamlConfiguration config = new YamlConfiguration();
|
|
|
|
config.loadFromString(IOUtils.toString(stream, "UTF-8"));
|
|
|
|
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
catch (IOException | InvalidConfigurationException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-12-05 20:08:24 +01:00
|
|
|
public static int getNewEntityId() {
|
|
|
|
return getNewEntityId(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int getNewEntityId(boolean increment) {
|
|
|
|
try {
|
|
|
|
int id = entityCountField.getInt(null);
|
|
|
|
|
|
|
|
if (increment) {
|
|
|
|
entityCountField.set(null, id + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
catch (IllegalAccessException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
public static Object createEntityInstance(String entityName) {
|
|
|
|
try {
|
2016-05-12 13:30:22 +02:00
|
|
|
Class<?> entityClass = getNmsClass("Entity" + entityName);
|
|
|
|
Object entityObject;
|
|
|
|
Object world = getWorldServer(Bukkit.getWorlds().get(0));
|
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
switch (entityName) {
|
2017-03-19 13:14:45 +01:00
|
|
|
case "Player":
|
|
|
|
Object minecraftServer = getNmsMethod("MinecraftServer", "getServer").invoke(null);
|
|
|
|
|
2017-06-23 23:21:34 +02:00
|
|
|
Object playerinteractmanager = getNmsClass("PlayerInteractManager")
|
|
|
|
.getDeclaredConstructor(getNmsClass("World")).newInstance(world);
|
2017-03-19 13:14:45 +01:00
|
|
|
|
2017-06-09 16:29:28 +02:00
|
|
|
WrappedGameProfile gameProfile = getGameProfile(new UUID(0, 0), "Steve");
|
2017-03-19 13:14:45 +01:00
|
|
|
|
2017-06-23 23:21:34 +02:00
|
|
|
entityObject = entityClass
|
|
|
|
.getDeclaredConstructor(getNmsClass("MinecraftServer"), getNmsClass("WorldServer"),
|
|
|
|
gameProfile.getHandleType(), playerinteractmanager.getClass())
|
|
|
|
.newInstance(minecraftServer, world, gameProfile.getHandle(), playerinteractmanager);
|
2017-03-19 13:14:45 +01:00
|
|
|
break;
|
|
|
|
case "EnderPearl":
|
2017-06-23 23:21:34 +02:00
|
|
|
entityObject = entityClass.getDeclaredConstructor(getNmsClass("World"), getNmsClass("EntityLiving"))
|
|
|
|
.newInstance(world, createEntityInstance("Cow"));
|
2017-03-19 13:14:45 +01:00
|
|
|
break;
|
|
|
|
case "Potion":
|
2017-06-23 23:21:34 +02:00
|
|
|
entityObject = entityClass
|
|
|
|
.getDeclaredConstructor(getNmsClass("World"), Double.TYPE, Double.TYPE, Double.TYPE,
|
|
|
|
getNmsClass("ItemStack"))
|
|
|
|
.newInstance(world, 0d, 0d, 0d, getNmsItem(new ItemStack(Material.SPLASH_POTION)));
|
2017-03-19 13:14:45 +01:00
|
|
|
break;
|
|
|
|
case "FishingHook":
|
2017-06-23 23:21:34 +02:00
|
|
|
entityObject = entityClass.getDeclaredConstructor(getNmsClass("World"), getNmsClass("EntityHuman"))
|
|
|
|
.newInstance(world, createEntityInstance("Player"));
|
2017-03-19 13:14:45 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
entityObject = entityClass.getDeclaredConstructor(getNmsClass("World")).newInstance(world);
|
|
|
|
break;
|
2016-05-12 13:30:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return entityObject;
|
|
|
|
}
|
2016-11-25 09:21:49 +01:00
|
|
|
catch (Exception e) {
|
2016-06-18 17:16:32 +02:00
|
|
|
e.printStackTrace();
|
2016-05-12 13:30:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
public static Object getMobEffectList(int id) {
|
2016-05-12 13:30:22 +02:00
|
|
|
Method nmsMethod = getNmsMethod("MobEffectList", "fromId", Integer.TYPE);
|
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
try {
|
2016-05-12 13:30:22 +02:00
|
|
|
return nmsMethod.invoke(null, id);
|
|
|
|
}
|
2016-11-25 09:21:49 +01:00
|
|
|
catch (IllegalAccessException | InvocationTargetException e) {
|
2016-05-12 13:30:22 +02:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
public static Object createMobEffect(PotionEffect effect) {
|
2017-03-19 13:14:45 +01:00
|
|
|
return createMobEffect(effect.getType().getId(), effect.getDuration(), effect.getAmplifier(),
|
|
|
|
effect.isAmbient(), effect.hasParticles());
|
2016-05-12 13:30:22 +02:00
|
|
|
}
|
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
public static Object createMobEffect(int id, int duration, int amplification, boolean ambient, boolean particles) {
|
|
|
|
try {
|
2017-06-23 23:21:34 +02:00
|
|
|
return getNmsClass("MobEffect")
|
|
|
|
.getDeclaredConstructor(getNmsClass("MobEffectList"), Integer.TYPE, Integer.TYPE, Boolean.TYPE,
|
|
|
|
Boolean.TYPE)
|
|
|
|
.newInstance(getMobEffectList(id), duration, amplification, ambient, particles);
|
2016-05-12 13:30:22 +02:00
|
|
|
}
|
2016-11-25 09:21:49 +01:00
|
|
|
catch (Exception e) {
|
2016-06-18 17:16:32 +02:00
|
|
|
e.printStackTrace();
|
2016-05-12 13:30:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
public static FakeBoundingBox getBoundingBox(Entity entity) {
|
|
|
|
try {
|
2016-05-12 13:30:22 +02:00
|
|
|
Object boundingBox = getNmsMethod("Entity", "getBoundingBox").invoke(getNmsEntity(entity));
|
|
|
|
|
|
|
|
double x = 0, y = 0, z = 0;
|
|
|
|
int stage = 0;
|
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
for (Field field : boundingBox.getClass().getDeclaredFields()) {
|
2018-10-24 00:09:16 +02:00
|
|
|
if (!field.getType().getSimpleName().equals("double")) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
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??");
|
2016-05-12 13:30:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new FakeBoundingBox(x, y, z);
|
|
|
|
}
|
2016-11-25 09:21:49 +01:00
|
|
|
catch (Exception ex) {
|
2016-06-18 17:16:32 +02:00
|
|
|
ex.printStackTrace();
|
2016-05-12 13:30:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
public static Entity getBukkitEntity(Object nmsEntity) {
|
|
|
|
try {
|
2016-05-12 13:30:22 +02:00
|
|
|
return (Entity) getNmsMethod("Entity", "getBukkitEntity").invoke(nmsEntity);
|
|
|
|
}
|
2016-11-25 09:21:49 +01:00
|
|
|
catch (Exception ex) {
|
2016-06-18 17:16:32 +02:00
|
|
|
ex.printStackTrace();
|
2016-05-12 13:30:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
public static ItemStack getBukkitItem(Object nmsItem) {
|
|
|
|
try {
|
2016-05-12 13:30:22 +02:00
|
|
|
return (ItemStack) craftItemClass.getMethod("asBukkitCopy", getNmsClass("ItemStack")).invoke(null, nmsItem);
|
|
|
|
}
|
2016-11-25 09:21:49 +01:00
|
|
|
catch (Exception e) {
|
2016-06-18 17:16:32 +02:00
|
|
|
e.printStackTrace();
|
2016-05-12 13:30:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
public static String getBukkitVersion() {
|
2016-05-12 13:30:22 +02:00
|
|
|
return bukkitVersion;
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
public static Class<?> getCraftClass(String className) {
|
|
|
|
try {
|
2016-05-12 13:30:22 +02:00
|
|
|
return Class.forName("org.bukkit.craftbukkit." + getBukkitVersion() + "." + className);
|
|
|
|
}
|
2016-11-25 09:21:49 +01:00
|
|
|
catch (Exception e) {
|
2016-06-18 17:16:32 +02:00
|
|
|
e.printStackTrace();
|
2016-05-12 13:30:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
public static Constructor getCraftConstructor(Class clazz, Class<?>... parameters) {
|
|
|
|
try {
|
2016-05-12 13:30:22 +02:00
|
|
|
Constructor declaredConstructor = clazz.getDeclaredConstructor(parameters);
|
|
|
|
declaredConstructor.setAccessible(true);
|
|
|
|
return declaredConstructor;
|
|
|
|
}
|
2016-11-25 09:21:49 +01:00
|
|
|
catch (NoSuchMethodException e) {
|
2016-06-18 17:16:32 +02:00
|
|
|
e.printStackTrace();
|
2016-05-12 13:30:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
public static Constructor getCraftConstructor(String className, Class<?>... parameters) {
|
2016-05-12 13:30:22 +02:00
|
|
|
return getCraftConstructor(getCraftClass(className), parameters);
|
|
|
|
}
|
|
|
|
|
2018-08-06 11:48:19 +02:00
|
|
|
public static Object getCraftSound(Sound sound) {
|
2016-11-25 09:21:49 +01:00
|
|
|
try {
|
2018-08-06 11:48:19 +02:00
|
|
|
return getCraftClass("CraftSound").getMethod("getSoundEffect", String.class)
|
|
|
|
.invoke(null, getSoundString(sound));
|
2016-05-12 13:30:22 +02:00
|
|
|
}
|
2016-11-25 09:21:49 +01:00
|
|
|
catch (Exception ex) {
|
2016-06-18 17:16:32 +02:00
|
|
|
ex.printStackTrace();
|
2016-05-12 13:30:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
public static Object getEntityTrackerEntry(Entity target) throws Exception {
|
2016-05-12 13:30:22 +02:00
|
|
|
Object world = getWorldServer(target.getWorld());
|
|
|
|
Object tracker = trackerField.get(world);
|
|
|
|
Object trackedEntities = entitiesField.get(tracker);
|
|
|
|
|
|
|
|
return ihmGet.invoke(trackedEntities, target.getEntityId());
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
public static Object getMinecraftServer() {
|
|
|
|
try {
|
2016-05-12 13:30:22 +02:00
|
|
|
return getCraftMethod("CraftServer", "getServer").invoke(Bukkit.getServer());
|
|
|
|
}
|
2016-11-25 09:21:49 +01:00
|
|
|
catch (IllegalAccessException | InvocationTargetException e) {
|
2016-05-12 13:30:22 +02:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
public static String getEnumArt(Art art) {
|
|
|
|
try {
|
2016-05-12 13:30:22 +02:00
|
|
|
Object enumArt = getCraftClass("CraftArt").getMethod("BukkitToNotch", Art.class).invoke(null, art);
|
2016-11-25 09:21:49 +01:00
|
|
|
for (Field field : enumArt.getClass().getDeclaredFields()) {
|
|
|
|
if (field.getType() == String.class) {
|
2016-05-12 13:30:22 +02:00
|
|
|
return (String) field.get(enumArt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-25 09:21:49 +01:00
|
|
|
catch (Exception ex) {
|
2016-06-18 17:16:32 +02:00
|
|
|
ex.printStackTrace();
|
2016-05-12 13:30:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
public static Object getBlockPosition(int x, int y, int z) {
|
|
|
|
try {
|
2017-06-23 23:21:34 +02:00
|
|
|
return getNmsClass("BlockPosition").getDeclaredConstructor(int.class, int.class, int.class)
|
|
|
|
.newInstance(x, y, z);
|
2016-05-12 13:30:22 +02:00
|
|
|
}
|
2016-11-25 09:21:49 +01:00
|
|
|
catch (Exception ex) {
|
2016-06-18 17:16:32 +02:00
|
|
|
ex.printStackTrace();
|
2016-05-12 13:30:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
public static Enum getEnumDirection(int direction) {
|
|
|
|
try {
|
2016-05-12 13:30:22 +02:00
|
|
|
return (Enum) getNmsMethod("EnumDirection", "fromType2", int.class).invoke(null, direction);
|
|
|
|
}
|
2016-11-25 09:21:49 +01:00
|
|
|
catch (Exception ex) {
|
2016-06-18 17:16:32 +02:00
|
|
|
ex.printStackTrace();
|
2016-05-12 13:30:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
public static Enum getEnumPlayerInfoAction(int action) {
|
|
|
|
try {
|
2016-05-12 13:30:22 +02:00
|
|
|
return (Enum) getNmsClass("PacketPlayOutPlayerInfo$EnumPlayerInfoAction").getEnumConstants()[action];
|
|
|
|
}
|
2016-11-25 09:21:49 +01:00
|
|
|
catch (Exception ex) {
|
2016-06-18 17:16:32 +02:00
|
|
|
ex.printStackTrace();
|
2016-05-12 13:30:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
public static Object getPlayerInfoData(Object playerInfoPacket, WrappedGameProfile gameProfile) {
|
|
|
|
try {
|
2017-06-23 23:21:34 +02:00
|
|
|
Object playerListName = getNmsClass("ChatComponentText").getDeclaredConstructor(String.class)
|
|
|
|
.newInstance(gameProfile.getName());
|
2016-05-12 13:30:22 +02:00
|
|
|
|
2017-06-23 23:21:34 +02:00
|
|
|
return getNmsClass("PacketPlayOutPlayerInfo$PlayerInfoData")
|
|
|
|
.getDeclaredConstructor(getNmsClass("PacketPlayOutPlayerInfo"), gameProfile.getHandleType(),
|
|
|
|
int.class, getNmsClass("EnumGamemode"), getNmsClass("IChatBaseComponent"))
|
|
|
|
.newInstance(playerInfoPacket, gameProfile.getHandle(), 0,
|
|
|
|
getNmsClass("EnumGamemode").getEnumConstants()[1], playerListName);
|
2016-05-12 13:30:22 +02:00
|
|
|
}
|
2016-11-25 09:21:49 +01:00
|
|
|
catch (Exception ex) {
|
2016-06-18 17:16:32 +02:00
|
|
|
ex.printStackTrace();
|
2016-05-12 13:30:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
public static WrappedGameProfile getGameProfile(Player player) {
|
2016-05-12 13:30:22 +02:00
|
|
|
return WrappedGameProfile.fromPlayer(player);
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
public static WrappedGameProfile getGameProfile(UUID uuid, String playerName) {
|
|
|
|
try {
|
2018-11-08 09:09:47 +01:00
|
|
|
return new WrappedGameProfile(uuid != null ? uuid : getRandomUUID(), playerName);
|
2016-05-12 13:30:22 +02:00
|
|
|
}
|
2016-11-25 09:21:49 +01:00
|
|
|
catch (Exception ex) {
|
2016-06-18 17:16:32 +02:00
|
|
|
ex.printStackTrace();
|
2016-05-12 13:30:22 +02:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
public static WrappedGameProfile getClonedProfile(WrappedGameProfile gameProfile) {
|
2016-06-14 11:08:53 +02:00
|
|
|
return getGameProfileWithThisSkin(null, gameProfile.getName(), gameProfile);
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
public static WrappedGameProfile getGameProfileWithThisSkin(UUID uuid, String playerName,
|
|
|
|
WrappedGameProfile profileWithSkin) {
|
|
|
|
try {
|
2018-11-08 09:09:47 +01:00
|
|
|
WrappedGameProfile gameProfile = new WrappedGameProfile(uuid != null ? uuid : getRandomUUID(), playerName);
|
2016-06-14 11:08:53 +02:00
|
|
|
|
2017-03-21 07:34:12 +01:00
|
|
|
if (profileWithSkin != null) {
|
2017-03-19 13:14:45 +01:00
|
|
|
gameProfile.getProperties().putAll(profileWithSkin.getProperties());
|
2017-03-21 07:34:12 +01:00
|
|
|
}
|
2016-06-14 11:08:53 +02:00
|
|
|
|
2016-05-12 13:30:22 +02:00
|
|
|
return gameProfile;
|
|
|
|
}
|
2016-11-25 09:21:49 +01:00
|
|
|
catch (Exception ex) {
|
2016-06-18 17:16:32 +02:00
|
|
|
ex.printStackTrace();
|
2016-05-12 13:30:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-11-08 09:09:47 +01:00
|
|
|
/**
|
|
|
|
* Used for generating a UUID with a custom version instead of the default 4. Workaround for China's NetEase servers
|
|
|
|
*/
|
|
|
|
private static UUID getRandomUUID() {
|
|
|
|
UUID uuid = UUID.randomUUID();
|
|
|
|
|
|
|
|
if (DisguiseConfig.getUUIDGeneratedVersion() == 4) {
|
|
|
|
return uuid;
|
|
|
|
}
|
|
|
|
|
|
|
|
ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
|
|
|
|
|
|
|
|
bb.putLong(uuid.getMostSignificantBits());
|
|
|
|
bb.putLong(uuid.getLeastSignificantBits());
|
|
|
|
|
|
|
|
bb.put(6, (byte) (bb.get(6) & 0x0f)); // clear version
|
|
|
|
bb.put(6, (byte) (bb.get(6) | DisguiseConfig.getUUIDGeneratedVersion())); // set to version X (Default 4)
|
|
|
|
|
|
|
|
bb.position(0);
|
|
|
|
|
|
|
|
long firstLong = bb.getLong();
|
|
|
|
long secondLong = bb.getLong();
|
|
|
|
|
|
|
|
return new UUID(firstLong, secondLong);
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
public static Class getNmsClass(String className) {
|
|
|
|
try {
|
2016-05-12 13:30:22 +02:00
|
|
|
return Class.forName("net.minecraft.server." + getBukkitVersion() + "." + className);
|
|
|
|
}
|
2016-11-25 09:21:49 +01:00
|
|
|
catch (Exception e) {
|
2016-06-18 17:16:32 +02:00
|
|
|
e.printStackTrace();
|
2016-05-12 13:30:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-08-06 11:48:19 +02:00
|
|
|
public static Class getNmsClassIgnoreErrors(String className) {
|
|
|
|
try {
|
|
|
|
return Class.forName("net.minecraft.server." + getBukkitVersion() + "." + className);
|
|
|
|
}
|
|
|
|
catch (Exception ignored) {
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
public static Constructor getNmsConstructor(Class clazz, Class<?>... parameters) {
|
|
|
|
try {
|
2016-05-12 13:30:22 +02:00
|
|
|
Constructor declaredConstructor = clazz.getDeclaredConstructor(parameters);
|
|
|
|
declaredConstructor.setAccessible(true);
|
|
|
|
return declaredConstructor;
|
|
|
|
}
|
2016-11-25 09:21:49 +01:00
|
|
|
catch (NoSuchMethodException e) {
|
2016-06-18 17:16:32 +02:00
|
|
|
e.printStackTrace();
|
2016-05-12 13:30:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
public static Constructor getNmsConstructor(String className, Class<?>... parameters) {
|
2016-05-12 13:30:22 +02:00
|
|
|
return getNmsConstructor(getNmsClass(className), parameters);
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
public static Object getNmsEntity(Entity entity) {
|
|
|
|
try {
|
2016-05-12 13:30:22 +02:00
|
|
|
return getCraftClass("entity.CraftEntity").getMethod("getHandle").invoke(entity);
|
|
|
|
}
|
2016-11-25 09:21:49 +01:00
|
|
|
catch (Exception ex) {
|
2016-06-18 17:16:32 +02:00
|
|
|
ex.printStackTrace();
|
2016-05-12 13:30:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
public static Field getNmsField(Class clazz, String fieldName) {
|
|
|
|
try {
|
2016-05-12 13:30:22 +02:00
|
|
|
Field declaredField = clazz.getDeclaredField(fieldName);
|
|
|
|
declaredField.setAccessible(true);
|
|
|
|
|
|
|
|
return declaredField;
|
|
|
|
}
|
2016-11-25 09:21:49 +01:00
|
|
|
catch (NoSuchFieldException e) {
|
2016-06-18 17:16:32 +02:00
|
|
|
e.printStackTrace();
|
2016-05-12 13:30:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
public static Field getNmsField(String className, String fieldName) {
|
2016-05-12 13:30:22 +02:00
|
|
|
return getNmsField(getNmsClass(className), fieldName);
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
public static Object getNmsItem(ItemStack itemstack) {
|
|
|
|
try {
|
2017-07-10 18:41:04 +02:00
|
|
|
return craftItemClass.getMethod("asNMSCopy", ItemStack.class).invoke(null, itemstack);
|
2016-05-12 13:30:22 +02:00
|
|
|
}
|
2016-11-25 09:21:49 +01:00
|
|
|
catch (Exception e) {
|
2016-06-18 17:16:32 +02:00
|
|
|
e.printStackTrace();
|
2016-05-12 13:30:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
public static Method getCraftMethod(String className, String methodName, Class<?>... parameters) {
|
2016-05-12 13:30:22 +02:00
|
|
|
return getCraftMethod(getCraftClass(className), methodName, parameters);
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
public static Method getCraftMethod(Class<?> clazz, String methodName, Class<?>... parameters) {
|
|
|
|
try {
|
2016-05-12 13:30:22 +02:00
|
|
|
Method declaredMethod = clazz.getDeclaredMethod(methodName, parameters);
|
|
|
|
declaredMethod.setAccessible(true);
|
|
|
|
|
|
|
|
return declaredMethod;
|
|
|
|
}
|
2016-11-25 09:21:49 +01:00
|
|
|
catch (NoSuchMethodException e) {
|
2016-06-18 17:16:32 +02:00
|
|
|
e.printStackTrace();
|
2016-05-12 13:30:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
public static Method getNmsMethod(Class<?> clazz, String methodName, Class<?>... parameters) {
|
|
|
|
try {
|
2016-05-12 13:30:22 +02:00
|
|
|
Method declaredMethod = clazz.getDeclaredMethod(methodName, parameters);
|
|
|
|
declaredMethod.setAccessible(true);
|
|
|
|
|
|
|
|
return declaredMethod;
|
|
|
|
}
|
2016-11-25 09:21:49 +01:00
|
|
|
catch (NoSuchMethodException e) {
|
2016-06-18 17:16:32 +02:00
|
|
|
e.printStackTrace();
|
2016-05-12 13:30:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
public static Method getNmsMethod(String className, String methodName, Class<?>... parameters) {
|
2016-05-12 13:30:22 +02:00
|
|
|
return getNmsMethod(getNmsClass(className), methodName, parameters);
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
public static double getPing(Player player) {
|
|
|
|
try {
|
2016-05-12 13:30:22 +02:00
|
|
|
return (double) pingField.getInt(ReflectionManager.getNmsEntity(player));
|
|
|
|
}
|
2016-11-25 09:21:49 +01:00
|
|
|
catch (Exception ex) {
|
2016-06-18 17:16:32 +02:00
|
|
|
ex.printStackTrace();
|
2016-05-12 13:30:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
public static float[] getSize(Entity entity) {
|
|
|
|
try {
|
2016-05-12 13:30:22 +02:00
|
|
|
float length = getNmsField("Entity", "length").getFloat(getNmsEntity(entity));
|
|
|
|
float width = getNmsField("Entity", "width").getFloat(getNmsEntity(entity));
|
|
|
|
|
|
|
|
float height = (Float) getNmsMethod("Entity", "getHeadHeight").invoke(getNmsEntity(entity));
|
2017-03-19 13:14:45 +01:00
|
|
|
return new float[]{length, width, height};
|
2016-05-12 13:30:22 +02:00
|
|
|
}
|
2016-11-25 09:21:49 +01:00
|
|
|
catch (Exception ex) {
|
2016-06-18 17:16:32 +02:00
|
|
|
ex.printStackTrace();
|
2016-05-12 13:30:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
public static WrappedGameProfile getSkullBlob(WrappedGameProfile gameProfile) {
|
|
|
|
try {
|
2017-06-27 21:46:24 +02:00
|
|
|
Object minecraftServer = getMinecraftServer();
|
2016-05-12 13:30:22 +02:00
|
|
|
|
2017-06-27 21:46:24 +02:00
|
|
|
for (Method method : getNmsClass("MinecraftServer").getMethods()) {
|
2016-11-25 09:21:49 +01:00
|
|
|
if (method.getReturnType().getSimpleName().equals("MinecraftSessionService")) {
|
2016-05-12 13:30:22 +02:00
|
|
|
Object session = method.invoke(minecraftServer);
|
|
|
|
|
2017-06-23 23:21:34 +02:00
|
|
|
return WrappedGameProfile.fromHandle(session.getClass()
|
|
|
|
.getDeclaredMethod("fillProfileProperties", gameProfile.getHandleType(), boolean.class)
|
|
|
|
.invoke(session, gameProfile.getHandle(), true));
|
2016-05-12 13:30:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-25 09:21:49 +01:00
|
|
|
catch (Exception ex) {
|
2016-06-18 17:16:32 +02:00
|
|
|
ex.printStackTrace();
|
2016-05-12 13:30:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
public static Float getSoundModifier(Object entity) {
|
|
|
|
try {
|
2016-05-12 13:30:22 +02:00
|
|
|
return (Float) damageAndIdleSoundMethod.invoke(entity);
|
|
|
|
}
|
2016-11-25 09:21:49 +01:00
|
|
|
catch (Exception ignored) {
|
2016-05-12 13:30:22 +02:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
public static WrappedGameProfile grabProfileAddUUID(String playername) {
|
|
|
|
try {
|
2017-06-27 21:46:24 +02:00
|
|
|
Object minecraftServer = getMinecraftServer();
|
2016-05-12 13:30:22 +02:00
|
|
|
|
2017-06-27 21:46:24 +02:00
|
|
|
for (Method method : getNmsClass("MinecraftServer").getMethods()) {
|
2016-11-25 09:21:49 +01:00
|
|
|
if (method.getReturnType().getSimpleName().equals("GameProfileRepository")) {
|
2016-05-12 13:30:22 +02:00
|
|
|
Object agent = Class.forName("com.mojang.authlib.Agent").getDeclaredField("MINECRAFT").get(null);
|
|
|
|
|
|
|
|
LibsProfileLookupCaller callback = new LibsProfileLookupCaller();
|
2017-06-27 21:46:24 +02:00
|
|
|
Object profileRepo = method.invoke(minecraftServer);
|
2016-05-12 13:30:22 +02:00
|
|
|
|
2017-06-27 21:46:24 +02:00
|
|
|
method.getReturnType().getMethod("findProfilesByNames", String[].class, agent.getClass(),
|
2017-06-23 23:21:34 +02:00
|
|
|
Class.forName("com.mojang.authlib.ProfileLookupCallback"))
|
|
|
|
.invoke(profileRepo, new String[]{playername}, agent, callback);
|
2016-05-12 13:30:22 +02:00
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
if (callback.getGameProfile() != null) {
|
2016-05-12 13:30:22 +02:00
|
|
|
return callback.getGameProfile();
|
|
|
|
}
|
|
|
|
|
|
|
|
return getGameProfile(null, playername);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-25 09:21:49 +01:00
|
|
|
catch (Exception ex) {
|
2016-06-18 17:16:32 +02:00
|
|
|
ex.printStackTrace();
|
2016-05-12 13:30:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
public static void setBoundingBox(Entity entity, FakeBoundingBox newBox) {
|
|
|
|
try {
|
2016-05-12 13:30:22 +02:00
|
|
|
Location loc = entity.getLocation();
|
|
|
|
|
2017-06-23 23:21:34 +02:00
|
|
|
Object boundingBox = boundingBoxConstructor
|
2018-10-24 00:09:16 +02:00
|
|
|
.newInstance(loc.getX() - (newBox.getX() / 2), loc.getY(), loc.getZ() - (newBox.getZ() / 2),
|
|
|
|
loc.getX() + (newBox.getX() / 2), loc.getY() + newBox.getY(),
|
|
|
|
loc.getZ() + (newBox.getZ() / 2));
|
2016-05-12 13:30:22 +02:00
|
|
|
|
|
|
|
setBoundingBoxMethod.invoke(getNmsEntity(entity), boundingBox);
|
|
|
|
}
|
2016-11-25 09:21:49 +01:00
|
|
|
catch (Exception ex) {
|
2016-06-18 17:16:32 +02:00
|
|
|
ex.printStackTrace();
|
2016-05-12 13:30:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
public static Enum getSoundCategory(String category) {
|
|
|
|
try {
|
2018-08-06 11:48:19 +02:00
|
|
|
Method method = getNmsMethod("SoundCategory", "a");
|
2016-05-12 13:30:22 +02:00
|
|
|
|
2018-08-06 11:48:19 +02:00
|
|
|
for (Enum anEnum : (Enum[]) getNmsClass("SoundCategory").getEnumConstants()) {
|
|
|
|
if (!category.equals(method.invoke(anEnum))) {
|
|
|
|
continue;
|
2016-05-12 13:30:22 +02:00
|
|
|
}
|
|
|
|
|
2018-08-06 11:48:19 +02:00
|
|
|
return anEnum;
|
|
|
|
}
|
2016-05-12 13:30:22 +02:00
|
|
|
}
|
2016-11-25 09:21:49 +01:00
|
|
|
catch (Exception e) {
|
2016-05-12 13:30:22 +02:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
public static Enum getSoundCategory(DisguiseType disguiseType) {
|
2016-05-12 13:30:22 +02:00
|
|
|
if (disguiseType == DisguiseType.PLAYER)
|
|
|
|
return getSoundCategory("player");
|
|
|
|
|
|
|
|
Class<? extends Entity> entityClass = disguiseType.getEntityType().getEntityClass();
|
|
|
|
|
|
|
|
if (Monster.class.isAssignableFrom(entityClass))
|
|
|
|
return getSoundCategory("hostile");
|
|
|
|
|
|
|
|
if (Ambient.class.isAssignableFrom(entityClass))
|
|
|
|
return getSoundCategory("ambient");
|
|
|
|
|
|
|
|
return getSoundCategory("neutral");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the NMS object EnumItemSlot from an EquipmentSlot.
|
|
|
|
*
|
|
|
|
* @param slot
|
|
|
|
* @return null if the equipment slot is null
|
|
|
|
*/
|
2016-11-25 09:21:49 +01:00
|
|
|
public static Enum createEnumItemSlot(EquipmentSlot slot) {
|
2016-05-12 13:30:22 +02:00
|
|
|
Class<?> clazz = getNmsClass("EnumItemSlot");
|
|
|
|
|
|
|
|
Object[] enums = clazz != null ? clazz.getEnumConstants() : null;
|
|
|
|
|
|
|
|
if (enums == null)
|
|
|
|
return null;
|
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
switch (slot) {
|
2017-03-19 13:14:45 +01:00
|
|
|
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;
|
2016-05-12 13:30:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the Bukkit object EquipmentSlot from an EnumItemSlot object.
|
|
|
|
*
|
|
|
|
* @return null if the object isn't an nms EnumItemSlot
|
|
|
|
*/
|
2016-11-25 09:21:49 +01:00
|
|
|
public static EquipmentSlot createEquipmentSlot(Object enumItemSlot) {
|
|
|
|
try {
|
2016-05-12 13:30:22 +02:00
|
|
|
Enum nmsSlot = (Enum) enumItemSlot;
|
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
switch (nmsSlot.name()) {
|
2017-03-19 13:14:45 +01:00
|
|
|
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.HEAD;
|
2016-05-12 13:30:22 +02:00
|
|
|
}
|
|
|
|
}
|
2016-11-25 09:21:49 +01:00
|
|
|
catch (Exception e) {
|
2016-05-12 13:30:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets equipment from this entity based on the slot given.
|
|
|
|
*
|
|
|
|
* @param slot
|
|
|
|
* @return null if the disguisedEntity is not an instance of a living entity
|
|
|
|
*/
|
2016-11-25 09:21:49 +01:00
|
|
|
public static ItemStack getEquipment(EquipmentSlot slot, Entity disguisedEntity) {
|
2016-05-12 13:30:22 +02:00
|
|
|
if (!(disguisedEntity instanceof LivingEntity))
|
|
|
|
return null;
|
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
switch (slot) {
|
2017-03-19 13:14:45 +01:00
|
|
|
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-05-12 13:30:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-06 11:48:19 +02:00
|
|
|
public static Object getSoundString(Sound sound) {
|
2016-11-25 09:21:49 +01:00
|
|
|
try {
|
2018-08-06 11:48:19 +02:00
|
|
|
return getCraftMethod("CraftSound", "getSound", Sound.class).invoke(null, sound);
|
2016-05-12 13:30:22 +02:00
|
|
|
}
|
2016-11-25 09:21:49 +01:00
|
|
|
catch (IllegalAccessException | InvocationTargetException e) {
|
2016-05-12 13:30:22 +02:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-08-05 10:34:02 +02:00
|
|
|
public static Object convertInvalidMeta(Object value) {
|
2016-11-25 09:21:49 +01:00
|
|
|
if (value instanceof Optional) {
|
2016-07-07 12:10:58 +02:00
|
|
|
Optional opt = (Optional) value;
|
|
|
|
|
|
|
|
if (!opt.isPresent())
|
|
|
|
return value;
|
|
|
|
|
|
|
|
Object val = opt.get();
|
|
|
|
|
2016-11-26 16:36:57 +01:00
|
|
|
if (val instanceof BlockPosition) {
|
2016-07-07 12:10:58 +02:00
|
|
|
BlockPosition pos = (BlockPosition) val;
|
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
try {
|
2017-06-23 23:21:34 +02:00
|
|
|
return Optional.of(getNmsConstructor("BlockPosition", int.class, int.class, int.class)
|
|
|
|
.newInstance(pos.getX(), pos.getY(), pos.getZ()));
|
2016-07-07 12:10:58 +02:00
|
|
|
}
|
2016-11-25 09:21:49 +01:00
|
|
|
catch (Exception ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
2017-03-19 13:14:45 +01:00
|
|
|
} else if (val instanceof WrappedBlockData) {
|
2016-11-25 09:21:49 +01:00
|
|
|
try {
|
|
|
|
return Optional.of(((WrappedBlockData) val).getHandle());
|
|
|
|
}
|
|
|
|
catch (Exception ex) {
|
2016-07-07 12:10:58 +02:00
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
2017-07-10 18:41:04 +02:00
|
|
|
} else if (val instanceof ItemStack) {
|
|
|
|
val = getNmsItem((ItemStack) val);
|
|
|
|
|
|
|
|
if (val == null)
|
2018-08-05 10:34:02 +02:00
|
|
|
return Optional.empty();
|
2017-07-10 18:41:04 +02:00
|
|
|
else
|
|
|
|
return Optional.of(val);
|
2018-08-05 16:22:36 +02:00
|
|
|
} else if (val instanceof WrappedChatComponent) {
|
|
|
|
return Optional.of(((WrappedChatComponent) val).getHandle());
|
2016-07-07 12:10:58 +02:00
|
|
|
}
|
2017-03-19 13:14:45 +01:00
|
|
|
} else if (value instanceof Vector3F) {
|
2016-07-08 07:59:30 +02:00
|
|
|
Vector3F angle = (Vector3F) value;
|
2016-07-07 12:10:58 +02:00
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
try {
|
2017-06-23 23:21:34 +02:00
|
|
|
return getNmsConstructor("Vector3f", float.class, float.class, float.class)
|
|
|
|
.newInstance(angle.getX(), angle.getY(), angle.getZ());
|
2016-07-07 12:10:58 +02:00
|
|
|
}
|
2016-11-25 09:21:49 +01:00
|
|
|
catch (Exception ex) {
|
2016-07-07 12:10:58 +02:00
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
2017-03-19 13:14:45 +01:00
|
|
|
} else if (value instanceof Direction) {
|
2016-11-25 09:21:49 +01:00
|
|
|
try {
|
2017-07-10 18:41:04 +02:00
|
|
|
return getNmsMethod("EnumDirection", "fromType1", int.class)
|
2017-06-23 23:21:34 +02:00
|
|
|
.invoke(null, ((Direction) value).ordinal());
|
2016-07-08 07:59:30 +02:00
|
|
|
}
|
2016-11-25 09:21:49 +01:00
|
|
|
catch (Exception ex) {
|
2016-07-08 07:59:30 +02:00
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
2017-03-19 13:14:45 +01:00
|
|
|
} else if (value instanceof BlockPosition) {
|
2016-07-08 12:13:40 +02:00
|
|
|
BlockPosition pos = (BlockPosition) value;
|
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
try {
|
2017-06-23 23:21:34 +02:00
|
|
|
return getNmsConstructor("BlockPosition", int.class, int.class, int.class)
|
|
|
|
.newInstance(pos.getX(), pos.getY(), pos.getZ());
|
2016-07-08 12:13:40 +02:00
|
|
|
}
|
2016-11-25 09:21:49 +01:00
|
|
|
catch (Exception ex) {
|
2016-07-08 12:13:40 +02:00
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
2017-03-19 13:14:45 +01:00
|
|
|
} else if (value instanceof ItemStack) {
|
2016-11-26 16:36:57 +01:00
|
|
|
return getNmsItem((ItemStack) value);
|
2018-08-05 10:34:02 +02:00
|
|
|
} else if (value instanceof Double) {
|
2017-06-02 15:51:03 +02:00
|
|
|
return ((Double) value).floatValue();
|
2018-08-05 10:34:02 +02:00
|
|
|
} else if (value instanceof NbtWrapper) {
|
2017-06-09 16:29:28 +02:00
|
|
|
return ((NbtWrapper) value).getHandle();
|
2018-09-23 01:07:54 +02:00
|
|
|
} else if (value instanceof WrappedParticle) {
|
|
|
|
return ((WrappedParticle) value).getHandle();
|
2018-08-05 10:34:02 +02:00
|
|
|
}
|
2016-07-07 12:10:58 +02:00
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2017-06-25 21:36:09 +02:00
|
|
|
public static String getMinecraftVersion() {
|
|
|
|
String version = Bukkit.getVersion();
|
|
|
|
version = version.substring(version.lastIndexOf(" ") + 1, version.length() - 1);
|
|
|
|
return version;
|
|
|
|
}
|
|
|
|
|
2018-08-05 10:34:02 +02:00
|
|
|
public static WrappedDataWatcherObject createDataWatcherObject(int id, Object value) {
|
2016-05-12 13:30:22 +02:00
|
|
|
if (value == null)
|
|
|
|
return null;
|
|
|
|
|
2018-08-05 10:34:02 +02:00
|
|
|
value = convertInvalidMeta(value);
|
2016-07-07 12:10:58 +02:00
|
|
|
|
2016-06-18 17:16:32 +02:00
|
|
|
Serializer serializer;
|
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
if (value instanceof Optional) {
|
2016-06-23 11:11:50 +02:00
|
|
|
Optional opt = (Optional) value;
|
|
|
|
|
2018-08-05 15:18:01 +02:00
|
|
|
if (opt.isPresent()) {
|
|
|
|
Object val = opt.get();
|
|
|
|
Class cl;
|
|
|
|
Class iBlockData = getNmsClass("IBlockData");
|
|
|
|
Class iChat = getNmsClass("IChatBaseComponent");
|
|
|
|
|
|
|
|
if (iBlockData.isInstance(val)) {
|
|
|
|
cl = iBlockData;
|
|
|
|
} else if (iChat.isInstance(val)) {
|
|
|
|
cl = iChat;
|
|
|
|
} else {
|
|
|
|
cl = val.getClass();
|
|
|
|
}
|
|
|
|
|
|
|
|
serializer = Registry.get(cl, true);
|
|
|
|
} else {
|
|
|
|
serializer = Registry.get(UUID.class, true);
|
|
|
|
}
|
2017-03-19 13:14:45 +01:00
|
|
|
} else {
|
2018-08-05 10:34:02 +02:00
|
|
|
serializer = Registry.get(getNmsClass("ParticleParam").isInstance(value) ? getNmsClass("ParticleParam") :
|
|
|
|
value.getClass());
|
2016-06-18 17:16:32 +02:00
|
|
|
}
|
2016-05-12 13:30:22 +02:00
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
if (serializer == null) {
|
2017-07-23 21:07:51 +02:00
|
|
|
if (value.getClass().getSimpleName().equals("NBTTagCompound"))
|
|
|
|
return null; // Handle PaperSpigot's bad coding
|
|
|
|
|
2017-12-05 20:08:24 +01:00
|
|
|
throw new IllegalArgumentException("Unable to find Serializer for " + value +
|
|
|
|
(value instanceof Optional && ((Optional) value).isPresent() ?
|
2017-05-28 00:23:15 +02:00
|
|
|
" (" + ((Optional) value).get().getClass().getName() + ")" :
|
2017-12-05 20:08:24 +01:00
|
|
|
value instanceof Optional || value == null ? "" : " " + value.getClass().getName()) +
|
|
|
|
"! Are you running " + "the latest " + "version of " + "ProtocolLib?");
|
2016-07-07 12:10:58 +02:00
|
|
|
}
|
|
|
|
|
2018-08-05 10:34:02 +02:00
|
|
|
return new WrappedDataWatcherObject(id, serializer);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This creates a DataWatcherItem usable with WrappedWatchableObject
|
|
|
|
*
|
|
|
|
* @param id
|
|
|
|
* @param value
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public static Object createDataWatcherItem(int id, Object value) {
|
|
|
|
WrappedDataWatcherObject watcherObject = createDataWatcherObject(id, value);
|
2016-05-12 13:30:22 +02:00
|
|
|
|
|
|
|
Constructor construct = getNmsConstructor("DataWatcher$Item", getNmsClass("DataWatcherObject"), Object.class);
|
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
try {
|
2018-08-05 10:34:02 +02:00
|
|
|
return construct.newInstance(watcherObject.getHandle(), convertInvalidMeta(value));
|
2016-05-12 13:30:22 +02:00
|
|
|
}
|
2016-11-25 09:21:49 +01:00
|
|
|
catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
|
2016-05-12 13:30:22 +02:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-08-29 03:05:29 +02:00
|
|
|
public static Object createMinecraftKey(String name) {
|
2018-09-03 05:23:08 +02:00
|
|
|
try {
|
2018-08-29 03:05:29 +02:00
|
|
|
return getNmsClass("MinecraftKey").getConstructor(String.class).newInstance(name);
|
2018-09-03 05:23:08 +02:00
|
|
|
}
|
|
|
|
catch (Exception ex) {
|
2018-08-29 03:05:29 +02:00
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-08-05 10:34:02 +02:00
|
|
|
public static int getEntityType(Object nmsEntity) {
|
|
|
|
try {
|
2018-08-06 11:48:19 +02:00
|
|
|
Field entityTypesField = null;
|
2018-08-05 10:34:02 +02:00
|
|
|
|
2018-08-06 11:48:19 +02:00
|
|
|
for (Method method : getNmsClass("Entity").getMethods()) {
|
|
|
|
if (!method.getReturnType().getSimpleName().equals("EntityTypes"))
|
2018-08-05 10:34:02 +02:00
|
|
|
continue;
|
|
|
|
|
2018-08-06 11:48:19 +02:00
|
|
|
Object entityType = method.invoke(nmsEntity);
|
2018-08-29 03:05:29 +02:00
|
|
|
Class typesClass = getNmsClass("IRegistry");
|
2018-08-06 11:48:19 +02:00
|
|
|
|
2018-08-29 03:05:29 +02:00
|
|
|
Object registry = typesClass.getField("ENTITY_TYPE").get(null);
|
2018-08-05 10:34:02 +02:00
|
|
|
|
|
|
|
return (int) registry.getClass().getMethod("a", Object.class).invoke(registry, entityType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
|
2018-08-06 11:48:19 +02:00
|
|
|
throw new IllegalStateException("Failed to find EntityType for " + nmsEntity.getClass().getSimpleName());
|
2018-08-05 10:34:02 +02:00
|
|
|
}
|
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
public static WrappedWatchableObject createWatchable(int index, Object obj) {
|
2017-07-24 20:58:58 +02:00
|
|
|
Object watcherItem = createDataWatcherItem(index, obj);
|
|
|
|
|
2017-07-25 00:41:07 +02:00
|
|
|
if (watcherItem == null)
|
|
|
|
return null;
|
|
|
|
|
2017-07-24 20:58:58 +02:00
|
|
|
return new WrappedWatchableObject(watcherItem);
|
2016-06-18 17:16:32 +02:00
|
|
|
}
|
|
|
|
|
2018-08-05 10:34:02 +02:00
|
|
|
public static int getCombinedIdByItemStack(ItemStack itemStack) {
|
|
|
|
try {
|
|
|
|
Object nmsItem = getNmsItem(itemStack);
|
|
|
|
Object item = getNmsMethod("ItemStack", "getItem").invoke(nmsItem);
|
|
|
|
Class blockClass = getNmsClass("Block");
|
|
|
|
|
|
|
|
Object nmsBlock = getNmsMethod(blockClass, "asBlock", getNmsClass("Item")).invoke(null, item);
|
|
|
|
|
2018-09-23 01:07:54 +02:00
|
|
|
Object iBlockData = getNmsMethod(blockClass, "getBlockData").invoke(nmsBlock);
|
|
|
|
|
|
|
|
return (int) getNmsMethod("Block", "getCombinedId", getNmsClass("IBlockData")).invoke(null, iBlockData);
|
2018-08-05 10:34:02 +02:00
|
|
|
}
|
|
|
|
catch (Exception ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ItemStack getItemStackByCombinedId(int id) {
|
|
|
|
try {
|
|
|
|
Method idMethod = getNmsMethod("Block", "getByCombinedId", int.class);
|
|
|
|
Object iBlockData = idMethod.invoke(null, id);
|
|
|
|
Class iBlockClass = getNmsClass("IBlockData");
|
|
|
|
|
|
|
|
Method getBlock = getNmsMethod(iBlockClass, "getBlock");
|
|
|
|
Object block = getBlock.invoke(iBlockData);
|
2016-05-12 13:30:22 +02:00
|
|
|
|
2018-08-05 10:34:02 +02:00
|
|
|
Method getItem = getNmsMethod("Block", "t", iBlockClass);
|
|
|
|
|
|
|
|
return getBukkitItem(getItem.invoke(block, iBlockData));
|
|
|
|
}
|
|
|
|
catch (Exception ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2016-05-12 13:30:22 +02:00
|
|
|
}
|
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
public static Object getWorldServer(World w) {
|
|
|
|
try {
|
2016-05-12 13:30:22 +02:00
|
|
|
return getCraftMethod("CraftWorld", "getHandle").invoke(w);
|
|
|
|
}
|
2016-11-25 09:21:49 +01:00
|
|
|
catch (IllegalAccessException | InvocationTargetException e) {
|
2016-05-12 13:30:22 +02:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
public static Object getPlayerInteractManager(World w) {
|
2016-05-12 13:30:22 +02:00
|
|
|
Object worldServer = getWorldServer(w);
|
|
|
|
|
2016-11-25 09:21:49 +01:00
|
|
|
try {
|
2016-05-12 13:30:22 +02:00
|
|
|
return getNmsConstructor("PlayerInteractManager", getNmsClass("World")).newInstance(worldServer);
|
|
|
|
}
|
2016-11-25 09:21:49 +01:00
|
|
|
catch (InstantiationException | InvocationTargetException | IllegalAccessException e) {
|
2016-05-12 13:30:22 +02:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|