Hooks
Event hooks allow 3rd party resources to define new behaviour without modifying the inventory code directly.
registerHook
exports.ox_inventory:registerHook(eventName, function(payload) end, options)
- eventName:
string
- payload:
table
- options?:
table
- print?:
boolean
- Print 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 (opens in a new tab) in the array.
- typeFilter?:
{ [string]: true }
- The event will only trigger for inventories with one of the provided types (e.g. 'player', 'stash')
- print?:
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:
table
- source:
number
- action:
'move'
or'stack'
or'swap'
or'give'
- fromInventory:
table
orstring
ornumber
- toInventory:
table
orstring
ornumber
- fromType:
string
- toType:
string
- fromSlot:
table
- toSlot?:
table
ornumber
- count:
number
- source:
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:
table
- source:
number
- inventoryId:
number
orstring
- inventoryType:
string
- source:
Triggered when a player tries to open a secondary inventory.
By returning false
, you can cancel the action and keep the player's inventory closed.
Example
Disables gloveboxes and trunks.
local hookId = exports.ox_inventory:registerHook('openInventory', function(payload)
print(json.encode(payload, { indent = true }))
return false
end, {
print = true,
inventoryFilter = {
'^glove[%w]+',
'^trunk[%w]+',
}
})
createItem
- Payload:
table
- inventoryId?:
number
orstring
- metadata:
table
- item:
table
- count:
number
- inventoryId?:
Triggered when an item is created, either by buying it, using AddItem, or when converting inventory data.
By returning a table you can modify or replace the metadata given to an item.
Example
Sets the label for "water" to "Mineral Water".
local hookId = exports.ox_inventory:registerHook('createItem', function(payload)
print(json.encode(payload, { indent = true }))
local metadata = payload.metadata
metadata.label = 'Mineral Water'
return metadata
end, {
print = true,
itemFilter = {
water = true
}
})
buyItem
- Payload:
table
- source:
number
- shopType:
string
- shopId:
number
- toInventory:
number
- toSlot:
number
- itemName:
string
- metadata:
table
- count:
number
- price:
number
- totalPrice:
number
- currency?:
string
- source:
Triggered when an item is about to be purchased and can return false
to prevent the transaction.
Example
Prevents players from purchasing items at General stores.
local hookId = exports.ox_inventory:registerHook('buyItem', function(payload)
print(json.encode(payload, { indent = true, sort_keys = true }))
return false
end, {
print = true,
itemFilter = {
water = true
},
})
craftItem
- Payload:
table
- source:
number
- benchId:
number
- benchIndex:
number
- recipe:
table
- count:
number
- duration:
number
- ingredients:
table<string, number>
- name:
string
- slot:
number
- weight:
number
- count:
- toInventory:
number
- toSlot:
number
- source:
Example
Prevent lockpicks from being crafted by players.
local hookId = exports.ox_inventory:registerHook('craftItem', function(payload)
print(json.encode(payload, { indent = true, sort_keys = true }))
return false
end, {
print = true,
itemFilter = {
lockpick = 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.ox_inventory:removeHooks(id)
- id?:
number