From 45996842d911a38016ca4d8ab12394474f6bebb1 Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Fri, 7 Feb 2020 16:30:52 +1300 Subject: [PATCH] Fix equip packets, again --- pom.xml | 2 +- .../utilities/packets/LibsPackets.java | 64 ++++++++++++++++++- .../packethandlers/PacketHandlerSpawn.java | 34 +++++----- 3 files changed, 79 insertions(+), 21 deletions(-) diff --git a/pom.xml b/pom.xml index d553f2d0..613c8c55 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ LibsDisguises LibsDisguises - 9.9.3 + 9.9.3-SNAPSHOT clean install diff --git a/src/main/java/me/libraryaddict/disguise/utilities/packets/LibsPackets.java b/src/main/java/me/libraryaddict/disguise/utilities/packets/LibsPackets.java index e32e8b12..8795d153 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/packets/LibsPackets.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/packets/LibsPackets.java @@ -3,11 +3,18 @@ package me.libraryaddict.disguise.utilities.packets; import com.comphenix.protocol.PacketType; import com.comphenix.protocol.ProtocolLibrary; import com.comphenix.protocol.events.PacketContainer; +import com.comphenix.protocol.reflect.StructureModifier; +import lombok.Getter; +import lombok.Setter; 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.Material; +import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; +import org.bukkit.inventory.EquipmentSlot; +import org.bukkit.inventory.ItemStack; import java.lang.reflect.InvocationTargetException; import java.util.*; @@ -22,6 +29,9 @@ public class LibsPackets { private Disguise disguise; private boolean doNothing; private int removeMetaAt = -1; + @Getter + @Setter + private boolean sendArmor; public LibsPackets(Disguise disguise) { this.disguise = disguise; @@ -83,8 +93,33 @@ public class LibsPackets { final boolean isRemoveCancel = isSpawnPacket && entry.getKey() >= removeMetaAt && removeMetaAt >= 0; Bukkit.getScheduler().scheduleSyncDelayedTask(LibsDisguises.getInstance(), () -> { + if (!disguise.isDisguiseInUse()) { + if (isRemoveCancel) { + PacketsManager.getPacketsHandler().removeCancel(disguise, observer); + } + + return; + } + if (isRemoveCancel) { PacketsManager.getPacketsHandler().removeCancel(disguise, observer); + + if (isSendArmor()) { + for (EquipmentSlot slot : EquipmentSlot.values()) { + PacketContainer packet = createPacket(slot); + + if (packet == null) { + continue; + } + + try { + ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet, false); + } + catch (InvocationTargetException e) { + e.printStackTrace(); + } + } + } } try { @@ -98,4 +133,31 @@ public class LibsPackets { }, entry.getKey()); } } + + private PacketContainer createPacket(EquipmentSlot slot) { + // Get what the disguise wants to show for its armor + ItemStack itemToSend = disguise.getWatcher().getItemStack(slot); + + // If the disguise armor isn't visible + if (itemToSend == null) { + itemToSend = ReflectionManager.getEquipment(slot, disguise.getEntity()); + + // If natural armor isn't sent either + if (itemToSend == null || itemToSend.getType() == Material.AIR) { + return null; + } + } else if (itemToSend.getType() == Material.AIR) { + return null; + } + + PacketContainer packet = new PacketContainer(PacketType.Play.Server.ENTITY_EQUIPMENT); + + StructureModifier mods = packet.getModifier(); + + mods.write(0, disguise.getEntity().getEntityId()); + mods.write(1, ReflectionManager.createEnumItemSlot(slot)); + mods.write(2, ReflectionManager.getNmsItem(itemToSend)); + + return packet; + } } \ No newline at end of file diff --git a/src/main/java/me/libraryaddict/disguise/utilities/packets/packethandlers/PacketHandlerSpawn.java b/src/main/java/me/libraryaddict/disguise/utilities/packets/packethandlers/PacketHandlerSpawn.java index 019faa1b..3a37041e 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/packets/packethandlers/PacketHandlerSpawn.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/packets/packethandlers/PacketHandlerSpawn.java @@ -404,16 +404,24 @@ public class PacketHandlerSpawn implements IPacketHandler { boolean delayedArmor = DisguiseConfig.isPlayerHideArmor() && (disguise.isPlayerDisguise() && disguisedEntity != observer) && disguisedEntity instanceof LivingEntity; + + if (delayedArmor) { + packets.setSendArmor(true); + packets.setRemoveMetaAt(7); + } // This sends the armor packets so that the player isn't naked. if (DisguiseConfig.isEquipmentPacketsEnabled() || delayedArmor) { for (EquipmentSlot slot : EquipmentSlot.values()) { // Get what the disguise wants to show for its armor - ItemStack itemToSend = disguise.getWatcher().getItemStack(slot); + ItemStack itemToSend; - // If the disguise armor isn't visible - if (itemToSend == null || itemToSend.getType() == Material.AIR) { - // If we need to send the natural armor if possible - if (delayedArmor) { + if (delayedArmor) { + itemToSend = new ItemStack(Material.AIR); + } else { + itemToSend = disguise.getWatcher().getItemStack(slot); + + // If the disguise armor isn't visible + if (itemToSend == null || itemToSend.getType() != Material.AIR) { itemToSend = ReflectionManager.getEquipment(slot, disguisedEntity); // If natural armor isn't sent either @@ -421,14 +429,7 @@ public class PacketHandlerSpawn implements IPacketHandler { continue; } } else { - // We don't need to send natural armor and disguise armor isn't visible - continue; - } - } else if (!delayedArmor && disguisedEntity instanceof LivingEntity) { - ItemStack item = ReflectionManager.getEquipment(slot, disguisedEntity); - - // If the item was going to be sent anyways - if (item != null && item.getType() != Material.AIR) { + // Its air which shouldn't be sent continue; } } @@ -441,12 +442,7 @@ public class PacketHandlerSpawn implements IPacketHandler { mods.write(1, ReflectionManager.createEnumItemSlot(slot)); mods.write(2, ReflectionManager.getNmsItem(itemToSend)); - if (delayedArmor) { - packets.addDelayedPacket(packet, 7); - packets.setRemoveMetaAt(7); - } else { - packets.addDelayedPacket(packet); - } + packets.addDelayedPacket(packet); } } }