Skip to content

Commit

Permalink
fix(client): updateInventory tweaks
Browse files Browse the repository at this point in the history
Trigger refreshSlots inside the function, rather than after; should help with ghost items.. maybe?
Check item count when triggering the remove function. Still has weird behaviour, especially with slot 1.
  • Loading branch information
thelindat committed Jan 18, 2022
1 parent c9551cf commit 4a2b6ef
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
47 changes: 27 additions & 20 deletions client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -619,11 +619,15 @@ local function updateInventory(items, weight)

if v then
local data = Items[v.name]
if data.client?.add then data.client.add(data.count, v.count) end
if data.client?.add then
data.client.add(data.count, v.count)
end
data.count += v.count
else
local data = Items[item.name]
if data.client?.remove then data.client.remove() end
if data.count == 0 and data.client?.remove then
data.client.remove()
end
end

PlayerData.inventory[slot] = v and v or nil
Expand All @@ -642,43 +646,46 @@ local function updateInventory(items, weight)

if v.count then
local data = Items[v.name]
if data.client?.add then data.client.add(data.count, v.count) end
if data.client?.add then
data.client.add(data.count, v.count)
end
data.count += v.count
else
local data = Items[v.name]
if data.client?.remove then data.client.remove() end
local data = Items[item.name]
if data.count == 0 and data.client?.remove then
data.client.remove()
end
end

PlayerData.inventory[v.slot] = v.count and v or nil
changes[v.slot] = v.count and v or false
if not v.count then v.name = nil end
PlayerData.inventory[v.slot] = v.name and v or nil
end
client.setPlayerData('weight', weight.left)
SendNUIMessage({ action = 'refreshSlots', data = items })
end
client.setPlayerData('inventory', PlayerData.inventory)
TriggerEvent('ox_inventory:updateInventory', changes)
end

AddEventHandler('ox_inventory:updateInventory', function(inventory)
for k, v in pairs(inventory) do
if type(v) == 'table' then
print(k, json.encode(v))
else
print(k, false)
end
end
print()
end)
-- AddEventHandler('ox_inventory:updateInventory', function(inventory)
-- for k, v in pairs(inventory) do
-- if type(v) == 'table' then
-- print(k, json.encode(v))
-- else
-- print(k, false)
-- end
-- end
-- print()
-- end)

RegisterNetEvent('ox_inventory:updateSlots', function(items, weights, count, removed)
updateInventory(items, weights)

if count then
local item = items[1].item
Utils.ItemNotify({item.label, item.name, shared.locale(removed and 'removed' or 'added', count)})
if (removed and item.count and item.count <= count) or not item.count then items[1].item.name = nil end
end

SendNUIMessage({ action = 'refreshSlots', data = items })
updateInventory(items, weights)
end)

RegisterNetEvent('ox_inventory:inventoryReturned', function(data)
Expand Down
3 changes: 2 additions & 1 deletion data/items.lua
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,14 @@ return {
consume = 0,
client = {
add = function(count, added)
print(count, added)
print('Added item', count, added)
if count == 0 and GetResourceState('npwd') == 'started' then
exports.npwd:setPhoneDisabled(false)
end
end,

remove = function()
print('Removed item')
if GetResourceState('npwd') == 'started' then
exports.npwd:setPhoneDisabled(true)
end
Expand Down

0 comments on commit 4a2b6ef

Please sign in to comment.