Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve multiple functions and one event #1232

Merged
merged 8 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 40 additions & 61 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[1]
end

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

Expand Down Expand Up @@ -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')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's lucky that the full pool is returned by default. Because then compatibility is broken because the default is to return an empty table, which is what the old method did. Now it will always return the pool. So it won't be backwards compatible.

Copy link
Contributor Author

@Arctos2win Arctos2win Sep 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

before if all the peds were requested the pool was returned and if not they would loop through
the pool table and add all peds except for the playerped to an empty table, now if the playerped is found then its removed from
the table and then the pool is being returned


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 +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])
Arctos2win marked this conversation as resolved.
Show resolved Hide resolved

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 active[i] ~= myPlayer) or not onlyOtherPlayers) then
if returnKeyValue then
players[active[i]] = ped
else
players[#players + 1] = returnPeds and ped or active[i]
end
end
end
end
end

return players
end
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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] = {
Expand All @@ -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)

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
Loading