Skip to content

Commit

Permalink
refactor(client): move itemCount event handler
Browse files Browse the repository at this point in the history
+ other misc changes
  • Loading branch information
thelindat committed Feb 22, 2023
1 parent 96d65b8 commit 61c1d23
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 24 deletions.
21 changes: 11 additions & 10 deletions client/framework/esx.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
if GetResourceState('es_extended') == 'missing' then return end

local groups = { 'job', 'job2' }
PlayerItems = {}
local playerGroups = {}
local usingOxInventory = GetResourceState('ox_inventory') ~= 'missing'
PlayerItems = {}

local function setPlayerData(playerData)
if playerData.inventory then
Expand Down Expand Up @@ -30,7 +31,7 @@ SetTimeout(0, function()
setPlayerData(ESX.PlayerData)
end

if GetResourceState('ox_inventory') ~= 'missing' then
if usingOxInventory then
setmetatable(PlayerItems, {
__index = function(self, index)
self[index] = exports.ox_inventory:Search('count', index) or 0
Expand All @@ -55,6 +56,14 @@ RegisterNetEvent('esx:setJob2', function(job)
playerGroups.job2 = job
end)

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

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

function PlayerHasGroups(filter)
local _type = type(filter)
for i = 1, #groups do
Expand Down Expand Up @@ -90,11 +99,3 @@ function PlayerHasGroups(filter)
end
end
end

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

RegisterNetEvent('esx:removeInventoryItem', function(name, count)
PlayerItems[name] = count
end)
7 changes: 1 addition & 6 deletions client/framework/ox.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ local success, result = pcall(function()
end)

local playerGroups = success and result or {}
PlayerItems = {}

AddEventHandler('ox:playerLoaded', function(data)
playerGroups = data.groups
Expand Down Expand Up @@ -47,9 +48,3 @@ function PlayerHasGroups(filter)
end
end
end

PlayerItems = {}

AddEventHandler('ox_inventory:itemCount', function(name, count)
PlayerItems[name] = count
end)
9 changes: 1 addition & 8 deletions client/framework/qb.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@ end)

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

PlayerItems = {}



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

Expand All @@ -22,11 +19,7 @@ local function setPlayerItems()
end
end

if usingOxInventory then
AddEventHandler('ox_inventory:itemCount', function(name, count)
PlayerItems[name] = count
end)
else
if not usingOxInventory then
setPlayerItems()
end

Expand Down
4 changes: 4 additions & 0 deletions client/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ if playerItems and GetResourceState('ox_inventory') ~= 'missing' then
return self[index]
end
})

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

function PlayerHasItems(filter, hasAny)
Expand Down

0 comments on commit 61c1d23

Please sign in to comment.