Skip to content

Commit

Permalink
feat(server/inventory): GetSlotForItem
Browse files Browse the repository at this point in the history
  • Loading branch information
thelindat committed Apr 26, 2023
1 parent ecf8675 commit 68ac807
Showing 1 changed file with 38 additions and 7 deletions.
45 changes: 38 additions & 7 deletions modules/inventory/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1944,18 +1944,49 @@ end

exports('ClearInventory', Inventory.Clear)

---@param inv inventory
---@return integer?
function Inventory.GetEmptySlot(inv)
inv = Inventory(inv) --[[@as OxInventory]]
local inventory = Inventory(inv)

if inv then
local items = inv.items
if not inventory then return end

for i = 1, inv.slots do
if not items[i] then
return i
end
local items = inventory.items

for i = 1, inventory.slots do
if not items[i] then
return i
end
end
end

---@param inv inventory
---@param itemName string
---@param metadata any
function Inventory.GetSlotForItem(inv, itemName, metadata)
local inventory = Inventory(inv)
local item = Items(itemName) --[[@as OxServerItem?]]

if not inventory or not item then return end

if type(metadata) ~= 'table' then
metadata = metadata and { type = metadata or nil }
end

local items = inventory.items
local emptySlot

for i = 1, inventory.slots do
local slotData = items[i]

if item.stack and slotData and slotData.name == item.name and table.matches(slotData.metadata, metadata) then
return i
elseif not item.stack and not slotData and not emptySlot then
emptySlot = i
end
end

return emptySlot
end

local function prepareSave(inv)
Expand Down

0 comments on commit 68ac807

Please sign in to comment.