2019-01-03 03:13:03 +01:00
|
|
|
package me.libraryaddict.disguise.utilities.packets;
|
|
|
|
|
|
|
|
import com.comphenix.protocol.PacketType;
|
|
|
|
import com.comphenix.protocol.ProtocolLibrary;
|
|
|
|
import com.comphenix.protocol.events.PacketContainer;
|
2020-02-07 04:30:52 +01:00
|
|
|
import com.comphenix.protocol.reflect.StructureModifier;
|
|
|
|
import lombok.Getter;
|
|
|
|
import lombok.Setter;
|
2019-01-03 03:13:03 +01:00
|
|
|
import me.libraryaddict.disguise.LibsDisguises;
|
|
|
|
import me.libraryaddict.disguise.disguisetypes.Disguise;
|
2020-02-07 04:30:52 +01:00
|
|
|
import me.libraryaddict.disguise.utilities.reflection.ReflectionManager;
|
2019-01-03 03:13:03 +01:00
|
|
|
import org.bukkit.Bukkit;
|
2020-02-07 04:30:52 +01:00
|
|
|
import org.bukkit.Material;
|
2019-01-03 03:13:03 +01:00
|
|
|
import org.bukkit.entity.Player;
|
2020-02-07 04:30:52 +01:00
|
|
|
import org.bukkit.inventory.EquipmentSlot;
|
|
|
|
import org.bukkit.inventory.ItemStack;
|
2019-01-03 03:13:03 +01:00
|
|
|
|
|
|
|
import java.lang.reflect.InvocationTargetException;
|
2019-02-17 14:02:10 +01:00
|
|
|
import java.util.*;
|
2019-01-03 03:13:03 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by libraryaddict on 3/01/2019.
|
|
|
|
*/
|
|
|
|
public class LibsPackets {
|
|
|
|
private ArrayList<PacketContainer> packets = new ArrayList<>();
|
|
|
|
private HashMap<Integer, ArrayList<PacketContainer>> delayedPackets = new HashMap<>();
|
|
|
|
private boolean isSpawnPacket;
|
|
|
|
private Disguise disguise;
|
|
|
|
private boolean doNothing;
|
2019-12-21 00:02:30 +01:00
|
|
|
private int removeMetaAt = -1;
|
2020-02-07 04:30:52 +01:00
|
|
|
@Getter
|
|
|
|
@Setter
|
|
|
|
private boolean sendArmor;
|
2019-01-03 03:13:03 +01:00
|
|
|
|
|
|
|
public LibsPackets(Disguise disguise) {
|
|
|
|
this.disguise = disguise;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setUnhandled() {
|
|
|
|
doNothing = true;
|
|
|
|
}
|
|
|
|
|
2019-12-21 00:02:30 +01:00
|
|
|
public void setRemoveMetaAt(int tick) {
|
|
|
|
removeMetaAt = tick;
|
|
|
|
}
|
|
|
|
|
2019-01-03 03:13:03 +01:00
|
|
|
public boolean isUnhandled() {
|
|
|
|
return doNothing;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Disguise getDisguise() {
|
|
|
|
return disguise;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setSpawnPacketCheck(PacketType type) {
|
|
|
|
isSpawnPacket = type.name().contains("SPAWN") && type.name().contains("ENTITY");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void addPacket(PacketContainer packet) {
|
|
|
|
packets.add(packet);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void addDelayedPacket(PacketContainer packet) {
|
|
|
|
addDelayedPacket(packet, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void clear() {
|
|
|
|
getPackets().clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void addDelayedPacket(PacketContainer packet, int ticksDelayed) {
|
|
|
|
if (!delayedPackets.containsKey(ticksDelayed))
|
|
|
|
delayedPackets.put(ticksDelayed, new ArrayList<>());
|
|
|
|
|
|
|
|
delayedPackets.get(ticksDelayed).add(packet);
|
|
|
|
}
|
|
|
|
|
|
|
|
public ArrayList<PacketContainer> getPackets() {
|
|
|
|
return packets;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Collection<ArrayList<PacketContainer>> getDelayedPackets() {
|
|
|
|
return delayedPackets.values();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void sendDelayed(final Player observer) {
|
2019-02-17 14:02:10 +01:00
|
|
|
Iterator<Map.Entry<Integer, ArrayList<PacketContainer>>> itel = delayedPackets.entrySet().iterator();
|
2019-01-03 03:13:03 +01:00
|
|
|
|
2019-02-17 14:02:10 +01:00
|
|
|
while (itel.hasNext()) {
|
|
|
|
Map.Entry<Integer, ArrayList<PacketContainer>> entry = itel.next();
|
|
|
|
// If this is the last delayed packet
|
2019-12-21 00:02:30 +01:00
|
|
|
final boolean isRemoveCancel = isSpawnPacket && entry.getKey() >= removeMetaAt && removeMetaAt >= 0;
|
2019-02-17 14:02:10 +01:00
|
|
|
|
|
|
|
Bukkit.getScheduler().scheduleSyncDelayedTask(LibsDisguises.getInstance(), () -> {
|
2020-02-15 08:55:29 +01:00
|
|
|
|
2020-02-19 04:30:25 +01:00
|
|
|
if (isRemoveCancel && "%%__USER__%%".equals("%%__USER__%%") && !"%%__USER__%%".equals("12345")) {
|
2020-02-15 08:55:29 +01:00
|
|
|
PacketsManager.getPacketsHandler().removeCancel(disguise, observer);
|
|
|
|
}
|
|
|
|
|
2020-02-07 04:30:52 +01:00
|
|
|
if (!disguise.isDisguiseInUse()) {
|
2020-02-15 08:55:29 +01:00
|
|
|
ArrayList<PacketContainer> packets = entry.getValue();
|
|
|
|
|
|
|
|
if (packets.stream().noneMatch(p -> p.getType() == PacketType.Play.Server.PLAYER_INFO)) {
|
|
|
|
return;
|
2020-02-07 04:30:52 +01:00
|
|
|
}
|
|
|
|
|
2020-02-15 08:55:29 +01:00
|
|
|
packets.removeIf(p -> p.getType() != PacketType.Play.Server.PLAYER_INFO);
|
2020-02-07 04:30:52 +01:00
|
|
|
}
|
|
|
|
|
2019-10-13 07:22:39 +02:00
|
|
|
if (isRemoveCancel) {
|
2020-02-07 04:30:52 +01:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-13 07:22:39 +02:00
|
|
|
}
|
|
|
|
|
2019-02-17 14:02:10 +01:00
|
|
|
try {
|
|
|
|
for (PacketContainer packet : entry.getValue()) {
|
|
|
|
ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet, false);
|
2019-01-03 03:13:03 +01:00
|
|
|
}
|
|
|
|
}
|
2019-02-17 14:02:10 +01:00
|
|
|
catch (InvocationTargetException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2019-01-03 03:13:03 +01:00
|
|
|
}, entry.getKey());
|
|
|
|
}
|
|
|
|
}
|
2020-02-07 04:30:52 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2019-01-03 03:13:03 +01:00
|
|
|
}
|