package net.minecraft.server.players;

import net.minecraft.network.protocol.Packet;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.nbt.CompoundTag;

import com.bergerkiller.bukkit.common.nbt.CommonTagCompound;

class PlayerList {
    public final (List<org.bukkit.entity.Player) List<ServerPlayer> players;

    //TODO: Is this actually used for anything at the moment?
#if version >= 1.21.9
    public int getMaxPlayers();
    public void setMaxPlayers(int maxPlayers) {
        // Defer to server
        ((net.minecraft.server.dedicated.DedicatedServer) instance.getServer()).setMaxPlayers(maxPlayers);
    }
#else
    #require PlayerList protected int maxPlayers;

    public int getMaxPlayers() { return instance#maxPlayers; }
    public void setMaxPlayers(int maxPlayers) { instance#maxPlayers = maxPlayers; }
#endif

    public (CommonTagCompound) CompoundTag migratePlayerData((CommonTagCompound) CompoundTag playerProfileData) {
#if version >= 1.20.5
        int dataVersion = net.minecraft.nbt.NbtUtils.getDataVersion(playerProfileData, -1);
  #if exists ca.spottedleaf.dataconverter.minecraft.MCDataConverter
        // Match Paper's PlayerDataStorage loader instead of bypassing its optimized converter.
        // Newer Paper versions also pass the vanilla type to preserve the DFU fallback for
        // versions not yet supported by the optimized converter.

        // Get the current server data version
    #if version >= 1.21.7 || exists ca.spottedleaf.dataconverter.minecraft.util.Version
        int currentVersion = ca.spottedleaf.dataconverter.minecraft.util.Version.getCurrentVersion();
    #elseif version >= 1.21.6
        int currentVersion = net.minecraft.SharedConstants.getCurrentVersion().dataVersion().version();
    #else
        int currentVersion = net.minecraft.SharedConstants.getCurrentVersion().getDataVersion().getVersion();
    #endif

    #if exists ca.spottedleaf.dataconverter.minecraft.MCDataConverter public static net.minecraft.nbt.CompoundTag convertTag(ca.spottedleaf.dataconverter.minecraft.datatypes.MCDataType type, net.minecraft.util.datafix.DataFixTypes vanillaType, net.minecraft.nbt.CompoundTag data, int fromVersion, int toVersion);
        return ca.spottedleaf.dataconverter.minecraft.MCDataConverter.convertTag(
                ca.spottedleaf.dataconverter.minecraft.datatypes.MCTypeRegistry.PLAYER,
                net.minecraft.util.datafix.DataFixTypes.PLAYER,
                playerProfileData, dataVersion, currentVersion);
    #else
        return ca.spottedleaf.dataconverter.minecraft.MCDataConverter.convertTag(
                ca.spottedleaf.dataconverter.minecraft.datatypes.MCTypeRegistry.PLAYER,
                playerProfileData, dataVersion, currentVersion);
    #endif
  #else
        #require net.minecraft.world.level.storage.PlayerDataStorage protected final com.mojang.datafixers.DataFixer fixerUpper;
        net.minecraft.world.level.storage.PlayerDataStorage storage = instance.playerIo;
        com.mojang.datafixers.DataFixer fixerUpper = storage#fixerUpper;

        return net.minecraft.util.datafix.DataFixTypes.PLAYER.updateToCurrentVersion(fixerUpper, playerProfileData, dataVersion);
  #endif
#else
        return playerProfileData;
#endif
    }

#if version >= 1.18
    public void savePlayers:saveAll();
    protected void savePlayerFile:save((org.bukkit.entity.Player) ServerPlayer entityplayer);
#else
    public void savePlayers();
    protected void savePlayerFile((org.bukkit.entity.Player) ServerPlayer entityplayer);
#endif

#if version >= 1.18
    public void sendRawPacketNearby((org.bukkit.World) ServerLevel world, double x, double y, double z, double radius, (Object) Packet<?> packet) {
        instance.broadcast((net.minecraft.world.entity.player.Player) null, x, y, z, radius, world.dimension(), packet);
    }
#elseif version >= 1.16
    public void sendRawPacketNearby((org.bukkit.World) ServerLevel world, double x, double y, double z, double radius, (Object) Packet<?> packet) {
        instance.sendPacketNearby((net.minecraft.world.entity.player.Player) null, x, y, z, radius, world.getDimensionKey(), packet);
    }
#elseif version >= 1.14
    public void sendRawPacketNearby((org.bukkit.World) ServerLevel world, double x, double y, double z, double radius, (Object) Packet<?> packet) {
        instance.sendPacketNearby((net.minecraft.world.entity.player.Player) null, x, y, z, radius, world.worldProvider.getDimensionManager().getType(), packet);
    }
#elseif version >= 1.9
    public void sendRawPacketNearby((org.bukkit.World) ServerLevel world, double x, double y, double z, double radius, (Object) Packet<?> packet) {
        instance.sendPacketNearby((net.minecraft.world.entity.player.Player) null, x, y, z, radius, world.dimension, packet);
    }
#else
    public void sendRawPacketNearby((org.bukkit.World) ServerLevel world, double x, double y, double z, double radius, (Object) Packet packet) {
        instance.sendPacketNearby((net.minecraft.world.entity.player.Player) null, x, y, z, radius, world.dimension, packet);
    }
#endif
}
