Custom Inventory
If you use an inventory that is not supported out of the box, set Config.Inventory = "custom" and fill in the stub files.
Setup
-- editable/shared/config.lua
Config.Inventory = "custom"
Two stub files are then loaded automatically:
| File | Side |
|---|---|
editable/custom/inventory_server.lua | Server |
editable/custom/inventory_client.lua | Client |
Server Stubs
Open editable/custom/inventory_server.lua and implement each function:
CustomInventory.Server = {}
-- Add `count` of `name` to the player's inventory.
-- metadata and slot are optional.
-- Return true on success.
CustomInventory.Server.AddItem = function(src, name, count, metadata, slot)
return false
end
-- Remove `count` of `name` from the player's inventory.
-- Return true on success.
CustomInventory.Server.RemoveItem = function(src, name, count, metadata, slot)
return false
end
-- Return the total count of `name` in the player's inventory.
CustomInventory.Server.GetItemCount = function(src, name, metadata)
return 0
end
-- Return the full inventory as { [itemName] = count }.
CustomInventory.Server.GetItems = function(src)
return {}
end
Client Stubs
Open editable/custom/inventory_client.lua and implement each function:
CustomInventory.Client = {}
-- Return the count of `item` in the local player's inventory.
CustomInventory.Client.GetItemCount = function(item)
return 0
end
-- Return the local player's full inventory as { [itemName] = count }.
CustomInventory.Client.GetItems = function()
return {}
end
Example
-- inventory_server.lua
CustomInventory.Server.AddItem = function(src, name, count, metadata, slot)
local ok = exports['my-inventory']:addItemToPlayer(src, name, count)
return ok == true
end
CustomInventory.Server.GetItemCount = function(src, name)
return exports['my-inventory']:getItemCount(src, name) or 0
end
CustomInventory.Server.GetItems = function(src)
local raw = exports['my-inventory']:getAllItems(src) or {}
local result = {}
for _, item in pairs(raw) do
if item.name then
result[item.name] = (result[item.name] or 0) + (item.count or 0)
end
end
return result
end
Once implemented, all standard koja-lib inventory calls route through your functions:
KOJA.Server.addInventoryItem(source, 'bread', 3)
KOJA.Server.HasItem(source, 'bread')
KOJA.Server.GetPlayerInventory(source)