> For the complete documentation index, see [llms.txt](https://devix.gitbook.io/devix/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://devix.gitbook.io/devix/devix-inventory/item-use-add-remove.md).

# Item: Use, Add, Remove

{% stepper %}
{% step %}

### 1. `client.add` — When an item is added to the inventory

After the server calls `AddItem` (or similar), it sends the new count via `devix-inventory:client:itemCountChanged`. The **config** `client.add` is then run on the client.

#### Usage

```lua
['my_item'] = {
    name   = 'my_item',
    label  = 'My Item',
    -- ... other fields ...
    client = {
        add = {
            event  = 'my_resource:client:onItemAdded',   -- optional: fired via TriggerEvent
            action = function(newTotal, itemName)        -- optional: called directly
                print(('Item added: %s, total: %s'):format(itemName, newTotal))
            end,
        },
    },
},
```

* **Event only:** `event = 'eventName'` → `TriggerEvent(eventName, itemName, newTotal)` is called.
* **Action only:** `action = function(newTotal, itemName) ... end` is called.
* **Both** can be defined; both will run.

#### Legacy format (single event name)

```lua
client = {
    addEvent = 'my_resource:client:onItemAdded',
},
```

In this case only `TriggerEvent(addEvent, itemName, newTotal)` runs.
{% endstep %}

{% step %}

### 2. `client.remove` — When an item is removed from the inventory

Again via `devix-inventory:client:itemCountChanged` after the server removes the item; when the **new count is 0**, `client.remove` is used.

#### Usage

```lua
client = {
    remove = {
        event  = 'my_resource:client:onItemRemoved',
        action = function(newTotal, itemName)
            -- newTotal is 0 here (item fully removed)
            print(('Item removed: %s'):format(itemName))
        end,
    },
},
```

* **Event only:** `TriggerEvent(event, itemName, 0)`.
* **Action only:** `action(0, itemName)`.

#### Legacy format

```lua
client = {
    removeEvent = 'my_resource:client:onItemRemoved',
},
```

{% endstep %}

{% step %}

### 3. `client.use` — When an item is used (client)

When the player clicks "Use" on an item in the inventory, the server triggers `devix-inventory:client:autoItemUse` on the client with `itemData`. The **config** `client.use` is then run.

#### Usage (working format)

```lua
client = {
    use = {
        event  = 'devix-inventory:client:autoItemUse',   -- this event is what the server triggers; required for action to run
        action = function(itemData, slot)
            -- itemData: { name, amount, slot, info, ... }
            print('use', json.encode(itemData), 'slot', slot)

            local payload = itemData
            if type(payload) ~= 'table' then
                payload = { slot = slot, name = 'my_item' }
            end

            -- Example: call another resource's export
            pcall(function()
                exports['my_resource']['useMyItem'](payload)
            end)
        end,
    },
},
```

* The part **actually called at runtime** is `client.use.action(itemData, itemData.slot)`.
* The `event` field tells the server which client event to trigger; normally use `devix-inventory:client:autoItemUse`.
  {% endstep %}

{% step %}

### 4. `client.useExport` — When an item is used (client, alternative format)

Same behaviour as `client.use`, with a different key name. **Runs at runtime**: the server triggers the event and the client runs the action.

#### Usage

```lua
client = {
    useExport = {
        event  = 'wasabi_wallet.openWallet',   -- server triggers this event on the client
        action = function(itemData, slot)
            local payload = itemData
            if type(payload) ~= 'table' then payload = { slot = slot, name = 'wallet' } end
            if GetResourceState('wasabi_wallet') == 'started' then
                pcall(function() exports['wasabi_wallet']['openWallet'](payload) end)
            end
        end,
    },
},
```

* **Server:** When the item is used, `client.useExport.event` is read as `clientUseEvent` and `TriggerClientEvent(clientUseEvent, src, itemData)` is fired.
* **Client:** When `devix-inventory:client:autoItemUse` is received, **`client.useExport.action`** is run if the item has no `client.use.action`.
* Clicking "Use" in the inventory UI also triggers both the event and the action.

For item use you can use: **`client.use`**, **`client.useExport`** (event + action), or **`server.useExport`**.
{% endstep %}

{% step %}

### 5. `server.useExport` — When an item is used (server only)

Use this when handling the item use entirely on the server, without running client use logic.

#### Usage

```lua
['nitrous_bottle'] = {
    name     = 'nitrous_bottle',
    label    = 'Nitrous Bottle',
    useable  = true,
    -- ...
    server = {
        useExport = 'werks_nitroloader.useNitrousBottle',
    },
},
```

In the `werks_nitroloader` resource:

```lua
exports('useNitrousBottle', function(src, itemData)
    -- src: player server id
    -- itemData: item data sent by the server
    print('useNitrousBottle', src, json.encode(itemData))
end)
```

* When the player uses the item, the server calls `exports['werks_nitroloader']['useNitrousBottle'](src, itemData)` directly.
* You do not need a `client.use` for this (you can still add one if you want both).
  {% endstep %}

{% step %}

### 6. Summary table

| Goal                                         | Where to define                                         | Example                                                                                                                                                                   |
| -------------------------------------------- | ------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Run something on client when item is added   | `client.add` (event and/or action)                      | `add = { event = '...', action = function(total, name) end }`                                                                                                             |
| Run something on client when item is removed | `client.remove` (event and/or action)                   | `remove = { event = '...', action = function(total, name) end }`                                                                                                          |
| Run something on client when item is used    | `client.use` **or** `client.useExport` (event + action) | `use = { event = 'devix-inventory:client:autoItemUse', action = function(itemData, slot) end }` or `useExport = { event = '...', action = function(itemData, slot) end }` |
| Handle item use on server only               | `server.useExport`                                      | `server = { useExport = 'resource.exportName' }`                                                                                                                          |
| {% endstep %}                                |                                                         |                                                                                                                                                                           |

{% step %}

### 7. When each event is triggered

* **`devix-inventory:client:itemCountChanged`**\
  Fired after the server runs AddItem/RemoveItem to notify the new count. Config `client.add` / `client.remove` are evaluated inside this handler.
* **`devix-inventory:client:autoItemUse`**\
  Fired by the server when the player uses an item; argument is `itemData`. Config `client.use.action` or `client.useExport.action` is called from this handler.
* **`devix-inventory:client:autoItemAdd` / `autoItemRemove`**\
  These are currently used **only** in the import\_items (function callback) flow. The `client.add` / `client.remove` entries in `config_items.lua` are **not** invoked from these events; they only run via `itemCountChanged`.
  {% endstep %}

{% step %}

### 8. Relation to import\_items

When generating `config_items.lua` with `/importitems`:

* **add** → `client.add = { event = '...', action = function(total, itemName) ... end }` or `addEvent`
* **remove** → `client.remove = { ... }` or `removeEvent`
* **use** → `client.use = { event = '...', action = function(itemData, slot) ... end }` or `client.useExport = { event = '...', action = ... }` (both formats run at runtime)
* **server use** → `server = { useExport = 'resource.functionName' }`

You can use the same structures when writing config by hand; the examples in this doc apply directly to `config_items.lua`.
{% endstep %}
{% endstepper %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://devix.gitbook.io/devix/devix-inventory/item-use-add-remove.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
