Skip to content

Commit

Permalink
refactor(client): move PlayerHasItems to utils
Browse files Browse the repository at this point in the history
Cut down on code duplication and improve maintainability by
moving PlayerHasItems (and the items table) to utils.
  • Loading branch information
thelindat committed Feb 15, 2023
1 parent 1777aae commit 13608d3
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 91 deletions.
32 changes: 5 additions & 27 deletions client/framework/esx.lua
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
if GetResourceState('es_extended') == 'missing' then return end

local groups = { 'job', 'job2' }
local playerItems = {}
PlayerItems = {}
local playerGroups = {}

local function setPlayerData(playerData)
if playerData.inventory then
for _, v in pairs(playerData.inventory) do
if v.count > 0 then
playerItems[v.name] = v.count
PlayerItems[v.name] = v.count
end
end
end
Expand All @@ -31,7 +31,7 @@ SetTimeout(0, function()
end

if GetResourceState('ox_inventory') ~= 'missing' then
setmetatable(playerItems, {
setmetatable(PlayerItems, {
__index = function(self, index)
self[index] = exports.ox_inventory:Search('count', index) or 0
return self[index]
Expand Down Expand Up @@ -92,31 +92,9 @@ function PlayerHasGroups(filter)
end

RegisterNetEvent('esx:addInventoryItem', function(name, count)
playerItems[name] = count
PlayerItems[name] = count
end)

RegisterNetEvent('esx:removeInventoryItem', function(name, count)
playerItems[name] = count
PlayerItems[name] = count
end)

function PlayerHasItems(filter)
local _type = type(filter)

if _type == 'string' then
if (playerItems[filter] or 0) < 1 then return end
elseif _type == 'table' then
local tabletype = table.type(filter)

if tabletype == 'hash' then
for name, amount in pairs(filter) do
if (playerItems[name] or 0) < amount then return end
end
elseif tabletype == 'array' then
for i = 1, #filter do
if (playerItems[filter[i]] or 0) < 1 then return end
end
end
end

return true
end
31 changes: 2 additions & 29 deletions client/framework/ox.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,35 +48,8 @@ function PlayerHasGroups(filter)
end
end

local playerItems = setmetatable({}, {
__index = function(self, index)
self[index] = exports.ox_inventory:Search('count', index) or 0
return self[index]
end
})
PlayerItems = {}

AddEventHandler('ox_inventory:itemCount', function(name, count)
playerItems[name] = count
PlayerItems[name] = count
end)

function PlayerHasItems(filter)
local _type = type(filter)

if _type == 'string' then
if playerItems[filter] < 1 then return end
elseif _type == 'table' then
local tabletype = table.type(filter)

if tabletype == 'hash' then
for name, amount in pairs(filter) do
if playerItems[name] < amount then return end
end
elseif tabletype == 'array' then
for i = 1, #filter do
if playerItems[filter[i]] < 1 then return end
end
end
end

return true
end
38 changes: 6 additions & 32 deletions client/framework/qb.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,25 @@ local success, result = pcall(function()
end)

local playerData = success and result or {}
local usingOxInventory = GetResourceState('ox_inventory') ~= 'missing'

PlayerItems = {}

local usingOxInventory = GetResourceState('ox_inventory') ~= "missing"

local playerItems = setmetatable({}, {
__index = function(self, index)
self[index] = usingOxInventory and exports.ox_inventory:Search('count', index) or 0
return self[index]
end
})

local function setPlayerItems()
if not playerData?.items then return end

table.wipe(playerItems)
table.wipe(PlayerItems)

for _, item in pairs(playerData.items) do
playerItems[item.name] = (playerItems[item.name] or 0) + item.amount
PlayerItems[item.name] = (PlayerItems[item.name] or 0) + item.amount
end
end

if usingOxInventory then
AddEventHandler('ox_inventory:itemCount', function(name, count)
playerItems[name] = count
PlayerItems[name] = count
end)
else
setPlayerItems()
Expand Down Expand Up @@ -85,25 +81,3 @@ function PlayerHasGroups(filter)
end
end
end

function PlayerHasItems(filter)
local _type = type(filter)

if _type == 'string' then
if playerItems[filter] < 1 then return end
elseif _type == 'table' then
local tabletype = table.type(filter)

if tabletype == 'hash' then
for name, amount in pairs(filter) do
if playerItems[name] < amount then return end
end
elseif tabletype == 'array' then
for i = 1, #filter do
if playerItems[filter[i]] < 1 then return end
end
end
end

return true
end
2 changes: 1 addition & 1 deletion client/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ local GetEntityType = GetEntityType
local HasEntityClearLosToEntity = HasEntityClearLosToEntity
local GetCurrentZone = GetCurrentZone
local PlayerHasGroups = PlayerHasGroups or function() return true end
local PlayerHasItems = PlayerHasItems or function() return true end
local PlayerHasItems = PlayerHasItems
local GetEntityBoneIndexByName = GetEntityBoneIndexByName
local GetWorldPositionOfEntityBone = GetWorldPositionOfEntityBone
local next = next
Expand Down
35 changes: 34 additions & 1 deletion client/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,37 @@ else
end
end
end
end
end

local playerItems = PlayerItems

if playerItems and GetResourceState('ox_inventory') ~= 'missing' then
setmetatable(playerItems, {
__index = function(self, index)
self[index] = exports.ox_inventory:Search('count', index) or 0
return self[index]
end
})
end

function PlayerHasItems(filter)
local _type = type(filter)

if _type == 'string' then
if playerItems[filter] < 1 then return end
elseif _type == 'table' then
local tabletype = table.type(filter)

if tabletype == 'hash' then
for name, amount in pairs(filter) do
if playerItems[name] < amount then return end
end
elseif tabletype == 'array' then
for i = 1, #filter do
if playerItems[filter[i]] < 1 then return end
end
end
end

return true
end
2 changes: 1 addition & 1 deletion fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ shared_scripts {

client_scripts {
'@ox_lib/init.lua',
'client/framework/*.lua',
'client/utils.lua',
'client/api.lua',
'client/debug.lua',
'client/defaults.lua',
'client/framework/*.lua',
'client/compat/*.lua',
'client/main.lua',
}
Expand Down

0 comments on commit 13608d3

Please sign in to comment.