Skip to content

Commit

Permalink
Merge pull request #1232 from Arctos2win/dev
Browse files Browse the repository at this point in the history
improve multiple functions and one event
  • Loading branch information
Arctos2win authored Oct 2, 2023
2 parents 8e71fd7 + 2ace296 commit 3376ba6
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 62 deletions.
98 changes: 42 additions & 56 deletions [core]/es_extended/client/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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[items[1]]
end

function ESX.SetPlayerData(key, val)
local current = ESX.PlayerData[key]
Expand Down Expand Up @@ -372,12 +359,13 @@ 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)

Expand Down Expand Up @@ -420,20 +408,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
Expand All @@ -442,18 +429,20 @@ 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 currentPlayer = active[i]
local ped = GetPlayerPed(currentPlayer)

if DoesEntityExist(ped) and ((onlyOtherPlayers and player ~= myPlayer) or not onlyOtherPlayers) then
if returnKeyValue then
players[player] = ped
else
players[#players + 1] = returnPeds and ped or player
if DoesEntityExist(ped) and ((onlyOtherPlayers and currentPlayer ~= myPlayer) or not onlyOtherPlayers) then
if returnKeyValue then
players[currentPlayer] = ped
else
players[#players + 1] = returnPeds and ped or currentPlayer
end
end
end
end
end

return players
end
Expand Down Expand Up @@ -978,12 +967,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
Expand Down Expand Up @@ -1041,8 +1026,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] = {
Expand All @@ -1055,8 +1041,8 @@ function ESX.ShowInventory()
rare = v.rare,
canRemove = v.canRemove
}
end
end
end
end

elements[1].title = TranslateCap('inventory', currentWeight, Config.MaxWeight)

Expand Down
12 changes: 6 additions & 6 deletions [core]/es_extended/client/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
ESX.UI.ShowInventoryItemNotification(false, v.label, v.count - count)
ESX.PlayerData.inventory[k].count = count
for i=1, #ESX.PlayerData.inventory do
if ESX.PlayerData.inventory[i].name == item then
ESX.UI.ShowInventoryItemNotification(false, ESX.PlayerData.inventory[i].label, ESX.PlayerData.inventory[i].count - count)
ESX.PlayerData.inventory[i].count = count
break
end
end
end
end

if showNotification then
ESX.UI.ShowInventoryItemNotification(false, item, count)
Expand Down

0 comments on commit 3376ba6

Please sign in to comment.