From 01597b4f5ddbeb06b6b651dd4fc782de82b09aa4 Mon Sep 17 00:00:00 2001 From: Andrew Date: Tue, 13 Aug 2013 15:19:50 +1200 Subject: [PATCH] Read desc Moved packet listeners to PacketsManager Fixed a bug with FlagWatcher setting values it shouldn't Cleaned up api calls Cleaned up code Added commenting --- .../libraryaddict/disguise/DisguiseAPI.java | 411 ++-------- .../disguise/DisguiseTypes/Disguise.java | 64 +- .../disguise/DisguiseTypes/FlagWatcher.java | 2 +- .../libraryaddict/disguise/LibsDisguises.java | 447 +---------- .../disguise/PacketsManager.java | 750 ++++++++++++++++++ 5 files changed, 849 insertions(+), 825 deletions(-) create mode 100644 src/me/libraryaddict/disguise/PacketsManager.java diff --git a/src/me/libraryaddict/disguise/DisguiseAPI.java b/src/me/libraryaddict/disguise/DisguiseAPI.java index 206dddb0..da01ea8f 100644 --- a/src/me/libraryaddict/disguise/DisguiseAPI.java +++ b/src/me/libraryaddict/disguise/DisguiseAPI.java @@ -1,23 +1,13 @@ package me.libraryaddict.disguise; 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; - import me.libraryaddict.disguise.DisguiseTypes.Disguise; -import me.libraryaddict.disguise.DisguiseTypes.DisguiseSound; -import me.libraryaddict.disguise.DisguiseTypes.DisguiseSound.SoundType; -import me.libraryaddict.disguise.DisguiseTypes.DisguiseType; -import me.libraryaddict.disguise.DisguiseTypes.MobDisguise; import me.libraryaddict.disguise.Events.DisguiseEvent; import me.libraryaddict.disguise.Events.UndisguiseEvent; import net.minecraft.server.v1_6_R2.AttributeMapServer; -import net.minecraft.server.v1_6_R2.Block; import net.minecraft.server.v1_6_R2.EntityHuman; import net.minecraft.server.v1_6_R2.EntityInsentient; import net.minecraft.server.v1_6_R2.EntityLiving; @@ -34,27 +24,17 @@ import net.minecraft.server.v1_6_R2.Packet40EntityMetadata; import net.minecraft.server.v1_6_R2.Packet41MobEffect; import net.minecraft.server.v1_6_R2.Packet44UpdateAttributes; import net.minecraft.server.v1_6_R2.Packet5EntityEquipment; -import net.minecraft.server.v1_6_R2.WatchableObject; -import net.minecraft.server.v1_6_R2.World; import net.minecraft.server.v1_6_R2.WorldServer; import org.bukkit.Bukkit; -import org.bukkit.Location; import org.bukkit.craftbukkit.v1_6_R2.entity.CraftEntity; -import org.bukkit.craftbukkit.v1_6_R2.entity.CraftLivingEntity; import org.bukkit.craftbukkit.v1_6_R2.entity.CraftPlayer; import org.bukkit.entity.Entity; -import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; import com.comphenix.protocol.Packets; import com.comphenix.protocol.ProtocolLibrary; -import com.comphenix.protocol.events.ConnectionSide; -import com.comphenix.protocol.events.ListenerPriority; -import com.comphenix.protocol.events.PacketAdapter; import com.comphenix.protocol.events.PacketContainer; -import com.comphenix.protocol.events.PacketEvent; -import com.comphenix.protocol.events.PacketListener; import com.comphenix.protocol.reflect.StructureModifier; public class DisguiseAPI { @@ -62,12 +42,8 @@ public class DisguiseAPI { private static HashMap disguises = new HashMap(); private static boolean hearSelfDisguise; private static LibsDisguises libsDisguises; - private static PacketListener packetListener; private static HashMap selfDisguisesIds = new HashMap(); private static boolean sendVelocity; - private static boolean soundsEnabled; - private static boolean viewDisguises; - private static PacketListener viewDisguisesListener; public static boolean canHearSelfDisguise() { return hearSelfDisguise; @@ -93,42 +69,43 @@ public class DisguiseAPI { * - The disguise to wear */ public static void disguiseToAll(Entity entity, Disguise disguise) { + // If they are trying to disguise a null entity or use a null disguise + // Just return. if (entity == null || disguise == null) return; - Disguise oldDisguise = getDisguise(entity); + // Fire a disguise event DisguiseEvent event = new DisguiseEvent(entity, disguise); Bukkit.getPluginManager().callEvent(event); - if (event.isCancelled()) { + // If they cancelled this disguise event. No idea why. + // Just return. + if (event.isCancelled()) return; - } else if (oldDisguise != null) { + // The event wasn't cancelled. Got to discard the old disguise + Disguise oldDisguise = getDisguise(entity); + // If there was a old disguise + if (oldDisguise != null) { oldDisguise.getScheduler().cancel(); } - + // If the disguise entity isn't the same as the one we are disguising if (disguise.getEntity() != entity) { + // If the disguise entity actually exists if (disguise.getEntity() != null) { + // Clone the disguise disguise = disguise.clone(); } + // Set the disguise's entity disguise.setEntity(entity); } + // Stick the disguise in the disguises bin disguises.put(entity.getEntityId(), disguise); + // Resend the disguised entity's packet refresh(entity); + // If he is a player, then self disguise himself setupPlayer(disguise); } - public static void enableSounds(boolean isSoundsEnabled) { - if (soundsEnabled != isSoundsEnabled) { - soundsEnabled = isSoundsEnabled; - if (soundsEnabled) { - ProtocolLibrary.getProtocolManager().addPacketListener(packetListener); - } else { - ProtocolLibrary.getProtocolManager().removePacketListener(packetListener); - } - } - } - /** - * @param Disguiser - * @return Disguise + * Get the disguise of a entity */ public static Disguise getDisguise(Entity disguiser) { if (disguiser == null) @@ -136,11 +113,9 @@ public class DisguiseAPI { return disguises.get(disguiser.getEntityId()); } - @Deprecated - public static Disguise getDisguise(Object disguiser) { - return getDisguise((Entity) disguiser); - } - + /** + * Get the ID of a fake disguise for a entityplayer + */ public static int getFakeDisguise(int id) { if (selfDisguisesIds.containsKey(id)) return selfDisguisesIds.get(id); @@ -149,248 +124,6 @@ public class DisguiseAPI { protected static void init(LibsDisguises mainPlugin) { libsDisguises = mainPlugin; - packetListener = new PacketAdapter(libsDisguises, ConnectionSide.SERVER_SIDE, ListenerPriority.NORMAL, - Packets.Server.NAMED_SOUND_EFFECT, Packets.Server.ENTITY_STATUS) { - @Override - public void onPacketSending(PacketEvent event) { - StructureModifier mods = event.getPacket().getModifier(); - Player observer = event.getPlayer(); - if (event.getPacketID() == Packets.Server.NAMED_SOUND_EFFECT) { - String soundName = (String) mods.read(0); - SoundType soundType = null; - Location soundLoc = new Location(observer.getWorld(), ((Integer) mods.read(1)) / 8D, - ((Integer) mods.read(2)) / 8D, ((Integer) mods.read(3)) / 8D); - Entity disguisedEntity = null; - DisguiseSound entitySound = null; - for (Entity entity : soundLoc.getChunk().getEntities()) { - if (DisguiseAPI.isDisguised(entity)) { - Location loc = entity.getLocation(); - loc = new Location(observer.getWorld(), ((int) (loc.getX() * 8)) / 8D, ((int) (loc.getY() * 8)) / 8D, - ((int) (loc.getZ() * 8)) / 8D); - if (loc.equals(soundLoc)) { - entitySound = DisguiseSound.getType(entity.getType().name()); - if (entitySound != null) { - if (entity instanceof LivingEntity && ((LivingEntity) entity).getHealth() == 0) { - soundType = SoundType.DEATH; - } else { - boolean hasInvun = false; - if (entity instanceof LivingEntity) { - net.minecraft.server.v1_6_R2.EntityLiving e = ((CraftLivingEntity) entity) - .getHandle(); - hasInvun = (e.noDamageTicks == e.maxNoDamageTicks); - } else { - net.minecraft.server.v1_6_R2.Entity e = ((CraftEntity) entity).getHandle(); - hasInvun = e.isInvulnerable(); - } - soundType = entitySound.getType(soundName, !hasInvun); - } - if (soundType != null) { - disguisedEntity = entity; - break; - } - } - } - } - } - Disguise disguise = DisguiseAPI.getDisguise(disguisedEntity); - if (disguise != null && (disguise.canHearSelfDisguise() || disguisedEntity != event.getPlayer())) { - if (disguise.replaceSounds()) { - String sound = null; - DisguiseSound dSound = DisguiseSound.getType(disguise.getType().name()); - if (dSound != null && soundType != null) - sound = dSound.getSound(soundType); - if (sound == null) { - event.setCancelled(true); - } else { - if (sound.equals("step.grass")) { - World world = ((CraftEntity) disguisedEntity).getHandle().world; - Block b = Block.byId[world.getTypeId(soundLoc.getBlockX(), soundLoc.getBlockY() - 1, - soundLoc.getBlockZ())]; - if (b != null) - mods.write(0, b.stepSound.getStepSound()); - // There is no else statement. Because seriously. This should never be null. Unless someone is - // sending fake sounds. In which case. Why cancel it. - } else { - mods.write(0, sound); - // Time to change the pitch and volume - if (soundType == SoundType.HURT || soundType == SoundType.DEATH - || soundType == SoundType.IDLE) { - // If the volume is the default - if (soundType != SoundType.IDLE - && ((Float) mods.read(4)).equals(entitySound.getDamageSoundVolume())) { - mods.write(4, dSound.getDamageSoundVolume()); - } - // Here I assume its the default pitch as I can't calculate if its real. - if (disguise instanceof MobDisguise && disguisedEntity instanceof LivingEntity - && ((MobDisguise) disguise).doesDisguiseAge()) { - boolean baby = ((CraftLivingEntity) disguisedEntity).getHandle().isBaby(); - if (((MobDisguise) disguise).isAdult() == baby) { - - float pitch = (Integer) mods.read(5); - if (baby) { - // If the pitch is not the expected - if (pitch > 97 || pitch < 111) - return; - pitch = (new Random().nextFloat() - new Random().nextFloat()) * 0.2F + 1.5F; - // Min = 1.5 - // Cap = 97.5 - // Max = 1.7 - // Cap = 110.5 - } else { - // If the pitch is not the expected - if (pitch >= 63 || pitch <= 76) - return; - pitch = (new Random().nextFloat() - new Random().nextFloat()) * 0.2F + 1.0F; - // Min = 1 - // Cap = 63 - // Max = 1.2 - // Cap = 75.6 - } - pitch *= 63; - if (pitch < 0) - pitch = 0; - if (pitch > 255) - pitch = 255; - mods.write(5, (int) pitch); - } - } - } - } - } - } - } - } else if (event.getPacketID() == Packets.Server.ENTITY_STATUS) { - if ((Byte) mods.read(1) == 2) { - // It made a damage animation - Entity entity = event.getPacket().getEntityModifier(observer.getWorld()).read(0); - Disguise disguise = getDisguise(entity); - if (disguise != null && (disguise.canHearSelfDisguise() || entity != event.getPlayer())) { - DisguiseSound disSound = DisguiseSound.getType(entity.getType().name()); - if (disSound == null) - return; - SoundType soundType = null; - if (entity instanceof LivingEntity && ((LivingEntity) entity).getHealth() == 0) { - soundType = SoundType.DEATH; - } else { - soundType = SoundType.HURT; - } - if (disSound.getSound(soundType) == null - || (soundType != null && disguise.canHearSelfDisguise() && entity == event.getPlayer())) { - disSound = DisguiseSound.getType(disguise.getType().name()); - if (disSound != null) { - String sound = disSound.getSound(soundType); - if (sound != null) { - Location loc = entity.getLocation(); - PacketContainer packet = new PacketContainer(Packets.Server.NAMED_SOUND_EFFECT); - mods = packet.getModifier(); - mods.write(0, sound); - mods.write(1, (int) (loc.getX() * 8D)); - mods.write(2, (int) (loc.getY() * 8D)); - mods.write(3, (int) (loc.getZ() * 8D)); - mods.write(4, disSound.getDamageSoundVolume()); - float pitch; - if (disguise instanceof MobDisguise && !((MobDisguise) disguise).isAdult()) { - pitch = (new Random().nextFloat() - new Random().nextFloat()) * 0.2F + 1.5F; - } else - pitch = (new Random().nextFloat() - new Random().nextFloat()) * 0.2F + 1.0F; - if (disguise.getType() == DisguiseType.BAT) - pitch *= 95F; - pitch *= 63; - if (pitch < 0) - pitch = 0; - if (pitch > 255) - pitch = 255; - mods.write(5, (int) pitch); - try { - ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet); - } catch (InvocationTargetException e) { - e.printStackTrace(); - } - } - } - } - } - } - } - } - }; - viewDisguisesListener = new PacketAdapter(libsDisguises, ConnectionSide.SERVER_SIDE, ListenerPriority.HIGHEST, - Packets.Server.NAMED_ENTITY_SPAWN, Packets.Server.ATTACH_ENTITY, Packets.Server.REL_ENTITY_MOVE, - Packets.Server.REL_ENTITY_MOVE_LOOK, Packets.Server.ENTITY_LOOK, Packets.Server.ENTITY_TELEPORT, - Packets.Server.ENTITY_HEAD_ROTATION, Packets.Server.ENTITY_METADATA, Packets.Server.ENTITY_EQUIPMENT, - Packets.Server.ARM_ANIMATION, Packets.Server.ENTITY_LOCATION_ACTION, Packets.Server.MOB_EFFECT, - Packets.Server.ENTITY_STATUS, Packets.Server.ENTITY_VELOCITY, Packets.Server.UPDATE_ATTRIBUTES) { - @Override - public void onPacketSending(PacketEvent event) { - StructureModifier entityModifer = event.getPacket().getEntityModifier(event.getPlayer().getWorld()); - org.bukkit.entity.Entity entity = entityModifer.read(0); - if (entity == event.getPlayer() && selfDisguisesIds.containsKey(entity.getEntityId())) { - PacketContainer[] packets = libsDisguises.transformPacket(event.getPacket(), event.getPlayer()); - try { - for (PacketContainer packet : packets) { - if (packet.equals(event.getPacket())) - packet = packet.deepClone(); - packet.getModifier().write(0, selfDisguisesIds.get(entity.getEntityId())); - ProtocolLibrary.getProtocolManager().sendServerPacket(event.getPlayer(), packet, false); - } - } catch (Exception ex) { - ex.printStackTrace(); - } - - if (event.getPacketID() == Packets.Server.ENTITY_METADATA) { - event.setPacket(event.getPacket().deepClone()); - StructureModifier mods = event.getPacket().getModifier(); - Iterator itel = ((List) mods.read(1)).iterator(); - while (itel.hasNext()) { - WatchableObject watch = itel.next(); - if (watch.a() == 0) { - byte b = (Byte) watch.b(); - byte a = (byte) (b | 1 << 5); - if ((b & 1 << 3) != 0) - a = (byte) (a | 1 << 3); - watch.a(a); - } - } - } else { - switch (event.getPacketID()) { - case Packets.Server.NAMED_ENTITY_SPAWN: - case Packets.Server.ATTACH_ENTITY: - case Packets.Server.REL_ENTITY_MOVE: - case Packets.Server.REL_ENTITY_MOVE_LOOK: - case Packets.Server.ENTITY_LOOK: - case Packets.Server.ENTITY_TELEPORT: - case Packets.Server.ENTITY_HEAD_ROTATION: - case Packets.Server.MOB_EFFECT: - case Packets.Server.ENTITY_EQUIPMENT: - if (event.getPacketID() == Packets.Server.NAMED_ENTITY_SPAWN) { - PacketContainer packet = new PacketContainer(Packets.Server.ENTITY_METADATA); - StructureModifier mods = packet.getModifier(); - mods.write(0, entity.getEntityId()); - List watchableList = new ArrayList(); - byte b = (byte) (0 | 1 << 5); - if (event.getPlayer().isSprinting()) - b = (byte) (b | 1 << 3); - watchableList.add(new WatchableObject(0, 0, b)); - mods.write(1, watchableList); - try { - ProtocolLibrary.getProtocolManager().sendServerPacket(event.getPlayer(), packet, false); - } catch (Exception ex) { - ex.printStackTrace(); - } - } - event.setCancelled(true); - break; - case Packets.Server.ENTITY_STATUS: - if (getDisguise(entity).canHearSelfDisguise() && (Byte) event.getPacket().getModifier().read(1) == 2) - event.setCancelled(true); - break; - default: - break; - } - } - } - } - }; } /** @@ -401,19 +134,21 @@ public class DisguiseAPI { return getDisguise(disguiser) != null; } - @Deprecated - public static boolean isDisguised(Object disguiser) { - return getDisguise((Entity) disguiser) != null; - } - public static boolean isSoundEnabled() { - return soundsEnabled; + return PacketsManager.isHearDisguisesEnabled(); } public static boolean isVelocitySent() { return sendVelocity; } + /** + * The default value if a player views his own disguise + */ + public static boolean isViewDisguises() { + return PacketsManager.isViewDisguisesListenerEnabled(); + } + /** * @param Resends * the entity to all the watching players, which is where the magic begins @@ -434,6 +169,7 @@ public class DisguiseAPI { private static void removeVisibleDisguise(Player player) { if (selfDisguisesIds.containsKey(player.getEntityId())) { + // Send a packet to destroy the fake entity PacketContainer packet = new PacketContainer(Packets.Server.DESTROY_ENTITY); packet.getModifier().write(0, new int[] { selfDisguisesIds.get(player.getEntityId()) }); try { @@ -441,13 +177,17 @@ public class DisguiseAPI { } catch (Exception ex) { ex.printStackTrace(); } + // Remove the fake entity ID from the disguise bin selfDisguisesIds.remove(player.getEntityId()); + // Get the entity tracker EntityPlayer entityplayer = ((CraftPlayer) player).getHandle(); EntityTrackerEntry tracker = (EntityTrackerEntry) ((WorldServer) entityplayer.world).tracker.trackedEntities .get(player.getEntityId()); + // If the tracker exists. Remove himself from his tracker if (tracker != null) { tracker.trackedPlayers.remove(entityplayer); } + // Resend entity metadata else he will be invisible to himself until its resent PacketContainer packetMetadata = new PacketContainer(Packets.Server.ENTITY_METADATA); StructureModifier mods = packetMetadata.getModifier(); mods.write(0, player.getEntityId()); @@ -466,13 +206,24 @@ public class DisguiseAPI { } } + public static void setSoundsEnabled(boolean isSoundsEnabled) { + PacketsManager.setHearDisguisesListener(isSoundsEnabled); + } + + /** + * Setup it so he can see himself when disguised + */ private static void setupPlayer(final Disguise disguise) { + // If the disguises entity is null, or the disguised entity isn't a player return if (disguise.getEntity() == null || !(disguise.getEntity() instanceof Player)) return; Player player = (Player) disguise.getEntity(); + // Remove the old disguise, else we have weird disguises around the place removeVisibleDisguise(player); + // If the disguised player can't see himself. Return if (!disguise.viewSelfDisguise()) return; + // Grab the entity player EntityPlayer entityplayer = ((CraftPlayer) player).getHandle(); EntityTrackerEntry tracker = (EntityTrackerEntry) ((WorldServer) entityplayer.world).tracker.trackedEntities.get(player .getEntityId()); @@ -486,25 +237,27 @@ public class DisguiseAPI { }); return; } + // Add himself to his own entity tracker tracker.trackedPlayers.add(entityplayer); - int id = 0; try { + // Grab the entity ID the fake disguise will use Field field = net.minecraft.server.v1_6_R2.Entity.class.getDeclaredField("entityCount"); field.setAccessible(true); - id = field.getInt(null); + int id = field.getInt(null); + // Set the entitycount plus one so we don't have the id being reused field.set(null, id + 1); selfDisguisesIds.put(player.getEntityId(), id); } catch (Exception ex) { ex.printStackTrace(); } - + // Send the player a packet with himself being spawned Packet20NamedEntitySpawn packet = new Packet20NamedEntitySpawn((EntityHuman) entityplayer); entityplayer.playerConnection.sendPacket(packet); if (!tracker.tracker.getDataWatcher().d()) { entityplayer.playerConnection.sendPacket(new Packet40EntityMetadata(player.getEntityId(), tracker.tracker .getDataWatcher(), true)); } - + // Send himself some entity attributes if (tracker.tracker instanceof EntityLiving) { AttributeMapServer attributemapserver = (AttributeMapServer) ((EntityLiving) tracker.tracker).aW(); Collection collection = attributemapserver.c(); @@ -514,6 +267,7 @@ public class DisguiseAPI { } } + // Why do we even have this? tracker.j = tracker.tracker.motX; tracker.k = tracker.tracker.motY; tracker.l = tracker.tracker.motZ; @@ -525,12 +279,13 @@ public class DisguiseAPI { } catch (Exception ex) { ex.printStackTrace(); } + // Send the velocity packets if (isMoving) { entityplayer.playerConnection.sendPacket(new Packet28EntityVelocity(player.getEntityId(), tracker.tracker.motX, tracker.tracker.motY, tracker.tracker.motZ)); } - // CraftBukkit start + // Why the hell would he even need this. Meh. if (tracker.tracker.vehicle != null && player.getEntityId() > tracker.tracker.vehicle.id) { entityplayer.playerConnection.sendPacket(new Packet39AttachEntity(0, tracker.tracker, tracker.tracker.vehicle)); } else if (tracker.tracker.passenger != null && player.getEntityId() > tracker.tracker.passenger.id) { @@ -541,26 +296,20 @@ public class DisguiseAPI { entityplayer.playerConnection.sendPacket(new Packet39AttachEntity(1, tracker.tracker, ((EntityInsentient) tracker.tracker).bI())); } - // CraftBukkit end - if (tracker.tracker instanceof EntityLiving) { - for (int i = 0; i < 5; ++i) { - ItemStack itemstack = ((EntityLiving) tracker.tracker).getEquipment(i); + // Resend the armor + for (int i = 0; i < 5; ++i) { + ItemStack itemstack = ((EntityLiving) tracker.tracker).getEquipment(i); - if (itemstack != null) { - entityplayer.playerConnection.sendPacket(new Packet5EntityEquipment(player.getEntityId(), i, itemstack)); - } + if (itemstack != null) { + entityplayer.playerConnection.sendPacket(new Packet5EntityEquipment(player.getEntityId(), i, itemstack)); } } - - if (tracker.tracker instanceof EntityHuman) { - EntityHuman entityhuman = (EntityHuman) tracker.tracker; - - if (entityhuman.isSleeping()) { - entityplayer.playerConnection.sendPacket(new Packet17EntityLocationAction(tracker.tracker, 0, (int) Math - .floor(tracker.tracker.locX), (int) Math.floor(tracker.tracker.locY), (int) Math - .floor(tracker.tracker.locZ))); - } + // If the disguised is sleeping for w/e reason + if (entityplayer.isSleeping()) { + entityplayer.playerConnection + .sendPacket(new Packet17EntityLocationAction(entityplayer, 0, (int) Math.floor(tracker.tracker.locX), + (int) Math.floor(tracker.tracker.locY), (int) Math.floor(tracker.tracker.locZ))); } // CraftBukkit start - Fix for nonsensical head yaw @@ -569,36 +318,28 @@ public class DisguiseAPI { tracker.broadcast(new Packet35EntityHeadRotation(player.getEntityId(), (byte) tracker.i)); // CraftBukkit end - if (tracker.tracker instanceof EntityLiving) { - EntityLiving entityliving = (EntityLiving) tracker.tracker; - Iterator iterator = entityliving.getEffects().iterator(); + // Resend any active potion effects + Iterator iterator = entityplayer.getEffects().iterator(); + while (iterator.hasNext()) { + MobEffect mobeffect = (MobEffect) iterator.next(); - while (iterator.hasNext()) { - MobEffect mobeffect = (MobEffect) iterator.next(); - - entityplayer.playerConnection.sendPacket(new Packet41MobEffect(player.getEntityId(), mobeffect)); - } + entityplayer.playerConnection.sendPacket(new Packet41MobEffect(player.getEntityId(), mobeffect)); } } + /** + * Disable velocity packets being sent for w/e reason. Maybe you want every ounce of performance you can get? + */ public static void setVelocitySent(boolean sendVelocityPackets) { sendVelocity = sendVelocityPackets; } public static void setViewDisguises(boolean seeOwnDisguise) { - if (viewDisguises != seeOwnDisguise) { - viewDisguises = seeOwnDisguise; - if (viewDisguises) { - ProtocolLibrary.getProtocolManager().addPacketListener(viewDisguisesListener); - } else { - ProtocolLibrary.getProtocolManager().removePacketListener(viewDisguisesListener); - } - } + PacketsManager.setViewDisguisesListener(seeOwnDisguise); } /** - * @param Disguiser - * - Undisguises him + * Undisguise the entity */ public static void undisguiseToAll(Entity entity) { Disguise disguise = getDisguise(entity); @@ -616,8 +357,4 @@ public class DisguiseAPI { refresh(entity); } } - - public static boolean viewDisguises() { - return viewDisguises; - } } \ No newline at end of file diff --git a/src/me/libraryaddict/disguise/DisguiseTypes/Disguise.java b/src/me/libraryaddict/disguise/DisguiseTypes/Disguise.java index f1bab5ff..f5beee0a 100644 --- a/src/me/libraryaddict/disguise/DisguiseTypes/Disguise.java +++ b/src/me/libraryaddict/disguise/DisguiseTypes/Disguise.java @@ -16,7 +16,6 @@ import net.minecraft.server.v1_6_R2.EntityTrackerEntry; import net.minecraft.server.v1_6_R2.WorldServer; import org.bukkit.craftbukkit.v1_6_R2.entity.CraftEntity; -import org.bukkit.entity.EntityType; import org.bukkit.entity.Horse.Variant; import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; @@ -36,7 +35,7 @@ public class Disguise { private boolean replaceSounds; private BukkitRunnable runnable; private boolean velocitySent = DisguiseAPI.isVelocitySent(); - private boolean viewSelfDisguise = DisguiseAPI.viewDisguises(); + private boolean viewSelfDisguise = DisguiseAPI.isViewDisguises(); private FlagWatcher watcher; protected Disguise(DisguiseType newType, boolean doSounds) { @@ -146,19 +145,6 @@ public class Disguise { return watcher; } - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((disguiseType == null) ? 0 : disguiseType.hashCode()); - result = prime * result + (hearSelfDisguise ? 1231 : 1237); - result = prime * result + (replaceSounds ? 1231 : 1237); - result = prime * result + (velocitySent ? 1231 : 1237); - result = prime * result + (viewSelfDisguise ? 1231 : 1237); - result = prime * result + ((watcher == null) ? 0 : watcher.hashCode()); - return result; - } - public boolean isMiscDisguise() { return this instanceof MiscDisguise; } @@ -179,11 +165,14 @@ public class Disguise { return replaceSounds; } + /** + * Set the entity of the disguise. Only used for internal things. + */ public void setEntity(final org.bukkit.entity.Entity entity) { if (this.entity != null) throw new RuntimeException("This disguise is already in use! Try .clone()"); - setupWatcher(entity.getClass()); this.entity = entity; + setupWatcher(); double fallSpeed = 0.0050; boolean movement = false; switch (getType()) { @@ -290,7 +279,7 @@ public class Disguise { StructureModifier mods = packet.getModifier(); mods.write(0, entity.getEntityId()); for (EntityPlayer player : getPerverts()) { - if (DisguiseAPI.viewDisguises() || entity != player) { + if (DisguiseAPI.isViewDisguises() || entity != player) { try { ProtocolLibrary.getProtocolManager().sendServerPacket(player.getBukkitEntity(), packet); } catch (InvocationTargetException e) { @@ -315,19 +304,13 @@ public class Disguise { } /** - * Sets up the FlagWatcher with the entityclass, it creates all the data it needs. + * Sets up the FlagWatcher with the entityclass, it creates all the data it needs to prevent conflicts when sending the + * datawatcher. */ - private void setupWatcher(Class entityClass) { + private void setupWatcher() { Class disguiseClass = Values.getEntityClass(getType()); HashMap disguiseValues = Values.getMetaValues(getType()); - EntityType entityType = null; - for (EntityType type : EntityType.values()) { - if (type.getEntityClass() != null && type.getEntityClass().isAssignableFrom(entityClass)) { - entityType = type; - break; - } - } - HashMap entityValues = Values.getMetaValues(DisguiseType.getType(entityType)); + HashMap entityValues = Values.getMetaValues(DisguiseType.getType(entity.getType())); // Start from 2 as they ALL share 0 and 1 for (int dataNo = 2; dataNo <= 31; dataNo++) { // If the watcher already set a metadata on this @@ -352,25 +335,12 @@ public class Disguise { getWatcher().setValue(dataNo, null); continue; } - // Hmm. They both have the datavalue. Time to check if they have different default values! - if (entityValues.get(dataNo) != disguiseValues.get(dataNo) - || !entityValues.get(dataNo).equals(disguiseValues.get(dataNo))) { - // They do! Set the default value! - getWatcher().setValue(dataNo, disguiseValues.get(dataNo)); - continue; - } - // Hmm. They both now have data values which are exactly the same. I need to do more intensive background checks. - // I HAVE to find juicy gossip on these! - // Maybe if I check that they extend each other.. - // Seeing as I only store the finished forms of entitys. This should raise no problems and allow for more shared - // datawatchers. - if (entityClass.isAssignableFrom(disguiseClass) || disguiseClass.isAssignableFrom(entityClass)) - continue; - + // Since they both share it. Time to check if its from something they extend. + // Better make this clear before I compare the values because some default values are different! // Entity is 0 & 1 - But we aint gonna be checking that // EntityAgeable is 16 // EntityInsentient is 10 & 11 - // EntityZombie is 12 & 13 & 14 - But + // EntityZombie is 12 & 13 & 14 - But it overrides other values and another check already does this. // EntityLiving is 6 & 7 & 8 & 9 // Lets use switch @@ -392,9 +362,17 @@ public class Disguise { default: break; } + Class entityClass = ((CraftEntity) entity).getHandle().getClass(); // If they both extend the same base class. They OBVIOUSLY share the same datavalue. Right..? if (baseClass != null && baseClass.isAssignableFrom(disguiseClass) && baseClass.isAssignableFrom(entityClass)) continue; + + // So they don't extend a basic class. + // Maybe if I check that they extend each other.. + // Seeing as I only store the finished forms of entitys. This should raise no problems and allow for more shared + // datawatchers. + if (entityClass.isAssignableFrom(disguiseClass) || disguiseClass.isAssignableFrom(entityClass)) + continue; // Well I can't find a reason I should leave it alone. They will probably conflict. // Time to set the value to the disguises value so no conflicts! getWatcher().setValue(dataNo, disguiseValues.get(dataNo)); diff --git a/src/me/libraryaddict/disguise/DisguiseTypes/FlagWatcher.java b/src/me/libraryaddict/disguise/DisguiseTypes/FlagWatcher.java index 3056a1b6..7f52fa33 100644 --- a/src/me/libraryaddict/disguise/DisguiseTypes/FlagWatcher.java +++ b/src/me/libraryaddict/disguise/DisguiseTypes/FlagWatcher.java @@ -175,7 +175,7 @@ public class FlagWatcher { mods.write(1, list); for (EntityPlayer player : disguise.getPerverts()) { Player p = player.getBukkitEntity(); - if (DisguiseAPI.viewDisguises() || p != entity) { + if (DisguiseAPI.isViewDisguises() || p != entity) { try { ProtocolLibrary.getProtocolManager().sendServerPacket(p, packet); } catch (InvocationTargetException e) { diff --git a/src/me/libraryaddict/disguise/LibsDisguises.java b/src/me/libraryaddict/disguise/LibsDisguises.java index 3478983c..0ec62656 100644 --- a/src/me/libraryaddict/disguise/LibsDisguises.java +++ b/src/me/libraryaddict/disguise/LibsDisguises.java @@ -2,64 +2,30 @@ package me.libraryaddict.disguise; import java.io.File; import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; import java.util.List; -import java.util.Random; - import me.libraryaddict.disguise.Commands.*; import me.libraryaddict.disguise.DisguiseTypes.Disguise; import me.libraryaddict.disguise.DisguiseTypes.DisguiseSound; import me.libraryaddict.disguise.DisguiseTypes.DisguiseType; import me.libraryaddict.disguise.DisguiseTypes.FlagWatcher; -import me.libraryaddict.disguise.DisguiseTypes.MiscDisguise; -import me.libraryaddict.disguise.DisguiseTypes.PlayerDisguise; import me.libraryaddict.disguise.DisguiseTypes.Values; import me.libraryaddict.disguise.DisguiseTypes.Watchers.AgeableWatcher; import me.libraryaddict.disguise.DisguiseTypes.Watchers.LivingWatcher; -import net.minecraft.server.v1_6_R2.AttributeSnapshot; import net.minecraft.server.v1_6_R2.ChatMessage; import net.minecraft.server.v1_6_R2.ChunkCoordinates; -import net.minecraft.server.v1_6_R2.DataWatcher; import net.minecraft.server.v1_6_R2.EntityHuman; import net.minecraft.server.v1_6_R2.EntityLiving; -import net.minecraft.server.v1_6_R2.EnumArt; -import net.minecraft.server.v1_6_R2.EnumEntitySize; import net.minecraft.server.v1_6_R2.GenericAttributes; -import net.minecraft.server.v1_6_R2.ItemStack; -import net.minecraft.server.v1_6_R2.MathHelper; import net.minecraft.server.v1_6_R2.WatchableObject; import net.minecraft.server.v1_6_R2.World; import org.bukkit.Bukkit; -import org.bukkit.Location; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.craftbukkit.v1_6_R2.CraftWorld; -import org.bukkit.craftbukkit.v1_6_R2.entity.CraftEntity; -import org.bukkit.craftbukkit.v1_6_R2.entity.CraftLivingEntity; -import org.bukkit.craftbukkit.v1_6_R2.inventory.CraftItemStack; import org.bukkit.entity.Ageable; -import org.bukkit.entity.Arrow; -import org.bukkit.entity.Entity; -import org.bukkit.entity.ExperienceOrb; -import org.bukkit.entity.Item; import org.bukkit.entity.LivingEntity; -import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; -import org.bukkit.util.Vector; - -import com.comphenix.protocol.Packets; -import com.comphenix.protocol.ProtocolLibrary; -import com.comphenix.protocol.ProtocolManager; -import com.comphenix.protocol.events.ConnectionSide; -import com.comphenix.protocol.events.ListenerPriority; -import com.comphenix.protocol.events.PacketAdapter; -import com.comphenix.protocol.events.PacketContainer; -import com.comphenix.protocol.events.PacketEvent; -import com.comphenix.protocol.reflect.StructureModifier; public class LibsDisguises extends JavaPlugin { private class DisguiseHuman extends EntityHuman { @@ -81,295 +47,6 @@ public class LibsDisguises extends JavaPlugin { } - private void addPacketListeners() { - ProtocolManager manager = ProtocolLibrary.getProtocolManager(); - manager.addPacketListener(new PacketAdapter(this, ConnectionSide.SERVER_SIDE, ListenerPriority.HIGH, - Packets.Server.NAMED_ENTITY_SPAWN, Packets.Server.ENTITY_METADATA, Packets.Server.ARM_ANIMATION, - Packets.Server.REL_ENTITY_MOVE_LOOK, Packets.Server.ENTITY_LOOK, Packets.Server.ENTITY_TELEPORT, - Packets.Server.ADD_EXP_ORB, Packets.Server.VEHICLE_SPAWN, Packets.Server.MOB_SPAWN, - Packets.Server.ENTITY_PAINTING, Packets.Server.COLLECT, Packets.Server.UPDATE_ATTRIBUTES, - Packets.Server.ENTITY_EQUIPMENT) { - @Override - public void onPacketSending(PacketEvent event) { - Player observer = event.getPlayer(); - // First get the entity, the one sending this packet - StructureModifier entityModifer = event.getPacket().getEntityModifier(observer.getWorld()); - org.bukkit.entity.Entity entity = entityModifer.read((Packets.Server.COLLECT == event.getPacketID() ? 1 : 0)); - // If the entity is the same as the sender. Don't disguise! - // Prevents problems and there is no advantage to be gained. - if (entity == observer) - return; - PacketContainer[] packets = transformPacket(event.getPacket(), event.getPlayer()); - if (packets.length == 0) - event.setCancelled(true); - else { - event.setPacket(packets[0]); - for (int i = 1; i < packets.length; i++) { - sendDelayedPacket(packets[i], event.getPlayer()); - } - } - } - }); - // Now add a client listener to cancel them interacting with uninteractable disguised entitys. - // You ain't supposed to be allowed to 'interact' with a item that cannot be clicked. - manager.addPacketListener(new PacketAdapter(this, ConnectionSide.CLIENT_SIDE, ListenerPriority.NORMAL, - Packets.Client.USE_ENTITY) { - @Override - public void onPacketReceiving(PacketEvent event) { - try { - Player observer = event.getPlayer(); - StructureModifier entityModifer = event.getPacket().getEntityModifier(observer.getWorld()); - org.bukkit.entity.Entity entity = entityModifer.read(1); - if (DisguiseAPI.isDisguised(entity) - && (entity instanceof ExperienceOrb || entity instanceof Item || entity instanceof Arrow)) { - event.setCancelled(true); - } - } catch (Exception e) { - e.printStackTrace(); - } - } - }); - } - - protected PacketContainer[] constructPacket(Disguise disguise, Entity disguisedEntity) { - if (disguise.getEntity() == null) - disguise.setEntity(disguisedEntity); - ProtocolManager manager = ProtocolLibrary.getProtocolManager(); - net.minecraft.server.v1_6_R2.Entity nmsEntity = ((CraftEntity) disguisedEntity).getHandle(); - ArrayList packets = new ArrayList(); - for (int i = 0; i < 5; i++) { - int slot = i - 1; - if (slot < 0) - slot = 4; - 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) { - PacketContainer packet = new PacketContainer(Packets.Server.ENTITY_EQUIPMENT); - StructureModifier mods = packet.getModifier(); - mods.write(0, disguisedEntity.getEntityId()); - mods.write(1, i); - mods.write(2, CraftItemStack.asNMSCopy(itemstack)); - packets.add(packet); - } - } - } - PacketContainer[] spawnPackets = new PacketContainer[2 + packets.size()]; - for (int i = 0; i < packets.size(); i++) { - spawnPackets[i + 2] = packets.get(i); - } - Location loc = disguisedEntity.getLocation(); - byte yaw = getYaw(disguise.getType(), DisguiseType.getType(disguise.getEntity().getType()), - (byte) (int) (loc.getYaw() * 256.0F / 360.0F)); - EnumEntitySize entitySize = Values.getValues(disguise.getType()).getEntitySize(); - - if (disguise.getType() == DisguiseType.EXPERIENCE_ORB) { - - spawnPackets[0] = manager.createPacket(Packets.Server.ADD_EXP_ORB); - StructureModifier mods = spawnPackets[0].getModifier(); - mods.write(0, disguisedEntity.getEntityId()); - mods.write(1, (int) Math.floor(loc.getX() * 32)); - mods.write(2, (int) Math.floor(loc.getY() * 32) + 2); - mods.write(3, (int) Math.floor(loc.getZ() * 32)); - mods.write(4, 1); - - } else if (disguise.getType() == DisguiseType.PAINTING) { - spawnPackets[0] = manager.createPacket(Packets.Server.ENTITY_PAINTING); - StructureModifier mods = spawnPackets[0].getModifier(); - mods.write(0, disguisedEntity.getEntityId()); - mods.write(1, loc.getBlockX()); - mods.write(2, loc.getBlockY()); - mods.write(3, loc.getBlockZ()); - mods.write(4, ((int) loc.getYaw()) % 4); - int id = ((MiscDisguise) disguise).getId(); - if (id == -1) - id = new Random().nextInt(EnumArt.values().length); - mods.write(5, EnumArt.values()[id].B); - - // Make the teleport packet to make it visible.. - spawnPackets[1] = manager.createPacket(Packets.Server.ENTITY_TELEPORT); - mods = spawnPackets[1].getModifier(); - mods.write(0, disguisedEntity.getEntityId()); - mods.write(1, (int) Math.floor(entitySize.a(loc.getX()) * 32D)); - mods.write(2, (int) Math.floor(loc.getY() * 32D)); - mods.write(3, (int) Math.floor(entitySize.a(loc.getZ()) * 32D)); - mods.write(4, yaw); - mods.write(5, (byte) (int) (loc.getPitch() * 256.0F / 360.0F)); - - } else if (disguise.getType().isPlayer()) { - - spawnPackets[0] = manager.createPacket(Packets.Server.NAMED_ENTITY_SPAWN); - StructureModifier mods = spawnPackets[0].getModifier(); - mods.write(0, disguisedEntity.getEntityId()); - mods.write(1, ((PlayerDisguise) disguise).getName()); - mods.write(2, (int) Math.floor(loc.getX() * 32)); - mods.write(3, (int) Math.floor(loc.getY() * 32)); - mods.write(4, (int) Math.floor(loc.getZ() * 32)); - mods.write(5, yaw); - mods.write(6, (byte) (int) (loc.getPitch() * 256F / 360F)); - ItemStack item = null; - if (disguisedEntity instanceof Player && ((Player) disguisedEntity).getItemInHand() != null) { - item = CraftItemStack.asNMSCopy(((Player) disguisedEntity).getItemInHand()); - } else if (disguisedEntity instanceof LivingEntity) { - item = CraftItemStack.asNMSCopy(((CraftLivingEntity) disguisedEntity).getEquipment().getItemInHand()); - } - mods.write(7, (item == null ? 0 : item.id)); - mods.write(8, convertDataWatcher(nmsEntity.getDataWatcher(), disguise.getWatcher())); - - } else if (disguise.getType().isMob()) { - - Vector vec = disguisedEntity.getVelocity(); - spawnPackets[0] = manager.createPacket(Packets.Server.MOB_SPAWN); - StructureModifier mods = spawnPackets[0].getModifier(); - mods.write(0, disguisedEntity.getEntityId()); - mods.write(1, (int) disguise.getType().getEntityType().getTypeId()); - double d1 = 3.9D; - double d2 = vec.getX(); - double d3 = vec.getY(); - double d4 = vec.getZ(); - if (d2 < -d1) - d2 = -d1; - if (d3 < -d1) - d3 = -d1; - if (d4 < -d1) - d4 = -d1; - if (d2 > d1) - d2 = d1; - if (d3 > d1) - d3 = d1; - if (d4 > d1) - d4 = d1; - mods.write(2, entitySize.a(loc.getX())); - mods.write(3, (int) Math.floor(loc.getY() * 32D)); - mods.write(4, entitySize.a(loc.getZ())); - mods.write(5, (int) (d2 * 8000.0D)); - mods.write(6, (int) (d3 * 8000.0D)); - mods.write(7, (int) (d4 * 8000.0D)); - mods.write(8, yaw); - 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, convertDataWatcher(nmsEntity.getDataWatcher(), 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. - - } else if (disguise.getType().isMisc()) { - - int id = disguise.getType().getEntityId(); - int data = 0; - if (((MiscDisguise) disguise).getId() >= 0) - if (((MiscDisguise) disguise).getData() >= 0) - data = (((MiscDisguise) disguise).getId() | ((MiscDisguise) disguise).getData() << 16); - else - data = ((MiscDisguise) disguise).getId(); - // This won't actually work. - // But if someone constructing the disguise uses it properly. It will work. - if (disguise.getType() == DisguiseType.FISHING_HOOK) - data = disguise.getEntity().getEntityId(); - else if (disguise.getType() == DisguiseType.ITEM_FRAME) - data = (int) Math.abs(loc.getYaw() % 4); - spawnPackets[0] = manager.createPacket(Packets.Server.VEHICLE_SPAWN); - StructureModifier mods = spawnPackets[0].getModifier(); - mods.write(0, disguisedEntity.getEntityId()); - mods.write(1, (int) Math.floor(loc.getX() * 32D)); - mods.write(2, (int) Math.floor(loc.getY() * 32D)); - mods.write(3, (int) Math.floor(loc.getZ() * 32D)); - if (data > 0) { - Vector vec = disguisedEntity.getVelocity(); - double d1 = vec.getX(); - double d2 = vec.getY(); - double d3 = vec.getZ(); - double d4 = 3.9D; - if (d1 < -d4) - d1 = -d4; - if (d2 < -d4) - d2 = -d4; - if (d3 < -d4) - d3 = -d4; - if (d1 > d4) - d1 = d4; - if (d2 > d4) - d2 = d4; - if (d3 > d4) - d3 = d4; - mods.write(4, (int) (d1 * 8000.0D)); - mods.write(5, (int) (d2 * 8000.0D)); - mods.write(6, (int) (d3 * 8000.0D)); - } - mods.write(7, (int) MathHelper.floor(loc.getPitch() * 256.0F / 360.0F)); - mods.write(8, yaw); - mods.write(9, id); - mods.write(10, data); - - } - if (spawnPackets[1] == null) { - // Make a packet to turn his head! - spawnPackets[1] = manager.createPacket(Packets.Server.ENTITY_HEAD_ROTATION); - StructureModifier mods = spawnPackets[1].getModifier(); - mods.write(0, disguisedEntity.getEntityId()); - mods.write(1, (byte) (int) Math.floor(loc.getYaw() * 256.0F / 360.0F)); - } - return spawnPackets; - } - - private DataWatcher convertDataWatcher(DataWatcher watcher, FlagWatcher flagWatcher) { - DataWatcher newWatcher = new DataWatcher(); - 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); - } - } catch (Exception ex) { - ex.printStackTrace(); - } - return newWatcher; - - } - - private byte getYaw(DisguiseType disguiseType, DisguiseType entityType, byte value) { - switch (disguiseType) { - case ENDER_DRAGON: - value -= 128; - break; - case ITEM_FRAME: - case ARROW: - value = (byte) -value; - break; - case PAINTING: - value = (byte) -(value + 128); - break; - default: - if (disguiseType.isMisc()) { - value -= 64; - } - break; - } - switch (entityType) { - case ENDER_DRAGON: - value += 128; - break; - case ITEM_FRAME: - case ARROW: - value = (byte) -value; - break; - case PAINTING: - value = (byte) -(value - 128); - break; - default: - if (entityType.isMisc()) { - value += 64; - } - break; - } - return value; - } - @Override public void onEnable() { saveDefaultConfig(); @@ -384,8 +61,9 @@ public class LibsDisguises extends JavaPlugin { config.set("HearSelfDisguise", getConfig().getBoolean("HearSelfDisguise")); if (!config.contains("SendVelocity")) config.set("SendVelocity", getConfig().getBoolean("SendVelocity")); + PacketsManager.init(this); DisguiseAPI.init(this); - DisguiseAPI.enableSounds(getConfig().getBoolean("DisguiseSounds")); + DisguiseAPI.setSoundsEnabled(getConfig().getBoolean("DisguiseSounds")); DisguiseAPI.setVelocitySent(getConfig().getBoolean("SendVelocity")); DisguiseAPI.setViewDisguises(getConfig().getBoolean("ViewDisguises")); DisguiseAPI.setHearSelfDisguise(getConfig().getBoolean("HearSelfDisguise")); @@ -398,7 +76,7 @@ public class LibsDisguises extends JavaPlugin { } catch (Exception ex) { ex.printStackTrace(); } - addPacketListeners(); + PacketsManager.addPacketListeners(this); DisguiseListener listener = new DisguiseListener(this); Bukkit.getPluginManager().registerEvents(listener, this); getCommand("disguise").setExecutor(new DisguiseCommand()); @@ -528,18 +206,6 @@ public class LibsDisguises extends JavaPlugin { } } - private void sendDelayedPacket(final PacketContainer packet, final Player player) { - Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() { - public void run() { - try { - ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet); - } catch (InvocationTargetException e) { - e.printStackTrace(); - } - } - }); - } - private String toReadable(String string) { StringBuilder builder = new StringBuilder(); for (String s : string.split("_")) { @@ -548,111 +214,4 @@ public class LibsDisguises extends JavaPlugin { return builder.toString(); } - protected PacketContainer[] transformPacket(PacketContainer sentPacket, Player observer) { - PacketContainer[] packets = new PacketContainer[] { sentPacket }; - try { - // First get the entity, the one sending this packet - StructureModifier entityModifer = sentPacket.getEntityModifier(observer.getWorld()); - org.bukkit.entity.Entity entity = entityModifer.read((Packets.Server.COLLECT == sentPacket.getID() ? 1 : 0)); - Disguise disguise = DisguiseAPI.getDisguise(entity); - // If disguised. - if (disguise != null) { - // If packet is Packets.Server.UPDATE_ATTRIBUTES - // This packet sends attributes - - switch (sentPacket.getID()) { - 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]; - } - break; - } - - // Else if the packet is sending entity metadata - case Packets.Server.ENTITY_METADATA: - - { - List watchableObjects = disguise.getWatcher().convert( - (List) packets[0].getModifier().read(1)); - packets[0] = new PacketContainer(sentPacket.getID()); - StructureModifier newMods = packets[0].getModifier(); - newMods.write(0, entity.getEntityId()); - newMods.write(1, watchableObjects); - break; - } - - // Else if the packet is spawning.. - case Packets.Server.NAMED_ENTITY_SPAWN: - case Packets.Server.MOB_SPAWN: - case Packets.Server.ADD_EXP_ORB: - case Packets.Server.VEHICLE_SPAWN: - case Packets.Server.ENTITY_PAINTING: - - { - packets = constructPacket(disguise, entity); - break; - } - - // Else if the disguise is attempting to send players a forbidden packet - case Packets.Server.ARM_ANIMATION: - case Packets.Server.COLLECT: - - { - if (disguise.getType().isMisc()) - packets = new PacketContainer[0]; - break; - - } - // Else if the disguise is moving. - case Packets.Server.REL_ENTITY_MOVE_LOOK: - case Packets.Server.ENTITY_LOOK: - case Packets.Server.ENTITY_TELEPORT: - - { - packets[0] = sentPacket.shallowClone(); - StructureModifier mods = packets[0].getModifier(); - byte value = (Byte) mods.read(4); - mods.write(4, getYaw(disguise.getType(), DisguiseType.getType(entity.getType()), value)); - break; - } - - case Packets.Server.ENTITY_EQUIPMENT: - - { - int slot = (Integer) packets[0].getModifier().read(1) - 1; - if (slot < 0) - slot = 4; - org.bukkit.inventory.ItemStack itemstack = disguise.getWatcher().getItemStack(slot); - if (itemstack != null) { - packets[0] = packets[0].shallowClone(); - packets[0].getModifier().write(2, - (itemstack.getTypeId() == 0 ? null : CraftItemStack.asNMSCopy(itemstack))); - } - break; - } - - default: - break; - } - } - } catch (Exception e) { - e.printStackTrace(); - } - return packets; - } } \ No newline at end of file diff --git a/src/me/libraryaddict/disguise/PacketsManager.java b/src/me/libraryaddict/disguise/PacketsManager.java new file mode 100644 index 00000000..1be49b6c --- /dev/null +++ b/src/me/libraryaddict/disguise/PacketsManager.java @@ -0,0 +1,750 @@ +package me.libraryaddict.disguise; + +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; + +import me.libraryaddict.disguise.DisguiseTypes.Disguise; +import me.libraryaddict.disguise.DisguiseTypes.DisguiseSound; +import me.libraryaddict.disguise.DisguiseTypes.DisguiseType; +import me.libraryaddict.disguise.DisguiseTypes.FlagWatcher; +import me.libraryaddict.disguise.DisguiseTypes.MiscDisguise; +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_R2.AttributeSnapshot; +import net.minecraft.server.v1_6_R2.Block; +import net.minecraft.server.v1_6_R2.DataWatcher; +import net.minecraft.server.v1_6_R2.EntityLiving; +import net.minecraft.server.v1_6_R2.EnumArt; +import net.minecraft.server.v1_6_R2.EnumEntitySize; +import net.minecraft.server.v1_6_R2.ItemStack; +import net.minecraft.server.v1_6_R2.MathHelper; +import net.minecraft.server.v1_6_R2.WatchableObject; +import net.minecraft.server.v1_6_R2.World; + +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.craftbukkit.v1_6_R2.entity.CraftEntity; +import org.bukkit.craftbukkit.v1_6_R2.entity.CraftLivingEntity; +import org.bukkit.craftbukkit.v1_6_R2.inventory.CraftItemStack; +import org.bukkit.entity.Arrow; +import org.bukkit.entity.Entity; +import org.bukkit.entity.ExperienceOrb; +import org.bukkit.entity.Item; +import org.bukkit.entity.LivingEntity; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.util.Vector; + +import com.comphenix.protocol.Packets; +import com.comphenix.protocol.ProtocolLibrary; +import com.comphenix.protocol.ProtocolManager; +import com.comphenix.protocol.events.ConnectionSide; +import com.comphenix.protocol.events.ListenerPriority; +import com.comphenix.protocol.events.PacketAdapter; +import com.comphenix.protocol.events.PacketContainer; +import com.comphenix.protocol.events.PacketEvent; +import com.comphenix.protocol.events.PacketListener; +import com.comphenix.protocol.reflect.StructureModifier; + +public class PacketsManager { + private static PacketListener soundsListener; + private static boolean soundsListenerEnabled; + private static PacketListener viewDisguisesListener; + private static boolean viewDisguisesListenerEnabled; + + protected static void addPacketListeners(final JavaPlugin libsDisguises) { + ProtocolManager manager = ProtocolLibrary.getProtocolManager(); + manager.addPacketListener(new PacketAdapter(libsDisguises, ConnectionSide.SERVER_SIDE, ListenerPriority.HIGH, + Packets.Server.NAMED_ENTITY_SPAWN, Packets.Server.ENTITY_METADATA, Packets.Server.ARM_ANIMATION, + Packets.Server.REL_ENTITY_MOVE_LOOK, Packets.Server.ENTITY_LOOK, Packets.Server.ENTITY_TELEPORT, + Packets.Server.ADD_EXP_ORB, Packets.Server.VEHICLE_SPAWN, Packets.Server.MOB_SPAWN, + Packets.Server.ENTITY_PAINTING, Packets.Server.COLLECT, Packets.Server.UPDATE_ATTRIBUTES, + Packets.Server.ENTITY_EQUIPMENT) { + @Override + public void onPacketSending(PacketEvent event) { + final Player observer = event.getPlayer(); + // First get the entity, the one sending this packet + StructureModifier entityModifer = event.getPacket().getEntityModifier(observer.getWorld()); + org.bukkit.entity.Entity entity = entityModifer.read((Packets.Server.COLLECT == event.getPacketID() ? 1 : 0)); + // If the entity is the same as the sender. Don't disguise! + // Prevents problems and there is no advantage to be gained. + if (entity == observer) + return; + PacketContainer[] packets = transformPacket(event.getPacket(), event.getPlayer()); + if (packets.length == 0) + event.setCancelled(true); + else { + event.setPacket(packets[0]); + final PacketContainer[] delayedPackets = new PacketContainer[packets.length - 1]; + for (int i = 1; i < packets.length; i++) + delayedPackets[i - 1] = packets[i]; + Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() { + public void run() { + try { + for (PacketContainer packet : delayedPackets) + ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + } + }); + } + } + }); + // Now add a client listener to cancel them interacting with uninteractable disguised entitys. + // You ain't supposed to be allowed to 'interact' with a item that cannot be clicked. + manager.addPacketListener(new PacketAdapter(libsDisguises, ConnectionSide.CLIENT_SIDE, ListenerPriority.NORMAL, + Packets.Client.USE_ENTITY) { + @Override + public void onPacketReceiving(PacketEvent event) { + try { + Player observer = event.getPlayer(); + StructureModifier entityModifer = event.getPacket().getEntityModifier(observer.getWorld()); + org.bukkit.entity.Entity entity = entityModifer.read(1); + if (DisguiseAPI.isDisguised(entity) + && (entity instanceof ExperienceOrb || entity instanceof Item || entity instanceof Arrow)) { + event.setCancelled(true); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + }); + } + + public static PacketContainer[] constructPacket(Disguise disguise, Entity disguisedEntity) { + if (disguise.getEntity() == null) + disguise.setEntity(disguisedEntity); + net.minecraft.server.v1_6_R2.Entity nmsEntity = ((CraftEntity) disguisedEntity).getHandle(); + ArrayList packets = new ArrayList(); + for (int i = 0; i < 5; i++) { + int slot = i - 1; + if (slot < 0) + slot = 4; + 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) { + PacketContainer packet = new PacketContainer(Packets.Server.ENTITY_EQUIPMENT); + StructureModifier mods = packet.getModifier(); + mods.write(0, disguisedEntity.getEntityId()); + mods.write(1, i); + mods.write(2, CraftItemStack.asNMSCopy(itemstack)); + packets.add(packet); + } + } + } + PacketContainer[] spawnPackets = new PacketContainer[2 + packets.size()]; + for (int i = 0; i < packets.size(); i++) { + spawnPackets[i + 2] = packets.get(i); + } + Location loc = disguisedEntity.getLocation(); + byte yaw = getYaw(disguise.getType(), DisguiseType.getType(disguise.getEntity().getType()), + (byte) (int) (loc.getYaw() * 256.0F / 360.0F)); + EnumEntitySize entitySize = Values.getValues(disguise.getType()).getEntitySize(); + + if (disguise.getType() == DisguiseType.EXPERIENCE_ORB) { + + spawnPackets[0] = new PacketContainer(Packets.Server.ADD_EXP_ORB); + StructureModifier mods = spawnPackets[0].getModifier(); + mods.write(0, disguisedEntity.getEntityId()); + mods.write(1, (int) Math.floor(loc.getX() * 32)); + mods.write(2, (int) Math.floor(loc.getY() * 32) + 2); + mods.write(3, (int) Math.floor(loc.getZ() * 32)); + mods.write(4, 1); + + } else if (disguise.getType() == DisguiseType.PAINTING) { + spawnPackets[0] = new PacketContainer(Packets.Server.ENTITY_PAINTING); + StructureModifier mods = spawnPackets[0].getModifier(); + mods.write(0, disguisedEntity.getEntityId()); + mods.write(1, loc.getBlockX()); + mods.write(2, loc.getBlockY()); + mods.write(3, loc.getBlockZ()); + mods.write(4, ((int) loc.getYaw()) % 4); + int id = ((MiscDisguise) disguise).getId(); + if (id == -1) + id = new Random().nextInt(EnumArt.values().length); + mods.write(5, EnumArt.values()[id].B); + + // Make the teleport packet to make it visible.. + spawnPackets[1] = new PacketContainer(Packets.Server.ENTITY_TELEPORT); + mods = spawnPackets[1].getModifier(); + mods.write(0, disguisedEntity.getEntityId()); + mods.write(1, (int) Math.floor(entitySize.a(loc.getX()) * 32D)); + mods.write(2, (int) Math.floor(loc.getY() * 32D)); + mods.write(3, (int) Math.floor(entitySize.a(loc.getZ()) * 32D)); + mods.write(4, yaw); + mods.write(5, (byte) (int) (loc.getPitch() * 256.0F / 360.0F)); + + } else if (disguise.getType().isPlayer()) { + + spawnPackets[0] = new PacketContainer(Packets.Server.NAMED_ENTITY_SPAWN); + StructureModifier mods = spawnPackets[0].getModifier(); + mods.write(0, disguisedEntity.getEntityId()); + mods.write(1, ((PlayerDisguise) disguise).getName()); + mods.write(2, (int) Math.floor(loc.getX() * 32)); + mods.write(3, (int) Math.floor(loc.getY() * 32)); + mods.write(4, (int) Math.floor(loc.getZ() * 32)); + mods.write(5, yaw); + mods.write(6, (byte) (int) (loc.getPitch() * 256F / 360F)); + ItemStack item = null; + if (disguisedEntity instanceof Player && ((Player) disguisedEntity).getItemInHand() != null) { + item = CraftItemStack.asNMSCopy(((Player) disguisedEntity).getItemInHand()); + } else if (disguisedEntity instanceof LivingEntity) { + item = CraftItemStack.asNMSCopy(((CraftLivingEntity) disguisedEntity).getEquipment().getItemInHand()); + } + mods.write(7, (item == null ? 0 : item.id)); + mods.write(8, convertDataWatcher(nmsEntity.getDataWatcher(), disguise.getWatcher())); + + } else if (disguise.getType().isMob()) { + + Vector vec = disguisedEntity.getVelocity(); + spawnPackets[0] = new PacketContainer(Packets.Server.MOB_SPAWN); + StructureModifier mods = spawnPackets[0].getModifier(); + mods.write(0, disguisedEntity.getEntityId()); + mods.write(1, (int) disguise.getType().getEntityType().getTypeId()); + double d1 = 3.9D; + double d2 = vec.getX(); + double d3 = vec.getY(); + double d4 = vec.getZ(); + if (d2 < -d1) + d2 = -d1; + if (d3 < -d1) + d3 = -d1; + if (d4 < -d1) + d4 = -d1; + if (d2 > d1) + d2 = d1; + if (d3 > d1) + d3 = d1; + if (d4 > d1) + d4 = d1; + mods.write(2, entitySize.a(loc.getX())); + mods.write(3, (int) Math.floor(loc.getY() * 32D)); + mods.write(4, entitySize.a(loc.getZ())); + mods.write(5, (int) (d2 * 8000.0D)); + mods.write(6, (int) (d3 * 8000.0D)); + mods.write(7, (int) (d4 * 8000.0D)); + mods.write(8, yaw); + 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, convertDataWatcher(nmsEntity.getDataWatcher(), 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. + + } else if (disguise.getType().isMisc()) { + + int id = disguise.getType().getEntityId(); + int data = 0; + if (((MiscDisguise) disguise).getId() >= 0) + if (((MiscDisguise) disguise).getData() >= 0) + data = (((MiscDisguise) disguise).getId() | ((MiscDisguise) disguise).getData() << 16); + else + data = ((MiscDisguise) disguise).getId(); + // This won't actually work. + // But if someone constructing the disguise uses it properly. It will work. + if (disguise.getType() == DisguiseType.FISHING_HOOK) + data = disguise.getEntity().getEntityId(); + else if (disguise.getType() == DisguiseType.ITEM_FRAME) + data = (int) Math.abs(loc.getYaw() % 4); + spawnPackets[0] = new PacketContainer(Packets.Server.VEHICLE_SPAWN); + StructureModifier mods = spawnPackets[0].getModifier(); + mods.write(0, disguisedEntity.getEntityId()); + mods.write(1, (int) Math.floor(loc.getX() * 32D)); + mods.write(2, (int) Math.floor(loc.getY() * 32D)); + mods.write(3, (int) Math.floor(loc.getZ() * 32D)); + if (data > 0) { + Vector vec = disguisedEntity.getVelocity(); + double d1 = vec.getX(); + double d2 = vec.getY(); + double d3 = vec.getZ(); + double d4 = 3.9D; + if (d1 < -d4) + d1 = -d4; + if (d2 < -d4) + d2 = -d4; + if (d3 < -d4) + d3 = -d4; + if (d1 > d4) + d1 = d4; + if (d2 > d4) + d2 = d4; + if (d3 > d4) + d3 = d4; + mods.write(4, (int) (d1 * 8000.0D)); + mods.write(5, (int) (d2 * 8000.0D)); + mods.write(6, (int) (d3 * 8000.0D)); + } + mods.write(7, (int) MathHelper.floor(loc.getPitch() * 256.0F / 360.0F)); + mods.write(8, yaw); + mods.write(9, id); + mods.write(10, data); + + } + if (spawnPackets[1] == null) { + // Make a packet to turn his head! + spawnPackets[1] = new PacketContainer(Packets.Server.ENTITY_HEAD_ROTATION); + StructureModifier mods = spawnPackets[1].getModifier(); + mods.write(0, disguisedEntity.getEntityId()); + mods.write(1, (byte) (int) Math.floor(loc.getYaw() * 256.0F / 360.0F)); + } + return spawnPackets; + } + + private static DataWatcher convertDataWatcher(DataWatcher watcher, FlagWatcher flagWatcher) { + DataWatcher newWatcher = new DataWatcher(); + 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); + } + } catch (Exception ex) { + ex.printStackTrace(); + } + return newWatcher; + + } + + private static byte getYaw(DisguiseType disguiseType, DisguiseType entityType, byte value) { + switch (disguiseType) { + case ENDER_DRAGON: + value -= 128; + break; + case ITEM_FRAME: + case ARROW: + value = (byte) -value; + break; + case PAINTING: + value = (byte) -(value + 128); + break; + default: + if (disguiseType.isMisc()) { + value -= 64; + } + break; + } + switch (entityType) { + case ENDER_DRAGON: + value += 128; + break; + case ITEM_FRAME: + case ARROW: + value = (byte) -value; + break; + case PAINTING: + value = (byte) -(value - 128); + break; + default: + if (entityType.isMisc()) { + value += 64; + } + break; + } + return value; + } + + protected static void init(JavaPlugin libsDisguises) { + soundsListener = new PacketAdapter(libsDisguises, ConnectionSide.SERVER_SIDE, ListenerPriority.NORMAL, + Packets.Server.NAMED_SOUND_EFFECT, Packets.Server.ENTITY_STATUS) { + @Override + public void onPacketSending(PacketEvent event) { + StructureModifier mods = event.getPacket().getModifier(); + Player observer = event.getPlayer(); + if (event.getPacketID() == Packets.Server.NAMED_SOUND_EFFECT) { + String soundName = (String) mods.read(0); + SoundType soundType = null; + Location soundLoc = new Location(observer.getWorld(), ((Integer) mods.read(1)) / 8D, + ((Integer) mods.read(2)) / 8D, ((Integer) mods.read(3)) / 8D); + Entity disguisedEntity = null; + DisguiseSound entitySound = null; + for (Entity entity : soundLoc.getChunk().getEntities()) { + if (DisguiseAPI.isDisguised(entity)) { + Location loc = entity.getLocation(); + loc = new Location(observer.getWorld(), ((int) (loc.getX() * 8)) / 8D, ((int) (loc.getY() * 8)) / 8D, + ((int) (loc.getZ() * 8)) / 8D); + if (loc.equals(soundLoc)) { + entitySound = DisguiseSound.getType(entity.getType().name()); + if (entitySound != null) { + if (entity instanceof LivingEntity && ((LivingEntity) entity).getHealth() == 0) { + soundType = SoundType.DEATH; + } else { + boolean hasInvun = false; + if (entity instanceof LivingEntity) { + net.minecraft.server.v1_6_R2.EntityLiving e = ((CraftLivingEntity) entity) + .getHandle(); + hasInvun = (e.noDamageTicks == e.maxNoDamageTicks); + } else { + net.minecraft.server.v1_6_R2.Entity e = ((CraftEntity) entity).getHandle(); + hasInvun = e.isInvulnerable(); + } + soundType = entitySound.getType(soundName, !hasInvun); + } + if (soundType != null) { + disguisedEntity = entity; + break; + } + } + } + } + } + Disguise disguise = DisguiseAPI.getDisguise(disguisedEntity); + if (disguise != null) { + if (disguise.canHearSelfDisguise() || disguisedEntity != event.getPlayer()) { + if (disguise.replaceSounds()) { + String sound = null; + DisguiseSound dSound = DisguiseSound.getType(disguise.getType().name()); + if (dSound != null && soundType != null) + sound = dSound.getSound(soundType); + if (sound == null) { + event.setCancelled(true); + } else { + if (sound.equals("step.grass")) { + World world = ((CraftEntity) disguisedEntity).getHandle().world; + Block b = Block.byId[world.getTypeId(soundLoc.getBlockX(), soundLoc.getBlockY() - 1, + soundLoc.getBlockZ())]; + if (b != null) + mods.write(0, b.stepSound.getStepSound()); + // There is no else statement. Because seriously. This should never be null. Unless + // someone is + // sending fake sounds. In which case. Why cancel it. + } else { + mods.write(0, sound); + // Time to change the pitch and volume + if (soundType == SoundType.HURT || soundType == SoundType.DEATH + || soundType == SoundType.IDLE) { + // If the volume is the default + if (soundType != SoundType.IDLE + && ((Float) mods.read(4)).equals(entitySound.getDamageSoundVolume())) { + mods.write(4, dSound.getDamageSoundVolume()); + } + // Here I assume its the default pitch as I can't calculate if its real. + if (disguise instanceof MobDisguise && disguisedEntity instanceof LivingEntity + && ((MobDisguise) disguise).doesDisguiseAge()) { + boolean baby = ((CraftLivingEntity) disguisedEntity).getHandle().isBaby(); + if (((MobDisguise) disguise).isAdult() == baby) { + + float pitch = (Integer) mods.read(5); + if (baby) { + // If the pitch is not the expected + if (pitch > 97 || pitch < 111) + return; + pitch = (new Random().nextFloat() - new Random().nextFloat()) * 0.2F + 1.5F; + // Min = 1.5 + // Cap = 97.5 + // Max = 1.7 + // Cap = 110.5 + } else { + // If the pitch is not the expected + if (pitch >= 63 || pitch <= 76) + return; + pitch = (new Random().nextFloat() - new Random().nextFloat()) * 0.2F + 1.0F; + // Min = 1 + // Cap = 63 + // Max = 1.2 + // Cap = 75.6 + } + pitch *= 63; + if (pitch < 0) + pitch = 0; + if (pitch > 255) + pitch = 255; + mods.write(5, (int) pitch); + } + } + } + } + } + } + } + } + } else if (event.getPacketID() == Packets.Server.ENTITY_STATUS) { + if ((Byte) mods.read(1) == 2) { + // It made a damage animation + Entity entity = event.getPacket().getEntityModifier(observer.getWorld()).read(0); + Disguise disguise = DisguiseAPI.getDisguise(entity); + if (disguise != null && (disguise.canHearSelfDisguise() || entity != event.getPlayer())) { + DisguiseSound disSound = DisguiseSound.getType(entity.getType().name()); + if (disSound == null) + return; + SoundType soundType = null; + if (entity instanceof LivingEntity && ((LivingEntity) entity).getHealth() == 0) { + soundType = SoundType.DEATH; + } else { + soundType = SoundType.HURT; + } + if (disSound.getSound(soundType) == null + || (disguise.canHearSelfDisguise() && entity == event.getPlayer())) { + disSound = DisguiseSound.getType(disguise.getType().name()); + if (disSound != null) { + String sound = disSound.getSound(soundType); + if (sound != null) { + Location loc = entity.getLocation(); + PacketContainer packet = new PacketContainer(Packets.Server.NAMED_SOUND_EFFECT); + mods = packet.getModifier(); + mods.write(0, sound); + mods.write(1, (int) (loc.getX() * 8D)); + mods.write(2, (int) (loc.getY() * 8D)); + mods.write(3, (int) (loc.getZ() * 8D)); + mods.write(4, disSound.getDamageSoundVolume()); + float pitch; + if (disguise instanceof MobDisguise && !((MobDisguise) disguise).isAdult()) { + pitch = (new Random().nextFloat() - new Random().nextFloat()) * 0.2F + 1.5F; + } else + pitch = (new Random().nextFloat() - new Random().nextFloat()) * 0.2F + 1.0F; + if (disguise.getType() == DisguiseType.BAT) + pitch *= 95F; + pitch *= 63; + if (pitch < 0) + pitch = 0; + if (pitch > 255) + pitch = 255; + mods.write(5, (int) pitch); + try { + ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + } + } + } + } + } + } + } + }; + viewDisguisesListener = new PacketAdapter(libsDisguises, ConnectionSide.SERVER_SIDE, ListenerPriority.HIGHEST, + Packets.Server.NAMED_ENTITY_SPAWN, Packets.Server.ATTACH_ENTITY, Packets.Server.REL_ENTITY_MOVE, + Packets.Server.REL_ENTITY_MOVE_LOOK, Packets.Server.ENTITY_LOOK, Packets.Server.ENTITY_TELEPORT, + Packets.Server.ENTITY_HEAD_ROTATION, Packets.Server.ENTITY_METADATA, Packets.Server.ENTITY_EQUIPMENT, + Packets.Server.ARM_ANIMATION, Packets.Server.ENTITY_LOCATION_ACTION, Packets.Server.MOB_EFFECT, + Packets.Server.ENTITY_STATUS, Packets.Server.ENTITY_VELOCITY, Packets.Server.UPDATE_ATTRIBUTES) { + @Override + public void onPacketSending(PacketEvent event) { + StructureModifier entityModifer = event.getPacket().getEntityModifier(event.getPlayer().getWorld()); + org.bukkit.entity.Entity entity = entityModifer.read(0); + if (entity == event.getPlayer()) { + int fakeId = DisguiseAPI.getFakeDisguise(entity.getEntityId()); + if (fakeId > 0) { + PacketContainer[] packets = transformPacket(event.getPacket(), event.getPlayer()); + try { + for (PacketContainer packet : packets) { + if (packet.equals(event.getPacket())) + packet = packet.deepClone(); + packet.getModifier().write(0, fakeId); + ProtocolLibrary.getProtocolManager().sendServerPacket(event.getPlayer(), packet, false); + } + } catch (Exception ex) { + ex.printStackTrace(); + } + + if (event.getPacketID() == Packets.Server.ENTITY_METADATA) { + event.setPacket(event.getPacket().deepClone()); + StructureModifier mods = event.getPacket().getModifier(); + Iterator itel = ((List) mods.read(1)).iterator(); + while (itel.hasNext()) { + WatchableObject watch = itel.next(); + if (watch.a() == 0) { + byte b = (Byte) watch.b(); + byte a = (byte) (b | 1 << 5); + if ((b & 1 << 3) != 0) + a = (byte) (a | 1 << 3); + watch.a(a); + } + } + } else { + switch (event.getPacketID()) { + case Packets.Server.NAMED_ENTITY_SPAWN: + case Packets.Server.ATTACH_ENTITY: + case Packets.Server.REL_ENTITY_MOVE: + case Packets.Server.REL_ENTITY_MOVE_LOOK: + case Packets.Server.ENTITY_LOOK: + case Packets.Server.ENTITY_TELEPORT: + case Packets.Server.ENTITY_HEAD_ROTATION: + case Packets.Server.MOB_EFFECT: + case Packets.Server.ENTITY_EQUIPMENT: + if (event.getPacketID() == Packets.Server.NAMED_ENTITY_SPAWN) { + PacketContainer packet = new PacketContainer(Packets.Server.ENTITY_METADATA); + StructureModifier mods = packet.getModifier(); + mods.write(0, entity.getEntityId()); + List watchableList = new ArrayList(); + byte b = (byte) (0 | 1 << 5); + if (event.getPlayer().isSprinting()) + b = (byte) (b | 1 << 3); + watchableList.add(new WatchableObject(0, 0, b)); + mods.write(1, watchableList); + try { + ProtocolLibrary.getProtocolManager().sendServerPacket(event.getPlayer(), packet, false); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + event.setCancelled(true); + break; + case Packets.Server.ENTITY_STATUS: + if (DisguiseAPI.getDisguise(entity).canHearSelfDisguise() + && (Byte) event.getPacket().getModifier().read(1) == 2) + event.setCancelled(true); + break; + default: + break; + } + } + } + } + } + }; + } + + public static boolean isHearDisguisesEnabled() { + return soundsListenerEnabled; + } + + public static boolean isViewDisguisesListenerEnabled() { + return viewDisguisesListenerEnabled; + } + + public static void setHearDisguisesListener(boolean enabled) { + if (soundsListenerEnabled != enabled) { + soundsListenerEnabled = enabled; + if (soundsListenerEnabled) { + ProtocolLibrary.getProtocolManager().addPacketListener(soundsListener); + } else { + ProtocolLibrary.getProtocolManager().removePacketListener(soundsListener); + } + } + } + + public static void setViewDisguisesListener(boolean enabled) { + if (viewDisguisesListenerEnabled != enabled) { + viewDisguisesListenerEnabled = enabled; + if (viewDisguisesListenerEnabled) { + ProtocolLibrary.getProtocolManager().addPacketListener(viewDisguisesListener); + } else { + ProtocolLibrary.getProtocolManager().removePacketListener(viewDisguisesListener); + } + } + } + + private static PacketContainer[] transformPacket(PacketContainer sentPacket, Player observer) { + PacketContainer[] packets = new PacketContainer[] { sentPacket }; + try { + // First get the entity, the one sending this packet + StructureModifier entityModifer = sentPacket.getEntityModifier(observer.getWorld()); + org.bukkit.entity.Entity entity = entityModifer.read((Packets.Server.COLLECT == sentPacket.getID() ? 1 : 0)); + Disguise disguise = DisguiseAPI.getDisguise(entity); + // If disguised. + if (disguise != null) { + // If packet is Packets.Server.UPDATE_ATTRIBUTES + // This packet sends attributes + + switch (sentPacket.getID()) { + 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]; + } + break; + } + + // Else if the packet is sending entity metadata + case Packets.Server.ENTITY_METADATA: + + { + List watchableObjects = disguise.getWatcher().convert( + (List) packets[0].getModifier().read(1)); + packets[0] = new PacketContainer(sentPacket.getID()); + StructureModifier newMods = packets[0].getModifier(); + newMods.write(0, entity.getEntityId()); + newMods.write(1, watchableObjects); + break; + } + + // Else if the packet is spawning.. + case Packets.Server.NAMED_ENTITY_SPAWN: + case Packets.Server.MOB_SPAWN: + case Packets.Server.ADD_EXP_ORB: + case Packets.Server.VEHICLE_SPAWN: + case Packets.Server.ENTITY_PAINTING: + + { + packets = constructPacket(disguise, entity); + break; + } + + // Else if the disguise is attempting to send players a forbidden packet + case Packets.Server.ARM_ANIMATION: + case Packets.Server.COLLECT: + + { + if (disguise.getType().isMisc()) + packets = new PacketContainer[0]; + break; + + } + // Else if the disguise is moving. + case Packets.Server.REL_ENTITY_MOVE_LOOK: + case Packets.Server.ENTITY_LOOK: + case Packets.Server.ENTITY_TELEPORT: + + { + packets[0] = sentPacket.shallowClone(); + StructureModifier mods = packets[0].getModifier(); + byte value = (Byte) mods.read(4); + mods.write(4, getYaw(disguise.getType(), DisguiseType.getType(entity.getType()), value)); + break; + } + + case Packets.Server.ENTITY_EQUIPMENT: + + { + int slot = (Integer) packets[0].getModifier().read(1) - 1; + if (slot < 0) + slot = 4; + org.bukkit.inventory.ItemStack itemstack = disguise.getWatcher().getItemStack(slot); + if (itemstack != null) { + packets[0] = packets[0].shallowClone(); + packets[0].getModifier().write(2, + (itemstack.getTypeId() == 0 ? null : CraftItemStack.asNMSCopy(itemstack))); + } + break; + } + + default: + break; + } + } + } catch (Exception e) { + e.printStackTrace(); + } + return packets; + } +}