Adding and removing items
Access player inventories and create events or exports for common item operations.
Inventory 2.0 does not include ready-made exports for these operations. Use the existing inventory class functions to create the events or exports your server needs.
Inventory class functions
All functions attached to the inventory class are defined in sv_main.lua. The following example exposes the existing getItemCount class function as an export.
Read an item count
Resolve the player's inventory and return the quantity of an item.
exports('getItemCount', function(source, item)
local identifier = Config.getIdentifier(source)
local Inventory = AquiverInventories[identifier]
if Inventory then
return Inventory.getItemCount(item)
end
end)
Add and remove items with events
These server events resolve the player's inventory before adding or removing an item. The cb argument is passed through the event and is not a framework callback.
Item events
Register server events for adding and removing player items.
RegisterNetEvent('AquiverInventory:Player:addItem')
RegisterNetEvent('AquiverInventory:Player:removeItem')
AddEventHandler('AquiverInventory:Player:addItem', function(item, count, vars, cb)
local source = source
local identifier = Config.getIdentifier(source)
if AquiverInventories[identifier] then
AquiverInventories[identifier].addItem(item, count, vars, function(response)
if cb then cb(response) end
end)
end
end)
AddEventHandler('AquiverInventory:Player:removeItem', function(item, count, cb)
local source = source
local identifier = Config.getIdentifier(source)
if AquiverInventories[identifier] then
AquiverInventories[identifier].removeItem(item, count, function(response)
if cb then cb(response) end
end)
end
end)
Add items with an export
The same inventory class function can be exposed as an export instead of an event.
Add an item
Create an export that adds an item to a player's inventory.
exports('addItemPlayer', function(source, item, count, vars, cb)
local identifier = Config.getIdentifier(source)
local Inventory = AquiverInventories[identifier]
if Inventory then
Inventory.addItem(item, count, vars, function(response)
if cb then cb(response) end
end)
end
end)