# Integration

### 📂 Tablet opening

**1️⃣ Opening with an item**

The resource configuration is pre-set for the tablet to open with an item by default. To make this feature work, you also need to create an item in your inventory with the ID of <mark style="color:blue;">app\_tablet</mark>.

{% tabs %}
{% tab title="ox\_inventory" %}
Add this section to <mark style="color:blue;">ox\_inventory/data/items.lua</mark>

```lua
['app_tablet'] = {
    label = 'Tablet',
    weight = 0,
    description = 'You can install different applications onto this.',
    stack = false,
    client = {
        export = 'rahe-tablet.tablet'
    }
}
```

Add the following image file as <mark style="color:blue;">ox\_inventory/web/images/app\_tablet.png</mark>

<https://media.rahe.dev/img/app_tablet.png>
{% endtab %}

{% tab title="qb-inventory" %}
Add the following line to <mark style="color:blue;">qb-core/shared/items.lua</mark>

```lua
['app_tablet'] = { ['name'] = 'app_tablet', ['label'] = 'Tablet', ['weight'] = 1000, ['type'] = 'item', ['image'] = 'app_tablet.png', ['unique'] = true, ['useable'] = true, ['shouldClose'] = true, ['combinable'] = nil, ['description'] = 'You can install different applications onto this.' },
```

Add the following image file as <mark style="color:blue;">qb-inventory/html/images/app\_tablet.png</mark>

<https://media.rahe.dev/img/app_tablet.png>
{% endtab %}

{% tab title="OTHER" %}
Create the item <mark style="color:blue;">app\_tablet</mark> in your inventory system and make it usable.&#x20;

If you are using qs-inventory or core\_inventory, you don't have to do anything extra.

If you are using any other inventory system, then on item use trigger:

&#x20;`exports['rahe-tablet']:tablet()`
{% endtab %}
{% endtabs %}

{% hint style="info" %}
If you didn't name the item <mark style="color:blue;">app\_tablet</mark>, adjust the <mark style="color:blue;">itemId</mark> in resource config accordingly.
{% endhint %}

***

**2️⃣** **Opening with a command**

If you don't wish the tablet to be an item, then in <mark style="color:blue;">resource/config/shared.lua</mark>, change the <mark style="color:blue;">openMethod</mark> option from <mark style="color:blue;">inventory</mark> to <mark style="color:blue;">command</mark>. Use the command <mark style="color:blue;">/tablet</mark> in-game to open the application tablet.

### 📑 Enabling application installs

By default, application installations are disabled, meaning all applications are visible on the tablet. However, you have the option to enable them:

* Navigate to <mark style="color:blue;">/resource/config/shared.lua</mark> and set <mark style="color:blue;">installEnabled</mark> to <mark style="color:blue;">true</mark>.
* In <mark style="color:blue;">config/client.lua</mark> set <mark style="color:blue;">requireInstall</mark> to <mark style="color:blue;">true</mark> for applications you want to require installation.

If you wish to configure options for RAHE resources, then head to <mark style="color:blue;">/resource/client/integrations.lua</mark>. In the default resource, all of our applications have <mark style="color:blue;">requireInstall</mark> set to <mark style="color:blue;">true</mark>, meaning they will need to be installed if you enable installations.

### 💿 Application installation

To install applications, you need to trigger the following <mark style="color:blue;">server-side</mark> export:

&#x20;<mark style="color:blue;">exports\['rahe-tablet']:installApplication(playerId, appId, slot)</mark>

This export will mark the application as installed, indicated by a progress circle. For example, to install our boosting application for player ID 1, who has a tablet in his inventory slot 4, you would use:

<mark style="color:blue;">exports\['rahe-tablet']:installApplication(1, 'rahe-boosting',  4)</mark>

You might wonder about the purpose of the <mark style="color:blue;">slot</mark> variable. This variable is necessary only when you've configured the <mark style="color:blue;">appInstallTarget</mark> in <mark style="color:blue;">/resource/config/shared.lua</mark> to <mark style="color:blue;">metadata</mark> (which is only applicable when using <mark style="color:blue;">ox\_inventory</mark>!). In this case, the <mark style="color:blue;">slot</mark> variable is used retrieve the item in that specific slot and save the installed application ID into the metadata of that item.

**Code example for&#x20;**<mark style="color:blue;">**ox\_inventory**</mark>

To install the application <mark style="color:blue;">rahe-racing</mark> from a USB stick, follow these steps:

1. Add the installation USB item to <mark style="color:blue;">ox\_inventory/data/items.lua</mark>:

```lua
['usb_racing'] = {
    label = 'USB stick',
    weight = 50,
    description = 'An USB stick with a racing program installed onto it.',
    stack = false,
    client = {
       export = 'rahe-tablet.usb_install'
    }
},
```

2. Add the <mark style="color:blue;">usb\_install</mark> export to a client-side file in <mark style="color:blue;">rahe-tablet</mark>, such as the end of <mark style="color:blue;">/resource/client/integrations.lua</mark>:

```lua
exports('usb_install', function(data, slot)
    local metadata = {}
    if data.name == 'usb_racing' then
        metadata = {
            appId = 'rahe-racing',
            appName = 'Racing'
        }
    elseif data.name == 'usb_boosting' then
        metadata = {
            appId = 'rahe-boosting',
            appName = 'Boosting'
        }    
    elseif data.name == 'usb_drifting' then
        metadata = {
            appId = 'rahe-drifting',
            appName = 'Drifting'
        }
    elseif data.name == 'usb_trucking' then
        metadata = {
            appId = 'kub_trucking',
            appName = 'Trucking'
        }
    end

    slot.metadata = metadata
    TriggerServerEvent('rahe-tablet:server:installApp', slot, false)
end)
```

3. Create the <mark style="color:blue;">server-side</mark> event to handle installation. Register the event as follows at the end of <mark style="color:blue;">/resource/server/installation.lua</mark>:

```lua
RegisterNetEvent('rahe-tablet:server:installApp', function(slot, useMetadata)
    local src = source
    if not slot or not slot.slot or not slot.metadata.appId then
        return
    end

    local tabletSlots = exports.ox_inventory:Search(src, 'slots', shConfig.itemId)
    if not next(tabletSlots) then
        lib.notify(src, { 
            title = ("You don't have a tablet to install the application '%s' onto."):format(slot.metadata.appName), 
            type = 'error' 
        })
        return
    end

    local canInstall = canStartInstallation(src, slot.metadata.appId, tabletSlots[1].slot)
    if canInstall ~= true then
        lib.notify(src, { title = canInstall, type = 'error' })
        return
    end

    if not exports.ox_inventory:RemoveItem(src, slot.name, 1, useMetadata and slot.metadata or nil, slot.slot) then
        return
    end

    installApplication(src, slot.metadata.appId, tabletSlots[1].slot)
    lib.notify(src, { 
        title = ("You have installed the application '%s' onto your tablet."):format(slot.metadata.appName), 
        type = 'success' 
    })
end)
```

(it's in the same file as the export function, so we are using the <mark style="color:blue;">installApplication</mark> function directly)
