Cleaned up game versions to support more than 2 versions

This commit is contained in:
libraryaddict 2014-01-19 08:21:55 +13:00
parent d940a44f49
commit 1c6dc8233f
3 changed files with 64 additions and 37 deletions

View File

@ -10,7 +10,7 @@ import com.comphenix.protocol.reflect.StructureModifier;
import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.ReflectionManager; import me.libraryaddict.disguise.utilities.ReflectionManager.LibVersion;
public class PlayerWatcher extends LivingWatcher { public class PlayerWatcher extends LivingWatcher {
private boolean isInBed; private boolean isInBed;
@ -58,7 +58,7 @@ public class PlayerWatcher extends LivingWatcher {
packet = new PacketContainer(PacketType.Play.Server.ANIMATION); packet = new PacketContainer(PacketType.Play.Server.ANIMATION);
StructureModifier<Integer> mods = packet.getIntegers(); StructureModifier<Integer> mods = packet.getIntegers();
mods.write(0, getDisguise().getEntity().getEntityId()); mods.write(0, getDisguise().getEntity().getEntityId());
mods.write(1, ReflectionManager.isAfter17() ? 3 : 2); mods.write(1, LibVersion.is1_7() ? 3 : 2);
} }
try { try {
for (Player player : DisguiseUtilities.getPerverts(getDisguise())) { for (Player player : DisguiseUtilities.getPerverts(getDisguise())) {

View File

@ -16,6 +16,7 @@ import me.libraryaddict.disguise.disguisetypes.MobDisguise;
import me.libraryaddict.disguise.disguisetypes.PlayerDisguise; import me.libraryaddict.disguise.disguisetypes.PlayerDisguise;
import me.libraryaddict.disguise.disguisetypes.watchers.PlayerWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.PlayerWatcher;
import me.libraryaddict.disguise.utilities.DisguiseSound.SoundType; import me.libraryaddict.disguise.utilities.DisguiseSound.SoundType;
import me.libraryaddict.disguise.utilities.ReflectionManager.LibVersion;
import org.bukkit.Art; import org.bukkit.Art;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -116,7 +117,7 @@ public class PacketsManager {
try { try {
Player observer = event.getPlayer(); Player observer = event.getPlayer();
StructureModifier<Entity> entityModifer = event.getPacket().getEntityModifier(observer.getWorld()); StructureModifier<Entity> 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 if (entity instanceof ExperienceOrb || entity instanceof Item || entity instanceof Arrow
|| entity == observer) { || entity == observer) {
event.setCancelled(true); event.setCancelled(true);
@ -560,7 +561,7 @@ public class PacketsManager {
soundLoc.getBlockY() - 1, soundLoc.getBlockZ()); soundLoc.getBlockY() - 1, soundLoc.getBlockZ());
Class blockClass = ReflectionManager.getNmsClass("Block"); Class blockClass = ReflectionManager.getNmsClass("Block");
Object block; Object block;
if (ReflectionManager.isAfter17()) { if (LibVersion.is1_7()) {
block = ReflectionManager.getNmsClass("RegistryMaterials") block = ReflectionManager.getNmsClass("RegistryMaterials")
.getMethod("a", int.class) .getMethod("a", int.class)
.invoke(blockClass.getField("REGISTRY").get(null), typeId); .invoke(blockClass.getField("REGISTRY").get(null), typeId);
@ -633,7 +634,7 @@ public class PacketsManager {
} }
} }
} else if (event.getPacketType() == PacketType.Play.Server.ENTITY_STATUS) { } 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 // It made a damage animation
Entity entity = event.getPacket().getEntityModifier(observer.getWorld()).read(0); Entity entity = event.getPacket().getEntityModifier(observer.getWorld()).read(0);
Disguise disguise = DisguiseAPI.getDisguise(observer, entity); Disguise disguise = DisguiseAPI.getDisguise(observer, entity);
@ -776,7 +777,7 @@ public class PacketsManager {
packet.getWatchableCollectionModifier().write(0, watchableList); packet.getWatchableCollectionModifier().write(0, watchableList);
event.setPacket(packet); event.setPacket(packet);
} else if (event.getPacketType() == PacketType.Play.Server.ANIMATION) { } 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); event.setCancelled(true);
} }
} else if (event.getPacketType() == PacketType.Play.Server.ATTACH_ENTITY } 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) { } else if (event.getPacketType() == PacketType.Play.Server.ENTITY_STATUS) {
Disguise disguise = DisguiseAPI.getDisguise(event.getPlayer(), event.getPlayer()); Disguise disguise = DisguiseAPI.getDisguise(event.getPlayer(), event.getPlayer());
if (disguise.isSelfDisguiseSoundsReplaced() && !disguise.getType().isPlayer() 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); event.setCancelled(true);
} }
} }
@ -1156,8 +1157,8 @@ public class PacketsManager {
// Else if the disguise is attempting to send players a forbidden packet // Else if the disguise is attempting to send players a forbidden packet
else if (sentPacket.getType() == PacketType.Play.Server.ANIMATION) { else if (sentPacket.getType() == PacketType.Play.Server.ANIMATION) {
if (disguise.getType().isMisc() if (disguise.getType().isMisc()
|| (packets[0].getIntegers().read(1) == (ReflectionManager.isAfter17() ? 2 : 3) && (!disguise || (packets[0].getIntegers().read(1) == (LibVersion.is1_7() ? 2 : 3) && (!disguise.getType()
.getType().isPlayer() || ((PlayerWatcher) disguise.getWatcher()).isSleeping()))) { .isPlayer() || ((PlayerWatcher) disguise.getWatcher()).isSleeping()))) {
packets = new PacketContainer[0]; packets = new PacketContainer[0];
} }
} }
@ -1169,7 +1170,7 @@ public class PacketsManager {
PacketContainer newPacket = new PacketContainer(PacketType.Play.Server.ANIMATION); PacketContainer newPacket = new PacketContainer(PacketType.Play.Server.ANIMATION);
StructureModifier<Integer> mods = newPacket.getIntegers(); StructureModifier<Integer> mods = newPacket.getIntegers();
mods.write(0, disguise.getEntity().getEntityId()); 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 }; packets = new PacketContainer[] { newPacket, sentPacket };
} }
} }

