Skip to content

Commit

Permalink
feat(server/inventory): send slot data to AddItem callback
Browse files Browse the repository at this point in the history
Sends item data such as the slot, count, metadata, etc. as the second
argument when an item was successfully given to the target.
Mostly useful for getting the generated metadata for an item.
  • Loading branch information
thelindat committed Apr 19, 2022
1 parent b52bb54 commit 5ff6994
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions modules/inventory/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,8 @@ function Inventory.AddItem(inv, item, count, metadata, slot, cb)
if type(item) ~= 'table' then item = Items(item) end
if type(inv) ~= 'table' then inv = Inventory(inv) end
count = math.floor(count + 0.5)
local success, reason = false, nil
local success, resp

if item then
if inv then
metadata, count = Items.Metadata(inv.id, item, metadata or {}, count)
Expand Down Expand Up @@ -570,23 +571,27 @@ function Inventory.AddItem(inv, item, count, metadata, slot, cb)
if slot then
Inventory.SetSlot(inv, item, count, metadata, slot)
inv.weight = inv.weight + (item.weight + (metadata?.weight or 0)) * count
success = true

if cb then
success = true
resp = inv.items[slot]
end

if inv.type == 'player' then
if shared.framework == 'esx' then Inventory.SyncInventory(inv) end
TriggerClientEvent('ox_inventory:updateSlots', inv.id, {{item = inv.items[slot], inventory = inv.type}}, {left=inv.weight, right=inv.open and Inventories[inv.open]?.weight}, count, false)
end
else
reason = 'inventory_full'
resp = cb and 'inventory_full'
end
else
reason = 'invalid_inventory'
resp = cb and 'invalid_inventory'
end
else
reason = 'invalid_item'
resp = cb and 'invalid_item'
end

if cb then cb(success, reason) end
if cb then cb(success, resp) end
end
exports('AddItem', Inventory.AddItem)

Expand Down

0 comments on commit 5ff6994

Please sign in to comment.