From 15fd2c3d462ed457d308f85560353e7998b68259 Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Thu, 27 May 2021 13:16:20 +1200 Subject: [PATCH] Don't remove items from creative players in self disguises --- .../disguise/DisguiseConfig.java | 4 + .../PacketListenerInventory.java | 318 +++++++++--------- src/main/resources/configs/protocol.yml | 3 +- 3 files changed, 163 insertions(+), 162 deletions(-) diff --git a/src/main/java/me/libraryaddict/disguise/DisguiseConfig.java b/src/main/java/me/libraryaddict/disguise/DisguiseConfig.java index a46455f7..7fce1f7e 100644 --- a/src/main/java/me/libraryaddict/disguise/DisguiseConfig.java +++ b/src/main/java/me/libraryaddict/disguise/DisguiseConfig.java @@ -85,6 +85,9 @@ public class DisguiseConfig { private static boolean hidingArmorFromSelf; @Getter @Setter + private static boolean hidingCreativeEquipmentFromSelf; + @Getter + @Setter private static boolean hidingHeldItemFromSelf; @Getter @Setter @@ -579,6 +582,7 @@ public class DisguiseConfig { setEquipmentPacketsEnabled(config.getBoolean("PacketsEnabled.Equipment")); setExplicitDisguisePermissions(config.getBoolean("Permissions.ExplicitDisguises")); setHideArmorFromSelf(config.getBoolean("RemoveArmor")); + setHidingCreativeEquipmentFromSelf(config.getBoolean("RemoveCreativeEquipment")); setHideDisguisedPlayers(config.getBoolean("HideDisguisedPlayersFromTab")); setHideHeldItemFromSelf(config.getBoolean("RemoveHeldItem")); setHorseSaddleable(config.getBoolean("SaddleableHorse")); diff --git a/src/main/java/me/libraryaddict/disguise/utilities/packets/packetlisteners/PacketListenerInventory.java b/src/main/java/me/libraryaddict/disguise/utilities/packets/packetlisteners/PacketListenerInventory.java index 54f4febe..a3b9c3cc 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/packets/packetlisteners/PacketListenerInventory.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/packets/packetlisteners/PacketListenerInventory.java @@ -9,11 +9,13 @@ import com.comphenix.protocol.events.PacketContainer; import com.comphenix.protocol.events.PacketEvent; import com.comphenix.protocol.reflect.StructureModifier; import me.libraryaddict.disguise.DisguiseAPI; +import me.libraryaddict.disguise.DisguiseConfig; import me.libraryaddict.disguise.LibsDisguises; import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.reflection.ReflectionManager; import org.bukkit.Bukkit; +import org.bukkit.GameMode; import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; @@ -27,17 +29,17 @@ public class PacketListenerInventory extends PacketAdapter { private LibsDisguises libsDisguises; public PacketListenerInventory(LibsDisguises plugin) { - super(plugin, ListenerPriority.HIGH, Server.SET_SLOT, Server.WINDOW_ITEMS, - PacketType.Play.Client.HELD_ITEM_SLOT, PacketType.Play.Client.SET_CREATIVE_SLOT, - PacketType.Play.Client.WINDOW_CLICK); + super(plugin, ListenerPriority.HIGH, Server.SET_SLOT, Server.WINDOW_ITEMS, PacketType.Play.Client.HELD_ITEM_SLOT, + PacketType.Play.Client.SET_CREATIVE_SLOT, PacketType.Play.Client.WINDOW_CLICK); libsDisguises = plugin; } @Override public void onPacketReceiving(final PacketEvent event) { - if (event.isCancelled() || event.isPlayerTemporary()) + if (event.isCancelled() || event.isPlayerTemporary()) { return; + } final Player player = event.getPlayer(); @@ -55,191 +57,187 @@ public class PacketListenerInventory extends PacketAdapter { return; } + if (!DisguiseConfig.isHidingCreativeEquipmentFromSelf() && player.getGameMode() == GameMode.CREATIVE) { + return; + } + Disguise disguise = DisguiseAPI.getDisguise(player, player); + // If player isn't disguise, isn't self disguised, or isn't hiding items from themselves // If player is disguised, views self disguises and has a inventory modifier - if (disguise != null && DisguiseUtilities.getSelfDisguised().contains(player.getUniqueId()) && - (disguise.isHidingArmorFromSelf() || disguise.isHidingHeldItemFromSelf())) { - // 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 (disguise == null || !DisguiseUtilities.getSelfDisguised().contains(player.getUniqueId()) || + (!disguise.isHidingArmorFromSelf() && !disguise.isHidingHeldItemFromSelf())) { + return; + } - if (slot >= 5 && slot <= 8) { - if (disguise.isHidingArmorFromSelf()) { - int armorSlot = Math.abs((slot - 5) - 3); + // 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); - org.bukkit.inventory.ItemStack item = player.getInventory().getArmorContents()[armorSlot]; + if (slot >= 5 && slot <= 8) { + if (disguise.isHidingArmorFromSelf()) { + int armorSlot = Math.abs((slot - 5) - 3); - if (item != null && item.getType() != Material.AIR && item.getType() != Material.ELYTRA) { - PacketContainer packet = new PacketContainer(Server.SET_SLOT); + org.bukkit.inventory.ItemStack item = player.getInventory().getArmorContents()[armorSlot]; - StructureModifier mods = packet.getModifier(); - - mods.write(0, 0); - mods.write(1, slot); - mods.write(2, - ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(Material.AIR))); - - try { - ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet, false); - } - catch (InvocationTargetException e) { - e.printStackTrace(); - } - } - } - } else if (slot >= 36 && slot <= 45) { - if (disguise.isHidingHeldItemFromSelf()) { - int currentSlot = player.getInventory().getHeldItemSlot(); - - if (slot + 36 == currentSlot || slot == 45) { - org.bukkit.inventory.ItemStack item = player.getInventory().getItemInMainHand(); - - 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(Material.AIR))); - - try { - ProtocolLibrary.getProtocolManager().sendServerPacket(player, 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 = player.getItemInHand(); - // If his old weapon isn't air - if (currentlyHeld != null && currentlyHeld.getType() != Material.AIR) { + if (item != null && item.getType() != Material.AIR && item.getType() != Material.ELYTRA) { PacketContainer packet = new PacketContainer(Server.SET_SLOT); StructureModifier mods = packet.getModifier(); mods.write(0, 0); - mods.write(1, player.getInventory().getHeldItemSlot() + 36); - mods.write(2, ReflectionManager.getNmsItem(currentlyHeld)); - - try { - ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet, false); - } - catch (InvocationTargetException e) { - e.printStackTrace(); - } - } - - org.bukkit.inventory.ItemStack newHeld = player.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(Material.AIR))); try { ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet, false); - } - catch (InvocationTargetException e) { + } catch (InvocationTargetException e) { e.printStackTrace(); } } } - } else if (event.getPacketType() == PacketType.Play.Client.WINDOW_CLICK) { - int slot = event.getPacket().getIntegers().read(1); + } else if (slot >= 36 && slot <= 45) { + if (disguise.isHidingHeldItemFromSelf()) { + int currentSlot = player.getInventory().getHeldItemSlot(); - org.bukkit.inventory.ItemStack clickedItem; + if (slot + 36 == currentSlot || slot == 45) { + org.bukkit.inventory.ItemStack item = player.getInventory().getItemInMainHand(); - 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() { - player.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 = player.getItemOnCursor(); - } - - if (clickedItem != null && clickedItem.getType() != Material.AIR && - clickedItem.getType() != Material.ELYTRA) { - // 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(Material.AIR))); + mods.write(2, ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(Material.AIR))); try { ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet, false); - } - catch (InvocationTargetException e) { + } catch (InvocationTargetException e) { e.printStackTrace(); } } - // Else if its a hotbar slot - } else if (slot >= 36 && slot <= 45) { - if (disguise.isHidingHeldItemFromSelf()) { - int currentSlot = player.getInventory().getHeldItemSlot(); - - // Check if the player is on the same slot as the slot that its setting - if (slot == currentSlot + 36 || slot == 45) { - 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(Material.AIR))); - - try { - ProtocolLibrary.getProtocolManager().sendServerPacket(player, 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 = player.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, player.getInventory().getHeldItemSlot() + 36); + mods.write(2, ReflectionManager.getNmsItem(currentlyHeld)); + + try { + ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet, false); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + } + + org.bukkit.inventory.ItemStack newHeld = player.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(Material.AIR))); + + try { + ProtocolLibrary.getProtocolManager().sendServerPacket(player, 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() { + player.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 = player.getItemOnCursor(); + } + + if (clickedItem != null && clickedItem.getType() != Material.AIR && clickedItem.getType() != Material.ELYTRA) { + // If the slot is a armor slot + if (slot >= 5 && slot <= 8) { + if (disguise.isHidingArmorFromSelf()) { + 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(Material.AIR))); + + try { + ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet, false); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + } + // Else if its a hotbar slot + } else if (slot >= 36 && slot <= 45) { + if (disguise.isHidingHeldItemFromSelf()) { + int currentSlot = player.getInventory().getHeldItemSlot(); + + // Check if the player is on the same slot as the slot that its setting + if (slot == currentSlot + 36 || slot == 45) { + 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(Material.AIR))); + + try { + ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet, false); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + } + } + } + } + } + } @Override @@ -247,8 +245,11 @@ public class PacketListenerInventory extends PacketAdapter { Player player = event.getPlayer(); // If the inventory is the players inventory - if (event.isPlayerTemporary() || player.getVehicle() != null || - event.getPacket().getIntegers().read(0) != 0) { + if (event.isPlayerTemporary() || player.getVehicle() != null || event.getPacket().getIntegers().read(0) != 0) { + return; + } + + if (!DisguiseConfig.isHidingCreativeEquipmentFromSelf() && player.getGameMode() == GameMode.CREATIVE) { return; } @@ -264,9 +265,6 @@ public class PacketListenerInventory extends PacketAdapter { // 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. @@ -283,8 +281,7 @@ public class PacketListenerInventory extends PacketAdapter { if (item != null && item.getType() != Material.AIR && item.getType() != Material.ELYTRA) { event.setPacket(event.getPacket().shallowClone()); - event.getPacket().getModifier().write(2, - ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(Material.AIR))); + event.getPacket().getModifier().write(2, ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(Material.AIR))); } } // Else if its a hotbar slot @@ -298,8 +295,7 @@ public class PacketListenerInventory extends PacketAdapter { if (item != null && item.getType() != Material.AIR) { event.setPacket(event.getPacket().shallowClone()); - event.getPacket().getModifier().write(2, - ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(Material.AIR))); + event.getPacket().getModifier().write(2, ReflectionManager.getNmsItem(new org.bukkit.inventory.ItemStack(Material.AIR))); } } } diff --git a/src/main/resources/configs/protocol.yml b/src/main/resources/configs/protocol.yml index 556ebbbc..1da408a7 100644 --- a/src/main/resources/configs/protocol.yml +++ b/src/main/resources/configs/protocol.yml @@ -12,9 +12,10 @@ RandomDisguiseOptions: true # For self disguises, they need to have the armor and the held item removed # Else they see floating armor, floating held items. # This turns the items invisible in the disguised players inventory. It does not actually remove them! -# Be warned that in creative this can actually delete the item from the inventory due to a weird bug RemoveArmor: true RemoveHeldItem: false +# An exception for creative mode as this can actually delete the item from the inventory due to creative being weird +RemoveCreativeEquipment: false # If you set a disguise to burning, it will no longer be able to be shown as sneaking or invisible. # Set this to true if you want the disguise to get the animations of the disguised entity. Such as invisible, on fire, sprinting, sneaking, blocking