From b1ca2926a13121b5b45bdc465b2eb199d60108d3 Mon Sep 17 00:00:00 2001 From: Mojito-Fivem Date: Thu, 7 Oct 2021 22:06:49 +0100 Subject: [PATCH 1/5] fix(client/functions): GetClosestX type checking --- client/functions.lua | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/client/functions.lua b/client/functions.lua index 4f30682..dfcf893 100644 --- a/client/functions.lua +++ b/client/functions.lua @@ -140,7 +140,11 @@ end function QBCore.Functions.GetClosestPed(coords, ignoreList) local ped = PlayerPedId() - local coords = coords or GetEntityCoords(ped) + if coords then + if type(coords) == 'table' then coords = vector3(coords.x, coords.y, coords.z) end + else + coords = GetEntityCoords(ped) + end local ignoreList = ignoreList or {} local peds = QBCore.Functions.GetPeds(ignoreList) local closestDistance = -1 @@ -159,7 +163,11 @@ end function QBCore.Functions.GetClosestPlayer(coords) local ped = PlayerPedId() - local coords = coords or GetEntityCoords(ped) + if coords then + if type(coords) == 'table' then coords = vector3(coords.x, coords.y, coords.z) end + else + coords = GetEntityCoords(ped) + end local closestPlayers = QBCore.Functions.GetPlayersFromCoords(coords) local closestDistance = -1 local closestPlayer = -1 @@ -179,7 +187,12 @@ end function QBCore.Functions.GetPlayersFromCoords(coords, distance) local players = QBCore.Functions.GetPlayers() - local coords = coords or GetEntityCoords(PlayerPedId()) + local ped = PlayerPedId() + if coords then + if type(coords) == 'table' then coords = vector3(coords.x, coords.y, coords.z) end + else + coords = GetEntityCoords(ped) + end local distance = distance or 5 local closePlayers = {} for _, player in pairs(players) do @@ -198,7 +211,11 @@ function QBCore.Functions.GetClosestVehicle(coords) local vehicles = GetGamePool('CVehicle') local closestDistance = -1 local closestVehicle = -1 - local coords = coords or GetEntityCoords(ped) + if coords then + if type(coords) == 'table' then coords = vector3(coords.x, coords.y, coords.z) end + else + coords = GetEntityCoords(ped) + end for i=1, #vehicles, 1 do local vehicleCoords = GetEntityCoords(vehicles[i]) local distance = #(vehicleCoords - coords) From ca30d45810de490d35162f4443fa35ffed6ee9e9 Mon Sep 17 00:00:00 2001 From: Mojito-Fivem Date: Thu, 7 Oct 2021 22:16:03 +0100 Subject: [PATCH 2/5] fix(client/functions): Additional type checking for vectors --- client/functions.lua | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/client/functions.lua b/client/functions.lua index dfcf893..c581286 100644 --- a/client/functions.lua +++ b/client/functions.lua @@ -233,7 +233,11 @@ function QBCore.Functions.GetClosestObject(coords) local objects = GetGamePool('CObject') local closestDistance = -1 local closestObject = -1 - local coords = coords or GetEntityCoords(ped) + if coords then + if type(coords) == 'table' then coords = vector3(coords.x, coords.y, coords.z) end + else + coords = GetEntityCoords(ped) + end for i = 1, #objects, 1 do local objectCoords = GetEntityCoords(objects[i]) local distance = #(objectCoords - coords) @@ -266,7 +270,12 @@ end function QBCore.Functions.SpawnVehicle(model, cb, coords, isnetworked) local model = GetHashKey(model) - local coords = coords or QBCore.Functions.GetCoords(PlayerPedId()) + local ped = PlayerPedId() + if coords then + if type(coords) == 'table' then coords = vector3(coords.x, coords.y, coords.z) end + else + coords = GetEntityCoords(ped) + end local isnetworked = isnetworked or true if not IsModelInCdimage(model) then return end RequestModel(model) From 34f03308c29c35d65c4f5ace9144f4f285fd6bad Mon Sep 17 00:00:00 2001 From: Mojito-Fivem Date: Wed, 13 Oct 2021 22:37:25 +0100 Subject: [PATCH 3/5] refactor(client/functions): fix indentation --- client/functions.lua | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/client/functions.lua b/client/functions.lua index c581286..36e96a0 100644 --- a/client/functions.lua +++ b/client/functions.lua @@ -141,9 +141,9 @@ end function QBCore.Functions.GetClosestPed(coords, ignoreList) local ped = PlayerPedId() if coords then - if type(coords) == 'table' then coords = vector3(coords.x, coords.y, coords.z) end + if type(coords) == 'table' then coords = vector3(coords.x, coords.y, coords.z) end else - coords = GetEntityCoords(ped) + coords = GetEntityCoords(ped) end local ignoreList = ignoreList or {} local peds = QBCore.Functions.GetPeds(ignoreList) @@ -164,9 +164,9 @@ end function QBCore.Functions.GetClosestPlayer(coords) local ped = PlayerPedId() if coords then - if type(coords) == 'table' then coords = vector3(coords.x, coords.y, coords.z) end + if type(coords) == 'table' then coords = vector3(coords.x, coords.y, coords.z) end else - coords = GetEntityCoords(ped) + coords = GetEntityCoords(ped) end local closestPlayers = QBCore.Functions.GetPlayersFromCoords(coords) local closestDistance = -1 @@ -189,9 +189,9 @@ function QBCore.Functions.GetPlayersFromCoords(coords, distance) local players = QBCore.Functions.GetPlayers() local ped = PlayerPedId() if coords then - if type(coords) == 'table' then coords = vector3(coords.x, coords.y, coords.z) end + if type(coords) == 'table' then coords = vector3(coords.x, coords.y, coords.z) end else - coords = GetEntityCoords(ped) + coords = GetEntityCoords(ped) end local distance = distance or 5 local closePlayers = {} @@ -212,9 +212,9 @@ function QBCore.Functions.GetClosestVehicle(coords) local closestDistance = -1 local closestVehicle = -1 if coords then - if type(coords) == 'table' then coords = vector3(coords.x, coords.y, coords.z) end + if type(coords) == 'table' then coords = vector3(coords.x, coords.y, coords.z) end else - coords = GetEntityCoords(ped) + coords = GetEntityCoords(ped) end for i=1, #vehicles, 1 do local vehicleCoords = GetEntityCoords(vehicles[i]) @@ -234,9 +234,9 @@ function QBCore.Functions.GetClosestObject(coords) local closestDistance = -1 local closestObject = -1 if coords then - if type(coords) == 'table' then coords = vector3(coords.x, coords.y, coords.z) end + if type(coords) == 'table' then coords = vector3(coords.x, coords.y, coords.z) end else - coords = GetEntityCoords(ped) + coords = GetEntityCoords(ped) end for i = 1, #objects, 1 do local objectCoords = GetEntityCoords(objects[i]) @@ -272,9 +272,9 @@ function QBCore.Functions.SpawnVehicle(model, cb, coords, isnetworked) local model = GetHashKey(model) local ped = PlayerPedId() if coords then - if type(coords) == 'table' then coords = vector3(coords.x, coords.y, coords.z) end + if type(coords) == 'table' then coords = vector3(coords.x, coords.y, coords.z) end else - coords = GetEntityCoords(ped) + coords = GetEntityCoords(ped) end local isnetworked = isnetworked or true if not IsModelInCdimage(model) then return end From e0d3a9ea6970b9d924e2a8ccbbb8067c55997446 Mon Sep 17 00:00:00 2001 From: Mojito-Fivem Date: Wed, 13 Oct 2021 23:01:06 +0100 Subject: [PATCH 4/5] refactor(client/functions): Simplify vector check with ternary --- client/functions.lua | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/client/functions.lua b/client/functions.lua index 36e96a0..31e1c2d 100644 --- a/client/functions.lua +++ b/client/functions.lua @@ -141,7 +141,7 @@ end function QBCore.Functions.GetClosestPed(coords, ignoreList) local ped = PlayerPedId() if coords then - if type(coords) == 'table' then coords = vector3(coords.x, coords.y, coords.z) end + coords = type(coords) == 'table' and vec3(coords.x, coords.y, coords.z) or coords else coords = GetEntityCoords(ped) end @@ -164,7 +164,7 @@ end function QBCore.Functions.GetClosestPlayer(coords) local ped = PlayerPedId() if coords then - if type(coords) == 'table' then coords = vector3(coords.x, coords.y, coords.z) end + coords = type(coords) == 'table' and vec3(coords.x, coords.y, coords.z) or coords else coords = GetEntityCoords(ped) end @@ -189,7 +189,7 @@ function QBCore.Functions.GetPlayersFromCoords(coords, distance) local players = QBCore.Functions.GetPlayers() local ped = PlayerPedId() if coords then - if type(coords) == 'table' then coords = vector3(coords.x, coords.y, coords.z) end + coords = type(coords) == 'table' and vec3(coords.x, coords.y, coords.z) or coords else coords = GetEntityCoords(ped) end @@ -212,7 +212,7 @@ function QBCore.Functions.GetClosestVehicle(coords) local closestDistance = -1 local closestVehicle = -1 if coords then - if type(coords) == 'table' then coords = vector3(coords.x, coords.y, coords.z) end + coords = type(coords) == 'table' and vec3(coords.x, coords.y, coords.z) or coords else coords = GetEntityCoords(ped) end @@ -234,7 +234,7 @@ function QBCore.Functions.GetClosestObject(coords) local closestDistance = -1 local closestObject = -1 if coords then - if type(coords) == 'table' then coords = vector3(coords.x, coords.y, coords.z) end + coords = type(coords) == 'table' and vec3(coords.x, coords.y, coords.z) or coords else coords = GetEntityCoords(ped) end @@ -272,7 +272,7 @@ function QBCore.Functions.SpawnVehicle(model, cb, coords, isnetworked) local model = GetHashKey(model) local ped = PlayerPedId() if coords then - if type(coords) == 'table' then coords = vector3(coords.x, coords.y, coords.z) end + coords = type(coords) == 'table' and vec3(coords.x, coords.y, coords.z) or coords else coords = GetEntityCoords(ped) end From c37bc775c610f4829ff7b1c79e292caf73944ca7 Mon Sep 17 00:00:00 2001 From: Mojito-Fivem Date: Thu, 14 Oct 2021 11:58:24 +0100 Subject: [PATCH 5/5] chore(client/functions): Convert tabs to spaces --- client/functions.lua | 64 ++++++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/client/functions.lua b/client/functions.lua index 89d999a..8723b4e 100644 --- a/client/functions.lua +++ b/client/functions.lua @@ -154,11 +154,11 @@ end function QBCore.Functions.GetClosestPed(coords, ignoreList) local ped = PlayerPedId() - if coords then - coords = type(coords) == 'table' and vec3(coords.x, coords.y, coords.z) or coords - else - coords = GetEntityCoords(ped) - end + if coords then + coords = type(coords) == 'table' and vec3(coords.x, coords.y, coords.z) or coords + else + coords = GetEntityCoords(ped) + end local ignoreList = ignoreList or {} local peds = QBCore.Functions.GetPeds(ignoreList) local closestDistance = -1 @@ -177,11 +177,11 @@ end function QBCore.Functions.GetClosestPlayer(coords) local ped = PlayerPedId() - if coords then - coords = type(coords) == 'table' and vec3(coords.x, coords.y, coords.z) or coords - else - coords = GetEntityCoords(ped) - end + if coords then + coords = type(coords) == 'table' and vec3(coords.x, coords.y, coords.z) or coords + else + coords = GetEntityCoords(ped) + end local closestPlayers = QBCore.Functions.GetPlayersFromCoords(coords) local closestDistance = -1 local closestPlayer = -1 @@ -201,12 +201,12 @@ end function QBCore.Functions.GetPlayersFromCoords(coords, distance) local players = QBCore.Functions.GetPlayers() - local ped = PlayerPedId() - if coords then - coords = type(coords) == 'table' and vec3(coords.x, coords.y, coords.z) or coords - else - coords = GetEntityCoords(ped) - end + local ped = PlayerPedId() + if coords then + coords = type(coords) == 'table' and vec3(coords.x, coords.y, coords.z) or coords + else + coords = GetEntityCoords(ped) + end local distance = distance or 5 local closePlayers = {} for _, player in pairs(players) do @@ -225,11 +225,11 @@ function QBCore.Functions.GetClosestVehicle(coords) local vehicles = GetGamePool('CVehicle') local closestDistance = -1 local closestVehicle = -1 - if coords then - coords = type(coords) == 'table' and vec3(coords.x, coords.y, coords.z) or coords - else - coords = GetEntityCoords(ped) - end + if coords then + coords = type(coords) == 'table' and vec3(coords.x, coords.y, coords.z) or coords + else + coords = GetEntityCoords(ped) + end for i = 1, #vehicles, 1 do local vehicleCoords = GetEntityCoords(vehicles[i]) local distance = #(vehicleCoords - coords) @@ -247,11 +247,11 @@ function QBCore.Functions.GetClosestObject(coords) local objects = GetGamePool('CObject') local closestDistance = -1 local closestObject = -1 - if coords then - coords = type(coords) == 'table' and vec3(coords.x, coords.y, coords.z) or coords - else - coords = GetEntityCoords(ped) - end + if coords then + coords = type(coords) == 'table' and vec3(coords.x, coords.y, coords.z) or coords + else + coords = GetEntityCoords(ped) + end for i = 1, #objects, 1 do local objectCoords = GetEntityCoords(objects[i]) local distance = #(objectCoords - coords) @@ -284,12 +284,12 @@ end function QBCore.Functions.SpawnVehicle(model, cb, coords, isnetworked) local model = GetHashKey(model) - local ped = PlayerPedId() - if coords then - coords = type(coords) == 'table' and vec3(coords.x, coords.y, coords.z) or coords - else - coords = GetEntityCoords(ped) - end + local ped = PlayerPedId() + if coords then + coords = type(coords) == 'table' and vec3(coords.x, coords.y, coords.z) or coords + else + coords = GetEntityCoords(ped) + end local isnetworked = isnetworked or true if not IsModelInCdimage(model) then return