package eu.mhsl.minenet.minigames.util;

import net.minestom.server.MinecraftServer;
import net.minestom.server.entity.GameMode;
import net.minestom.server.entity.Player;
import net.minestom.server.entity.PlayerSkin;
import net.minestom.server.network.packet.server.play.PlayerInfoUpdatePacket;

import java.util.List;

public class PacketUtil {
    public static void resendPlayerList(Player p) {
        MinecraftServer.getConnectionManager().getOnlinePlayers().forEach(player -> {
            if(player.getUuid().equals(p.getUuid())) return;

            final PlayerSkin skin = player.getSkin();
            List<PlayerInfoUpdatePacket.Property> properties =
                    skin != null ? List.of(new PlayerInfoUpdatePacket.Property("textures", skin.textures(), skin.signature())) : List.of();

            p.sendPacket(
                    new PlayerInfoUpdatePacket(PlayerInfoUpdatePacket.Action.ADD_PLAYER,
                            new PlayerInfoUpdatePacket.Entry(
                                    player.getUuid(),
                                    player.getUsername(),
                                    properties,
                                    true,
                                    0,
                                    GameMode.SURVIVAL,
                                    null,
                                    null
                            )
                    )
            );
//            p.sendPacket( //TODO spawnPlayerPacket does no longer exists
//                    new SpawnPlayerPacket(player.getEntityId(), player.getUuid(), player.getPosition())
//            );
        });
    }
}
