From 1c6dc8233f8d3e5e9210a4a9b1a6a5b13d970538 Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Sun, 19 Jan 2014 08:21:55 +1300 Subject: [PATCH] Cleaned up game versions to support more than 2 versions --- .../disguisetypes/watchers/PlayerWatcher.java | 4 +- .../disguise/utilities/PacketsManager.java | 17 ++-- .../disguise/utilities/ReflectionManager.java | 80 ++++++++++++------- 3 files changed, 64 insertions(+), 37 deletions(-) diff --git a/src/me/libraryaddict/disguise/disguisetypes/watchers/PlayerWatcher.java b/src/me/libraryaddict/disguise/disguisetypes/watchers/PlayerWatcher.java index dd7418ad..9db8a8bc 100644 --- a/src/me/libraryaddict/disguise/disguisetypes/watchers/PlayerWatcher.java +++ b/src/me/libraryaddict/disguise/disguisetypes/watchers/PlayerWatcher.java @@ -10,7 +10,7 @@ import com.comphenix.protocol.reflect.StructureModifier; import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.utilities.DisguiseUtilities; -import me.libraryaddict.disguise.utilities.ReflectionManager; +import me.libraryaddict.disguise.utilities.ReflectionManager.LibVersion; public class PlayerWatcher extends LivingWatcher { private boolean isInBed; @@ -58,7 +58,7 @@ public class PlayerWatcher extends LivingWatcher { packet = new PacketContainer(PacketType.Play.Server.ANIMATION); StructureModifier mods = packet.getIntegers(); mods.write(0, getDisguise().getEntity().getEntityId()); - mods.write(1, ReflectionManager.isAfter17() ? 3 : 2); + mods.write(1, LibVersion.is1_7() ? 3 : 2); } try { for (Player player : DisguiseUtilities.getPerverts(getDisguise())) { diff --git a/src/me/libraryaddict/disguise/utilities/PacketsManager.java b/src/me/libraryaddict/disguise/utilities/PacketsManager.java index 202b3ab8..dd26524e 100644 --- a/src/me/libraryaddict/disguise/utilities/PacketsManager.java +++ b/src/me/libraryaddict/disguise/utilities/PacketsManager.java @@ -16,6 +16,7 @@ import me.libraryaddict.disguise.disguisetypes.MobDisguise; import me.libraryaddict.disguise.disguisetypes.PlayerDisguise; import me.libraryaddict.disguise.disguisetypes.watchers.PlayerWatcher; import me.libraryaddict.disguise.utilities.DisguiseSound.SoundType; +import me.libraryaddict.disguise.utilities.ReflectionManager.LibVersion; import org.bukkit.Art; import org.bukkit.Bukkit; @@ -116,7 +117,7 @@ public class PacketsManager { try { Player observer = event.getPlayer(); StructureModifier entityModifer = event.getPacket().getEntityModifier(observer.getWorld()); - org.bukkit.entity.Entity entity = entityModifer.read(ReflectionManager.isAfter17() ? 0 : 1); + org.bukkit.entity.Entity entity = entityModifer.read(LibVersion.is1_7() ? 0 : 1); if (entity instanceof ExperienceOrb || entity instanceof Item || entity instanceof Arrow || entity == observer) { event.setCancelled(true); @@ -560,7 +561,7 @@ public class PacketsManager { soundLoc.getBlockY() - 1, soundLoc.getBlockZ()); Class blockClass = ReflectionManager.getNmsClass("Block"); Object block; - if (ReflectionManager.isAfter17()) { + if (LibVersion.is1_7()) { block = ReflectionManager.getNmsClass("RegistryMaterials") .getMethod("a", int.class) .invoke(blockClass.getField("REGISTRY").get(null), typeId); @@ -633,7 +634,7 @@ public class PacketsManager { } } } else if (event.getPacketType() == PacketType.Play.Server.ENTITY_STATUS) { - if ((Byte) mods.read(1) == (ReflectionManager.isAfter17() ? 2 : 1)) { + if ((Byte) mods.read(1) == (LibVersion.is1_7() ? 2 : 1)) { // It made a damage animation Entity entity = event.getPacket().getEntityModifier(observer.getWorld()).read(0); Disguise disguise = DisguiseAPI.getDisguise(observer, entity); @@ -776,7 +777,7 @@ public class PacketsManager { packet.getWatchableCollectionModifier().write(0, watchableList); event.setPacket(packet); } else if (event.getPacketType() == PacketType.Play.Server.ANIMATION) { - if (event.getPacket().getIntegers().read(1) != (ReflectionManager.isAfter17() ? 2 : 3)) { + if (event.getPacket().getIntegers().read(1) != (LibVersion.is1_7() ? 2 : 3)) { event.setCancelled(true); } } else if (event.getPacketType() == PacketType.Play.Server.ATTACH_ENTITY @@ -793,7 +794,7 @@ public class PacketsManager { } else if (event.getPacketType() == PacketType.Play.Server.ENTITY_STATUS) { Disguise disguise = DisguiseAPI.getDisguise(event.getPlayer(), event.getPlayer()); if (disguise.isSelfDisguiseSoundsReplaced() && !disguise.getType().isPlayer() - && event.getPacket().getBytes().read(0) == (ReflectionManager.isAfter17() ? 2 : 1)) { + && event.getPacket().getBytes().read(0) == (LibVersion.is1_7() ? 2 : 1)) { event.setCancelled(true); } } @@ -1156,8 +1157,8 @@ public class PacketsManager { // Else if the disguise is attempting to send players a forbidden packet else if (sentPacket.getType() == PacketType.Play.Server.ANIMATION) { if (disguise.getType().isMisc() - || (packets[0].getIntegers().read(1) == (ReflectionManager.isAfter17() ? 2 : 3) && (!disguise - .getType().isPlayer() || ((PlayerWatcher) disguise.getWatcher()).isSleeping()))) { + || (packets[0].getIntegers().read(1) == (LibVersion.is1_7() ? 2 : 3) && (!disguise.getType() + .isPlayer() || ((PlayerWatcher) disguise.getWatcher()).isSleeping()))) { packets = new PacketContainer[0]; } } @@ -1169,7 +1170,7 @@ public class PacketsManager { PacketContainer newPacket = new PacketContainer(PacketType.Play.Server.ANIMATION); StructureModifier mods = newPacket.getIntegers(); mods.write(0, disguise.getEntity().getEntityId()); - mods.write(1, ReflectionManager.isAfter17() ? 3 : 2); + mods.write(1, LibVersion.is1_7() ? 3 : 2); packets = new PacketContainer[] { newPacket, sentPacket }; } } diff --git a/src/me/libraryaddict/disguise/utilities/ReflectionManager.java b/src/me/libraryaddict/disguise/utilities/ReflectionManager.java index 93ce5e4a..ef7568ec 100644 --- a/src/me/libraryaddict/disguise/utilities/ReflectionManager.java +++ b/src/me/libraryaddict/disguise/utilities/ReflectionManager.java @@ -15,10 +15,45 @@ import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; public class ReflectionManager { - private static boolean after17 = true; + public enum LibVersion { + V1_6, V1_7; + private static LibVersion currentVersion; + static { + if (getBukkitVersion().startsWith("v1_")) { + try { + int version = Integer.parseInt(getBukkitVersion().split("_")[1]); + if (version == 7) { + currentVersion = LibVersion.V1_7; + } else { + if (version < 7) { + currentVersion = LibVersion.V1_6; + } else { + currentVersion = LibVersion.V1_7; + } + } + } catch (Exception ex) { + + } + } + } + + public static LibVersion getGameVersion() { + return currentVersion; + } + + public static boolean is1_6() { + return getGameVersion() == V1_6; + } + + public static boolean is1_7() { + return getGameVersion() == V1_7; + } + } + private static String bukkitVersion = Bukkit.getServer().getClass().getName().split("\\.")[3]; private static Class itemClass; private static Method soundMethod; + static { for (Method method : getNmsClass("EntityLiving").getDeclaredMethods()) { try { @@ -47,26 +82,6 @@ public class ReflectionManager { } catch (Exception e) { e.printStackTrace(); } - if (bukkitVersion.startsWith("v1_")) { - try { - if (Integer.parseInt(bukkitVersion.split("_")[1]) < 7) { - after17 = false; - } - } catch (Exception ex) { - - } - } - } - - public static void setAllowSleep(Player player) { - try { - Object nmsEntity = getNmsEntity(player); - Object connection = nmsEntity.getClass().getField("playerConnection").get(nmsEntity); - Field check = connection.getClass().getField("checkMovement"); - check.setBoolean(connection, true); - } catch (Exception ex) { - ex.printStackTrace(); - } } public static Object createEntityInstance(String entityName) { @@ -78,7 +93,7 @@ public class ReflectionManager { Object minecraftServer = getNmsClass("MinecraftServer").getMethod("getServer").invoke(null); Object playerinteractmanager = getNmsClass("PlayerInteractManager").getConstructor(getNmsClass("World")) .newInstance(world); - if (isAfter17()) { + if (LibVersion.is1_7()) { Object gameProfile = getGameProfile("LibsDisguises"); entityObject = entityClass.getConstructor(getNmsClass("MinecraftServer"), getNmsClass("WorldServer"), gameProfile.getClass(), playerinteractmanager.getClass()).newInstance(minecraftServer, world, @@ -157,9 +172,13 @@ public class ReflectionManager { return null; } + public static String getBukkitVersion() { + return bukkitVersion; + } + public static Class getCraftClass(String className) { try { - return Class.forName("org.bukkit.craftbukkit." + bukkitVersion + "." + className); + return Class.forName("org.bukkit.craftbukkit." + getBukkitVersion() + "." + className); } catch (Exception e) { e.printStackTrace(); } @@ -178,7 +197,7 @@ public class ReflectionManager { public static String getEnumArt(Art art) { try { - Class craftArt = Class.forName("org.bukkit.craftbukkit." + bukkitVersion + ".CraftArt"); + Class craftArt = Class.forName("org.bukkit.craftbukkit." + getBukkitVersion() + ".CraftArt"); Object enumArt = craftArt.getMethod("BukkitToNotch", Art.class).invoke(null, art); for (Field field : enumArt.getClass().getFields()) { if (field.getType() == String.class) { @@ -203,7 +222,7 @@ public class ReflectionManager { public static Class getNmsClass(String className) { try { - return Class.forName("net.minecraft.server." + bukkitVersion + "." + className); + return Class.forName("net.minecraft.server." + getBukkitVersion() + "." + className); } catch (Exception e) { // e.printStackTrace(); } @@ -258,8 +277,15 @@ public class ReflectionManager { return null; } - public static boolean isAfter17() { - return after17; + public static void setAllowSleep(Player player) { + try { + Object nmsEntity = getNmsEntity(player); + Object connection = nmsEntity.getClass().getField("playerConnection").get(nmsEntity); + Field check = connection.getClass().getField("checkMovement"); + check.setBoolean(connection, true); + } catch (Exception ex) { + ex.printStackTrace(); + } } public static void setBoundingBox(Entity entity, FakeBoundingBox newBox, float[] entitySize) {