From 58b7086cd2e83476a89d2eb98f9d887dae93e3a2 Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Fri, 25 Nov 2016 21:21:49 +1300 Subject: [PATCH] Fix IBlockData and WrappedBlockData, fixed FishingRod constructor --- .../disguise/utilities/ReflectionManager.java | 557 +++++++----------- 1 file changed, 201 insertions(+), 356 deletions(-) diff --git a/src/me/libraryaddict/disguise/utilities/ReflectionManager.java b/src/me/libraryaddict/disguise/utilities/ReflectionManager.java index 760a6888..aaa8ff98 100644 --- a/src/me/libraryaddict/disguise/utilities/ReflectionManager.java +++ b/src/me/libraryaddict/disguise/utilities/ReflectionManager.java @@ -29,6 +29,7 @@ import com.comphenix.protocol.wrappers.BlockPosition; import com.comphenix.protocol.wrappers.EnumWrappers.Direction; import com.comphenix.protocol.wrappers.MinecraftKey; import com.comphenix.protocol.wrappers.Vector3F; +import com.comphenix.protocol.wrappers.WrappedBlockData; import com.comphenix.protocol.wrappers.WrappedDataWatcher.Registry; import com.comphenix.protocol.wrappers.WrappedDataWatcher.Serializer; import com.comphenix.protocol.wrappers.WrappedDataWatcher.WrappedDataWatcherObject; @@ -38,8 +39,7 @@ import com.google.common.base.Optional; import me.libraryaddict.disguise.disguisetypes.DisguiseType; -public class ReflectionManager -{ +public class ReflectionManager { private static final String bukkitVersion = Bukkit.getServer().getClass().getName().split("\\.")[3]; private static final Class craftItemClass; private static Method damageAndIdleSoundMethod; @@ -51,29 +51,23 @@ public class ReflectionManager private static final Field trackerField; public static final Field entityCountField; - static - { - for (Method method : getNmsClass("EntityLiving").getDeclaredMethods()) - { - try - { + static { + for (Method method : getNmsClass("EntityLiving").getDeclaredMethods()) { + try { if (method.getReturnType() == float.class && Modifier.isProtected(method.getModifiers()) - && method.getParameterTypes().length == 0) - { + && method.getParameterTypes().length == 0) { Object entity = createEntityInstance("Cow"); method.setAccessible(true); float value = (Float) method.invoke(entity); - if (value == 0.4F) - { + if (value == 0.4F) { damageAndIdleSoundMethod = method; break; } } } - catch (Exception ex) - { + catch (Exception ex) { ex.printStackTrace(); } } @@ -98,16 +92,13 @@ public class ReflectionManager entityCountField.setAccessible(true); } - public static Object createEntityInstance(String entityName) - { - try - { + public static Object createEntityInstance(String entityName) { + try { Class entityClass = getNmsClass("Entity" + entityName); Object entityObject; Object world = getWorldServer(Bukkit.getWorlds().get(0)); - switch (entityName) - { + switch (entityName) { case "Player": Object minecraftServer = getNmsMethod("MinecraftServer", "getServer").invoke(null); @@ -131,6 +122,10 @@ public class ReflectionManager getNmsClass("ItemStack")) .newInstance(world, 0d, 0d, 0d, getNmsItem(new ItemStack(Material.SPLASH_POTION))); break; + case "FishingHook": + entityObject = entityClass.getDeclaredConstructor(getNmsClass("World"), getNmsClass("EntityHuman")) + .newInstance(world, createEntityInstance("Player")); + break; default: entityObject = entityClass.getDeclaredConstructor(getNmsClass("World")).newInstance(world); break; @@ -138,69 +133,56 @@ public class ReflectionManager return entityObject; } - catch (Exception e) - { + catch (Exception e) { e.printStackTrace(); } return null; } - public static Object getMobEffectList(int id) - { + public static Object getMobEffectList(int id) { Method nmsMethod = getNmsMethod("MobEffectList", "fromId", Integer.TYPE); - try - { + try { return nmsMethod.invoke(null, id); } - catch (IllegalAccessException | InvocationTargetException e) - { + catch (IllegalAccessException | InvocationTargetException e) { e.printStackTrace(); } return null; } - public static Object createMobEffect(PotionEffect effect) - { + public static Object createMobEffect(PotionEffect effect) { return createMobEffect(effect.getType().getId(), effect.getDuration(), effect.getAmplifier(), effect.isAmbient(), effect.hasParticles()); } - public static Object createMobEffect(int id, int duration, int amplification, boolean ambient, boolean particles) - { - try - { + 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) - { + catch (Exception e) { e.printStackTrace(); } return null; } - public static FakeBoundingBox getBoundingBox(Entity entity) - { - try - { + public static FakeBoundingBox getBoundingBox(Entity entity) { + try { Object boundingBox = getNmsMethod("Entity", "getBoundingBox").invoke(getNmsEntity(entity)); double x = 0, y = 0, z = 0; int stage = 0; - for (Field field : boundingBox.getClass().getDeclaredFields()) - { - if (field.getType().getSimpleName().equals("double")) - { + for (Field field : boundingBox.getClass().getDeclaredFields()) { + if (field.getType().getSimpleName().equals("double")) { stage++; - switch (stage) - { + switch (stage) { case 1: x -= field.getDouble(boundingBox); break; @@ -227,98 +209,79 @@ public class ReflectionManager return new FakeBoundingBox(x, y, z); } - catch (Exception ex) - { + catch (Exception ex) { ex.printStackTrace(); } return null; } - public static Entity getBukkitEntity(Object nmsEntity) - { - try - { + public static Entity getBukkitEntity(Object nmsEntity) { + try { return (Entity) getNmsMethod("Entity", "getBukkitEntity").invoke(nmsEntity); } - catch (Exception ex) - { + catch (Exception ex) { ex.printStackTrace(); } return null; } - public static ItemStack getBukkitItem(Object nmsItem) - { - try - { + public static ItemStack getBukkitItem(Object nmsItem) { + try { return (ItemStack) craftItemClass.getMethod("asBukkitCopy", getNmsClass("ItemStack")).invoke(null, nmsItem); } - catch (Exception e) - { + catch (Exception e) { e.printStackTrace(); } return null; } - public static String getBukkitVersion() - { + public static String getBukkitVersion() { return bukkitVersion; } - public static Class getCraftClass(String className) - { - try - { + public static Class getCraftClass(String className) { + try { return Class.forName("org.bukkit.craftbukkit." + getBukkitVersion() + "." + className); } - catch (Exception e) - { + catch (Exception e) { e.printStackTrace(); } return null; } - public static Constructor getCraftConstructor(Class clazz, Class... parameters) - { - try - { + public static Constructor getCraftConstructor(Class clazz, Class... parameters) { + try { Constructor declaredConstructor = clazz.getDeclaredConstructor(parameters); declaredConstructor.setAccessible(true); return declaredConstructor; } - catch (NoSuchMethodException e) - { + catch (NoSuchMethodException e) { e.printStackTrace(); } return null; } - public static Constructor getCraftConstructor(String className, Class... parameters) - { + public static Constructor getCraftConstructor(String className, Class... parameters) { return getCraftConstructor(getCraftClass(className), parameters); } - public static String getCraftSound(Sound sound) - { - try - { + public static String getCraftSound(Sound sound) { + try { return (String) getCraftClass("CraftSound").getMethod("getSound", Sound.class).invoke(null, sound); } - catch (Exception ex) - { + catch (Exception ex) { ex.printStackTrace(); } return null; } - public static Object getEntityTrackerEntry(Entity target) throws Exception - { + public static Object getEntityTrackerEntry(Entity target) throws Exception { Object world = getWorldServer(target.getWorld()); Object tracker = trackerField.get(world); Object trackedEntities = entitiesField.get(tracker); @@ -326,86 +289,67 @@ public class ReflectionManager return ihmGet.invoke(trackedEntities, target.getEntityId()); } - public static Object getMinecraftServer() - { - try - { + public static Object getMinecraftServer() { + try { return getCraftMethod("CraftServer", "getServer").invoke(Bukkit.getServer()); } - catch (IllegalAccessException | InvocationTargetException e) - { + catch (IllegalAccessException | InvocationTargetException e) { e.printStackTrace(); } return null; } - public static String getEnumArt(Art art) - { - try - { + public static String getEnumArt(Art art) { + try { Object enumArt = getCraftClass("CraftArt").getMethod("BukkitToNotch", Art.class).invoke(null, art); - for (Field field : enumArt.getClass().getDeclaredFields()) - { - if (field.getType() == String.class) - { + for (Field field : enumArt.getClass().getDeclaredFields()) { + if (field.getType() == String.class) { return (String) field.get(enumArt); } } } - catch (Exception ex) - { + catch (Exception ex) { ex.printStackTrace(); } return null; } - public static Object getBlockPosition(int x, int y, int z) - { - try - { + public static Object getBlockPosition(int x, int y, int z) { + try { return getNmsClass("BlockPosition").getDeclaredConstructor(int.class, int.class, int.class).newInstance(x, y, z); } - catch (Exception ex) - { + catch (Exception ex) { ex.printStackTrace(); } return null; } - public static Enum getEnumDirection(int direction) - { - try - { + public static Enum getEnumDirection(int direction) { + try { return (Enum) getNmsMethod("EnumDirection", "fromType2", int.class).invoke(null, direction); } - catch (Exception ex) - { + catch (Exception ex) { ex.printStackTrace(); } return null; } - public static Enum getEnumPlayerInfoAction(int action) - { - try - { + public static Enum getEnumPlayerInfoAction(int action) { + try { return (Enum) getNmsClass("PacketPlayOutPlayerInfo$EnumPlayerInfoAction").getEnumConstants()[action]; } - catch (Exception ex) - { + catch (Exception ex) { ex.printStackTrace(); } return null; } - public static Object getPlayerInfoData(Object playerInfoPacket, WrappedGameProfile gameProfile) - { - try - { + public static Object getPlayerInfoData(Object playerInfoPacket, WrappedGameProfile gameProfile) { + try { Object playerListName = getNmsClass("ChatComponentText").getDeclaredConstructor(String.class) .newInstance(gameProfile.getName()); @@ -415,229 +359,185 @@ public class ReflectionManager .newInstance(playerInfoPacket, gameProfile.getHandle(), 0, getNmsClass("EnumGamemode").getEnumConstants()[1], playerListName); } - catch (Exception ex) - { + catch (Exception ex) { ex.printStackTrace(); } return null; } - public static WrappedGameProfile getGameProfile(Player player) - { + public static WrappedGameProfile getGameProfile(Player player) { return WrappedGameProfile.fromPlayer(player); } - public static WrappedGameProfile getGameProfile(UUID uuid, String playerName) - { - try - { + public static WrappedGameProfile getGameProfile(UUID uuid, String playerName) { + try { return new WrappedGameProfile(uuid != null ? uuid : UUID.randomUUID(), playerName); } - catch (Exception ex) - { + catch (Exception ex) { ex.printStackTrace(); } return null; } - public static WrappedGameProfile getClonedProfile(WrappedGameProfile gameProfile) - { + public static WrappedGameProfile getClonedProfile(WrappedGameProfile gameProfile) { return getGameProfileWithThisSkin(null, gameProfile.getName(), gameProfile); } - public static WrappedGameProfile getGameProfileWithThisSkin(UUID uuid, String playerName, WrappedGameProfile profileWithSkin) - { - try - { + public static WrappedGameProfile getGameProfileWithThisSkin(UUID uuid, String playerName, + WrappedGameProfile profileWithSkin) { + try { WrappedGameProfile gameProfile = new WrappedGameProfile(uuid != null ? uuid : UUID.randomUUID(), playerName); gameProfile.getProperties().putAll(profileWithSkin.getProperties()); return gameProfile; } - catch (Exception ex) - { + catch (Exception ex) { ex.printStackTrace(); } return null; } - public static Class getNmsClass(String className) - { - try - { + public static Class getNmsClass(String className) { + try { return Class.forName("net.minecraft.server." + getBukkitVersion() + "." + className); } - catch (Exception e) - { + catch (Exception e) { e.printStackTrace(); } return null; } - public static Constructor getNmsConstructor(Class clazz, Class... parameters) - { - try - { + public static Constructor getNmsConstructor(Class clazz, Class... parameters) { + try { Constructor declaredConstructor = clazz.getDeclaredConstructor(parameters); declaredConstructor.setAccessible(true); return declaredConstructor; } - catch (NoSuchMethodException e) - { + catch (NoSuchMethodException e) { e.printStackTrace(); } return null; } - public static Constructor getNmsConstructor(String className, Class... parameters) - { + public static Constructor getNmsConstructor(String className, Class... parameters) { return getNmsConstructor(getNmsClass(className), parameters); } - public static Object getNmsEntity(Entity entity) - { - try - { + public static Object getNmsEntity(Entity entity) { + try { return getCraftClass("entity.CraftEntity").getMethod("getHandle").invoke(entity); } - catch (Exception ex) - { + catch (Exception ex) { ex.printStackTrace(); } return null; } - public static Field getNmsField(Class clazz, String fieldName) - { - try - { + public static Field getNmsField(Class clazz, String fieldName) { + try { Field declaredField = clazz.getDeclaredField(fieldName); declaredField.setAccessible(true); return declaredField; } - catch (NoSuchFieldException e) - { + catch (NoSuchFieldException e) { e.printStackTrace(); } return null; } - public static Field getNmsField(String className, String fieldName) - { + public static Field getNmsField(String className, String fieldName) { return getNmsField(getNmsClass(className), fieldName); } - public static Object getNmsItem(ItemStack itemstack) - { - try - { + public static Object getNmsItem(ItemStack itemstack) { + try { return craftItemClass.getDeclaredMethod("asNMSCopy", ItemStack.class).invoke(null, itemstack); } - catch (Exception e) - { + catch (Exception e) { e.printStackTrace(); } return null; } - public static Method getCraftMethod(String className, String methodName, Class... parameters) - { + 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 - { + public static Method getCraftMethod(Class clazz, String methodName, Class... parameters) { + try { Method declaredMethod = clazz.getDeclaredMethod(methodName, parameters); declaredMethod.setAccessible(true); return declaredMethod; } - catch (NoSuchMethodException e) - { + catch (NoSuchMethodException e) { e.printStackTrace(); } return null; } - public static Method getNmsMethod(Class clazz, String methodName, Class... parameters) - { - try - { + public static Method getNmsMethod(Class clazz, String methodName, Class... parameters) { + try { Method declaredMethod = clazz.getDeclaredMethod(methodName, parameters); declaredMethod.setAccessible(true); return declaredMethod; } - catch (NoSuchMethodException e) - { + catch (NoSuchMethodException e) { e.printStackTrace(); } return null; } - public static Method getNmsMethod(String className, String methodName, Class... parameters) - { + public static Method getNmsMethod(String className, String methodName, Class... parameters) { return getNmsMethod(getNmsClass(className), methodName, parameters); } - public static double getPing(Player player) - { - try - { + public static double getPing(Player player) { + try { return (double) pingField.getInt(ReflectionManager.getNmsEntity(player)); } - catch (Exception ex) - { + catch (Exception ex) { ex.printStackTrace(); } return 0; } - public static float[] getSize(Entity entity) - { - try - { + public static float[] getSize(Entity entity) { + try { 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)); - return new float[] - { - length, width, height - }; + return new float[] { + length, width, height + }; } - catch (Exception ex) - { + catch (Exception ex) { ex.printStackTrace(); } return null; } - public static WrappedGameProfile getSkullBlob(WrappedGameProfile gameProfile) - { - try - { + public static WrappedGameProfile getSkullBlob(WrappedGameProfile gameProfile) { + try { Object minecraftServer = getNmsMethod("MinecraftServer", "getServer").invoke(null); - for (Method method : getNmsClass("MinecraftServer").getDeclaredMethods()) - { - if (method.getReturnType().getSimpleName().equals("MinecraftSessionService")) - { + for (Method method : getNmsClass("MinecraftServer").getDeclaredMethods()) { + if (method.getReturnType().getSimpleName().equals("MinecraftSessionService")) { Object session = method.invoke(minecraftServer); return WrappedGameProfile.fromHandle(session.getClass() @@ -646,52 +546,44 @@ public class ReflectionManager } } } - catch (Exception ex) - { + catch (Exception ex) { ex.printStackTrace(); } return null; } - public static Float getSoundModifier(Object entity) - { - try - { + public static Float getSoundModifier(Object entity) { + try { damageAndIdleSoundMethod.setAccessible(true); return (Float) damageAndIdleSoundMethod.invoke(entity); } - catch (Exception ignored) - { + catch (Exception ignored) { } return null; } - public static WrappedGameProfile grabProfileAddUUID(String playername) - { - try - { + public static WrappedGameProfile grabProfileAddUUID(String playername) { + try { Object minecraftServer = getNmsMethod("MinecraftServer", "getServer").invoke(null); - for (Method method : getNmsClass("MinecraftServer").getDeclaredMethods()) - { - if (method.getReturnType().getSimpleName().equals("GameProfileRepository")) - { + for (Method method : getNmsClass("MinecraftServer").getDeclaredMethods()) { + if (method.getReturnType().getSimpleName().equals("GameProfileRepository")) { Object profileRepo = method.invoke(minecraftServer); Object agent = Class.forName("com.mojang.authlib.Agent").getDeclaredField("MINECRAFT").get(null); LibsProfileLookupCaller callback = new LibsProfileLookupCaller(); - profileRepo.getClass().getDeclaredMethod("findProfilesByNames", String[].class, agent.getClass(), - Class.forName("com.mojang.authlib.ProfileLookupCallback")).invoke(profileRepo, new String[] - { - playername - }, agent, callback); + profileRepo.getClass() + .getDeclaredMethod("findProfilesByNames", String[].class, agent.getClass(), + Class.forName("com.mojang.authlib.ProfileLookupCallback")) + .invoke(profileRepo, new String[] { + playername + }, agent, callback); - if (callback.getGameProfile() != null) - { + if (callback.getGameProfile() != null) { return callback.getGameProfile(); } @@ -699,23 +591,19 @@ public class ReflectionManager } } } - catch (Exception ex) - { + catch (Exception ex) { ex.printStackTrace(); } return null; } - public static void removePlayer(Player player) - { + public static void removePlayer(Player player) { // Some future remove code if needed } - public static void setBoundingBox(Entity entity, FakeBoundingBox newBox) - { - try - { + 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(), @@ -724,27 +612,22 @@ public class ReflectionManager setBoundingBoxMethod.invoke(getNmsEntity(entity), boundingBox); } - catch (Exception ex) - { + catch (Exception ex) { ex.printStackTrace(); } } - public static Enum getSoundCategory(String category) - { + public static Enum getSoundCategory(String category) { Method method = getNmsMethod("SoundCategory", "a", String.class); - try - { + try { Enum invoke = (Enum) method.invoke(null, category.toLowerCase()); - if (invoke == null) - { + if (invoke == null) { Class clazz = getNmsClass("SoundCategory"); Enum[] enums = clazz != null ? (Enum[]) clazz.getEnumConstants() : null; - for (Enum anEnum : enums != null ? enums : new Enum[0]) - { + for (Enum anEnum : enums != null ? enums : new Enum[0]) { if (anEnum.name().equals(category.toUpperCase())) return anEnum; } @@ -752,16 +635,14 @@ public class ReflectionManager return invoke; } - catch (Exception e) - { + catch (Exception e) { e.printStackTrace(); } return null; } - public static Enum getSoundCategory(DisguiseType disguiseType) - { + public static Enum getSoundCategory(DisguiseType disguiseType) { if (disguiseType == DisguiseType.PLAYER) return getSoundCategory("player"); @@ -782,8 +663,7 @@ public class ReflectionManager * @param slot * @return null if the equipment slot is null */ - public static Enum createEnumItemSlot(EquipmentSlot slot) - { + public static Enum createEnumItemSlot(EquipmentSlot slot) { Class clazz = getNmsClass("EnumItemSlot"); Object[] enums = clazz != null ? clazz.getEnumConstants() : null; @@ -791,8 +671,7 @@ public class ReflectionManager if (enums == null) return null; - switch (slot) - { + switch (slot) { case HAND: return (Enum) enums[0]; case OFF_HAND: @@ -815,14 +694,11 @@ public class ReflectionManager * * @return null if the object isn't an nms EnumItemSlot */ - public static EquipmentSlot createEquipmentSlot(Object enumItemSlot) - { - try - { + public static EquipmentSlot createEquipmentSlot(Object enumItemSlot) { + try { Enum nmsSlot = (Enum) enumItemSlot; - switch (nmsSlot.name()) - { + switch (nmsSlot.name()) { case "MAINHAND": return EquipmentSlot.HAND; case "OFFHAND": @@ -837,8 +713,7 @@ public class ReflectionManager return EquipmentSlot.HEAD; } } - catch (Exception e) - { + catch (Exception e) { } return null; @@ -850,13 +725,11 @@ public class ReflectionManager * @param slot * @return null if the disguisedEntity is not an instance of a living entity */ - public static ItemStack getEquipment(EquipmentSlot slot, Entity disguisedEntity) - { + public static ItemStack getEquipment(EquipmentSlot slot, Entity disguisedEntity) { if (!(disguisedEntity instanceof LivingEntity)) return null; - switch (slot) - { + switch (slot) { case HAND: return ((LivingEntity) disguisedEntity).getEquipment().getItemInMainHand(); case OFF_HAND: @@ -879,42 +752,34 @@ public class ReflectionManager * * @return */ - public static String convertSoundEffectToString(Object soundEffect) - { - try - { + 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) - { + catch (IllegalAccessException e) { e.printStackTrace(); } return null; } - public static Object getCraftSoundEffect(String sound) - { - try - { + public static Object getCraftSoundEffect(String sound) { + try { return getCraftMethod("CraftSound", "getSoundEffect", String.class).invoke(null, sound); } - catch (IllegalAccessException | InvocationTargetException e) - { + catch (IllegalAccessException | InvocationTargetException e) { e.printStackTrace(); } return null; } - private static Object convertInvalidItem(Object value) - { - if (value instanceof Optional) - { + private static Object convertInvalidItem(Object value) { + if (value instanceof Optional) { Optional opt = (Optional) value; if (!opt.isPresent()) @@ -922,60 +787,56 @@ public class ReflectionManager Object val = opt.get(); - if (val instanceof ItemStack) - { + if (val instanceof ItemStack) { return Optional.of(getNmsItem((ItemStack) val)); } - else if (val instanceof BlockPosition) - { + else if (val instanceof BlockPosition) { BlockPosition pos = (BlockPosition) val; - try - { + try { return Optional.of(getNmsConstructor("BlockPosition", int.class, int.class, int.class).newInstance(pos.getX(), pos.getY(), pos.getZ())); } - catch (Exception ex) - { + catch (Exception ex) { + ex.printStackTrace(); + } + } + else if (val instanceof WrappedBlockData) { + try { + return Optional.of(((WrappedBlockData) val).getHandle()); + } + catch (Exception ex) { ex.printStackTrace(); } } } - else if (value instanceof Vector3F) - { + else if (value instanceof Vector3F) { Vector3F angle = (Vector3F) value; - try - { + try { return getNmsConstructor("Vector3f", float.class, float.class, float.class).newInstance(angle.getX(), angle.getY(), angle.getZ()); } - catch (Exception ex) - { + catch (Exception ex) { ex.printStackTrace(); } } - else if (value instanceof Direction) - { - try - { + else if (value instanceof Direction) { + try { return (Enum) getNmsMethod("EnumDirection", "fromType1", int.class).invoke(null, ((Direction) value).ordinal()); } - catch (Exception ex) - { + catch (Exception ex) { ex.printStackTrace(); } - } else if (value instanceof BlockPosition) - { + } + else if (value instanceof BlockPosition) { BlockPosition pos = (BlockPosition) value; - try - { - return getNmsConstructor("BlockPosition", int.class, int.class, int.class).newInstance(pos.getX(), - pos.getY(), pos.getZ()); + try { + return getNmsConstructor("BlockPosition", int.class, int.class, int.class).newInstance(pos.getX(), pos.getY(), + pos.getZ()); } - catch (Exception ex) - { + catch (Exception ex) { ex.printStackTrace(); } } @@ -990,8 +851,7 @@ public class ReflectionManager * @param value * @return */ - private static Object createDataWatcherItem(int id, Object value) - { + private static Object createDataWatcherItem(int id, Object value) { if (value == null) return null; @@ -999,19 +859,18 @@ public class ReflectionManager Serializer serializer; - if (value instanceof Optional) - { + if (value instanceof Optional) { Optional opt = (Optional) value; - serializer = Registry.get((opt.isPresent() ? opt.get().getClass() : UUID.class), true); + serializer = Registry.get((opt.isPresent() + ? getNmsClass("IBlockData").isInstance(opt.get()) ? getNmsClass("IBlockData") : opt.get().getClass() + : UUID.class), true); } - else - { + else { serializer = Registry.get(value.getClass()); } - if (serializer == null) - { + if (serializer == null) { throw new IllegalArgumentException( "Unable to find Serializer for " + value + "! Are you running the latest version of ProtocolLib?"); } @@ -1020,79 +879,65 @@ public class ReflectionManager Constructor construct = getNmsConstructor("DataWatcher$Item", getNmsClass("DataWatcherObject"), Object.class); - try - { + try { return construct.newInstance(watcherObject.getHandle(), value); } - catch (InstantiationException | IllegalAccessException | InvocationTargetException e) - { + catch (InstantiationException | IllegalAccessException | InvocationTargetException e) { e.printStackTrace(); } return null; } - public static WrappedWatchableObject createWatchable(int index, Object obj) - { + public static WrappedWatchableObject createWatchable(int index, Object obj) { return new WrappedWatchableObject(createDataWatcherItem(index, obj)); } - public static EntityEquipment createEntityEquipment(Entity entity) - { + public static EntityEquipment createEntityEquipment(Entity entity) { if (!(entity instanceof LivingEntity)) return null; Constructor construct = getCraftConstructor("inventory.CraftEntityEquipment", getCraftClass("entity.CraftLivingEntity")); - try - { + try { return (EntityEquipment) construct.newInstance((LivingEntity) entity); } - catch (InstantiationException | IllegalAccessException | InvocationTargetException | ClassCastException e) - { + catch (InstantiationException | IllegalAccessException | InvocationTargetException | ClassCastException e) { e.printStackTrace(); } return null; } - public static int getCombinedId(int id, int data) - { + public static int getCombinedId(int id, int data) { return id + (data << 12); } - public static Pair getFromCombinedId(int combinedId) - { + public static Pair getFromCombinedId(int combinedId) { int j = combinedId & 4095; int k = combinedId >> 12 & 15; return new ImmutablePair<>(j, k); } - public static Object getWorldServer(World w) - { - try - { + public static Object getWorldServer(World w) { + try { return getCraftMethod("CraftWorld", "getHandle").invoke(w); } - catch (IllegalAccessException | InvocationTargetException e) - { + catch (IllegalAccessException | InvocationTargetException e) { e.printStackTrace(); } return null; } - public static Object getPlayerInteractManager(World w) - { + public static Object getPlayerInteractManager(World w) { Object worldServer = getWorldServer(w); - try - { + try { return getNmsConstructor("PlayerInteractManager", getNmsClass("World")).newInstance(worldServer); } - catch (InstantiationException | InvocationTargetException | IllegalAccessException e) - { + catch (InstantiationException | InvocationTargetException | IllegalAccessException e) { e.printStackTrace(); }