Remove meta cancel only once and when it's the last delayed packet

This commit is contained in:
libraryaddict 2019-02-18 02:02:10 +13:00
parent 6d64f44b15
commit b813c5b899

View File

@ -9,10 +9,7 @@ import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.*;
/**
* Created by libraryaddict on 3/01/2019.
@ -72,9 +69,19 @@ public class LibsPackets {
}
public void sendDelayed(final Player observer) {
for (final Map.Entry<Integer, ArrayList<PacketContainer>> entry : delayedPackets.entrySet()) {
Bukkit.getScheduler().scheduleSyncDelayedTask(LibsDisguises.getInstance(), new Runnable() {
public void run() {
Iterator<Map.Entry<Integer, ArrayList<PacketContainer>>> itel = delayedPackets.entrySet().iterator();
Optional<Integer> largestTick = delayedPackets.keySet().stream().max(Integer::compare);
if (!largestTick.isPresent()) {
return;
}
while (itel.hasNext()) {
Map.Entry<Integer, ArrayList<PacketContainer>> entry = itel.next();
// If this is the last delayed packet
final boolean isRemoveCancel = isSpawnPacket && largestTick.get().equals(entry.getKey());
Bukkit.getScheduler().scheduleSyncDelayedTask(LibsDisguises.getInstance(), () -> {
try {
for (PacketContainer packet : entry.getValue()) {
ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet, false);
@ -84,10 +91,9 @@ public class LibsPackets {
e.printStackTrace();
}
if (isSpawnPacket) {
if (isRemoveCancel) {
PacketsManager.getPacketsHandler().removeCancel(disguise, observer);
}
}
}, entry.getKey());
}
}