mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-18 15:36:02 +01:00
feat(imports): server-side entity iterators (#458)
Co-authored-by: Linden <65407488+thelindat@users.noreply.github.com>
This commit is contained in:
28
imports/getClosestPlayer/server.lua
Normal file
28
imports/getClosestPlayer/server.lua
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
---@param coords vector3 The coords to check from.
|
||||||
|
---@param maxDistance number The max distance to check.
|
||||||
|
---@return number? playerId
|
||||||
|
---@return number? playerPed
|
||||||
|
---@return vector3? playerCoords
|
||||||
|
function lib.getClosestPlayer(coords, maxDistance)
|
||||||
|
local players = GetActivePlayers()
|
||||||
|
local closestId, closestPed, closestCoords
|
||||||
|
maxDistance = maxDistance or 2.0
|
||||||
|
|
||||||
|
for i = 1, #players do
|
||||||
|
local playerId = players[i]
|
||||||
|
local playerPed = GetPlayerPed(playerId)
|
||||||
|
local playerCoords = GetEntityCoords(playerPed)
|
||||||
|
local distance = #(coords - playerCoords)
|
||||||
|
|
||||||
|
if distance < maxDistance then
|
||||||
|
maxDistance = distance
|
||||||
|
closestId = playerId
|
||||||
|
closestPed = playerPed
|
||||||
|
closestCoords = playerCoords
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return closestId, closestPed, closestCoords
|
||||||
|
end
|
||||||
|
|
||||||
|
return lib.getClosestPlayer
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
---@param coords vector3 The coords to check from.
|
---@param coords vector3 The coords to check from.
|
||||||
---@param maxDistance number The max distance to check.
|
---@param maxDistance number The max distance to check.
|
||||||
---@param includePlayerVehicle boolean Whether or not to include the player's current vehicle.
|
---@param includePlayerVehicle boolean Whether or not to include the player's current vehicle. Ignored on the server.
|
||||||
---@return number? vehicle
|
---@return number? vehicle
|
||||||
---@return vector3? vehicleCoords
|
---@return vector3? vehicleCoords
|
||||||
function lib.getClosestVehicle(coords, maxDistance, includePlayerVehicle)
|
function lib.getClosestVehicle(coords, maxDistance, includePlayerVehicle)
|
||||||
@@ -11,7 +11,7 @@ function lib.getClosestVehicle(coords, maxDistance, includePlayerVehicle)
|
|||||||
for i = 1, #vehicles do
|
for i = 1, #vehicles do
|
||||||
local vehicle = vehicles[i]
|
local vehicle = vehicles[i]
|
||||||
|
|
||||||
if not cache.vehicle or vehicle ~= cache.vehicle or includePlayerVehicle then
|
if lib.context == 'server' or not cache.vehicle or vehicle ~= cache.vehicle or includePlayerVehicle then
|
||||||
local vehicleCoords = GetEntityCoords(vehicle)
|
local vehicleCoords = GetEntityCoords(vehicle)
|
||||||
local distance = #(coords - vehicleCoords)
|
local distance = #(coords - vehicleCoords)
|
||||||
|
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
---@param coords vector3 The coords to check from.
|
---@param coords vector3 The coords to check from.
|
||||||
---@param maxDistance number The max distance to check.
|
---@param maxDistance? number The max distance to check.
|
||||||
---@return { object: number, coords: vector3 }[]
|
---@return { object: number, coords: vector3 }[]
|
||||||
function lib.getNearbyObjects(coords, maxDistance)
|
function lib.getNearbyObjects(coords, maxDistance)
|
||||||
local objects = GetGamePool('CObject')
|
local objects = GetGamePool('CObject')
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
---@param coords vector3 The coords to check from.
|
---@param coords vector3 The coords to check from.
|
||||||
---@param maxDistance number The max distance to check.
|
---@param maxDistance? number The max distance to check.
|
||||||
---@return { ped: number, coords: vector3 }[]
|
---@return { ped: number, coords: vector3 }[]
|
||||||
function lib.getNearbyPeds(coords, maxDistance)
|
function lib.getNearbyPeds(coords, maxDistance)
|
||||||
local peds = GetGamePool('CPed')
|
local peds = GetGamePool('CPed')
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
---@param coords vector3 The coords to check from.
|
---@param coords vector3 The coords to check from.
|
||||||
---@param maxDistance number The max distance to check.
|
---@param maxDistance? number The max distance to check.
|
||||||
---@param includePlayer boolean? Whether or not to include the current player.
|
---@param includePlayer? boolean Whether or not to include the current player.
|
||||||
---@return { id: number, ped: number, coords: vector3 }[]
|
---@return { id: number, ped: number, coords: vector3 }[]
|
||||||
function lib.getNearbyPlayers(coords, maxDistance, includePlayer)
|
function lib.getNearbyPlayers(coords, maxDistance, includePlayer)
|
||||||
local players = GetActivePlayers()
|
local players = GetActivePlayers()
|
||||||
|
|||||||
29
imports/getNearbyPlayers/server.lua
Normal file
29
imports/getNearbyPlayers/server.lua
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
---@param coords vector3 The coords to check from.
|
||||||
|
---@param maxDistance? number The max distance to check.
|
||||||
|
---@return { id: number, ped: number, coords: vector3 }[]
|
||||||
|
function lib.getNearbyPlayers(coords, maxDistance)
|
||||||
|
local players = GetActivePlayers()
|
||||||
|
local nearby = {}
|
||||||
|
local count = 0
|
||||||
|
maxDistance = maxDistance or 2.0
|
||||||
|
|
||||||
|
for i = 1, #players do
|
||||||
|
local playerId = players[i]
|
||||||
|
local playerPed = GetPlayerPed(playerId)
|
||||||
|
local playerCoords = GetEntityCoords(playerPed)
|
||||||
|
local distance = #(coords - playerCoords)
|
||||||
|
|
||||||
|
if distance < maxDistance then
|
||||||
|
count += 1
|
||||||
|
nearby[count] = {
|
||||||
|
id = playerId,
|
||||||
|
ped = playerPed,
|
||||||
|
coords = playerCoords,
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return nearby
|
||||||
|
end
|
||||||
|
|
||||||
|
return lib.getNearbyPlayers
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
---@param coords vector3 The coords to check from.
|
---@param coords vector3 The coords to check from.
|
||||||
---@param maxDistance number The max distance to check.
|
---@param maxDistance? number The max distance to check.
|
||||||
---@param includePlayerVehicle boolean Whether or not to include the player's current vehicle.
|
---@param includePlayerVehicle? boolean Whether or not to include the player's current vehicle.
|
||||||
---@return { vehicle: number, coords: vector3 }[]
|
---@return { vehicle: number, coords: vector3 }[]
|
||||||
function lib.getNearbyVehicles(coords, maxDistance, includePlayerVehicle)
|
function lib.getNearbyVehicles(coords, maxDistance, includePlayerVehicle)
|
||||||
local vehicles = GetGamePool('CVehicle')
|
local vehicles = GetGamePool('CVehicle')
|
||||||
@@ -11,7 +11,7 @@ function lib.getNearbyVehicles(coords, maxDistance, includePlayerVehicle)
|
|||||||
for i = 1, #vehicles do
|
for i = 1, #vehicles do
|
||||||
local vehicle = vehicles[i]
|
local vehicle = vehicles[i]
|
||||||
|
|
||||||
if not cache.vehicle or vehicle ~= cache.vehicle or includePlayerVehicle then
|
if lib.context == 'server' or not cache.vehicle or vehicle ~= cache.vehicle or includePlayerVehicle then
|
||||||
local vehicleCoords = GetEntityCoords(vehicle)
|
local vehicleCoords = GetEntityCoords(vehicle)
|
||||||
local distance = #(coords - vehicleCoords)
|
local distance = #(coords - vehicleCoords)
|
||||||
|
|
||||||
27
init.lua
27
init.lua
@@ -222,6 +222,33 @@ else
|
|||||||
function lib.notify(playerId, data)
|
function lib.notify(playerId, data)
|
||||||
TriggerClientEvent(notifyEvent, playerId, data)
|
TriggerClientEvent(notifyEvent, playerId, data)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local poolNatives = {
|
||||||
|
CPed = GetAllPeds,
|
||||||
|
CObject = GetAllObjects,
|
||||||
|
CVehicle = GetAllVehicles,
|
||||||
|
}
|
||||||
|
|
||||||
|
---@param poolName 'CPed' | 'CObject' | 'CVehicle'
|
||||||
|
---@return number[]
|
||||||
|
---Server-side parity for the `GetGamePool` client native.
|
||||||
|
function GetGamePool(poolName)
|
||||||
|
local fn = poolNatives[poolName]
|
||||||
|
return fn and fn() --[[@as number[] ]]
|
||||||
|
end
|
||||||
|
|
||||||
|
---@return number[]
|
||||||
|
---Server-side parity for the `GetPlayers` client native.
|
||||||
|
function GetActivePlayers()
|
||||||
|
local playerNum = GetNumPlayerIndices()
|
||||||
|
local players = table.create(playerNum, 0)
|
||||||
|
|
||||||
|
for i = 1, playerNum do
|
||||||
|
players[i] = tonumber(GetPlayerFromIndex(i - 1))
|
||||||
|
end
|
||||||
|
|
||||||
|
return players
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
for i = 1, GetNumResourceMetadata(cache.resource, 'ox_lib') do
|
for i = 1, GetNumResourceMetadata(cache.resource, 'ox_lib') do
|
||||||
|
|||||||
Reference in New Issue
Block a user