Skip to content

Commit 4981885

Browse files
authored
Clarify event hook descriptions and add post-hook info
Updated terminology and added post-hook event section. Signed-off-by: Linden <65407488+thelindat@users.noreply.github.com>
1 parent 91c2858 commit 4981885

1 file changed

Lines changed: 67 additions & 11 deletions

File tree

pages/ox_inventory/Functions/Server/Hooks.mdx

Lines changed: 67 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
1+
import { Callout } from 'nextra/components'
2+
13
# Hooks
24

3-
Event hooks allow 3rd party resources to define new behaviour without modifying the inventory code directly.
5+
Event hooks allow third-party resources to define new behaviour without modifying the inventory code directly.
46

57
## registerHook
68

79
```lua
810
exports.ox_inventory:registerHook(eventName, function(payload) end, options)
911
```
1012

13+
<Callout type="error">
14+
Hook callbacks are intended for validation only and should avoid side effects such as modifying data, writing to a database, or triggering additional operations.
15+
16+
Because actions may still be in progress or may fail, modifying item or inventory state before completion can lead to race conditions or inconsistent behavior.
17+
18+
To avoid issues, perform state changes or follow-up logic in post-hook events.
19+
</Callout>
20+
1121
- eventName: `string`
12-
- payload: `table`
22+
- callback: `function(payload: table)`
1323
- options?: `table`
1424
- print?: `boolean`
1525
- Print to the console when triggering the event.
@@ -22,7 +32,7 @@ exports.ox_inventory:registerHook(eventName, function(payload) end, options)
2232

2333
Return:
2434

25-
- hookId: `number`
35+
- hookId: `string`
2636

2737
### swapItems
2838

@@ -40,7 +50,8 @@ By returning `false`, you can cancel the action and revert the inventory state.
4050
- toSlot?: `table` or `number`
4151
- count: `number`
4252

43-
**Example**
53+
<details>
54+
<summary><b>Example</b></summary>
4455

4556
Blacklists "water" from being moved into or from gloveboxes and trunks.
4657

@@ -60,6 +71,8 @@ end, {
6071
})
6172
```
6273

74+
</details>
75+
6376
### openInventory
6477

6578
- Payload: `table`
@@ -70,7 +83,8 @@ end, {
7083
Triggered when a player tries to open a secondary inventory.
7184
By returning `false`, you can cancel the action and keep the player's inventory closed.
7285

73-
**Example**
86+
<details>
87+
<summary><b>Example</b></summary>
7488

7589
Disables gloveboxes and trunks.
7690

@@ -87,6 +101,8 @@ end, {
87101
})
88102
```
89103

104+
</summary>
105+
90106
### openShop
91107

92108
- Payload: `table`
@@ -103,7 +119,8 @@ end, {
103119
Triggered when a player tries to open a shop inventory.
104120
By returning `false`, you can cancel the action and keep the shop inventory closed.
105121

106-
**Example**
122+
<details>
123+
<summary><b>Example</b></summary>
107124

108125
Disable General stores.
109126

@@ -119,6 +136,8 @@ end, {
119136
})
120137
```
121138

139+
</details>
140+
122141
### createItem
123142

124143
- Payload: `table`
@@ -130,7 +149,10 @@ end, {
130149
Triggered when an item is created, either by buying it, using AddItem, or when converting inventory data.
131150
By returning a table you can modify or replace the metadata given to an item.
132151

133-
**Example**
152+
Post-hook events are not necessary for this event, but are still recommended
153+
154+
<details>
155+
<summary><b>Example</b></summary>
134156

135157
Sets the label for "water" to "Mineral Water".
136158

@@ -148,6 +170,8 @@ end, {
148170
})
149171
```
150172

173+
</details>
174+
151175
### buyItem
152176

153177
- Payload: `table`
@@ -165,7 +189,8 @@ end, {
165189

166190
Triggered when an item is about to be purchased and can return `false` to prevent the transaction.
167191

168-
**Example**
192+
<details>
193+
<summary><b>Example</b></summary>
169194

170195
Prevents players from purchasing items at General stores.
171196

@@ -182,6 +207,8 @@ end, {
182207

183208
```
184209

210+
</details>
211+
185212
### craftItem
186213

187214
- Payload: `table`
@@ -198,7 +225,8 @@ end, {
198225
- toInventory: `number`
199226
- toSlot: `number`
200227

201-
**Example**
228+
<details>
229+
<summary><b>Example</b></summary>
202230

203231
Prevent lockpicks from being crafted by players.
204232

@@ -215,6 +243,8 @@ end, {
215243

216244
```
217245

246+
</details>
247+
218248
### usingItem
219249

220250
Triggered when an item is about to be used.
@@ -234,7 +264,8 @@ By returning `false`, you can fully prevent it from being used.
234264
- metadata: `table`
235265
- consume: `number`
236266

237-
**Example**
267+
<details>
268+
<summary><b>Example</b></summary>
238269

239270
Prevent the player from using the "water" item.
240271

@@ -250,6 +281,8 @@ end, {
250281
})
251282
```
252283

284+
</details>
285+
253286
## removeHooks
254287

255288
Removes a hook created by the invoking resource with the the specified id.
@@ -259,4 +292,27 @@ If no id is specified then all hooks registered by the resource are removed.
259292
exports.ox_inventory:removeHooks(id)
260293
```
261294

262-
- id?: `number`
295+
- id?: `string`
296+
297+
## Post-hook events
298+
299+
Post-hook events let you register an event handler that runs after all hooks have finished and the action has either completed successfully or been rejected.
300+
301+
Use the `hookId` returned by registerHook with AddEventHandler to handle post-hook logic.
302+
303+
```lua
304+
---Use filter logic so only relevant inventories trigger the post-hook event.
305+
local hookId = exports.ox_inventory:registerHook('swapItems', nil, {
306+
inventoryFilter = {
307+
'^glove[%w]+',
308+
'^trunk[%w]+',
309+
}
310+
})
311+
312+
---Print everytime an item is moved to or from a vehicle inventory.
313+
---Success will be false if using a hook callback for validation, or if the action was rejected internally by ox_inventory.
314+
AddEventHandler(hookId, function(success, payload)
315+
print(hookId, success)
316+
lib.print.info(payload)
317+
end)
318+
```

0 commit comments

Comments
 (0)