From c25519158ad091813ecd5ac2ae966b8528ae30ef Mon Sep 17 00:00:00 2001 From: Andrew Date: Tue, 19 Nov 2013 00:49:04 +1300 Subject: [PATCH] Update for reflection --- pom.xml | 2 +- .../libraryaddict/disguise/DisguiseAPI.java | 4 +- .../libraryaddict/disguise/LibsDisguises.java | 10 +- .../disguise/PacketsManager.java | 92 +++++++++---------- .../disguise/ReflectionManager.java | 41 +++++++-- .../disguise/disguisetypes/Disguise.java | 89 +++++++++--------- .../disguise/disguisetypes/DisguiseSound.java | 13 +-- .../disguise/disguisetypes/FlagWatcher.java | 69 ++++++-------- .../disguise/disguisetypes/Values.java | 16 ++-- .../watchers/DroppedItemWatcher.java | 4 +- .../watchers/ItemFrameWatcher.java | 3 +- .../disguisetypes/watchers/LivingWatcher.java | 84 ++++++++++++----- 12 files changed, 232 insertions(+), 195 deletions(-) diff --git a/pom.xml b/pom.xml index 341a32a8..892b1aaa 100644 --- a/pom.xml +++ b/pom.xml @@ -56,7 +56,7 @@ com.comphenix.protocol ProtocolLib - 2.5.0 + 2.7.0 org.bukkit diff --git a/src/me/libraryaddict/disguise/DisguiseAPI.java b/src/me/libraryaddict/disguise/DisguiseAPI.java index 581f5d59..cd4da8ae 100644 --- a/src/me/libraryaddict/disguise/DisguiseAPI.java +++ b/src/me/libraryaddict/disguise/DisguiseAPI.java @@ -48,7 +48,7 @@ public class DisguiseAPI { disguise = disguise.clone(); } try { - Field field = net.minecraft.server.v1_6_R3.Entity.class.getDeclaredField("entityCount"); + Field field = ReflectionManager.getNmsClass("Entity").getDeclaredField("entityCount"); field.setAccessible(true); int id = field.getInt(null); disguises.put(id, disguise); @@ -268,7 +268,7 @@ public class DisguiseAPI { return; try { // Grab the entity ID the fake disguise will use - Field field = net.minecraft.server.v1_6_R3.Entity.class.getDeclaredField("entityCount"); + Field field = ReflectionManager.getNmsClass("Entity").getDeclaredField("entityCount"); field.setAccessible(true); int id = field.getInt(null); // Set the entitycount plus one so we don't have the id being reused diff --git a/src/me/libraryaddict/disguise/LibsDisguises.java b/src/me/libraryaddict/disguise/LibsDisguises.java index c3552ebb..7b815a6e 100644 --- a/src/me/libraryaddict/disguise/LibsDisguises.java +++ b/src/me/libraryaddict/disguise/LibsDisguises.java @@ -3,7 +3,6 @@ package me.libraryaddict.disguise; import java.io.File; import java.io.IOException; import java.lang.reflect.Field; -import java.lang.reflect.Method; import java.util.List; import me.libraryaddict.disguise.commands.*; @@ -23,7 +22,6 @@ import org.bukkit.entity.Entity; import org.bukkit.entity.LivingEntity; import org.bukkit.plugin.java.JavaPlugin; -import com.comphenix.protocol.wrappers.WrappedAttribute; import com.comphenix.protocol.wrappers.WrappedDataWatcher; import com.comphenix.protocol.wrappers.WrappedWatchableObject; @@ -174,7 +172,8 @@ public class LibsDisguises extends JavaPlugin { } try { Object entity = ReflectionManager.getEntityInstance(name); - Entity bukkitEntity = (Entity) entity.getClass().getMethod("getBukkitEntity").invoke(entity); + Entity bukkitEntity = (Entity) ReflectionManager.getNmsClass("Entity").getMethod("getBukkitEntity") + .invoke(entity); int size = 0; for (Field field : ReflectionManager.getNmsClass("Entity").getFields()) { if (field.getType().getName().equals("EnumEntitySize")) { @@ -188,11 +187,6 @@ public class LibsDisguises extends JavaPlugin { List watchers = dataWatcher.getWatchableObjects(); for (WrappedWatchableObject watch : watchers) value.setMetaValue(watch.getTypeID(), watch.getValue()); - WrappedAttribute s; - if (bukkitEntity instanceof LivingEntity) { - value.setAttributesValue("generic.movementSpeed", livingEntity.getAttributeInstance(GenericAttributes.d) - .getValue()); - } DisguiseSound sound = DisguiseSound.getType(disguiseType.name()); if (sound != null) { Float soundStrength = ReflectionManager.getSoundModifier(entity); diff --git a/src/me/libraryaddict/disguise/PacketsManager.java b/src/me/libraryaddict/disguise/PacketsManager.java index ed6089f3..7294ed94 100644 --- a/src/me/libraryaddict/disguise/PacketsManager.java +++ b/src/me/libraryaddict/disguise/PacketsManager.java @@ -4,7 +4,6 @@ import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.Collection; -import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Random; @@ -18,10 +17,13 @@ import me.libraryaddict.disguise.disguisetypes.MobDisguise; import me.libraryaddict.disguise.disguisetypes.PlayerDisguise; import me.libraryaddict.disguise.disguisetypes.Values; import me.libraryaddict.disguise.disguisetypes.DisguiseSound.SoundType; +import net.minecraft.server.v1_6_R3.*; +import org.bukkit.Art; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; +import org.bukkit.craftbukkit.v1_6_R3.entity.*; import org.bukkit.entity.Arrow; import org.bukkit.entity.Entity; import org.bukkit.entity.ExperienceOrb; @@ -42,6 +44,8 @@ import com.comphenix.protocol.events.PacketContainer; import com.comphenix.protocol.events.PacketEvent; import com.comphenix.protocol.events.PacketListener; import com.comphenix.protocol.reflect.StructureModifier; +import com.comphenix.protocol.wrappers.WrappedDataWatcher; +import com.comphenix.protocol.wrappers.WrappedWatchableObject; public class PacketsManager { private static boolean cancelSound; @@ -131,9 +135,12 @@ public class PacketsManager { org.bukkit.inventory.ItemStack itemstack = disguise.getWatcher().getItemStack(slot); if (itemstack != null && itemstack.getTypeId() != 0) { ItemStack item = null; - if (nmsEntity instanceof EntityLiving) - item = ((EntityLiving) nmsEntity).getEquipment(i); - if (item == null) { + if (disguisedEntity instanceof LivingEntity) + if (i != 4) + item = ((LivingEntity) disguisedEntity).getEquipment().getArmorContents()[i]; + else + item = ((LivingEntity) disguisedEntity).getEquipment().getItemInHand(); + if (item == null || item.getType() == Material.AIR) { PacketContainer packet = new PacketContainer(Packets.Server.ENTITY_EQUIPMENT); StructureModifier mods = packet.getModifier(); mods.write(0, disguisedEntity.getEntityId()); @@ -171,8 +178,8 @@ public class PacketsManager { mods.write(4, ((int) loc.getYaw()) % 4); int id = ((MiscDisguise) disguise).getData(); if (id == -1) - id = new Random().nextInt(EnumArt.values().length); - mods.write(5, EnumArt.values()[id % EnumArt.values().length].B); + id = new Random().nextInt(Art.values().length); + mods.write(5, ReflectionManager.getEnumArt(Art.values()[id % Art.values().length])); // Make the teleport packet to make it visible.. spawnPackets[1] = new PacketContainer(Packets.Server.ENTITY_TELEPORT); @@ -202,8 +209,9 @@ public class PacketsManager { } else if (disguisedEntity instanceof LivingEntity) { item = ((LivingEntity) disguisedEntity).getEquipment().getItemInHand(); } - mods.write(8, (item == null || item.getType() == Material.AIR ? 0 : item.getTypeId()); - mods.write(9, createDataWatcher(nmsEntity.getDataWatcher(), disguise.getWatcher())); + mods.write(8, (item == null || item.getType() == Material.AIR ? 0 : item.getTypeId())); + spawnPackets[0].getDataWatcherModifier().write(0, + createDataWatcher(WrappedDataWatcher.getEntityWatcher(disguisedEntity), disguise.getWatcher())); } else if (disguise.getType().isMob()) { @@ -239,7 +247,8 @@ public class PacketsManager { mods.write(9, (byte) (int) (loc.getPitch() * 256.0F / 360.0F)); if (nmsEntity instanceof EntityLiving) mods.write(10, (byte) (int) (((EntityLiving) nmsEntity).aA * 256.0F / 360.0F)); - mods.write(11, createDataWatcher(nmsEntity.getDataWatcher(), disguise.getWatcher())); + spawnPackets[0].getDataWatcherModifier().write(0, + createDataWatcher(WrappedDataWatcher.getEntityWatcher(disguisedEntity), disguise.getWatcher())); // Theres a list sometimes written with this. But no problems have appeared! // Probably just the metadata to be sent. But the next meta packet after fixes that anyways. @@ -309,16 +318,13 @@ public class PacketsManager { /** * Create a new datawatcher but with the 'correct' values */ - private static DataWatcher createDataWatcher(DataWatcher watcher, FlagWatcher flagWatcher) { - DataWatcher newWatcher = new DataWatcher(); + private static WrappedDataWatcher createDataWatcher(WrappedDataWatcher watcher, FlagWatcher flagWatcher) { + WrappedDataWatcher newWatcher = new WrappedDataWatcher(); try { - Field map = newWatcher.getClass().getDeclaredField("c"); - map.setAccessible(true); - HashMap c = (HashMap) map.get(newWatcher); // Calling c() gets the watchable objects exactly as they are. - List list = watcher.c(); - for (WatchableObject watchableObject : flagWatcher.convert(list)) { - c.put(watchableObject.a(), watchableObject); + List list = watcher.getWatchableObjects(); + for (WrappedWatchableObject watchableObject : flagWatcher.convert(list)) { + newWatcher.setObject(watchableObject.getIndex(), watchableObject.getValue()); } } catch (Exception ex) { ex.printStackTrace(); @@ -611,16 +617,16 @@ public class PacketsManager { if (event.getPacketID() == Packets.Server.ENTITY_METADATA) { event.setPacket(event.getPacket().deepClone()); - StructureModifier mods = event.getPacket().getModifier(); - Iterator itel = ((List) mods.read(1)).iterator(); + Iterator itel = event.getPacket().getWatchableCollectionModifier().read(0) + .iterator(); while (itel.hasNext()) { - WatchableObject watch = itel.next(); - if (watch.a() == 0) { - byte b = (Byte) watch.b(); + WrappedWatchableObject watch = itel.next(); + if (watch.getTypeID() == 0) { + byte b = (Byte) watch.getValue(); byte a = (byte) (b | 1 << 5); if ((b & 1 << 3) != 0) a = (byte) (a | 1 << 3); - watch.a(a); + watch.setValue(a); } } } else { @@ -642,7 +648,7 @@ public class PacketsManager { byte b = (byte) (0 | 1 << 5); if (event.getPlayer().isSprinting()) b = (byte) (b | 1 << 3); - watchableList.add(new WatchableObject(0, 0, b)); + watchableList.add(new WrappedWatchableObject(0, b)); mods.write(1, watchableList); try { ProtocolLibrary.getProtocolManager().sendServerPacket(event.getPlayer(), packet, false); @@ -710,7 +716,8 @@ public class PacketsManager { org.bukkit.inventory.ItemStack item = event.getPlayer().getItemInHand(); if (item != null && item.getType() != Material.AIR) { event.setPacket(event.getPacket().shallowClone()); - event.getPacket().getModifier() + event.getPacket() + .getModifier() .write(2, ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(0))); } } @@ -723,8 +730,8 @@ public class PacketsManager { */ case Packets.Server.WINDOW_ITEMS: { event.setPacket(event.getPacket().deepClone()); - StructureModifier mods = event.getPacket().getModifier(); - ItemStack[] items = (ItemStack[]) mods.read(1); + StructureModifier mods = event.getPacket().getItemArrayModifier(); + ItemStack[] items = mods.read(0); for (int slot = 0; slot < items.length; slot++) { if (slot >= 5 && slot <= 8) { if (disguise.isHidingArmorFromSelf()) { @@ -732,7 +739,7 @@ public class PacketsManager { int armorSlot = Math.abs((slot - 5) - 3); org.bukkit.inventory.ItemStack item = event.getPlayer().getInventory().getArmorContents()[armorSlot]; if (item != null && item.getType() != Material.AIR) { - items[slot] = ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(0)); + items[slot] = new org.bukkit.inventory.ItemStack(0); } } // Else if its a hotbar slot @@ -743,12 +750,13 @@ public class PacketsManager { if (slot == currentSlot + 36) { org.bukkit.inventory.ItemStack item = event.getPlayer().getItemInHand(); if (item != null && item.getType() != Material.AIR) { - items[slot] = ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(0)); + items[slot] = new org.bukkit.inventory.ItemStack(0); } } } } } + mods.write(0, items); break; } default: @@ -1006,7 +1014,7 @@ public class PacketsManager { // Resend the armor for (int i = 0; i < 5; ++i) { - ItemStack itemstack = ((EntityLiving) tracker.tracker).getEquipment(i); + net.minecraft.server.v1_6_R3.ItemStack itemstack = ((EntityLiving) tracker.tracker).getEquipment(i); if (itemstack != null) { entityplayer.playerConnection.sendPacket(new Packet5EntityEquipment(player.getEntityId(), i, itemstack)); @@ -1113,22 +1121,8 @@ public class PacketsManager { case Packets.Server.UPDATE_ATTRIBUTES: { - // Grab the values which are 'approved' to be sent for this entity - HashMap values = Values.getAttributesValues(disguise.getType()); - Collection collection = new ArrayList(); - for (AttributeSnapshot att : (List) sentPacket.getModifier().read(1)) { - if (values.containsKey(att.a())) { - collection.add(new AttributeSnapshot(null, att.a(), values.get(att.a()), att.c())); - } - } - if (collection.size() > 0) { - packets[0] = new PacketContainer(sentPacket.getID()); - StructureModifier mods = packets[0].getModifier(); - mods.write(0, entity.getEntityId()); - mods.write(1, collection); - } else { - packets = new PacketContainer[0]; - } + + packets = new PacketContainer[0]; break; } @@ -1136,12 +1130,12 @@ public class PacketsManager { case Packets.Server.ENTITY_METADATA: { - List watchableObjects = disguise.getWatcher().convert( - (List) packets[0].getModifier().read(1)); + List watchableObjects = disguise.getWatcher().convert( + packets[0].getWatchableCollectionModifier().read(0)); packets[0] = new PacketContainer(sentPacket.getID()); StructureModifier newMods = packets[0].getModifier(); newMods.write(0, entity.getEntityId()); - newMods.write(1, watchableObjects); + packets[0].getWatchableCollectionModifier().write(0, watchableObjects); break; } diff --git a/src/me/libraryaddict/disguise/ReflectionManager.java b/src/me/libraryaddict/disguise/ReflectionManager.java index 01d29fd1..321bd638 100644 --- a/src/me/libraryaddict/disguise/ReflectionManager.java +++ b/src/me/libraryaddict/disguise/ReflectionManager.java @@ -1,12 +1,11 @@ package me.libraryaddict.disguise; -import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Field; import java.lang.reflect.Method; -import net.minecraft.server.v1_6_R3.World; - +import org.bukkit.Art; import org.bukkit.Bukkit; -import org.bukkit.craftbukkit.v1_6_R3.inventory.CraftItemStack; +import org.bukkit.Sound; import org.bukkit.inventory.ItemStack; public class ReflectionManager { @@ -20,18 +19,44 @@ public class ReflectionManager { } } + public static String getEnumArt(Art art) { + try { + Class craftArt = Class.forName("org.bukkit.craftbukkit." + bukkitVersion + ".CraftArt"); + Object enumArt = craftArt.getMethod("BukkitToNotch", Art.class).invoke(null, art); + for (Field field : enumArt.getClass().getFields()) { + if (field.getType() == String.class) { + return (String) field.get(enumArt); + } + } + } catch (Exception ex) { + ex.printStackTrace(); + } + return null; + } + + public static String getCraftSound(Sound sound) { + try { + Class c = Class.forName("org.bukkit.craftbukkit." + bukkitVersion + ".CraftSound"); + return (String) c.getMethod("getSound", Sound.class).invoke(null, sound); + } catch (Exception ex) { + ex.printStackTrace(); + } + return null; + } + public static Object getEntityInstance(String entityName) { try { Class entityClass = getNmsClass("Entity" + entityName); Object entityObject; Object world = getWorld(); if (entityName.equals("Human")) { - entityObject = entityClass.getConstructor(world.getClass(), String.class).newInstance(world, "LibsDisguises"); + entityObject = entityClass.getConstructor(getNmsClass("World"), String.class).newInstance(world, "LibsDisguises"); } else { - entityObject = entityClass.getConstructor(world.getClass()).newInstance(world); + entityObject = entityClass.getConstructor(getNmsClass("World")).newInstance(world); } return entityObject; } catch (Exception e) { + e.printStackTrace(); } return null; } @@ -58,7 +83,9 @@ public class ReflectionManager { private static Object getWorld() { try { - return World.class.getMethod("getHandle").invoke(Bukkit.getWorlds().get(0)); + return Class.forName("org.bukkit.craftbukkit." + bukkitVersion + ".CraftWorld").getMethod("getHandle") + .invoke(Bukkit.getWorlds().get(0)); + } catch (Exception e) { e.printStackTrace(); } diff --git a/src/me/libraryaddict/disguise/disguisetypes/Disguise.java b/src/me/libraryaddict/disguise/disguisetypes/Disguise.java index f4b16c06..c88055c1 100644 --- a/src/me/libraryaddict/disguise/disguisetypes/Disguise.java +++ b/src/me/libraryaddict/disguise/disguisetypes/Disguise.java @@ -1,25 +1,25 @@ package me.libraryaddict.disguise.disguisetypes; +import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; -import java.util.List; - import me.libraryaddict.disguise.DisguiseAPI; import me.libraryaddict.disguise.PacketsManager; +import me.libraryaddict.disguise.ReflectionManager; import me.libraryaddict.disguise.disguisetypes.watchers.AgeableWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.HorseWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.ZombieWatcher; import net.minecraft.server.v1_6_R3.EntityAgeable; import net.minecraft.server.v1_6_R3.EntityInsentient; import net.minecraft.server.v1_6_R3.EntityLiving; -import net.minecraft.server.v1_6_R3.EntityPlayer; import net.minecraft.server.v1_6_R3.EntityTrackerEntry; import net.minecraft.server.v1_6_R3.WorldServer; import org.bukkit.Location; import org.bukkit.craftbukkit.v1_6_R3.entity.CraftEntity; +import org.bukkit.entity.Entity; import org.bukkit.entity.Horse.Variant; import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; @@ -190,10 +190,7 @@ public abstract class Disguise { i++; if (i % 40 == 0) { i = 0; - List players = new ArrayList(); - for (EntityPlayer p : getPerverts()) - players.add(p.getBukkitEntity()); - ProtocolLibrary.getProtocolManager().updateEntity(getEntity(), players); + ProtocolLibrary.getProtocolManager().updateEntity(getEntity(), getPerverts()); } } // If the vectorY isn't 0. Cos if it is. Then it doesn't want to send any vectors. @@ -219,35 +216,36 @@ public abstract class Disguise { mods.write(5, (byte) Math.floor(loc.getPitch() * 256.0F / 360.0F)); selfLookPacket = lookPacket.shallowClone(); } - for (EntityPlayer player : getPerverts()) { - PacketContainer packet = new PacketContainer(Packets.Server.ENTITY_VELOCITY); - StructureModifier mods = packet.getModifier(); - if (entity == player.getBukkitEntity()) { - if (!viewSelfDisguise()) - continue; - if (selfLookPacket != null) { - try { - ProtocolLibrary.getProtocolManager().sendServerPacket(player.getBukkitEntity(), - selfLookPacket, false); - } catch (InvocationTargetException e) { - e.printStackTrace(); + try { + Field ping = ReflectionManager.getNmsClass("EntityPlayer").getField("ping"); + Field handle = Entity.class.getField("getHandle"); + for (Player player : getPerverts()) { + PacketContainer packet = new PacketContainer(Packets.Server.ENTITY_VELOCITY); + StructureModifier mods = packet.getModifier(); + if (entity == player) { + if (!viewSelfDisguise()) + continue; + if (selfLookPacket != null) { + try { + ProtocolLibrary.getProtocolManager().sendServerPacket(player, selfLookPacket, + false); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } } - } - mods.write(0, DisguiseAPI.getFakeDisguise(entity.getEntityId())); - } else - mods.write(0, entity.getEntityId()); - mods.write(1, (int) (vector.getX() * 8000)); - mods.write(2, (int) (8000 * (vectorY * (double) player.ping * 0.069))); - mods.write(3, (int) (vector.getZ() * 8000)); - try { + mods.write(0, DisguiseAPI.getFakeDisguise(entity.getEntityId())); + } else + mods.write(0, entity.getEntityId()); + mods.write(1, (int) (vector.getX() * 8000)); + mods.write(2, (int) (8000 * (vectorY * (double) ping.getInt(handle.get(player)) * 0.069))); + mods.write(3, (int) (vector.getZ() * 8000)); if (lookPacket != null) - ProtocolLibrary.getProtocolManager().sendServerPacket(player.getBukkitEntity(), - lookPacket, false); - ProtocolLibrary.getProtocolManager() - .sendServerPacket(player.getBukkitEntity(), packet, false); - } catch (InvocationTargetException e) { - e.printStackTrace(); + ProtocolLibrary.getProtocolManager().sendServerPacket(player, lookPacket, false); + ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet, false); + } + } catch (Exception e) { + e.printStackTrace(); } } // If we need to send more packets because else it still 'sinks' @@ -255,10 +253,10 @@ public abstract class Disguise { PacketContainer packet = new PacketContainer(Packets.Server.REL_ENTITY_MOVE); StructureModifier mods = packet.getModifier(); mods.write(0, entity.getEntityId()); - for (EntityPlayer player : getPerverts()) { + for (Player player : getPerverts()) { if (DisguiseAPI.isViewDisguises() || entity != player) { try { - ProtocolLibrary.getProtocolManager().sendServerPacket(player.getBukkitEntity(), packet); + ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet); } catch (InvocationTargetException e) { e.printStackTrace(); } @@ -281,14 +279,21 @@ public abstract class Disguise { /** * Get all EntityPlayers who have this entity in their Entity Tracker */ - protected EntityPlayer[] getPerverts() { - EntityTrackerEntry entry = (EntityTrackerEntry) ((WorldServer) ((CraftEntity) entity).getHandle().world).tracker.trackedEntities - .get(entity.getEntityId()); - if (entry != null) { - EntityPlayer[] players = (EntityPlayer[]) entry.trackedPlayers.toArray(new EntityPlayer[entry.trackedPlayers.size()]); - return players; + protected ArrayList getPerverts() { + ArrayList players = new ArrayList(); + try { + EntityTrackerEntry entry = (EntityTrackerEntry) ((WorldServer) ((CraftEntity) entity).getHandle().world).tracker.trackedEntities + .get(entity.getEntityId()); + if (entry != null) { + Field field = ReflectionManager.getNmsClass("Entity").getField("getBukkitEntity"); + for (Object p : entry.trackedPlayers) { + players.add((Player) field.get(p)); + } + } + } catch (Exception ex) { + ex.printStackTrace(); } - return new EntityPlayer[0]; + return players; } /** diff --git a/src/me/libraryaddict/disguise/disguisetypes/DisguiseSound.java b/src/me/libraryaddict/disguise/disguisetypes/DisguiseSound.java index 2707fcb6..b3251eb0 100644 --- a/src/me/libraryaddict/disguise/disguisetypes/DisguiseSound.java +++ b/src/me/libraryaddict/disguise/disguisetypes/DisguiseSound.java @@ -3,8 +3,9 @@ package me.libraryaddict.disguise.disguisetypes; import java.util.HashMap; import java.util.HashSet; +import me.libraryaddict.disguise.ReflectionManager; + import org.bukkit.Sound; -import org.bukkit.craftbukkit.v1_6_R3.CraftSound; /** * Only living disguises go in here! @@ -103,10 +104,6 @@ public enum DisguiseSound { CANCEL, DEATH, HURT, IDLE, STEP; } - public static String getSoundName(Sound sound) { - return CraftSound.getSound(sound); - } - public static DisguiseSound getType(String name) { try { return valueOf(name); @@ -128,7 +125,7 @@ public enum DisguiseSound { else if (obj instanceof String) s = (String) obj; else if (obj instanceof Sound) - s = CraftSound.getSound((Sound) obj); + s = ReflectionManager.getCraftSound((Sound) obj); else throw new RuntimeException("Was given a unknown object " + obj); switch (i) { @@ -195,7 +192,7 @@ public enum DisguiseSound { } public void removeSound(SoundType type, Sound sound) { - removeSound(type, CraftSound.getSound(sound)); + removeSound(type, ReflectionManager.getCraftSound(sound)); } public void removeSound(SoundType type, String sound) { @@ -211,7 +208,7 @@ public enum DisguiseSound { } public void setSound(SoundType type, Sound sound) { - setSound(type, CraftSound.getSound(sound)); + setSound(type, ReflectionManager.getCraftSound(sound)); } public void setSound(SoundType type, String sound) { diff --git a/src/me/libraryaddict/disguise/disguisetypes/FlagWatcher.java b/src/me/libraryaddict/disguise/disguisetypes/FlagWatcher.java index fd9bbc29..f20d1556 100644 --- a/src/me/libraryaddict/disguise/disguisetypes/FlagWatcher.java +++ b/src/me/libraryaddict/disguise/disguisetypes/FlagWatcher.java @@ -7,7 +7,6 @@ import java.util.HashSet; import java.util.Iterator; import java.util.List; -import org.bukkit.craftbukkit.v1_6_R3.inventory.CraftItemStack; import org.bukkit.entity.Entity; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; @@ -17,14 +16,11 @@ import com.comphenix.protocol.Packets; import com.comphenix.protocol.ProtocolLibrary; import com.comphenix.protocol.events.PacketContainer; import com.comphenix.protocol.reflect.StructureModifier; +import com.comphenix.protocol.wrappers.WrappedWatchableObject; import me.libraryaddict.disguise.DisguiseAPI; import me.libraryaddict.disguise.PacketsManager; import me.libraryaddict.disguise.ReflectionManager; -import net.minecraft.server.v1_6_R3.ChunkCoordinates; -import net.minecraft.server.v1_6_R3.EntityPlayer; -import net.minecraft.server.v1_6_R3.ItemStack; -import net.minecraft.server.v1_6_R3.WatchableObject; public class FlagWatcher { public enum SlotType { @@ -40,16 +36,6 @@ public class FlagWatcher { } } - private static HashMap classTypes = new HashMap(); - static { - classTypes.put(Byte.class, 0); - classTypes.put(Short.class, 1); - classTypes.put(Integer.class, 2); - classTypes.put(Float.class, 3); - classTypes.put(String.class, 4); - classTypes.put(ItemStack.class, 5); - classTypes.put(ChunkCoordinates.class, 6); - } /** * This is the entity values I need to add else it could crash them.. */ @@ -68,7 +54,6 @@ public class FlagWatcher { try { cloned = getClass().getConstructor(Disguise.class).newInstance(disguise); } catch (Exception e) { - // TODO Auto-generated catch block e.printStackTrace(); } cloned.entityValues = (HashMap) entityValues.clone(); @@ -76,14 +61,14 @@ public class FlagWatcher { return cloned; } - public List convert(List list) { - Iterator itel = list.iterator(); - List newList = new ArrayList(); + public List convert(List list) { + Iterator itel = list.iterator(); + List newList = new ArrayList(); HashSet sentValues = new HashSet(); boolean sendAllCustom = false; while (itel.hasNext()) { - WatchableObject watch = itel.next(); - int dataType = watch.a(); + WrappedWatchableObject watch = itel.next(); + int dataType = watch.getTypeID(); sentValues.add(dataType); // Its sending the air metadata. This is the least commonly sent metadata which all entitys still share. // I send my custom values if I see this! @@ -100,15 +85,15 @@ public class FlagWatcher { value = backupEntityValues.get(dataType); } if (value != null) { - boolean doD = watch.d(); - watch = new WatchableObject(classTypes.get(value.getClass()), dataType, value); + boolean doD = watch.getDirtyState(); + watch = new WrappedWatchableObject(dataType, value); if (!doD) - watch.a(false); + watch.setDirtyState(doD); } else { - boolean doD = watch.d(); - watch = new WatchableObject(watch.c(), dataType, watch.b()); + boolean doD = watch.getDirtyState(); + watch = new WrappedWatchableObject(dataType, watch.getValue()); if (!doD) - watch.a(false); + watch.setDirtyState(doD); } newList.add(watch); } @@ -120,16 +105,16 @@ public class FlagWatcher { Object obj = entityValues.get(value); if (obj == null) continue; - WatchableObject watch = new WatchableObject(classTypes.get(obj.getClass()), value, obj); + WrappedWatchableObject watch = new WrappedWatchableObject(value, obj); newList.add(watch); } } // Here we check for if there is a health packet that says they died. if (disguise.viewSelfDisguise() && disguise.getEntity() != null && disguise.getEntity() instanceof Player) { - for (WatchableObject watch : newList) { + for (WrappedWatchableObject watch : newList) { // Its a health packet - if (watch.a() == 6) { - Object value = watch.b(); + if (watch.getTypeID() == 6) { + Object value = watch.getValue(); if (value != null && value instanceof Float) { float newHealth = (Float) value; if (newHealth > 0 && hasDied) { @@ -210,17 +195,16 @@ public class FlagWatcher { return; Entity entity = disguise.getEntity(); Object value = entityValues.get(data); - List list = new ArrayList(); - list.add(new WatchableObject(classTypes.get(value.getClass()), data, value)); + List list = new ArrayList(); + list.add(new WrappedWatchableObject(data, value)); PacketContainer packet = new PacketContainer(Packets.Server.ENTITY_METADATA); StructureModifier mods = packet.getModifier(); mods.write(0, entity.getEntityId()); - mods.write(1, list); - for (EntityPlayer player : disguise.getPerverts()) { - Player p = player.getBukkitEntity(); - if (DisguiseAPI.isViewDisguises() || p != entity) { + packet.getWatchableCollectionModifier().write(0, list); + for (Player player : disguise.getPerverts()) { + if (DisguiseAPI.isViewDisguises() || player != entity) { try { - ProtocolLibrary.getProtocolManager().sendServerPacket(p, packet); + ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet); } catch (InvocationTargetException e) { e.printStackTrace(); } @@ -277,7 +261,7 @@ public class FlagWatcher { } } - ItemStack itemToSend = null; + Object itemToSend = null; if (itemStack != null && itemStack.getTypeId() != 0) itemToSend = ReflectionManager.getNmsItem(itemStack); items[slot] = itemStack; @@ -291,11 +275,10 @@ public class FlagWatcher { mods.write(0, disguise.getEntity().getEntityId()); mods.write(1, slot); mods.write(2, itemToSend); - for (EntityPlayer player : disguise.getPerverts()) { - Player p = player.getBukkitEntity(); - if (p != disguise.getEntity()) { + for (Player player : disguise.getPerverts()) { + if (player != disguise.getEntity()) { try { - ProtocolLibrary.getProtocolManager().sendServerPacket(p, packet); + ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet); } catch (InvocationTargetException e) { e.printStackTrace(); } diff --git a/src/me/libraryaddict/disguise/disguisetypes/Values.java b/src/me/libraryaddict/disguise/disguisetypes/Values.java index dd913b98..6acedb3b 100644 --- a/src/me/libraryaddict/disguise/disguisetypes/Values.java +++ b/src/me/libraryaddict/disguise/disguisetypes/Values.java @@ -2,15 +2,13 @@ package me.libraryaddict.disguise.disguisetypes; import java.util.HashMap; -import net.minecraft.server.v1_6_R3.EnumEntitySize; - public class Values { private static HashMap values = new HashMap(); - public static HashMap getAttributesValues(DisguiseType type) { + /* public static HashMap getAttributesValues(DisguiseType type) { return getValues(type).getAttributesValues(); - } + }*/ public static Class getEntityClass(DisguiseType type) { return getValues(type).getEntityClass(); @@ -47,7 +45,7 @@ public class Values { return values.get(type); } - private HashMap attributesValues = new HashMap(); + // private HashMap attributesValues = new HashMap(); private Class declared; private int enumEntitySize; @@ -60,9 +58,9 @@ public class Values { declared = classType; } - public HashMap getAttributesValues() { + /*public HashMap getAttributesValues() { return attributesValues; - } + }*/ public Class getEntityClass() { return declared; @@ -115,9 +113,9 @@ public class Values { return metaValues; } - public void setAttributesValue(String attribute, Double value) { + /*public void setAttributesValue(String attribute, Double value) { attributesValues.put(attribute, value); - } + }*/ public void setMetaValue(int no, Object value) { metaValues.put(no, value); diff --git a/src/me/libraryaddict/disguise/disguisetypes/watchers/DroppedItemWatcher.java b/src/me/libraryaddict/disguise/disguisetypes/watchers/DroppedItemWatcher.java index 15830fe1..250ac3d0 100644 --- a/src/me/libraryaddict/disguise/disguisetypes/watchers/DroppedItemWatcher.java +++ b/src/me/libraryaddict/disguise/disguisetypes/watchers/DroppedItemWatcher.java @@ -4,7 +4,6 @@ import me.libraryaddict.disguise.ReflectionManager; import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.FlagWatcher; -import org.bukkit.craftbukkit.v1_6_R3.inventory.CraftItemStack; import org.bukkit.inventory.ItemStack; public class DroppedItemWatcher extends FlagWatcher { @@ -14,8 +13,7 @@ public class DroppedItemWatcher extends FlagWatcher { } public ItemStack getItemStack() { - return CraftItemStack.asBukkitCopy((net.minecraft.server.v1_6_R3.ItemStack) getValue(10, - ReflectionManager.getNmsItem(new ItemStack(1)))); + return ReflectionManager.getBukkitItem(getValue(10, ReflectionManager.getNmsItem(new ItemStack(1)))); } public void setItemStack(ItemStack item) { diff --git a/src/me/libraryaddict/disguise/disguisetypes/watchers/ItemFrameWatcher.java b/src/me/libraryaddict/disguise/disguisetypes/watchers/ItemFrameWatcher.java index 47d2fceb..b1d07aea 100644 --- a/src/me/libraryaddict/disguise/disguisetypes/watchers/ItemFrameWatcher.java +++ b/src/me/libraryaddict/disguise/disguisetypes/watchers/ItemFrameWatcher.java @@ -4,7 +4,6 @@ import me.libraryaddict.disguise.ReflectionManager; import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.FlagWatcher; -import org.bukkit.craftbukkit.v1_6_R3.inventory.CraftItemStack; import org.bukkit.inventory.ItemStack; public class ItemFrameWatcher extends FlagWatcher { @@ -20,7 +19,7 @@ public class ItemFrameWatcher extends FlagWatcher { public ItemStack getItemStack() { if (getValue(2, null) == null) return new ItemStack(0); - return CraftItemStack.asBukkitCopy((net.minecraft.server.v1_6_R3.ItemStack) getValue(2, null)); + return ReflectionManager.getBukkitItem(getValue(2, null)); } public void setItemRotation(int rotation) { diff --git a/src/me/libraryaddict/disguise/disguisetypes/watchers/LivingWatcher.java b/src/me/libraryaddict/disguise/disguisetypes/watchers/LivingWatcher.java index 4b683a36..f3ab2b6a 100644 --- a/src/me/libraryaddict/disguise/disguisetypes/watchers/LivingWatcher.java +++ b/src/me/libraryaddict/disguise/disguisetypes/watchers/LivingWatcher.java @@ -1,18 +1,38 @@ package me.libraryaddict.disguise.disguisetypes.watchers; +import java.lang.reflect.Method; import java.util.HashSet; -import java.util.Iterator; - +import me.libraryaddict.disguise.ReflectionManager; import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.FlagWatcher; -import net.minecraft.server.v1_6_R3.MobEffect; -import net.minecraft.server.v1_6_R3.PotionBrewer; import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; public class LivingWatcher extends FlagWatcher { - private HashSet potionEffects = new HashSet(); + private HashSet potionEffects = new HashSet(); + static Object[] list; + static Method potionNo; + static { + try { + Class mobEffectList = ReflectionManager.getNmsClass("MobEffectList"); + list = (Object[]) mobEffectList.getField("byId").get(null); + for (Object obj : list) { + if (obj != null) { + for (Method field : obj.getClass().getMethods()) { + if (field.getReturnType() == int.class) { + if ((Integer) field.invoke(obj) > 10000) { + potionNo = field; + break; + } + } + } + } + } + } catch (Exception ex) { + ex.printStackTrace(); + } + } public LivingWatcher(Disguise disguise) { super(disguise); @@ -22,21 +42,21 @@ public class LivingWatcher extends FlagWatcher { public void addPotionEffect(PotionEffect potionEffect) { if (hasPotionEffect(potionEffect.getType())) removePotionEffect(potionEffect.getType()); - potionEffects.add(new MobEffect(potionEffect.getType().getId(), potionEffect.getDuration(), potionEffect.getAmplifier())); + potionEffects.add(potionEffect.getType().getId()); sendPotionEffects(); } public void addPotionEffect(PotionEffectType potionEffect) { if (hasPotionEffect(potionEffect)) removePotionEffect(potionEffect); - potionEffects.add(new MobEffect(potionEffect.getId(), 0, 0)); + potionEffects.add(potionEffect.getId()); sendPotionEffects(); } @Override public LivingWatcher clone(Disguise disguise) { LivingWatcher clone = (LivingWatcher) super.clone(disguise); - clone.potionEffects = (HashSet) potionEffects.clone(); + clone.potionEffects = (HashSet) potionEffects.clone(); return clone; } @@ -57,10 +77,7 @@ public class LivingWatcher extends FlagWatcher { } public boolean hasPotionEffect(PotionEffectType type) { - for (MobEffect effect : potionEffects) - if (effect.getEffectId() == type.getId()) - return true; - return false; + return potionEffects.contains(type.getId()); } public boolean isCustomNameVisible() { @@ -68,14 +85,9 @@ public class LivingWatcher extends FlagWatcher { } public void removePotionEffect(PotionEffectType type) { - Iterator itel = potionEffects.iterator(); - while (itel.hasNext()) { - MobEffect effect = itel.next(); - if (effect.getEffectId() == type.getId()) { - itel.remove(); - sendPotionEffects(); - return; - } + if (potionEffects.contains(type.getId())) { + potionEffects.remove(type.getId()); + sendPotionEffects(); } } @@ -85,10 +97,40 @@ public class LivingWatcher extends FlagWatcher { } private void sendPotionEffects() { - setValue(7, PotionBrewer.a(potionEffects)); + setValue(7, getPotions()); sendData(7); } + private int getPotions() { + int m = 3694022; + + if (potionEffects.isEmpty()) { + return m; + } + + float f1 = 0.0F; + float f2 = 0.0F; + float f3 = 0.0F; + float f4 = 0.0F; + try { + for (int localMobEffect : potionEffects) { + int n = (Integer) potionNo.invoke(list[localMobEffect]); + f1 += (n >> 16 & 0xFF) / 255.0F; + f2 += (n >> 8 & 0xFF) / 255.0F; + f3 += (n >> 0 & 0xFF) / 255.0F; + f4 += 1.0F; + } + } catch (Exception ex) { + ex.printStackTrace(); + } + + f1 = f1 / f4 * 255.0F; + f2 = f2 / f4 * 255.0F; + f3 = f3 / f4 * 255.0F; + + return (int) f1 << 16 | (int) f2 << 8 | (int) f3; + } + public void setCustomName(String name) { if (name.length() > 64) name = name.substring(0, 64);