diff --git a/docs/ox_inventory/Functions/Server/Hooks.md b/docs/ox_inventory/Functions/Server/Hooks.md new file mode 100644 index 00000000..605feb7b --- /dev/null +++ b/docs/ox_inventory/Functions/Server/Hooks.md @@ -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]() 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". diff --git a/docs/ox_inventory/Functions/Server/Inventory/AddItem.md b/docs/ox_inventory/Functions/Server/Inventory/AddItem.md index cf2db810..bbe945eb 100644 --- a/docs/ox_inventory/Functions/Server/Inventory/AddItem.md +++ b/docs/ox_inventory/Functions/Server/Inventory/AddItem.md @@ -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 diff --git a/docs/ox_inventory/Functions/Server/Inventory/RemoveItem.md b/docs/ox_inventory/Functions/Server/Inventory/RemoveItem.md index ff376eec..0670b959 100644 --- a/docs/ox_inventory/Functions/Server/Inventory/RemoveItem.md +++ b/docs/ox_inventory/Functions/Server/Inventory/RemoveItem.md @@ -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) -``` \ No newline at end of file +-- Removes 2 water from the glovebox for the given plate. +local success = exports.ox_inventory:RemoveItem('gloveVGH283', 'water', 2) +``` diff --git a/docs/ox_inventory/Guides/shops.md b/docs/ox_inventory/Guides/shops.md new file mode 100644 index 00000000..68fdf21c --- /dev/null +++ b/docs/ox_inventory/Guides/shops.md @@ -0,0 +1,86 @@ +--- +title: Creating shops +--- + +import IconButton from '@site/src/components/IconButton' +import { BsGithub } from 'react-icons/bs' + +Builtin hops are defined in [data/shops.lua](https://github.com/overextended/ox_inventory/blob/main/data/shops.lua), and more can be added here to benefit from the built-in markers or zones support. + +## Shop definition + +```lua +{ + General = { + name = 'Shop', + blip = { + id = 59, + colour = 69, + scale = 0.8 + }, + inventory = { + { name = 'burger', price = 10 }, + { name = 'water', price = 10 }, + { name = 'cola', price = 10 }, + }, + locations = { + vec3(25.7, -1347.3, 29.49), + }, + targets = { + { + loc = vec3(25.06, -1347.32, 29.5), + length = 0.7, + width = 0.5, + heading = 0.0, + minZ = 29.5, + maxZ = 29.9, + distance = 1.5 + }, + } + } +} +``` + +luke i hate this pls fix + +- name: `string` + - The label to display when the shop is open. +- blip?: `table` + - Creates a blip with the given settings. Leave it undefined for no blip to be created. +- groups?: `table` + - Key-value pairs of job name and minimum grade to access the shop. + - `{["police"] = 0, ["ambulance"] = 2}` +- inventory: `table` + - { name: `string`, price: `number`, currency?: `string`, count?: `number`, license?: `string`, metadata?: `table`, grade?: `number` } +- locations?: `vector3[]` + - An array of coordinates to create unique instances of the shop archetype at, using markers. +- targets?: `table[]` + - An array of BoxZone settings to create unique instances of the shop archetype at. +- model?: `number[]` + - An array of models that can be targetted to open a shop. Used for vending machines. + +Targets and model are only available when using a targeting resource like ox_target. + +## Register during runtime + +Shops can be added using `exports.ox_inventory:RegisterShop` on the server, however they cannot utilise any client-only features. +- Blips, markers, and zones will not be created. +- Must use "locations" and not "targets" to define each shop using the archetype. + +**Example:** +```lua +exports.ox_inventory:RegisterShop('TestShop', { + name = 'Test shop', + inventory = { + { name = 'burger', price = 10 }, + { name = 'water', price = 10 }, + { name = 'cola', price = 10 }, + }, + locations = { + vec3(223.832962, -792.619751, 30.695190), + }, + groups = { + police = 0 + }, +}) +```