From eec2bd8f64be48fd220e0f9a1271ba5344a14ab7 Mon Sep 17 00:00:00 2001 From: Arctos2win Date: Fri, 29 Sep 2023 20:02:14 +0200 Subject: [PATCH 1/7] improve functions and removeInventoryItem event --- [core]/es_extended/client/functions.lua | 99 ++++++++++--------------- [core]/es_extended/client/main.lua | 10 +-- 2 files changed, 44 insertions(+), 65 deletions(-) diff --git a/[core]/es_extended/client/functions.lua b/[core]/es_extended/client/functions.lua index 46afb208c..1e438fda6 100644 --- a/[core]/es_extended/client/functions.lua +++ b/[core]/es_extended/client/functions.lua @@ -25,32 +25,19 @@ function ESX.GetPlayerData() end function ESX.SearchInventory(items, count) - if type(items) == 'string' then - items = { items } - end - - local returnData = {} - local itemCount = #items - - for i = 1, itemCount do - local itemName = items[i] - returnData[itemName] = count and 0 + items = type(items) == 'string' and { items } or items - for _, item in pairs(ESX.PlayerData.inventory) do - if item.name == itemName then - if count then - returnData[itemName] = returnData[itemName] + item.count - else - returnData[itemName] = item - end - end - end - end + local data = {} + for i=1, #items do + for c=1, #ESX.PlayerData.inventory do + if ESX.PlayerData.inventory[c].name == items[i] then + data[items[i]] = (count and ESX.PlayerData.inventory[c].count) or ESX.PlayerData.inventory[c] + end + end + end - if next(returnData) then - return itemCount == 1 and returnData[items[1]] or returnData - end -end + return #items > 1 and data or data[1] +end function ESX.SetPlayerData(key, val) local current = ESX.PlayerData[key] @@ -372,12 +359,7 @@ function ESX.Game.SpawnVehicle(vehicleModel, coords, heading, cb, networked) if not vector or not playerCoords then return end - local dist = #(playerCoords - vector) - if dist > 424 then -- Onesync infinity Range (https://docs.fivem.net/docs/scripting-reference/onesync/) - local executingResource = GetInvokingResource() or "Unknown" - return print(("[^1ERROR^7] Resource ^5%s^7 Tried to spawn vehicle on the client but the position is too far away (Out of onesync range)."):format(executingResource)) - end - + CreateThread(function() ESX.Streaming.RequestModel(model) @@ -420,20 +402,19 @@ function ESX.Game.GetObjects() -- Leave the function for compatibility end function ESX.Game.GetPeds(onlyOtherPeds) - local myPed, pool = ESX.PlayerData.ped, GetGamePool('CPed') - - if not onlyOtherPeds then - return pool - end - - local peds = {} - for i = 1, #pool do - if pool[i] ~= myPed then - peds[#peds + 1] = pool[i] + local pool = GetGamePool('CPed') + + if onlyOtherPeds then + local myPed = ESX.PlayerData.ped + for i = 1, #pool do + if pool[i] == myPed then + table.remove(pool, i) + break + end end - end + end - return peds + return pool end function ESX.Game.GetVehicles() -- Leave the function for compatibility @@ -442,18 +423,19 @@ end function ESX.Game.GetPlayers(onlyOtherPlayers, returnKeyValue, returnPeds) local players, myPlayer = {}, PlayerId() + local active = GetActivePlayers() - for _, player in ipairs(GetActivePlayers()) do - local ped = GetPlayerPed(player) + for i=1, #active do + local ped = GetPlayerPed(active[i]) - if DoesEntityExist(ped) and ((onlyOtherPlayers and player ~= myPlayer) or not onlyOtherPlayers) then - if returnKeyValue then - players[player] = ped - else + if DoesEntityExist(ped) and ((onlyOtherPlayers and active[i] ~= myPlayer) or not onlyOther) then + if returnKeyValue then + players[active[i]] = ped + else players[#players + 1] = returnPeds and ped or player end - end - end + end + end return players end @@ -978,12 +960,8 @@ function ESX.Game.Utils.DrawText3D(coords, text, size, font) local camCoords = GetFinalRenderedCamCoord() local distance = #(vector - camCoords) - if not size then - size = 1 - end - if not font then - font = 0 - end + size = size or 1 + font = font or 0 local scale = (size / distance) * 2 local fov = (1 / GetGameplayCamFov()) * 100 @@ -1041,8 +1019,9 @@ function ESX.ShowInventory() end end - for _, v in ipairs(ESX.PlayerData.inventory) do - if v.count > 0 then + for i=1, #ESX.PlayerData.inventory do + local v = ESX.PlayerData.inventory[i] + if v.count > 0 then currentWeight = currentWeight + (v.weight * v.count) elements[#elements + 1] = { @@ -1055,8 +1034,8 @@ function ESX.ShowInventory() rare = v.rare, canRemove = v.canRemove } - end - end + end + end elements[1].title = TranslateCap('inventory', currentWeight, Config.MaxWeight) diff --git a/[core]/es_extended/client/main.lua b/[core]/es_extended/client/main.lua index 3804cfc10..3ca7150b0 100644 --- a/[core]/es_extended/client/main.lua +++ b/[core]/es_extended/client/main.lua @@ -309,13 +309,13 @@ if not Config.OxInventory then RegisterNetEvent('esx:removeInventoryItem') AddEventHandler('esx:removeInventoryItem', function(item, count, showNotification) - for k, v in ipairs(ESX.PlayerData.inventory) do - if v.name == item then + for i=1, #ESX.PlayerData.inventory do + if ESX.PlayerData.inventory[i].name == item then ESX.UI.ShowInventoryItemNotification(false, v.label, v.count - count) - ESX.PlayerData.inventory[k].count = count + ESX.PlayerData.inventory[i].count = count break - end - end + end + end if showNotification then ESX.UI.ShowInventoryItemNotification(false, item, count) From d7403e0a65f7bfeeea7490f45cc12c1c7f4193b5 Mon Sep 17 00:00:00 2001 From: Arctos2win <116841243+Arctos2win@users.noreply.github.com> Date: Fri, 29 Sep 2023 20:09:19 +0200 Subject: [PATCH 2/7] fix --- [core]/es_extended/client/functions.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/[core]/es_extended/client/functions.lua b/[core]/es_extended/client/functions.lua index 1e438fda6..565a2f347 100644 --- a/[core]/es_extended/client/functions.lua +++ b/[core]/es_extended/client/functions.lua @@ -432,7 +432,7 @@ function ESX.Game.GetPlayers(onlyOtherPlayers, returnKeyValue, returnPeds) if returnKeyValue then players[active[i]] = ped else - players[#players + 1] = returnPeds and ped or player + players[#players + 1] = returnPeds and ped or active[i] end end end From be835db4ee192303822f9148bf6496d2409fc21d Mon Sep 17 00:00:00 2001 From: Arctos2win Date: Fri, 29 Sep 2023 20:21:27 +0200 Subject: [PATCH 3/7] fix event --- [core]/es_extended/client/main.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/[core]/es_extended/client/main.lua b/[core]/es_extended/client/main.lua index 3ca7150b0..dd64baa83 100644 --- a/[core]/es_extended/client/main.lua +++ b/[core]/es_extended/client/main.lua @@ -311,7 +311,7 @@ if not Config.OxInventory then AddEventHandler('esx:removeInventoryItem', function(item, count, showNotification) for i=1, #ESX.PlayerData.inventory do if ESX.PlayerData.inventory[i].name == item then - ESX.UI.ShowInventoryItemNotification(false, v.label, v.count - count) + ESX.UI.ShowInventoryItemNotification(false, ESX.PlayerData.inventory[i].label, ESX.PlayerData.inventory[i].count - count) ESX.PlayerData.inventory[i].count = count break end From f61e6648dd167356cf2a4997b72ad212c749ee50 Mon Sep 17 00:00:00 2001 From: Arctos2win Date: Fri, 29 Sep 2023 22:30:06 +0200 Subject: [PATCH 4/7] fix last stuff --- [core]/es_extended/client/functions.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/[core]/es_extended/client/functions.lua b/[core]/es_extended/client/functions.lua index 565a2f347..19e21ac29 100644 --- a/[core]/es_extended/client/functions.lua +++ b/[core]/es_extended/client/functions.lua @@ -428,7 +428,7 @@ function ESX.Game.GetPlayers(onlyOtherPlayers, returnKeyValue, returnPeds) for i=1, #active do local ped = GetPlayerPed(active[i]) - if DoesEntityExist(ped) and ((onlyOtherPlayers and active[i] ~= myPlayer) or not onlyOther) then + if DoesEntityExist(ped) and ((onlyOtherPlayers and active[i] ~= myPlayer) or not onlyOtherPlayers) then if returnKeyValue then players[active[i]] = ped else From 6fc40c7c4963acd1ccac174342268e3bfbd8ff89 Mon Sep 17 00:00:00 2001 From: Arctos2win <116841243+Arctos2win@users.noreply.github.com> Date: Fri, 29 Sep 2023 23:11:12 +0200 Subject: [PATCH 5/7] Update functions.lua --- [core]/es_extended/client/functions.lua | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/[core]/es_extended/client/functions.lua b/[core]/es_extended/client/functions.lua index 19e21ac29..d8c20a17f 100644 --- a/[core]/es_extended/client/functions.lua +++ b/[core]/es_extended/client/functions.lua @@ -426,13 +426,14 @@ function ESX.Game.GetPlayers(onlyOtherPlayers, returnKeyValue, returnPeds) local active = GetActivePlayers() for i=1, #active do - local ped = GetPlayerPed(active[i]) + local currentPlayer = active[i] + local ped = GetPlayerPed(currentPlayer) - if DoesEntityExist(ped) and ((onlyOtherPlayers and active[i] ~= myPlayer) or not onlyOtherPlayers) then + if DoesEntityExist(ped) and ((onlyOtherPlayers and currentPlayer ~= myPlayer) or not onlyOtherPlayers) then if returnKeyValue then - players[active[i]] = ped + players[currentPlayer] = ped else - players[#players + 1] = returnPeds and ped or active[i] + players[#players + 1] = returnPeds and ped or currentPlayer end end end From 75799c88c74bee043119d6e45a2c93f9c6b4742c Mon Sep 17 00:00:00 2001 From: Arctos2win <116841243+Arctos2win@users.noreply.github.com> Date: Mon, 2 Oct 2023 20:52:42 +0200 Subject: [PATCH 6/7] Update functions.lua --- [core]/es_extended/client/functions.lua | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/[core]/es_extended/client/functions.lua b/[core]/es_extended/client/functions.lua index d8c20a17f..b7f6ffdf4 100644 --- a/[core]/es_extended/client/functions.lua +++ b/[core]/es_extended/client/functions.lua @@ -359,6 +359,12 @@ function ESX.Game.SpawnVehicle(vehicleModel, coords, heading, cb, networked) if not vector or not playerCoords then return end + + local dist = #(playerCoords - vector) + if dist > 424 then -- Onesync infinity Range (https://docs.fivem.net/docs/scripting-reference/onesync/) + local executingResource = GetInvokingResource() or "Unknown" + return print(("[^1ERROR^7] Resource ^5%s^7 Tried to spawn vehicle on the client but the position is too far away (Out of onesync range)."):format(executingResource)) + end CreateThread(function() ESX.Streaming.RequestModel(model) From 2ace296fb228ff99947f911eae314592015072bf Mon Sep 17 00:00:00 2001 From: Arctos2win <116841243+Arctos2win@users.noreply.github.com> Date: Mon, 2 Oct 2023 22:37:06 +0200 Subject: [PATCH 7/7] fix repo --- [core]/es_extended/client/functions.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/[core]/es_extended/client/functions.lua b/[core]/es_extended/client/functions.lua index b7f6ffdf4..22485fc4f 100644 --- a/[core]/es_extended/client/functions.lua +++ b/[core]/es_extended/client/functions.lua @@ -36,7 +36,7 @@ function ESX.SearchInventory(items, count) end end - return #items > 1 and data or data[1] + return #items > 1 and data or data[items[1]] end function ESX.SetPlayerData(key, val)