View File

@ -15,10 +15,45 @@ import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
public class ReflectionManager { 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 String bukkitVersion = Bukkit.getServer().getClass().getName().split("\\.")[3];
private static Class itemClass; private static Class itemClass;
private static Method soundMethod; private static Method soundMethod;
static { static {
for (Method method : getNmsClass("EntityLiving").getDeclaredMethods()) { for (Method method : getNmsClass("EntityLiving").getDeclaredMethods()) {
try { try {
@ -47,26 +82,6 @@ public class ReflectionManager {
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); 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) { public static Object createEntityInstance(String entityName) {
@ -78,7 +93,7 @@ public class ReflectionManager {
Object minecraftServer = getNmsClass("MinecraftServer").getMethod("getServer").invoke(null); Object minecraftServer = getNmsClass("MinecraftServer").getMethod("getServer").invoke(null);
Object playerinteractmanager = getNmsClass("PlayerInteractManager").getConstructor(getNmsClass("World")) Object playerinteractmanager = getNmsClass("PlayerInteractManager").getConstructor(getNmsClass("World"))
.newInstance(world); .newInstance(world);
if (isAfter17()) { if (LibVersion.is1_7()) {
Object gameProfile = getGameProfile("LibsDisguises"); Object gameProfile = getGameProfile("LibsDisguises");
entityObject = entityClass.getConstructor(getNmsClass("MinecraftServer"), getNmsClass("WorldServer"), entityObject = entityClass.getConstructor(getNmsClass("MinecraftServer"), getNmsClass("WorldServer"),
gameProfile.getClass(), playerinteractmanager.getClass()).newInstance(minecraftServer, world, gameProfile.getClass(), playerinteractmanager.getClass()).newInstance(minecraftServer, world,
@ -157,9 +172,13 @@ public class ReflectionManager {
return null; return null;
} }
public static String getBukkitVersion() {
return bukkitVersion;
}
public static Class getCraftClass(String className) { public static Class getCraftClass(String className) {
try { try {
return Class.forName("org.bukkit.craftbukkit." + bukkitVersion + "." + className); return Class.forName("org.bukkit.craftbukkit." + getBukkitVersion() + "." + className);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -178,7 +197,7 @@ public class ReflectionManager {
public static String getEnumArt(Art art) { public static String getEnumArt(Art art) {
try { 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); Object enumArt = craftArt.getMethod("BukkitToNotch", Art.class).invoke(null, art);
for (Field field : enumArt.getClass().getFields()) { for (Field field : enumArt.getClass().getFields()) {
if (field.getType() == String.class) { if (field.getType() == String.class) {
@ -203,7 +222,7 @@ public class ReflectionManager {
public static Class getNmsClass(String className) { public static Class getNmsClass(String className) {
try { try {
return Class.forName("net.minecraft.server." + bukkitVersion + "." + className); return Class.forName("net.minecraft.server." + getBukkitVersion() + "." + className);
} catch (Exception e) { } catch (Exception e) {
// e.printStackTrace(); // e.printStackTrace();
} }
@ -258,8 +277,15 @@ public class ReflectionManager {
return null; return null;
} }
public static boolean isAfter17() { public static void setAllowSleep(Player player) {
return after17; 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) { public static void setBoundingBox(Entity entity, FakeBoundingBox newBox, float[] entitySize) {