Hooks

Hooks are a way to modify the behavior of the inventory system. They are registered on the server and can be used to modify the behavior of the inventory system, for example to prevent players from moving items to a specific inventory. There are some examples of hooks in jaksam_inventory/_hooks folder

Use Case Examples

  • Prevent players from stealing items that have the 'sole_owner' metadata field (e.g., VIP items)

  • Prevent players from moving police weapons into their personal inventory

  • Allow only one backpack per player inventory

  • Crafting items by dragging a specific item over another item (for example dragging bread on meat can make a sandwich)

  • Block item usage when player is handcuffed or in specific zones

  • Track item usage statistics and achievements

  • Prevent using certain items while in vehicles

API Functions

Register a Hook

exports['jaksam_inventory']:registerHook(eventName, callback, options, priority)

Parameters:

  • eventName (string): The name of the hook event to listen for (list of available events below)

  • callback (function): The function to execute when the hook is triggered

  • options (table, optional): Filters and configuration options (list of available options below)

  • priority (number, optional): Execution priority (higher numbers execute first, default: 0)

Returns:

  • hookId (string): Unique identifier for the registered hook (used to unregister the hook)

Unregister a Hook

Parameters:

  • hookId (string): The unique identifier returned when registering the hook

Unregister All Resource Hooks

Parameters:

  • resourceName (string): Name of the resource to unregister all hooks for

Options Parameter

The options parameter accepts a table with filters to optimize performance:

Common Filters (All Events)

Inventory Filters (onItemAdded, onItemRemoved)

Transfer Filters (onItemTransferred only)

Available Hook Events

onItemAdded

Triggered when an item is added to an inventory.

Payload:

onItemRemoved

Triggered when an item is removed from an inventory.

Payload:

onItemTransferred

Triggered when an item is transferred between inventories (including intra-inventory moves)

Payload:

onPreUseItem

Triggered BEFORE an item is used (before consume, animations, and delays). This hook can cancel item usage

Execution Order: After STATIC_ITEM.canUse and oxServerExport 'usingItem', before consume

Payload:

Note: This hook can prevent item usage by returning false. Useful for global item usage restrictions (e.g., handcuffed players, vehicle restrictions, zone restrictions)

onPostUseItem

Triggered AFTER an item has been used (after consume, animations, delays, and all callbacks)

Execution Order: At the very end of the item usage process, after oxServerExport 'usedItem'

Payload:

Note: This hook is notification-only and cannot cancel item usage. Useful for logging, statistics, achievements, and triggering external systems

Hook Behavior

  • Priority: Higher numbers execute first (default: 0)

  • Return Values:

    • return nil or return true: Allow the action to continue

    • return false, "message", "notifyType": Prevents the action and stops further hook execution

    • The message parameter is optional and will be displayed to the player

    • The notifyType parameter is optional and can be "error", "success", "info"

Quick Examples

Block Police Weapons in Player Inventory

One Backpack Per Player

Simple Crafting (Drag Items Together)

Filter by Specific Inventory Name

Block Item Usage When Handcuffed

Block Food Usage in Vehicles

Log All Item Usage

Track Food Consumption Statistics

Best Practices

  1. Use Filters: Always use appropriate filters to avoid unnecessary hook executions

  2. Early Returns: Use early returns to exit hooks when conditions aren't met

  3. Performance: Keep hook logic lightweight to avoid impacting inventory performance

Last updated

Was this helpful?