package net.md_5.bungee.tab;

import java.util.Collection;
import java.util.HashSet;
import net.md_5.bungee.api.tab.TabListAdapter;
import net.md_5.bungee.protocol.packet.PacketC9PlayerListItem;

public class ServerUnique extends TabListAdapter
{

    private final Collection<String> usernames = new HashSet<>();

    @Override
    public void onServerChange()
    {
        synchronized ( usernames )
        {
            for ( String username : usernames )
            {
                this.getPlayer().unsafe().sendPacket( new PacketC9PlayerListItem( username, false, (short) 9999 ) );
            }
            usernames.clear();
        }
    }

    @Override
    public boolean onListUpdate(String name, boolean online, int ping)
    {
        if ( online )
        {
            usernames.add( name );
        } else
        {
            usernames.remove( name );
        }

        return true;
    }
}
