attempt to make luke happy

Lazy and poorly "typed" shops guide.
Update AddItem and RemoveItem typings and improve examples.
Add hooks.
This commit is contained in:
Linden committed 2022-11-07 08:26:27 +11:00
1 parent 9f803f6a99
commit 2bfdefcc9d
4 files changed
+253 -42

No files matched your search

+106
View File
@@ -0,0 +1,106 @@
Event hooks allow 3rd party resources to define new behaviour without modifying the inventory code directly.
```lua
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](<http://www.easyuo.com/openeuo/wiki/index.php/Lua_Patterns_and_Captures_(Regular_Expressions)>) in the array.
## 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:**
- source: `number`
- action: `move` or `stack` or `swap` or `give`
- fromInventory: `number` or `string`
- toInventory:` number` or `string`
- fromType: `string`
- toType: `string`
- fromSlot: `table`
- toSlot?: `table` or `number`
- count: `number`
**Example:**
```lua
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]+',
}
})
```
Blacklists "water" from being moved into or from gloveboxes and trunks.
## openInventory
**Payload:**
- source: `number`
- inventoryId: `number` or `string`
- inventoryType: `string`
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.
```lua
exports.ox_inventory:registerHook('openInventory', function(payload)
print(json.encode(payload, { indent = true }))
return false
end, {
print = true,
inventoryFilter = {
'^glove[%w]+',
'^trunk[%w]+',
}
})
```
Disables gloveboxes and trunks.
## createItem
**Payload:**
- inventoryId?: `number` or `string`
- metadata: `table`
- item: `table`
- count: `number`
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.
```lua
exports.ox_inventory:registerHook('openInventory', function(payload)
print(json.encode(payload, { indent = true }))
local metadata = payload.metadata
metadata.label = 'Mineral Water'
return metadata
end, {
print = true,
itemFilter = {
water = true
}
})
```
Sets the label for "water" to "Mineral Water".
@@ -3,43 +3,51 @@ Adds an item into the specified inventory.
Should be used alongside [CanCarryItem](./CanCarryItem.md) otherwise, the maximum weight may be exceeded.
```lua
-- inv: string or number
-- item: table or string
-- count: number
-- metadata: table or string (optional)
-- slot: number (optional)
-- cb: function (optional)
exports.ox_inventory:AddItem(inv, item, count, metadata, slot, cb)
```
**Example:**
```lua
exports.ox_inventory:AddItem(1, 'bread', 4, nil, nil, function(success, reason)
if success then
print(json.encode(reason, {indent=true}))
--[[
{
"metadata": [],
"label": "Bread",
"slot": 1,
"stack": true,
"close": true,
"name": "bread",
"count": 1,
"weight": 150
}
]]
else
print(success)
-- nil
- inv: `table` or `string` or `number`
- The inventory's unique id, or a table with the id and owner.
- playerId: `1`, inventoryId: `gloveVGH283`, `{ id = 'personallocker', owner = 'license:xxxxxx'}`
- item: `string`
- The name of the item to add to the target.
- count: `number`
- The number of items to add.
- metadata?: `table` or `string`
- A table of unique data to attach to the item object. A string will create a table with the "type" field.
- slot?: `number`
- A specific slot to add the item to. If the slot is invalid, the first available slot will be used instead.
- cb?: function(success: `boolean`, response?: `string`)
print(reason)
-- for example if all slots are taken it will give us "inventory_full"
end
end)
Returns `success, response` if cb is undefined, otherwise they are used in the callback only.
**Example:**
```lua
local success, response = exports.ox_inventory:AddItem('gloveVGH283', 'bread', 4)
if not success then
-- if no slots are available, the value will be "inventory_full"
return print(response)
end
print(json.encode(response, {indent=true}))
--[[
{
"metadata": [],
"label": "Bread",
"slot": 1,
"stack": true,
"close": true,
"name": "bread",
"count": 1,
"weight": 150
}
]]
```
**Possible value of the "reason" argument for the callback function:**
+ "invalid_item": the item doesn't exist
+ "invalid_inventory": the inventory doesn't exist
+ "inventory_full": no free slots
**Possible value of the "response" argument, on failure:**
- "invalid_item": the item doesn't exist
- "invalid_inventory": the inventory doesn't exist
- "inventory_full": no free slots
@@ -1,16 +1,27 @@
Removes the specified item from the specified inventory.
```lua
-- inv: string or table
-- item: table or string
-- count: number
-- metadata: table or string (optional)
-- slot: number (optional)
exports.ox_inventory:RemoveItem(inv, item, count, metadata, slot)
```
- inv: `table` or `string` or `number`
- The inventory's unique id, or a table with the id and owner.
- playerId: `1`, inventoryId: `gloveVGH283`, `{ id = 'personallocker', owner = 'license:xxxxxx'}`
- item: `string`
- The name of the item to remove from the target.
- count: `number`
- The number of items to remove.
- metadata?: `table` or `string`
- Only remove items with matching metadata properties.
- slot?: `number`
- A specific slot to remove the item from. If the slot is invalid, the first available slot will be used instead.
- cb?: function(success: `boolean`, response?: `string`)
Returns `success` as a boolean, stating whether the item was successfully removed.
**Example:**
```lua
-- Removes 1 water from the inventory
exports.ox_inventory:RemoveItem(source, 'water', 1)
```
-- Removes 2 water from the glovebox for the given plate.
local success = exports.ox_inventory:RemoveItem('gloveVGH283', 'water', 2)
```