package net.md_5.bungee.tablist;

import java.util.HashSet;
import java.util.Set;
import lombok.Synchronized;
import net.md_5.bungee.BungeeCord;
import net.md_5.bungee.UserConnection;
import net.md_5.bungee.api.TabListHandler;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.packet.PacketC9PlayerListItem;

public class GlobalTabList implements TabListHandler
{

    private final Set<ProxiedPlayer> sentPings = new HashSet<>();

    @Override
    public void onConnect(ProxiedPlayer player)
    {
        for (UserConnection c : BungeeCord.getInstance().connections.values())
        {
            c.packetQueue.add(new PacketC9PlayerListItem(c.displayName, true, c.getPing()));
        }
    }

    @Override
    @Synchronized(value = "sentPings")
    public void onPingChange(ProxiedPlayer player, int ping)
    {
        if (!sentPings.contains(player))
        {
            BungeeCord.getInstance().broadcast(new PacketC9PlayerListItem(player.getDisplayName(), true, player.getPing()));
            sentPings.add(player);
        }
    }

    @Override
    @Synchronized(value = "sentPings")
    public void onDisconnect(ProxiedPlayer player)
    {
        BungeeCord.getInstance().broadcast(new PacketC9PlayerListItem(player.getDisplayName(), false, 9999));
        sentPings.remove(player);
    }

    @Override
    public void onServerChange(ProxiedPlayer player)
    {
    }

    @Override
    public boolean onListUpdate(ProxiedPlayer player, String name, boolean online, int ping)
    {
        return false;
    }
}

@@ -703,32 +703,153 @@
 ()));%0A        %7D%0A
+        BungeeCord.getInstance().broadcast(new PacketC9PlayerListItem(player.getDisplayName(), true, player.getPing()));%0A
     %7D%0A%0A    @Over

Update everyones player list when a new player joins (GLOBAL tab list)