# Examples and Commands

\-- Add supplies to a specific town

```lua
RegisterCommand('addSupplies', function(source, args, rawCommand)
    local town = args[1]  -- The town name passed as the first argument
    local suppliesAmount = tonumber(args[2])  -- The amount of supplies to add

    if town and suppliesAmount then
        TriggerServerEvent('codex-studios-supplies:city_resupplies', town, suppliesAmount)
    else
        CodexCore.Notification("Invalid parameters. Usage: /addSupplies [town] [amount]", 2000)
    end
end, false)
```

\-- Remove supplies from a specific town

```lua
-- Remove supplies from a specific town
RegisterCommand('removeSupplies', function(source, args, rawCommand)
    local town = args[1]  -- The town name passed as the first argument
    local suppliesAmount = tonumber(args[2])  -- The amount of supplies to remove

    if town and suppliesAmount then
        TriggerServerEvent('codex-studios-supplies:city_removesupplies', town, suppliesAmount)
    else
        CodexCore.Notification("Invalid parameters. Usage: /removeSupplies [town] [amount]", 2000)
    end
end, false)
```

\-- Refetch the supplies data for all towns

```lua
RegisterCommand('refetchSupplies', function()
    TriggerServerEvent('codex-studios-supplies:city_supplies_refetch')
end, false)
```

\-- Call the exported function to get the town supplies

```lua
RegisterCommand("townsupplies", function(source, args, rawCommand)
    -- Call the exported function to get the town supplies
    local townSupplies = exports['codex_supplies']:CodexStudiosGetTownSupplies()

    -- If town supplies are found, send them to the player in chat
    if #townSupplies > 0 then
        local message = "Town Supplies:\n"

        -- Iterate through the townSupplies table and format the message
        for _, townData in ipairs(townSupplies) do
            message = message .. townData.town .. ": " .. townData.supplies .. " supplies\n"
        end

        -- Send the formatted message to the player
        TriggerClientEvent('chat:addMessage', source, {
            args = { "^2[Town Supplies]^0", message }
        })
    else
        -- If no supplies data is found, notify the player
        TriggerClientEvent('chat:addMessage', source, {
            args = { "^1[Error]^0", "No town supplies data available." }
        })
    end
end, false)  -- The "false" makes the command not restricted to admins only
```

**How it works:**

* **Command registration**: The `RegisterCommand` function registers a command (`/townsupplies`) that players can type in the game.
* **Fetching the supplies**: When the command is triggered, it calls the exported function `CodexStudiosGetTownSupplies` from the resource using `exports['`codex\_supplies`']:CodexStudiosGetTownSupplies()`.
* **Message formatting**: If the town supplies are available, it formats a message displaying the supplies for each town and sends that message back to the player using `TriggerClientEvent('chat:addMessage', source, { args = {...} })`.
* **Error handling**: If no supplies data is found, it sends an error message back to the player.

**3. Client-side Message Display:**

The player will see a formatted chat message like this when they use the `/townsupplies` command:

```lua
[Town Supplies] 
TownName1: 50 supplies
TownName2: 120 supplies
TownName3: 30 supplies
```

If there’s no data:

```lua
[Error] No town supplies data available.
```

**4. How to Trigger the Command:**

In-game, players can simply type the following in the chat box to use the command:

```lua
/townsupplies
```

**Explanation of Commands:**

* **`/addSupplies [town] [amount]`**: Adds supplies to the specified town. It triggers the server event `codex-studios-supplies:city_resupplies`.
* **`/removeSupplies [town] [amount]`**: Removes supplies from the specified town. It triggers the server event `codex-studios-supplies:city_removesupplies`.
* **`/refetchSupplies`**: Refetches the supplies data from the server. It triggers the server event `codex-studios-supplies:city_supplies_refetch`.
* **`/exportSupplies`**: Exports the supplies data for all towns from the server. This fetches the supplies data and can be used to interact with it client-side.
