From 4feda3ba0879c9643c90ef25917856f4aa585dfa Mon Sep 17 00:00:00 2001 From: Ilias Rbayti <63159154+Kenshiin13@users.noreply.github.com> Date: Sat, 23 Nov 2024 00:54:37 +0100 Subject: [PATCH 1/2] feat(es_extended/shared/modules/table): add ESX.Table.ToArray --- [core]/es_extended/shared/modules/table.lua | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/[core]/es_extended/shared/modules/table.lua b/[core]/es_extended/shared/modules/table.lua index fc3452005..aacb25840 100644 --- a/[core]/es_extended/shared/modules/table.lua +++ b/[core]/es_extended/shared/modules/table.lua @@ -223,3 +223,11 @@ function ESX.Table.Sort(t, order) end end end + +function ESX.Table.ToArray(table) + local array = {} + for _, v in pairs(table) do + array[#array + 1] = v + end + return array +end From 3a61ae53103f30bae189b5efbe23a0527c9c03bd Mon Sep 17 00:00:00 2001 From: Ilias Rbayti <63159154+Kenshiin13@users.noreply.github.com> Date: Sat, 23 Nov 2024 01:00:47 +0100 Subject: [PATCH 2/2] refactor(es_extended/server/functions): refactor ESX.GetExtendedPlayers --- [core]/es_extended/server/functions.lua | 30 +++++++++++++------------ 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/[core]/es_extended/server/functions.lua b/[core]/es_extended/server/functions.lua index 606ab0e74..cec340b53 100644 --- a/[core]/es_extended/server/functions.lua +++ b/[core]/es_extended/server/functions.lua @@ -276,15 +276,15 @@ end ESX.GetPlayers = GetPlayers -local function checkTable(key, val, player, xPlayers) +local function checkTable(key, val, xPlayer, xPlayers) for valIndex = 1, #val do local value = val[valIndex] if not xPlayers[value] then xPlayers[value] = {} end - if (key == "job" and player.job.name == value) or player[key] == value then - xPlayers[value][#xPlayers[value] + 1] = player + if (key == "job" and xPlayer.job.name == value) or xPlayer[key] == value then + xPlayers[value][#xPlayers[value] + 1] = xPlayer end end end @@ -293,20 +293,22 @@ end ---@param val? string|table ---@return table function ESX.GetExtendedPlayers(key, val) + if not key then + return ESX.Table.ToArray(ESX.Players) + end + local xPlayers = {} if type(val) == "table" then - for _, v in pairs(ESX.Players) do - checkTable(key, val, v, xPlayers) + for i, xPlayer in pairs(ESX.Players) do + checkTable(key, val, xPlayer, xPlayers) end - else - for _, v in pairs(ESX.Players) do - if key then - if (key == "job" and v.job.name == val) or v[key] == val then - xPlayers[#xPlayers + 1] = v - end - else - xPlayers[#xPlayers + 1] = v - end + + return xPlayers + end + + for i, xPlayer in pairs(ESX.Players) do + if (key == "job" and xPlayer.job.name == val) or xPlayer[key] == val then + xPlayers[#xPlayers + 1] = xPlayer end end