Skip to content

Commit

Permalink
fix(bridge/qb/client): make functions strictly backwards compatible
Browse files Browse the repository at this point in the history
* fix(bridge/qb/client): make functions strictly backwards compatible

* refactor(bridge/qb/client): simplify `GetPlayers`

Co-authored-by: Manason <clanerp@gmail.com>

---------

Co-authored-by: Manason <clanerp@gmail.com>
  • Loading branch information
D4isDAVID and Manason committed Feb 2, 2024
1 parent 5829db5 commit 93053da
Showing 1 changed file with 23 additions and 29 deletions.
52 changes: 23 additions & 29 deletions bridge/qb/client/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -143,33 +143,17 @@ local function getEntities(pool, ignoreList) -- luacheck: ignore
end

---@deprecated use the GetGamePool('CVehicle') native directly
functions.GetVehicles = function(ignoreList)
return getEntities('CVehicle', ignoreList)
functions.GetVehicles = function()
return GetGamePool('CVehicle')
end

---@deprecated use the GetGamePool('CObject') native directly
functions.GetObjects = function(ignoreList)
return getEntities('CObject', ignoreList)
functions.GetObjects = function()
return GetGamePool('CObject')
end

---@deprecated use the GetActivePlayers native directly
functions.GetPlayers = function(ignoreList)
ignoreList = ignoreList or {}
local plys = GetActivePlayers()
local players = {}
local ignoreMap = {}
for i = 1, #ignoreList do
ignoreMap[ignoreList[i]] = true
end

for i = 1, #plys do
local player = plys[i]
if not ignoreMap[player] then
players[#players + 1] = player
end
end
return players
end
functions.GetPlayers = GetActivePlayers

---@deprecated use the GetGamePool('CPed') native directly
functions.GetPeds = function(ignoreList)
Expand Down Expand Up @@ -206,11 +190,21 @@ end
functions.IsWearingGloves = qbx.isWearingGloves

---@deprecated use lib.getClosestPlayer from ox_lib
functions.GetClosestPlayer = function(coords, maxDistance) -- luacheck: ignore
functions.GetClosestPlayer = function(coords) -- luacheck: ignore
coords = type(coords) == 'table' and vec3(coords.x, coords.y, coords.z) or coords or GetEntityCoords(cache.ped)
local playerId, _, playerCoords = lib.getClosestPlayer(coords, maxDistance or 50, false)
local closestDistance = playerCoords and #(playerCoords - coords) or nil
return playerId, closestDistance
local players = GetActivePlayers()
local closestDistance = -1
local closestPlayer = -1
for i = 1, #players do
local player = players[i]
local playerCoords = GetEntityCoords(GetPlayerPed(player))
local distance = #(playerCoords - coords)
if closestDistance == -1 or closestDistance > distance then
closestPlayer = player
closestDistance = distance
end
end
return closestPlayer, closestDistance
end

---@deprecated use lib.getNearbyPlayers from ox_lib
Expand All @@ -227,13 +221,13 @@ functions.GetPlayersFromCoords = function(coords, radius)
end

---@deprecated use lib.getClosestVehicle from ox_lib
functions.GetClosestVehicle = function(coords, ignoreList)
return getClosestEntity(getEntities('CVehicle', ignoreList), coords)
functions.GetClosestVehicle = function(coords)
return getClosestEntity(GetGamePool('CVehicle'), coords)
end

---@deprecated use lib.getClosestObject from ox_lib
functions.GetClosestObject = function(coords, ignoreList)
return getClosestEntity(getEntities('CObject', ignoreList), coords)
functions.GetClosestObject = function(coords)
return getClosestEntity(GetGamePool('CObject'), coords)
end

---@deprecated use the GetWorldPositionOfEntityBone native and calculate distance directly
Expand Down

0 comments on commit 93053da

Please sign in to comment.