From eff6dc30257ad9bb7c29f8e0528a2b6e5a171dc7 Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Fri, 13 May 2016 08:27:53 +1200 Subject: [PATCH] Cleanup, fix a bug --- .../disguise/utilities/DisguiseUtilities.java | 61 +- .../PacketListenerClientInteract.java | 36 +- .../PacketListenerInventory.java | 524 +++++++++--------- .../packetlisteners/PacketListenerMain.java | 79 +-- .../packetlisteners/PacketListenerSounds.java | 198 +++---- .../PacketListenerViewDisguises.java | 222 ++++---- 6 files changed, 569 insertions(+), 551 deletions(-) diff --git a/src/me/libraryaddict/disguise/utilities/DisguiseUtilities.java b/src/me/libraryaddict/disguise/utilities/DisguiseUtilities.java index 789629e0..22ac3132 100644 --- a/src/me/libraryaddict/disguise/utilities/DisguiseUtilities.java +++ b/src/me/libraryaddict/disguise/utilities/DisguiseUtilities.java @@ -446,51 +446,48 @@ public class DisguiseUtilities { continue; } - try - { - int chunkX = (int) Math.floor(loc.getX() / 16D) - 17, chunkZ = (int) Math.floor(loc.getZ() / 16D) - 17; - chunkX -= chunkX % 8; - chunkZ -= chunkZ % 8; + int chunkX = (int) Math.floor(loc.getX() / 16D) - 17, chunkZ = (int) Math.floor(loc.getZ() / 16D) - 17; - xChunk.set(bedChunk, chunkX); - zChunk.set(bedChunk, chunkZ); - } - catch (Exception ex) - { - ex.printStackTrace(System.out); - } + chunkX -= chunkX % 8; + chunkZ -= chunkZ % 8; // Make unload packets - try - { - packets[i] = ProtocolLibrary.getProtocolManager().createPacketConstructor(Server.MAP_CHUNK, bedChunk, 0) - .createPacket(bedChunk, 0); - } - catch (IllegalArgumentException ex) - { - try - { - packets[i] = ProtocolLibrary.getProtocolManager() - .createPacketConstructor(Server.MAP_CHUNK, bedChunk, true, 0, 40).createPacket(bedChunk, true, 0, 48); - } - catch (IllegalArgumentException ex1) - { - packets[i] = ProtocolLibrary.getProtocolManager().createPacketConstructor(Server.MAP_CHUNK, bedChunk, true, 0) - .createPacket(bedChunk, true, 0); - } - } + + packets[i] = ProtocolLibrary.getProtocolManager().createPacketConstructor(Server.UNLOAD_CHUNK, chunkX, chunkZ) + .createPacket(bedChunk, 0); i++; + // Make load packets if (oldLoc == null || i > 1) { + try + { + xChunk.set(bedChunk, chunkX); + zChunk.set(bedChunk, chunkZ); + } + catch (Exception ex) + { + ex.printStackTrace(System.out); + } + // MAP_CHUNK_BULK was replaced in 1.9 with several seperated chunk packets // packets[i] = ProtocolLibrary.getProtocolManager() // .createPacketConstructor(Server.MAP_CHUNK_BULK, Arrays.asList(bedChunk)) // .createPacket(Arrays.asList(bedChunk)); - packets[i] = ProtocolLibrary.getProtocolManager().createPacketConstructor(Server.MAP_CHUNK, bedChunk, true, 0) - .createPacket(bedChunk, true, 0); + // Make unload packets + try + { + packets[i] = ProtocolLibrary.getProtocolManager().createPacketConstructor(Server.MAP_CHUNK, bedChunk, 0) + .createPacket(bedChunk, 0); + } + catch (IllegalArgumentException ex) + { + packets[i] = ProtocolLibrary.getProtocolManager().createPacketConstructor(Server.MAP_CHUNK, bedChunk, true, 0) + .createPacket(bedChunk, true, 0); + } + i++; } } diff --git a/src/me/libraryaddict/disguise/utilities/packetlisteners/PacketListenerClientInteract.java b/src/me/libraryaddict/disguise/utilities/packetlisteners/PacketListenerClientInteract.java index b65118fc..476fc85a 100644 --- a/src/me/libraryaddict/disguise/utilities/packetlisteners/PacketListenerClientInteract.java +++ b/src/me/libraryaddict/disguise/utilities/packetlisteners/PacketListenerClientInteract.java @@ -53,28 +53,32 @@ public class PacketListenerClientInteract extends PacketAdapter event.setCancelled(true); } - ItemStack item = observer.getItemInHand(); - - if (item != null && item.getType() == Material.INK_SACK) + for (ItemStack item : new ItemStack[] + { + observer.getInventory().getItemInMainHand(), observer.getInventory().getItemInOffHand() + }) { + if (item == null || item.getType() != Material.INK_SACK) + continue; + Disguise disguise = DisguiseAPI.getDisguise(observer, entity); - if (disguise != null && (disguise.getType() == DisguiseType.SHEEP || disguise.getType() == DisguiseType.WOLF)) + if (disguise == null || (disguise.getType() != DisguiseType.SHEEP && disguise.getType() != DisguiseType.WOLF)) + continue; + + AnimalColor color = AnimalColor.getColor(item.getDurability()); + + if (disguise.getType() == DisguiseType.SHEEP) { - AnimalColor color = AnimalColor.getColor(item.getDurability()); + SheepWatcher watcher = (SheepWatcher) disguise.getWatcher(); - if (disguise.getType() == DisguiseType.SHEEP) - { - SheepWatcher watcher = (SheepWatcher) disguise.getWatcher(); + watcher.setColor(DisguiseConfig.isSheepDyeable() ? color : watcher.getColor()); + } + else + { + WolfWatcher watcher = (WolfWatcher) disguise.getWatcher(); - watcher.setColor(DisguiseConfig.isSheepDyeable() ? color : watcher.getColor()); - } - else - { - WolfWatcher watcher = (WolfWatcher) disguise.getWatcher(); - - watcher.setCollarColor(DisguiseConfig.isWolfDyeable() ? color : watcher.getCollarColor()); - } + watcher.setCollarColor(DisguiseConfig.isWolfDyeable() ? color : watcher.getCollarColor()); } } } diff --git a/src/me/libraryaddict/disguise/utilities/packetlisteners/PacketListenerInventory.java b/src/me/libraryaddict/disguise/utilities/packetlisteners/PacketListenerInventory.java index 43438c06..49057c09 100644 --- a/src/me/libraryaddict/disguise/utilities/packetlisteners/PacketListenerInventory.java +++ b/src/me/libraryaddict/disguise/utilities/packetlisteners/PacketListenerInventory.java @@ -41,123 +41,38 @@ public class PacketListenerInventory extends PacketAdapter if (event.getPlayer().getName().contains("UNKNOWN[")) // If the player is temporary return; - if (!(event.getPlayer() instanceof com.comphenix.net.sf.cglib.proxy.Factory) && event.getPlayer().getVehicle() == null) + if (event.getPlayer() instanceof com.comphenix.net.sf.cglib.proxy.Factory || event.getPlayer().getVehicle() != null) { - Disguise disguise = DisguiseAPI.getDisguise(event.getPlayer(), event.getPlayer()); + return; + } - // If player is disguised, views self disguises and has a inventory modifier - if (disguise != null && disguise.isSelfDisguiseVisible() - && (disguise.isHidingArmorFromSelf() || disguise.isHidingHeldItemFromSelf())) + Disguise disguise = DisguiseAPI.getDisguise(event.getPlayer(), event.getPlayer()); + + // If player is disguised, views self disguises and has a inventory modifier + if (disguise != null && disguise.isSelfDisguiseVisible() + && (disguise.isHidingArmorFromSelf() || disguise.isHidingHeldItemFromSelf())) + { + // If they are in creative and clicked on a slot + if (event.getPacketType() == PacketType.Play.Client.SET_CREATIVE_SLOT) { - // If they are in creative and clicked on a slot - if (event.getPacketType() == PacketType.Play.Client.SET_CREATIVE_SLOT) + int slot = event.getPacket().getIntegers().read(0); + + if (slot >= 5 && slot <= 8) { - int slot = event.getPacket().getIntegers().read(0); - - if (slot >= 5 && slot <= 8) + if (disguise.isHidingArmorFromSelf()) { - if (disguise.isHidingArmorFromSelf()) - { - int armorSlot = Math.abs((slot - 5) - 3); + int armorSlot = Math.abs((slot - 5) - 3); - org.bukkit.inventory.ItemStack item = event.getPlayer().getInventory().getArmorContents()[armorSlot]; + org.bukkit.inventory.ItemStack item = event.getPlayer().getInventory().getArmorContents()[armorSlot]; - if (item != null && item.getType() != Material.AIR) - { - PacketContainer packet = new PacketContainer(Server.SET_SLOT); - - StructureModifier mods = packet.getModifier(); - - mods.write(0, 0); - mods.write(1, slot); - mods.write(2, ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(0))); - - try - { - ProtocolLibrary.getProtocolManager().sendServerPacket(event.getPlayer(), packet, false); - } - catch (InvocationTargetException e) - { - e.printStackTrace(); - } - } - } - } - else if (slot >= 36 && slot <= 44) - { - if (disguise.isHidingHeldItemFromSelf()) - { - int currentSlot = event.getPlayer().getInventory().getHeldItemSlot(); - - if (slot + 36 == currentSlot) - { - org.bukkit.inventory.ItemStack item = event.getPlayer().getItemInHand(); - - if (item != null && item.getType() != Material.AIR) - { - PacketContainer packet = new PacketContainer(Server.SET_SLOT); - - StructureModifier mods = packet.getModifier(); - mods.write(0, 0); - mods.write(1, slot); - mods.write(2, ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(0))); - - try - { - ProtocolLibrary.getProtocolManager().sendServerPacket(event.getPlayer(), packet, false); - } - catch (InvocationTargetException e) - { - e.printStackTrace(); - } - } - } - } - } - } - // If the player switched item, aka he moved from slot 1 to slot 2 - else if (event.getPacketType() == PacketType.Play.Client.HELD_ITEM_SLOT) - { - if (disguise.isHidingHeldItemFromSelf()) - { - // From logging, it seems that both bukkit and nms uses the same thing for the slot switching. - // 0 1 2 3 - 8 - // If the packet is coming, then I need to replace the item they are switching to - // As for the old item, I need to restore it. - org.bukkit.inventory.ItemStack currentlyHeld = event.getPlayer().getItemInHand(); - // If his old weapon isn't air - if (currentlyHeld != null && currentlyHeld.getType() != Material.AIR) + if (item != null && item.getType() != Material.AIR) { PacketContainer packet = new PacketContainer(Server.SET_SLOT); StructureModifier mods = packet.getModifier(); mods.write(0, 0); - mods.write(1, event.getPlayer().getInventory().getHeldItemSlot() + 36); - mods.write(2, ReflectionManager.getNmsItem(currentlyHeld)); - - try - { - ProtocolLibrary.getProtocolManager().sendServerPacket(event.getPlayer(), packet, false); - } - catch (InvocationTargetException e) - { - e.printStackTrace(); - } - } - - org.bukkit.inventory.ItemStack newHeld = event.getPlayer().getInventory() - .getItem(event.getPacket().getIntegers().read(0)); - - // If his new weapon isn't air either! - if (newHeld != null && newHeld.getType() != Material.AIR) - { - PacketContainer packet = new PacketContainer(Server.SET_SLOT); - - StructureModifier mods = packet.getModifier(); - - mods.write(0, 0); - mods.write(1, event.getPacket().getIntegers().read(0) + 36); + mods.write(1, slot); mods.write(2, ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(0))); try @@ -171,55 +86,21 @@ public class PacketListenerInventory extends PacketAdapter } } } - else if (event.getPacketType() == PacketType.Play.Client.WINDOW_CLICK) + else if (slot >= 36 && slot <= 44) { - int slot = event.getPacket().getIntegers().read(1); - - org.bukkit.inventory.ItemStack clickedItem; - - if (event.getPacket().getShorts().read(0) == 1) + if (disguise.isHidingHeldItemFromSelf()) { - // Its a shift click - clickedItem = event.getPacket().getItemModifier().read(0); + int currentSlot = event.getPlayer().getInventory().getHeldItemSlot(); - if (clickedItem != null && clickedItem.getType() != Material.AIR) + if (slot + 36 == currentSlot) { - // Rather than predict the clients actions - // Lets just update the entire inventory.. - Bukkit.getScheduler().runTask(libsDisguises, new Runnable() - { - public void run() - { - event.getPlayer().updateInventory(); - } - }); - } + org.bukkit.inventory.ItemStack item = event.getPlayer().getItemInHand(); - return; - } - else - { - // If its not a player inventory click - // Shift clicking is exempted for the item in hand.. - if (event.getPacket().getIntegers().read(0) != 0) - { - return; - } - - clickedItem = event.getPlayer().getItemOnCursor(); - } - - if (clickedItem != null && clickedItem.getType() != Material.AIR) - { - // If the slot is a armor slot - if (slot >= 5 && slot <= 8) - { - if (disguise.isHidingArmorFromSelf()) + if (item != null && item.getType() != Material.AIR) { PacketContainer packet = new PacketContainer(Server.SET_SLOT); StructureModifier mods = packet.getModifier(); - mods.write(0, 0); mods.write(1, slot); mods.write(2, ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(0))); @@ -233,83 +114,126 @@ public class PacketListenerInventory extends PacketAdapter e.printStackTrace(); } } - // Else if its a hotbar slot - } - else if (slot >= 36 && slot <= 44) - { - if (disguise.isHidingHeldItemFromSelf()) - { - int currentSlot = event.getPlayer().getInventory().getHeldItemSlot(); - - // Check if the player is on the same slot as the slot that its setting - if (slot == currentSlot + 36) - { - PacketContainer packet = new PacketContainer(Server.SET_SLOT); - - StructureModifier mods = packet.getModifier(); - mods.write(0, 0); - mods.write(1, slot); - mods.write(2, ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(0))); - - try - { - ProtocolLibrary.getProtocolManager().sendServerPacket(event.getPlayer(), packet, false); - } - catch (InvocationTargetException e) - { - e.printStackTrace(); - } - } - } } } } } - } - } - - @Override - public void onPacketSending(PacketEvent event) - { - // If the inventory is the players inventory - if (!(event.getPlayer() instanceof com.comphenix.net.sf.cglib.proxy.Factory) && event.getPlayer().getVehicle() == null - && event.getPacket().getIntegers().read(0) == 0) - { - - Disguise disguise = DisguiseAPI.getDisguise(event.getPlayer(), event.getPlayer()); - - // If the player is disguised, views self disguises and is hiding a item. - if (disguise != null && disguise.isSelfDisguiseVisible() - && (disguise.isHidingArmorFromSelf() || disguise.isHidingHeldItemFromSelf())) + // If the player switched item, aka he moved from slot 1 to slot 2 + else if (event.getPacketType() == PacketType.Play.Client.HELD_ITEM_SLOT) { - // If the server is setting the slot - // Need to set it to air if its in a place it shouldn't be. - // Things such as picking up a item, spawned in item. Plugin sets the item. etc. Will fire this - /** - * Done - */ - if (event.getPacketType() == Server.SET_SLOT) + if (disguise.isHidingHeldItemFromSelf()) { - // The raw slot - // nms code has the start of the hotbar being 36. - int slot = event.getPacket().getIntegers().read(1); + // From logging, it seems that both bukkit and nms uses the same thing for the slot switching. + // 0 1 2 3 - 8 + // If the packet is coming, then I need to replace the item they are switching to + // As for the old item, I need to restore it. + org.bukkit.inventory.ItemStack currentlyHeld = event.getPlayer().getItemInHand(); + // If his old weapon isn't air + if (currentlyHeld != null && currentlyHeld.getType() != Material.AIR) + { + PacketContainer packet = new PacketContainer(Server.SET_SLOT); + StructureModifier mods = packet.getModifier(); + + mods.write(0, 0); + mods.write(1, event.getPlayer().getInventory().getHeldItemSlot() + 36); + mods.write(2, ReflectionManager.getNmsItem(currentlyHeld)); + + try + { + ProtocolLibrary.getProtocolManager().sendServerPacket(event.getPlayer(), packet, false); + } + catch (InvocationTargetException e) + { + e.printStackTrace(); + } + } + + org.bukkit.inventory.ItemStack newHeld = event.getPlayer().getInventory() + .getItem(event.getPacket().getIntegers().read(0)); + + // If his new weapon isn't air either! + if (newHeld != null && newHeld.getType() != Material.AIR) + { + PacketContainer packet = new PacketContainer(Server.SET_SLOT); + + StructureModifier mods = packet.getModifier(); + + mods.write(0, 0); + mods.write(1, event.getPacket().getIntegers().read(0) + 36); + mods.write(2, ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(0))); + + try + { + ProtocolLibrary.getProtocolManager().sendServerPacket(event.getPlayer(), packet, false); + } + catch (InvocationTargetException e) + { + e.printStackTrace(); + } + } + } + } + else if (event.getPacketType() == PacketType.Play.Client.WINDOW_CLICK) + { + int slot = event.getPacket().getIntegers().read(1); + + org.bukkit.inventory.ItemStack clickedItem; + + if (event.getPacket().getShorts().read(0) == 1) + { + // Its a shift click + clickedItem = event.getPacket().getItemModifier().read(0); + + if (clickedItem != null && clickedItem.getType() != Material.AIR) + { + // Rather than predict the clients actions + // Lets just update the entire inventory.. + Bukkit.getScheduler().runTask(libsDisguises, new Runnable() + { + public void run() + { + event.getPlayer().updateInventory(); + } + }); + } + + return; + } + else + { + // If its not a player inventory click + // Shift clicking is exempted for the item in hand.. + if (event.getPacket().getIntegers().read(0) != 0) + { + return; + } + + clickedItem = event.getPlayer().getItemOnCursor(); + } + + if (clickedItem != null && clickedItem.getType() != Material.AIR) + { // If the slot is a armor slot if (slot >= 5 && slot <= 8) { if (disguise.isHidingArmorFromSelf()) { - // Get the bukkit armor slot! - int armorSlot = Math.abs((slot - 5) - 3); + PacketContainer packet = new PacketContainer(Server.SET_SLOT); - org.bukkit.inventory.ItemStack item = event.getPlayer().getInventory().getArmorContents()[armorSlot]; + StructureModifier mods = packet.getModifier(); - if (item != null && item.getType() != Material.AIR) + mods.write(0, 0); + mods.write(1, slot); + mods.write(2, ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(0))); + + try { - event.setPacket(event.getPacket().shallowClone()); - - event.getPacket().getModifier().write(2, - ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(0))); + ProtocolLibrary.getProtocolManager().sendServerPacket(event.getPlayer(), packet, false); + } + catch (InvocationTargetException e) + { + e.printStackTrace(); } } // Else if its a hotbar slot @@ -323,68 +247,150 @@ public class PacketListenerInventory extends PacketAdapter // Check if the player is on the same slot as the slot that its setting if (slot == currentSlot + 36) { - org.bukkit.inventory.ItemStack item = event.getPlayer().getItemInHand(); + PacketContainer packet = new PacketContainer(Server.SET_SLOT); - if (item != null && item.getType() != Material.AIR) + StructureModifier mods = packet.getModifier(); + mods.write(0, 0); + mods.write(1, slot); + mods.write(2, ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(0))); + + try { - event.setPacket(event.getPacket().shallowClone()); - event.getPacket().getModifier().write(2, - ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(0))); + ProtocolLibrary.getProtocolManager().sendServerPacket(event.getPlayer(), packet, false); + } + catch (InvocationTargetException e) + { + e.printStackTrace(); } } } } } - else if (event.getPacketType() == Server.WINDOW_ITEMS) - { - event.setPacket(event.getPacket().deepClone()); - - 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()) - { - // Get the bukkit armor slot! - 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] = new org.bukkit.inventory.ItemStack(0); - } - } - // Else if its a hotbar slot - } - else if (slot >= 36 && slot <= 44) - { - if (disguise.isHidingHeldItemFromSelf()) - { - int currentSlot = event.getPlayer().getInventory().getHeldItemSlot(); - - // Check if the player is on the same slot as the slot that its setting - if (slot == currentSlot + 36) - { - org.bukkit.inventory.ItemStack item = event.getPlayer().getItemInHand(); - - if (item != null && item.getType() != Material.AIR) - { - items[slot] = new org.bukkit.inventory.ItemStack(0); - } - } - } - } - } - - mods.write(0, items); - } } } + + } + + @Override + public void onPacketSending(PacketEvent event) + { + // If the inventory is the players inventory + if (event.getPlayer() instanceof com.comphenix.net.sf.cglib.proxy.Factory || event.getPlayer().getVehicle() != null + || event.getPacket().getIntegers().read(0) != 0) + { + return; + } + + Disguise disguise = DisguiseAPI.getDisguise(event.getPlayer(), event.getPlayer()); + + if (disguise == null || !disguise.isSelfDisguiseVisible() + || (!disguise.isHidingArmorFromSelf() && !disguise.isHidingHeldItemFromSelf())) + { + return; + } + + // If the player is disguised, views self disguises and is hiding a item. + + // If the server is setting the slot + // Need to set it to air if its in a place it shouldn't be. + // Things such as picking up a item, spawned in item. Plugin sets the item. etc. Will fire this + /** + * Done + */ + if (event.getPacketType() == Server.SET_SLOT) + { + // The raw slot + // nms code has the start of the hotbar being 36. + int slot = event.getPacket().getIntegers().read(1); + + // If the slot is a armor slot + if (slot >= 5 && slot <= 8) + { + if (disguise.isHidingArmorFromSelf()) + { + // Get the bukkit armor slot! + int armorSlot = Math.abs((slot - 5) - 3); + + org.bukkit.inventory.ItemStack item = event.getPlayer().getInventory().getArmorContents()[armorSlot]; + + if (item != null && item.getType() != Material.AIR) + { + event.setPacket(event.getPacket().shallowClone()); + + event.getPacket().getModifier().write(2, + ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(0))); + } + } + // Else if its a hotbar slot + } + else if (slot >= 36 && slot <= 44) + { + if (disguise.isHidingHeldItemFromSelf()) + { + int currentSlot = event.getPlayer().getInventory().getHeldItemSlot(); + + // Check if the player is on the same slot as the slot that its setting + if (slot == currentSlot + 36) + { + org.bukkit.inventory.ItemStack item = event.getPlayer().getItemInHand(); + + if (item != null && item.getType() != Material.AIR) + { + event.setPacket(event.getPacket().shallowClone()); + event.getPacket().getModifier().write(2, + ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(0))); + } + } + } + } + } + else if (event.getPacketType() == Server.WINDOW_ITEMS) + { + event.setPacket(event.getPacket().deepClone()); + + 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()) + { + // Get the bukkit armor slot! + 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] = new org.bukkit.inventory.ItemStack(0); + } + } + // Else if its a hotbar slot + } + else if (slot >= 36 && slot <= 44) + { + if (disguise.isHidingHeldItemFromSelf()) + { + int currentSlot = event.getPlayer().getInventory().getHeldItemSlot(); + + // Check if the player is on the same slot as the slot that its setting + if (slot == currentSlot + 36) + { + org.bukkit.inventory.ItemStack item = event.getPlayer().getItemInHand(); + + if (item != null && item.getType() != Material.AIR) + { + items[slot] = new org.bukkit.inventory.ItemStack(0); + } + } + } + } + } + + mods.write(0, items); + } } } diff --git a/src/me/libraryaddict/disguise/utilities/packetlisteners/PacketListenerMain.java b/src/me/libraryaddict/disguise/utilities/packetlisteners/PacketListenerMain.java index 01b37912..6d93d939 100644 --- a/src/me/libraryaddict/disguise/utilities/packetlisteners/PacketListenerMain.java +++ b/src/me/libraryaddict/disguise/utilities/packetlisteners/PacketListenerMain.java @@ -53,45 +53,50 @@ public class PacketListenerMain extends PacketAdapter PacketContainer[][] packets = PacketsManager.transformPacket(event.getPacket(), event.getPlayer(), entity); - if (packets != null) + if (packets == null) { - event.setCancelled(true); - - try - { - for (PacketContainer packet : packets[0]) - { - ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet, false); - } - - final PacketContainer[] delayed = packets[1]; - - if (delayed.length > 0) - { - Bukkit.getScheduler().scheduleSyncDelayedTask(libsDisguises, new Runnable() - { - public void run() - { - try - { - for (PacketContainer packet : delayed) - { - ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet, false); - } - } - catch (InvocationTargetException e) - { - e.printStackTrace(); - } - } - }, 2); - } - } - catch (InvocationTargetException ex) - { - ex.printStackTrace(); - } + return; } + + event.setCancelled(true); + + try + { + for (PacketContainer packet : packets[0]) + { + ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet, false); + } + + final PacketContainer[] delayed = packets[1]; + + if (delayed.length == 0) + { + return; + } + + Bukkit.getScheduler().scheduleSyncDelayedTask(libsDisguises, new Runnable() + { + public void run() + { + try + { + for (PacketContainer packet : delayed) + { + ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet, false); + } + } + catch (InvocationTargetException e) + { + e.printStackTrace(); + } + } + }, 2); + } + catch (InvocationTargetException ex) + { + ex.printStackTrace(); + } + } } diff --git a/src/me/libraryaddict/disguise/utilities/packetlisteners/PacketListenerSounds.java b/src/me/libraryaddict/disguise/utilities/packetlisteners/PacketListenerSounds.java index 42ff8301..54aabb33 100644 --- a/src/me/libraryaddict/disguise/utilities/packetlisteners/PacketListenerSounds.java +++ b/src/me/libraryaddict/disguise/utilities/packetlisteners/PacketListenerSounds.java @@ -286,116 +286,118 @@ public class PacketListenerSounds extends PacketAdapter } else if (event.getPacketType() == Server.ENTITY_STATUS) { - if ((byte) mods.read(1) == 2) + if ((byte) mods.read(1) != 2) { - // It made a damage animation - Entity entity = event.getPacket().getEntityModifier(observer.getWorld()).read(0); + return; + } - Disguise disguise = DisguiseAPI.getDisguise(observer, entity); + // It made a damage animation + Entity entity = event.getPacket().getEntityModifier(observer.getWorld()).read(0); - if (disguise != null && !disguise.getType().isPlayer() - && (disguise.isSelfDisguiseSoundsReplaced() || entity != event.getPlayer())) + Disguise disguise = DisguiseAPI.getDisguise(observer, entity); + + if (disguise != null && !disguise.getType().isPlayer() + && (disguise.isSelfDisguiseSoundsReplaced() || entity != event.getPlayer())) + { + DisguiseSound disSound = DisguiseSound.getType(entity.getType().name()); + + if (disSound == null) + return; + + SoundType soundType = null; + Object obj = null; + + if (entity instanceof LivingEntity) { - DisguiseSound disSound = DisguiseSound.getType(entity.getType().name()); - - if (disSound == null) - return; - - SoundType soundType = null; - Object obj = null; - - if (entity instanceof LivingEntity) + try { - try - { - obj = LivingEntity.class.getMethod("getHealth").invoke(entity); + obj = LivingEntity.class.getMethod("getHealth").invoke(entity); - if (obj instanceof Double ? (Double) obj == 0 : (Integer) obj == 0) + if (obj instanceof Double ? (Double) obj == 0 : (Integer) obj == 0) + { + soundType = SoundType.DEATH; + } + else + { + obj = null; + } + } + catch (Exception e) + { + e.printStackTrace(); + } + } + + if (obj == null) + { + soundType = SoundType.HURT; + } + + if (disSound.getSound(soundType) == null + || (disguise.isSelfDisguiseSoundsReplaced() && entity == event.getPlayer())) + { + if (disguise.isSelfDisguiseSoundsReplaced() && entity == event.getPlayer()) + { + cancelSound = !cancelSound; + + if (cancelSound) + return; + } + + 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(Server.NAMED_SOUND_EFFECT); + + mods = packet.getModifier(); + + Object craftSoundEffect = ReflectionManager.getCraftSoundEffect(sound); + + mods.write(0, craftSoundEffect); + mods.write(1, ReflectionManager.getSoundCategory(disguise.getType())); // Meh + mods.write(2, (int) (loc.getX() * 8D)); + mods.write(3, (int) (loc.getY() * 8D)); + mods.write(4, (int) (loc.getZ() * 8D)); + mods.write(5, disSound.getDamageAndIdleSoundVolume()); + + float pitch; + + if (disguise instanceof MobDisguise && !((MobDisguise) disguise).isAdult()) { - soundType = SoundType.DEATH; + pitch = (DisguiseUtilities.random.nextFloat() - DisguiseUtilities.random.nextFloat()) * 0.2F + + 1.5F; } else + pitch = (DisguiseUtilities.random.nextFloat() - DisguiseUtilities.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(6, (int) pitch); + + try { - obj = null; + ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet, false); } - } - catch (Exception e) - { - e.printStackTrace(); - } - } - - if (obj == null) - { - soundType = SoundType.HURT; - } - - if (disSound.getSound(soundType) == null - || (disguise.isSelfDisguiseSoundsReplaced() && entity == event.getPlayer())) - { - if (disguise.isSelfDisguiseSoundsReplaced() && entity == event.getPlayer()) - { - cancelSound = !cancelSound; - - if (cancelSound) - return; - } - - disSound = DisguiseSound.getType(disguise.getType().name()); - - if (disSound != null) - { - String sound = disSound.getSound(soundType); - - if (sound != null) + catch (InvocationTargetException e) { - Location loc = entity.getLocation(); - - PacketContainer packet = new PacketContainer(Server.NAMED_SOUND_EFFECT); - - mods = packet.getModifier(); - - Object craftSoundEffect = ReflectionManager.getCraftSoundEffect(sound); - - mods.write(0, craftSoundEffect); - mods.write(1, ReflectionManager.getSoundCategory(disguise.getType())); // Meh - mods.write(2, (int) (loc.getX() * 8D)); - mods.write(3, (int) (loc.getY() * 8D)); - mods.write(4, (int) (loc.getZ() * 8D)); - mods.write(5, disSound.getDamageAndIdleSoundVolume()); - - float pitch; - - if (disguise instanceof MobDisguise && !((MobDisguise) disguise).isAdult()) - { - pitch = (DisguiseUtilities.random.nextFloat() - DisguiseUtilities.random.nextFloat()) * 0.2F - + 1.5F; - } - else - pitch = (DisguiseUtilities.random.nextFloat() - DisguiseUtilities.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(6, (int) pitch); - - try - { - ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet, false); - } - catch (InvocationTargetException e) - { - e.printStackTrace(); - } + e.printStackTrace(); } } } diff --git a/src/me/libraryaddict/disguise/utilities/packetlisteners/PacketListenerViewDisguises.java b/src/me/libraryaddict/disguise/utilities/packetlisteners/PacketListenerViewDisguises.java index f6dad140..2b6742fa 100644 --- a/src/me/libraryaddict/disguise/utilities/packetlisteners/PacketListenerViewDisguises.java +++ b/src/me/libraryaddict/disguise/utilities/packetlisteners/PacketListenerViewDisguises.java @@ -47,141 +47,145 @@ public class PacketListenerViewDisguises extends PacketAdapter if (observer.getName().contains("UNKNOWN[")) // If the player is temporary return; - if (event.getPacket().getIntegers().read(0) == observer.getEntityId()) + if (event.getPacket().getIntegers().read(0) != observer.getEntityId()) { - if (DisguiseAPI.isSelfDisguised(observer)) + return; + } + + if (!DisguiseAPI.isSelfDisguised(observer)) + { + return; + } + + // Here I grab the packets to convert them to, So I can display them as if the disguise sent them. + PacketContainer[][] transformed = PacketsManager.transformPacket(event.getPacket(), observer, observer); + + PacketContainer[] packets = transformed == null ? null : transformed[0]; + + final PacketContainer[] delayedPackets = transformed == null ? null : transformed[1]; + + if (packets == null) + { + packets = new PacketContainer[] + { + event.getPacket() + }; + } + + for (PacketContainer packet : packets) + { + if (packet.getType() != Server.PLAYER_INFO) { - // Here I grab the packets to convert them to, So I can display them as if the disguise sent them. - PacketContainer[][] transformed = PacketsManager.transformPacket(event.getPacket(), observer, observer); - - PacketContainer[] packets = transformed == null ? null : transformed[0]; - - final PacketContainer[] delayedPackets = transformed == null ? null : transformed[1]; - - if (packets == null) + if (packet.equals(event.getPacket())) { - packets = new PacketContainer[] - { - event.getPacket() - }; + packet = packet.shallowClone(); } - for (PacketContainer packet : packets) + packet.getIntegers().write(0, DisguiseAPI.getSelfDisguiseId()); + } + + try + { + ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet, false); + } + catch (InvocationTargetException e) + { + e.printStackTrace(); + } + } + + if (delayedPackets != null && delayedPackets.length > 0) + { + Bukkit.getScheduler().scheduleSyncDelayedTask(libsDisguises, new Runnable() + { + public void run() { - if (packet.getType() != Server.PLAYER_INFO) - { - if (packet.equals(event.getPacket())) - { - packet = packet.shallowClone(); - } - - packet.getIntegers().write(0, DisguiseAPI.getSelfDisguiseId()); - } - try { - ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet, false); + for (PacketContainer packet : delayedPackets) + { + ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet, false); + } } catch (InvocationTargetException e) { e.printStackTrace(); } } + }, 2); + } - if (delayedPackets != null && delayedPackets.length > 0) + if (event.getPacketType() == Server.ENTITY_METADATA) + { + event.setPacket(event.getPacket().deepClone()); + + for (WrappedWatchableObject watch : event.getPacket().getWatchableCollectionModifier().read(0)) + { + if (watch.getIndex() == 0) { - Bukkit.getScheduler().scheduleSyncDelayedTask(libsDisguises, new Runnable() - { - public void run() - { - try - { - for (PacketContainer packet : delayedPackets) - { - ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet, false); - } - } - catch (InvocationTargetException e) - { - e.printStackTrace(); - } - } - }, 2); + byte b = (byte) watch.getValue(); + + byte a = (byte) (b | 1 << 5); + + if ((b & 1 << 3) != 0) + a = (byte) (a | 1 << 3); + + watch.setValue(a); } + } + } + else if (event.getPacketType() == Server.NAMED_ENTITY_SPAWN) + { + event.setCancelled(true); - if (event.getPacketType() == Server.ENTITY_METADATA) - { - event.setPacket(event.getPacket().deepClone()); + PacketContainer packet = new PacketContainer(Server.ENTITY_METADATA); - for (WrappedWatchableObject watch : event.getPacket().getWatchableCollectionModifier().read(0)) - { - if (watch.getIndex() == 0) - { - byte b = (byte) watch.getValue(); + StructureModifier mods = packet.getModifier(); - byte a = (byte) (b | 1 << 5); + mods.write(0, observer.getEntityId()); - if ((b & 1 << 3) != 0) - a = (byte) (a | 1 << 3); + List watchableList = new ArrayList<>(); + Byte b = 1 << 5; - watch.setValue(a); - } - } - } - else if (event.getPacketType() == Server.NAMED_ENTITY_SPAWN) - { - event.setCancelled(true); + if (observer.isSprinting()) + b = (byte) (b | 1 << 3); - PacketContainer packet = new PacketContainer(Server.ENTITY_METADATA); + WrappedWatchableObject watch = new WrappedWatchableObject(ReflectionManager.createDataWatcherItem(0, b)); - StructureModifier mods = packet.getModifier(); + watchableList.add(watch); + packet.getWatchableCollectionModifier().write(0, watchableList); - mods.write(0, observer.getEntityId()); + try + { + ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet); + } + catch (InvocationTargetException e) + { + e.printStackTrace(); + } + } + else if (event.getPacketType() == Server.ANIMATION) + { + if (event.getPacket().getIntegers().read(1) != 2) + { + event.setCancelled(true); + } + } + else if (event.getPacketType() == Server.ATTACH_ENTITY || event.getPacketType() == Server.REL_ENTITY_MOVE + || event.getPacketType() == Server.REL_ENTITY_MOVE_LOOK || event.getPacketType() == Server.ENTITY_LOOK + || event.getPacketType() == Server.ENTITY_TELEPORT || event.getPacketType() == Server.ENTITY_HEAD_ROTATION + || event.getPacketType() == Server.ENTITY_EFFECT || event.getPacketType() == Server.ENTITY_EQUIPMENT) + { + event.setCancelled(true); + } + else if (event.getPacketType() == Server.ENTITY_STATUS) + { + Disguise disguise = DisguiseAPI.getDisguise(event.getPlayer(), event.getPlayer()); - List watchableList = new ArrayList<>(); - Byte b = 1 << 5; - - if (observer.isSprinting()) - b = (byte) (b | 1 << 3); - - WrappedWatchableObject watch = new WrappedWatchableObject(ReflectionManager.createDataWatcherItem(0, b)); - - watchableList.add(watch); - packet.getWatchableCollectionModifier().write(0, watchableList); - - try - { - ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet); - } - catch (InvocationTargetException e) - { - e.printStackTrace(); - } - } - else if (event.getPacketType() == Server.ANIMATION) - { - if (event.getPacket().getIntegers().read(1) != 2) - { - event.setCancelled(true); - } - } - else if (event.getPacketType() == Server.ATTACH_ENTITY || event.getPacketType() == Server.REL_ENTITY_MOVE - || event.getPacketType() == Server.REL_ENTITY_MOVE_LOOK || event.getPacketType() == Server.ENTITY_LOOK - || event.getPacketType() == Server.ENTITY_TELEPORT || event.getPacketType() == Server.ENTITY_HEAD_ROTATION - || event.getPacketType() == Server.ENTITY_EFFECT || event.getPacketType() == Server.ENTITY_EQUIPMENT) - { - event.setCancelled(true); - } - else if (event.getPacketType() == Server.ENTITY_STATUS) - { - Disguise disguise = DisguiseAPI.getDisguise(event.getPlayer(), event.getPlayer()); - - if (disguise.isSelfDisguiseSoundsReplaced() && !disguise.getType().isPlayer() - && event.getPacket().getBytes().read(0) == 2) - { - event.setCancelled(true); - } - } + if (disguise.isSelfDisguiseSoundsReplaced() && !disguise.getType().isPlayer() + && event.getPacket().getBytes().read(0) == 2) + { + event.setCancelled(true); } } }