From 13608d3a967759ba80d0e4763d92ea5b65e5a37a Mon Sep 17 00:00:00 2001 From: Linden <65407488+thelindat@users.noreply.github.com> Date: Wed, 15 Feb 2023 13:14:22 +1100 Subject: [PATCH] refactor(client): move PlayerHasItems to utils Cut down on code duplication and improve maintainability by moving PlayerHasItems (and the items table) to utils. --- client/framework/esx.lua | 32 +++++--------------------------- client/framework/ox.lua | 31 ++----------------------------- client/framework/qb.lua | 38 ++++++-------------------------------- client/main.lua | 2 +- client/utils.lua | 35 ++++++++++++++++++++++++++++++++++- fxmanifest.lua | 2 +- 6 files changed, 49 insertions(+), 91 deletions(-) diff --git a/client/framework/esx.lua b/client/framework/esx.lua index b89bf69..9f9107e 100644 --- a/client/framework/esx.lua +++ b/client/framework/esx.lua @@ -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 @@ -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] @@ -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 \ No newline at end of file diff --git a/client/framework/ox.lua b/client/framework/ox.lua index 0c6259e..6beb6bf 100644 --- a/client/framework/ox.lua +++ b/client/framework/ox.lua @@ -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 \ No newline at end of file diff --git a/client/framework/qb.lua b/client/framework/qb.lua index 6826848..c80b72f 100644 --- a/client/framework/qb.lua +++ b/client/framework/qb.lua @@ -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() @@ -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 diff --git a/client/main.lua b/client/main.lua index 635303d..bd13392 100644 --- a/client/main.lua +++ b/client/main.lua @@ -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 diff --git a/client/utils.lua b/client/utils.lua index 9ad42ca..fff187d 100644 --- a/client/utils.lua +++ b/client/utils.lua @@ -102,4 +102,37 @@ else end end end -end \ No newline at end of file +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 diff --git a/fxmanifest.lua b/fxmanifest.lua index 0f2e472..24a8d82 100644 --- a/fxmanifest.lua +++ b/fxmanifest.lua @@ -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', }