From c335b245b5a661c7bb08a0d10766154cc5232dfa Mon Sep 17 00:00:00 2001 From: Byteflux Date: Sun, 7 Dec 2014 07:59:38 -0800 Subject: [PATCH] Work towards 1.8 support TODO: Remove future disguise types and test backwards compatibility --- pom.xml | 13 ++--- .../disguisetypes/TargetedDisguise.java | 2 - .../disguise/utilities/DisguiseUtilities.java | 54 ++++++++++++------- .../disguise/utilities/PacketsManager.java | 2 +- .../disguise/utilities/ReflectionManager.java | 34 ++++++++---- 5 files changed, 63 insertions(+), 42 deletions(-) diff --git a/pom.xml b/pom.xml index 213b801c..4c7131ab 100644 --- a/pom.xml +++ b/pom.xml @@ -64,14 +64,9 @@ http://repo.md-5.net/content/groups/public/ - comphenix-rep - Comphenix Repository - http://repo.comphenix.net/content/groups/public - - - comphenix-snapshot-rep - Comphenix Snapshot Repository - http://repo.comphenix.net/content/repositories/snapshots/ + shadowvolt-repo + Shadowvolt Maven Repository + http://ci.shadowvolt.com/plugin/repository/everything/ @@ -79,7 +74,7 @@ com.comphenix.protocol ProtocolLib - 3.5.0-SNAPSHOT + 3.6.3-SNAPSHOT org.spigotmc diff --git a/src/me/libraryaddict/disguise/disguisetypes/TargetedDisguise.java b/src/me/libraryaddict/disguise/disguisetypes/TargetedDisguise.java index 8e0dd11d..235241bf 100644 --- a/src/me/libraryaddict/disguise/disguisetypes/TargetedDisguise.java +++ b/src/me/libraryaddict/disguise/disguisetypes/TargetedDisguise.java @@ -34,8 +34,6 @@ public abstract class TargetedDisguise extends Disguise { } public boolean canSee(Player player) { - if (getType() != null && (getType().is1_8() && !ReflectionManager.is1_8(player))) - return false; return canSee(player.getName()); } diff --git a/src/me/libraryaddict/disguise/utilities/DisguiseUtilities.java b/src/me/libraryaddict/disguise/utilities/DisguiseUtilities.java index cd21d9ae..df8eb691 100644 --- a/src/me/libraryaddict/disguise/utilities/DisguiseUtilities.java +++ b/src/me/libraryaddict/disguise/utilities/DisguiseUtilities.java @@ -81,12 +81,14 @@ public class DisguiseUtilities { static { try { + Object server = ReflectionManager.getNmsMethod("MinecraftServer", "getServer").invoke(null); + Object world = ((List) server.getClass().getField("worlds").get(server)).get(0); bedChunk = ReflectionManager.getNmsClass("Chunk") - .getConstructor(ReflectionManager.getNmsClass("World"), int.class, int.class).newInstance(null, 0, 0); + .getConstructor(ReflectionManager.getNmsClass("World"), int.class, int.class).newInstance(world, 0, 0); Field cSection = bedChunk.getClass().getDeclaredField("sections"); cSection.setAccessible(true); Object chunkSection = ReflectionManager.getNmsClass("ChunkSection").getConstructor(int.class, boolean.class) - .newInstance(0, false); + .newInstance(0, true); Object block; try { block = ReflectionManager.getNmsClass("Block").getMethod("getById", int.class) @@ -95,23 +97,35 @@ public class DisguiseUtilities { block = ((Object[]) ReflectionManager.getNmsField(ReflectionManager.getNmsClass("Block"), "byId").get(null))[Material.BED_BLOCK .getId()]; } - Method setId = chunkSection.getClass().getMethod("setTypeId", int.class, int.class, int.class, - ReflectionManager.getNmsClass("Block")); - Method setData = chunkSection.getClass().getMethod("setData", int.class, int.class, int.class, int.class); - Method setSky = chunkSection.getClass().getMethod("setSkyLight", int.class, int.class, int.class, int.class); - Method setEmitted = chunkSection.getClass().getMethod("setEmittedLight", int.class, int.class, int.class, int.class); - for (BlockFace face : new BlockFace[] { BlockFace.EAST, BlockFace.WEST, BlockFace.NORTH, BlockFace.SOUTH }) { - setId.invoke(chunkSection, 1 + face.getModX(), 0, 1 + face.getModZ(), block); - setData.invoke(chunkSection, 1 + face.getModX(), 0, 1 + face.getModZ(), face.ordinal()); - setSky.invoke(chunkSection, 1 + face.getModX(), 0, 1 + face.getModZ(), 0); - setEmitted.invoke(chunkSection, 1 + face.getModX(), 0, 1 + face.getModZ(), 0); + + if (LibVersion.is1_8()) { + Method fromLegacyData = block.getClass().getMethod("fromLegacyData", int.class); + Method setType = chunkSection.getClass().getMethod("setType", int.class, int.class, int.class, + ReflectionManager.getNmsClass("IBlockData")); + Method setSky = chunkSection.getClass().getMethod("a", int.class, int.class, int.class, int.class); + Method setEmitted = chunkSection.getClass().getMethod("b", int.class, int.class, int.class, int.class); + for (BlockFace face : new BlockFace[] { BlockFace.EAST, BlockFace.WEST, BlockFace.NORTH, BlockFace.SOUTH }) { + setType.invoke(chunkSection, 1 + face.getModX(), 0, 1 + face.getModZ(), fromLegacyData.invoke(block, face.ordinal())); + setSky.invoke(chunkSection, 1 + face.getModX(), 0, 1 + face.getModZ(), 0); + setEmitted.invoke(chunkSection, 1 + face.getModX(), 0, 1 + face.getModZ(), 0); + } + } else { + Method setId = chunkSection.getClass().getMethod("setTypeId", int.class, int.class, int.class, + ReflectionManager.getNmsClass("Block")); + Method setData = chunkSection.getClass().getMethod("setData", int.class, int.class, int.class, int.class); + Method setSky = chunkSection.getClass().getMethod("setSkyLight", int.class, int.class, int.class, int.class); + Method setEmitted = chunkSection.getClass().getMethod("setEmittedLight", int.class, int.class, int.class, int.class); + for (BlockFace face : new BlockFace[] { BlockFace.EAST, BlockFace.WEST, BlockFace.NORTH, BlockFace.SOUTH }) { + setId.invoke(chunkSection, 1 + face.getModX(), 0, 1 + face.getModZ(), block); + setData.invoke(chunkSection, 1 + face.getModX(), 0, 1 + face.getModZ(), face.ordinal()); + setSky.invoke(chunkSection, 1 + face.getModX(), 0, 1 + face.getModZ(), 0); + setEmitted.invoke(chunkSection, 1 + face.getModX(), 0, 1 + face.getModZ(), 0); + } } + Object[] array = (Object[]) Array.newInstance(chunkSection.getClass(), 16); array[0] = chunkSection; cSection.set(bedChunk, array); - Object server = ReflectionManager.getNmsMethod("MinecraftServer", "getServer").invoke(null); - Object world = ((List) server.getClass().getField("worlds").get(server)).get(0); - bedChunk.getClass().getField("world").set(bedChunk, world); xChunk = bedChunk.getClass().getField("locX"); xChunk.setAccessible(true); zChunk = bedChunk.getClass().getField("locZ"); @@ -327,7 +341,7 @@ public class DisguiseUtilities { try { packets[i] = ProtocolLibrary.getProtocolManager() .createPacketConstructor(PacketType.Play.Server.MAP_CHUNK, bedChunk, true, 0, 40) - .createPacket(bedChunk, true, 0, ReflectionManager.is1_8(player) ? 48 : 0); + .createPacket(bedChunk, true, 0, LibVersion.is1_8() ? 48 : 0); } catch (IllegalArgumentException ex) { packets[i] = ProtocolLibrary.getProtocolManager() .createPacketConstructor(PacketType.Play.Server.MAP_CHUNK, bedChunk, true, 0) @@ -339,10 +353,10 @@ public class DisguiseUtilities { try { packets[i] = ProtocolLibrary.getProtocolManager() .createPacketConstructor(PacketType.Play.Server.MAP_CHUNK_BULK, Arrays.asList(bedChunk), 40) - .createPacket(Arrays.asList(bedChunk), ReflectionManager.is1_8(player) ? 48 : 0); + .createPacket(Arrays.asList(bedChunk), LibVersion.is1_8() ? 48 : 0); } catch (IllegalArgumentException ex) { packets[i] = ProtocolLibrary.getProtocolManager() - .createPacketConstructor(PacketType.Play.Server.MAP_CHUNK_BULK, Arrays.asList(bedChunk)) + .createPacketConstructor(PacketType.Play.Server.MAP_CHUNK_BULK, List.class) .createPacket(Arrays.asList(bedChunk)); } i++; @@ -356,7 +370,7 @@ public class DisguiseUtilities { PacketContainer setBed = new PacketContainer(PacketType.Play.Server.BED); StructureModifier bedInts = setBed.getIntegers(); bedInts.write(0, entity.getEntityId()); - if (ReflectionManager.is1_8(player)) { + if (LibVersion.is1_8()) { PlayerWatcher watcher = disguise.getWatcher(); int chunkX = (int) Math.floor(playerLocation.getX() / 16D) - 17, chunkZ = (int) Math .floor(playerLocation.getZ() / 16D) - 17; @@ -599,7 +613,7 @@ public class DisguiseUtilities { */ public static List rebuildForVersion(Player player, FlagWatcher watcher, List list) { - if (!ReflectionManager.is1_8(player)) + if (!LibVersion.is1_8()) return list; ArrayList rebuiltList = new ArrayList(); ArrayList backups = new ArrayList(); diff --git a/src/me/libraryaddict/disguise/utilities/PacketsManager.java b/src/me/libraryaddict/disguise/utilities/PacketsManager.java index 12918a4e..d64ca7ea 100644 --- a/src/me/libraryaddict/disguise/utilities/PacketsManager.java +++ b/src/me/libraryaddict/disguise/utilities/PacketsManager.java @@ -266,7 +266,7 @@ public class PacketsManager { } } - if (ReflectionManager.is1_8(player)) { + if (LibVersion.is1_8()) { ArrayList newPackets = new ArrayList(); newPackets.add(null); for (int i = 0; i < spawnPackets.length; i++) { diff --git a/src/me/libraryaddict/disguise/utilities/ReflectionManager.java b/src/me/libraryaddict/disguise/utilities/ReflectionManager.java index 87645a3c..16c37256 100644 --- a/src/me/libraryaddict/disguise/utilities/ReflectionManager.java +++ b/src/me/libraryaddict/disguise/utilities/ReflectionManager.java @@ -36,20 +36,16 @@ public class ReflectionManager { if (mcVersion.startsWith("1.")) { if (mcVersion.compareTo("1.7") < 0) { currentVersion = LibVersion.V1_6; - } else { + } else if (mcVersion.startsWith("1.7")) { if (mcVersion.equals("1.7.10")) { currentVersion = LibVersion.V1_7_10; } else { currentVersion = mcVersion.compareTo("1.7.6") < 0 ? LibVersion.V1_7 : LibVersion.V1_7_6; } + } else { + currentVersion = V1_8; } } - try { - Class.forName("org.spigotmc.ProtocolData"); - currentVersion = V1_8; - } catch (Exception ex) { - // Its not 1.8 - } } public static LibVersion getGameVersion() { @@ -249,6 +245,9 @@ public class ReflectionManager { playerinteractmanager.getClass()).newInstance(minecraftServer, world, "LibsDisguises", playerinteractmanager); } + } else if (LibVersion.is1_8() && entityName.equals("EnderPearl")) { + entityObject = entityClass.getConstructor(getNmsClass("World"), getNmsClass("EntityLiving")) + .newInstance(world, createEntityInstance("Sheep")); } else { entityObject = entityClass.getConstructor(getNmsClass("World")).newInstance(world); } @@ -265,7 +264,12 @@ public class ReflectionManager { public static FakeBoundingBox getBoundingBox(Entity entity) { try { - Object boundingBox = getNmsField("Entity", "boundingBox").get(getNmsEntity(entity)); + Object boundingBox; + if (LibVersion.is1_8()) { + boundingBox = getNmsMethod("Entity", "getBoundingBox").invoke(getNmsEntity(entity)); + } else { + boundingBox = getNmsField("Entity", "boundingBox").get(getNmsEntity(entity)); + } double x = 0, y = 0, z = 0; int stage = 0; for (Field field : boundingBox.getClass().getFields()) { @@ -492,7 +496,12 @@ public class ReflectionManager { try { float length = getNmsField("Entity", "length").getFloat(getNmsEntity(entity)); float width = getNmsField("Entity", "width").getFloat(getNmsEntity(entity)); - float height = getNmsField("Entity", "height").getFloat(getNmsEntity(entity)); + float height; + if (LibVersion.is1_8()) { + height = (Float) getNmsMethod("Entity", "getHeadHeight").invoke(getNmsEntity(entity)); + } else { + height = getNmsField("Entity", "height").getFloat(getNmsEntity(entity)); + } return new float[] { length, width, height }; } catch (Exception ex) { ex.printStackTrace(); @@ -611,7 +620,12 @@ public class ReflectionManager { public static void setBoundingBox(Entity entity, FakeBoundingBox newBox) { try { - Object boundingBox = getNmsField("Entity", "boundingBox").get(getNmsEntity(entity)); + Object boundingBox; + if (LibVersion.is1_8()) { + boundingBox = getNmsMethod("Entity", "getBoundingBox").invoke(getNmsEntity(entity)); + } else { + boundingBox = getNmsField("Entity", "boundingBox").get(getNmsEntity(entity)); + } int stage = 0; Location loc = entity.getLocation(); for (Field field : boundingBox.getClass().getFields()) {