Hooks
Event hooks allow 3rd party resources to define new behaviour without modifying the inventory code directly.
All hooks work server-side only.
Usage is identical to ox_inventory, with additional features included.
As with ox_inventory, you can cancel the operation or change the item's data using the return command.
RegisterHook
exports["tgiann-inventory"]:RregisterHook(eventName, function(payload) end, options)eventName:
stringpayload:
tableoptions?:
tableprint?:
booleanPrint to the console when triggering the event.
itemFilter?:
{ [string]: true }The event will only trigger for items defined as keys in a set.
inventoryFilter?:
string[]The event will only trigger for inventories that match one of the patterns in the array.
toInventoryFilter?:
string[]The event will only trigger for other(right Inventory) inventories that match one of the patterns in the array.
fromInventoryFilter?:
string[]The event will only trigger for player(left Inventory) inventories that match one of the patterns in the array.
typeFilter?:
{ [string]: true }The event will only trigger for inventories with one of the provided types (e.g. 'player', 'stash')
Return:
hookId:
number
swapItems
Triggered when moving any item from one slot to another, or when "giving" an item.
By returning false, you can cancel the action and revert the inventory state.
Payload:
tablesource:
numberaction:
'move'or'stack'or'swap'fromInventory:
tableorstringornumbertoInventory:
tableorstringornumberfromType:
stringtoType:
stringfromSlot:
tabletoSlot?:
numbercount:
number
Example
Blacklists "water" from being moved into or from gloveboxes and trunks.
local hookId = exports.ox_inventory:registerHook('swapItems', function(payload)
print(json.encode(payload, { indent = true }))
return false
end, {
print = true,
itemFilter = {
water = true,
},
inventoryFilter = {
'^glove[%w]+',
'^trunk[%w]+',
}
})openInventory
Payload:
tablesource:
numberinventoryId:
stringinventoryType:
string
Triggered when a player tries to open a secondary inventory.
Example
local hookId = exports.ox_inventory:registerHook('openInventory', function(payload)
print(json.encode(payload, { indent = true }))
end, {
print = true,
inventoryFilter = {
'^glove[%w]+',
'^trunk[%w]+',
}
})createItem
Payload:
tableinventoryId?:
numberorstringmetadata:
tableitem:
tablecount:
number
Triggered when an item is created, either by buying it, using AddItem, or when converting inventory data.
Example
local hookId = exports.ox_inventory:registerHook('createItem', function(payload)
print(json.encode(payload, { indent = true }))
end, {
print = true,
itemFilter = {
water = true
}
})buyItem
Payload:
tablesource:
numbershopType:
stringshopId:
numbertoInventory:
numbertoSlot:
numberitemName:
stringmetadata:
tablecount:
numberprice:
numbertotalPrice:
numbercurrency?:
string
Triggered when an item is about to be purchased
Example
local hookId = exports.ox_inventory:registerHook('buyItem', function(payload)
print(json.encode(payload, { indent = true, sort_keys = true }))
end, {
print = true,
itemFilter = {
water = true
},
})usingItem
Payload:
tablesource:
numberinventoryId:
stringitem:
tableconsume:
table
Example
local hookId = exports.ox_inventory:registerHook('usingItem', function(payload)
print(json.encode(payload, { indent = true, sort_keys = true }))
end, {
print = true,
itemFilter = {
water = true
},
})RemoveHooks
Removes a hook created by the invoking resource with the the specified id. If no id is specified then all hooks registered by the resource are removed.
exports["tgiann-inventory"]:RemoveHooks(id)id?:
number
Last updated