Fix equip packets, again

This commit is contained in:
libraryaddict 2020-02-07 16:30:52 +13:00
parent 1b2085c03f
commit 45996842d9
No known key found for this signature in database
GPG Key ID: 052E4FBCD257AEA4
3 changed files with 79 additions and 21 deletions

View File

@ -5,7 +5,7 @@
<!-- A good example on why temporary names for project identification shouldn't be used -->
<groupId>LibsDisguises</groupId>
<artifactId>LibsDisguises</artifactId>
<version>9.9.3</version>
<version>9.9.3-SNAPSHOT</version>
<build>
<defaultGoal>clean install</defaultGoal>

View File

@ -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,10 +93,35 @@ 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 {
for (PacketContainer packet : entry.getValue()) {
ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet, false);
@ -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<Object> mods = packet.getModifier();
mods.write(0, disguise.getEntity().getEntityId());
mods.write(1, ReflectionManager.createEnumItemSlot(slot));
mods.write(2, ReflectionManager.getNmsItem(itemToSend));
return packet;
}
}

View File

@ -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 (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) {
// If we need to send the natural armor if possible
if (delayedArmor) {
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,13 +442,8 @@ 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);
}
}
}
}
}