package com.tiedup.remake.util;

import java.util.ArrayList;
import java.util.List;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.AABB;

/**
 * General utility functions for TiedUp mod.
 */
public class TiedUpUtils {

    /**
     * Get all players within a radius.
     *
     * @param level The world
     * @param pos The center position
     * @param distance The search radius
     * @return List of ServerPlayer found
     */
    public static List<ServerPlayer> getPlayersAround(
        Level level,
        BlockPos pos,
        double distance
    ) {
        List<ServerPlayer> players = new ArrayList<>();

        if (level == null || level.isClientSide) {
            return players;
        }

        AABB searchBox = new AABB(
            pos.getX() - distance,
            pos.getY() - distance,
            pos.getZ() - distance,
            pos.getX() + distance,
            pos.getY() + distance,
            pos.getZ() + distance
        );

        List<ServerPlayer> allPlayers = level.getEntitiesOfClass(
            ServerPlayer.class,
            searchBox
        );
        players.addAll(allPlayers);

        return players;
    }
}
