# Inventory Integration

{% tabs %}
{% tab title="qb-inventory (1.2.4)" %}
Make sure your `trunkitems` and `gloveboxitems` database tables have at least 1 entry in them or your vehicle inventory will not update when plates are changed

Add the following item images to `qb-inventory/html/images/` if not there already. Also located in `mk_plates/images/`&#x20;

<div><figure><img src="https://51694285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSadY5GIZVZCGCeog04Gq%2Fuploads%2FUe7S8fvEMaRm0AIrHg6w%2Ffakeplate.png?alt=media&#x26;token=c8a56475-239b-4038-9c31-b58f0212f31b" alt="" width="75"><figcaption></figcaption></figure> <figure><img src="https://51694285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSadY5GIZVZCGCeog04Gq%2Fuploads%2FFbI63GWRYhYeMNopV9kk%2Fvanityplate.png?alt=media&#x26;token=1090ee15-a283-4fa7-9315-a35eb645e195" alt="" width="92"><figcaption></figcaption></figure></div>

Locate function `IsVehicleOwned` inside of `qb-inventory/server/main.lua` and replace it with this.

```lua
local function IsVehicleOwned(plate)
    local result = MySQL.scalar.await('SELECT 1 from player_vehicles WHERE plate = ? OR fakeplate = ?', {plate, plate})
	return result
end
```

Locate `local ownedItems = GetOwnedVehicleGloveboxItems(id)` inside of `qb-inventory/server/main.lua` and replace the if statement underneath it to match this. There are `2` places you must do this replace. make sure you do both.

```lua
if IsVehicleOwned(id) and next(ownedItems) then
	secondInv.inventory = ownedItems
	Gloveboxes[id] = {}
	Gloveboxes[id].items = ownedItems
	Gloveboxes[id].isOpen = src
	Gloveboxes[id].label = secondInv.label
elseif Gloveboxes[id] and not Gloveboxes[id].isOpen then
	secondInv.inventory = Gloveboxes[id].items
	Gloveboxes[id].isOpen = src
	Gloveboxes[id].label = secondInv.label
else
	Gloveboxes[id] = {}
	Gloveboxes[id].items = {}
	Gloveboxes[id].isOpen = src
	Gloveboxes[id].label = secondInv.label
end
```

<figure><img src="https://51694285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSadY5GIZVZCGCeog04Gq%2Fuploads%2FjSVhOSLZpk8MF2dKy0xS%2Fqb-edit.png?alt=media&#x26;token=e666e15a-a4d9-46f0-bbd4-6c631d1c2ebe" alt=""><figcaption></figcaption></figure>

The statement should look like the above image. Make sure to make this edit in both places in this file or your glovebox items will not update properly.
{% endtab %}

{% tab title="qb-inventory (2.0.0)" %}
Add the following item images to `qb-inventory/html/images/` if not there already. Also located in `mk_plates/images/`

<figure><img src="https://51694285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSadY5GIZVZCGCeog04Gq%2Fuploads%2FFbI63GWRYhYeMNopV9kk%2Fvanityplate.png?alt=media&#x26;token=1090ee15-a283-4fa7-9315-a35eb645e195" alt="" width="92"><figcaption></figcaption></figure>

<figure><img src="https://51694285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSadY5GIZVZCGCeog04Gq%2Fuploads%2FUe7S8fvEMaRm0AIrHg6w%2Ffakeplate.png?alt=media&#x26;token=c8a56475-239b-4038-9c31-b58f0212f31b" alt="" width="75"><figcaption></figcaption></figure>

Add the following event to `qb-inventory/server/main.lua`&#x20;

**NOTE** - This edit is also required for `mk_usedvehicles` If you have already added this event as part of that scripts setup you can skip to the next step!

```lua
RegisterNetEvent('qb-inventory:server:updateVehicle', function(currentPlate, newPlate)
    if (GetInvokingResource() ~= 'mk_usedvehicles' and GetInvokingResource() ~= 'mk_plates') then return end --prevent exploits
    if not Inventories or not next(Inventories) then return end

    if not newPlate then --clear inventory
        if Inventories['trunk-'..currentPlate] then Inventories['trunk-'..currentPlate] = nil end
        if Inventories['glovebox-'..currentPlate] then Inventories['glovebox-'..currentPlate] = nil end
    else --update inventory
        if Inventories['trunk-'..currentPlate] then
            Inventories['trunk-'..newPlate] = Inventories?['trunk-'..currentPlate]
            if Inventories['trunk-'..newPlate]?.label then Inventories['trunk-'..newPlate].label = 'trunk-'..newPlate end
            Inventories['trunk-'..currentPlate] = nil
        end

        if Inventories['glovebox-'..currentPlate] then
            Inventories['glovebox-'..newPlate] = Inventories?['glovebox-'..currentPlate]
            if Inventories['glovebox-'..newPlate]?.label then Inventories['glovebox-'..newPlate].label = 'glovebox-'..newPlate end
            Inventories['glovebox-'..currentPlate] = nil
        end
    end
end)
```

Locate the callback `qb-inventory:client:vehicleCheck` in `qb-inventory/client/vehicles.lua` and replace it with the following

```lua
QBCore.Functions.CreateClientCallback('qb-inventory:client:vehicleCheck', function(cb)
    local ped = PlayerPedId()

    -- Glovebox
    local inVehicle = GetVehiclePedIsIn(ped, false)
    if inVehicle ~= 0 then
        local plate = QBCore.Shared.Trim(GetVehicleNumberPlateText(inVehicle))
        local class = GetVehicleClass(inVehicle)
        local inventory = 'glovebox-' .. plate
        cb(inventory, class)
        return
    end

    -- Trunk
    local vehicle, distance = QBCore.Functions.GetClosestVehicle()
    if vehicle ~= 0 and distance < 5 then
        local pos = GetEntityCoords(ped)
        local dimensionMin, dimensionMax = GetModelDimensions(GetEntityModel(vehicle))
        local trunkpos = GetOffsetFromEntityInWorldCoords(vehicle, 0.0, (dimensionMin.y), 0.0)
        if BackEngineVehicles[GetEntityModel(vehicle)] then trunkpos = GetOffsetFromEntityInWorldCoords(vehicle, 0.0, (dimensionMax.y), 0.0) end
        if #(pos - trunkpos) < 1.5 then
            if GetVehicleDoorLockStatus(vehicle) < 2 then
                OpenTrunk(vehicle)
                local class = GetVehicleClass(vehicle)
                local plate = QBCore.Shared.Trim(GetVehicleNumberPlateText(vehicle))
                local inventory = 'trunk-' .. plate
                cb(inventory, class)
            else
                QBCore.Functions.Notify(Lang:t('notify.vlocked'), 'error')
                return
            end
        end
    end
    cb(nil)
end)
```

{% endtab %}

{% tab title="ox\_inventory" %}
Version 2.23.4+

Add the following items into `ox_inventory/data/items.lua` if not there already

```lua
['fakeplate'] = {
    label = 'Fake Plate',
    weight = 160,
    stack = false,
    close = true,
},

['screwdriverset'] = {
    label = 'Toolset',
    weight = 160,
},

['vanityplate'] = {
    label = 'Vanity Plate',
    weight = 160,
    stack = false,
    close = true,
},
```

Add the following images into `ox_inventory/web/images` if not there already. Also located in `mk_plates/images/`&#x20;

<div><figure><img src="https://51694285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSadY5GIZVZCGCeog04Gq%2Fuploads%2FUe7S8fvEMaRm0AIrHg6w%2Ffakeplate.png?alt=media&#x26;token=c8a56475-239b-4038-9c31-b58f0212f31b" alt="" width="75"><figcaption></figcaption></figure> <figure><img src="https://51694285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSadY5GIZVZCGCeog04Gq%2Fuploads%2FFbI63GWRYhYeMNopV9kk%2Fvanityplate.png?alt=media&#x26;token=1090ee15-a283-4fa7-9315-a35eb645e195" alt="" width="92"><figcaption></figcaption></figure> <figure><img src="https://51694285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSadY5GIZVZCGCeog04Gq%2Fuploads%2FUPc7nqKgKsd1TmsSNNQj%2Fscrewdriverset.png?alt=media&#x26;token=79682d2d-60ed-4a1d-b934-ad75f2add55d" alt="" width="75"><figcaption></figcaption></figure></div>

Make the following changes inside of `ox_inventory/modules/inventory/server.lua`&#x20;

Locate function `prepareInventorySave`and replace with the following

```lua
local function prepareInventorySave(inv, buffer, time)
    local shouldSave = not inv.datastore and inv.changed
    local n = 0

    for k, v in pairs(inv.items) do
        if not Items.UpdateDurability(inv, v, Items(v.name), nil, time) and shouldSave then
            n += 1
            buffer[n] = {
                name = v.name,
                count = v.count,
                slot = k,
                metadata = next(v.metadata) and v.metadata or nil
            }
        end
    end

    if not shouldSave then return end

    local data = next(buffer) and json.encode(buffer) or nil
    inv.changed = false
    table.wipe(buffer)

    if inv.player then
        if shared.framework == 'esx' then return end

        return 1, { data, inv.owner }
    end

    if inv.type == 'trunk' then
        return 2, fakeColumn and {data, inv.dbId, inv.dbId } or { data, inv.dbId }
    end

    if inv.type == 'glovebox' then
        return 3, fakeColumn and {data, inv.dbId, inv.dbId } or { data, inv.dbId }
    end

    return 4, { data, inv.owner and tostring(inv.owner) or '', inv.dbId }
end
```

Locate function `Inventory.UpdateVehicle`and replace with the following

```lua
function Inventory.UpdateVehicle(oldPlate, newPlate)
	oldPlate = oldPlate:upper()
	newPlate = newPlate:upper()

	if server.trimplate then
		oldPlate = string.strtrim(oldPlate)
		newPlate = string.strtrim(newPlate)
	end

	local trunk = Inventory(('trunk%s'):format(oldPlate))
	local glove = Inventory(('glove%s'):format(oldPlate))

	if trunk then
		Inventory.CloseAll(trunk)

		Inventories[trunk.id] = nil
		trunk.label = newPlate
		trunk.dbId = type(trunk.dbId) == 'number' and trunk.dbId or newPlate
		trunk.id = ('trunk%s'):format(newPlate)
		Inventories[trunk.id] = trunk
	end

	if glove then
		Inventory.CloseAll(glove)

		Inventories[glove.id] = nil
		glove.label = newPlate
		glove.dbId = type(glove.dbId) == 'number' and glove.dbId or newPlate
		glove.id = ('glove%s'):format(newPlate)
		Inventories[glove.id] = glove
	end
end
```

Make the following changes inside of `ox_inventory/modules/mysql/server.lua`&#x20;

On the first empty line in this file add the following

```lua
fakeColumn = false
```

Locate `if shared.framework == 'ox' then` if/then statement and replace it with the following

```lua
if shared.framework == 'ox' then
    playerTable = 'character_inventory'
    playerColumn = 'charid'
    vehicleTable = 'vehicles'
    vehicleColumn = 'id'
elseif shared.framework == 'esx' then
    playerTable = 'users'
    playerColumn = 'identifier'
    vehicleTable = 'owned_vehicles'
    vehicleColumn = 'plate'
    fakeColumn = 'fakeplate'
elseif shared.framework == 'nd' then
    playerTable = 'nd_characters'
    playerColumn = 'charid'
    vehicleTable = 'nd_vehicles'
    vehicleColumn = 'id'
elseif shared.framework == 'qb' then
    playerTable = 'players'
    playerColumn = 'citizenid'
    vehicleTable = 'player_vehicles'
    vehicleColumn = 'plate'
    fakeColumn = 'fakeplate'
elseif shared.framework == 'qbx' then
    playerTable = 'players'
    playerColumn = 'citizenid'
    vehicleTable = 'player_vehicles'
    vehicleColumn = 'id'
end
```

Locate `Query[k] =` statement and replace with the following

```lua
if fakeColumn then
    Query[k] = v:gsub('{vehicle_column}` = %?', '{vehicle_column}`'):gsub('{user_table}', playerTable):gsub('{user_column}', playerColumn):gsub('{vehicle_table}', vehicleTable):gsub('`{vehicle_column}`', vehicleColumn and (fakeColumn and ('`'..vehicleColumn..'` = ? OR `'..fakeColumn..'` = ?') or '`'..vehicleColumn..'` = ?') or '`'..vehicleColumn..'` = ?')
else
    Query[k] = v:gsub('{user_table}', playerTable):gsub('{user_column}', playerColumn):gsub('{vehicle_table}',
    vehicleTable):gsub('{vehicle_column}', vehicleColumn)
end
```

<figure><img src="https://51694285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSadY5GIZVZCGCeog04Gq%2Fuploads%2FV92oxsZmLcYL6SOlp3uU%2Fgsubquery.png?alt=media&#x26;token=a93b7d6e-7f09-41f2-8ef2-d7f999145e2b" alt=""><figcaption><p>This edit should look similar to this image</p></figcaption></figure>

Locate functions `db.saveGlovebox` `db.loadGlovebox` `db.saveTrunk` `db.loadTrunk` and replace them with the following

```lua
function db.saveGlovebox(id, inventory)
    return MySQL.prepare(Query.UPDATE_GLOVEBOX, fakeColumn and { inventory,  id, id } or {inventory, id})
end

function db.loadGlovebox(id)
    return MySQL.prepare.await(Query.SELECT_GLOVEBOX, fakeColumn and { id, id } or { id })
end

function db.saveTrunk(id, inventory)
    return MySQL.prepare(Query.UPDATE_TRUNK, fakeColumn and { inventory, id, id } or { inventory, id })
end

function db.loadTrunk(id)
    return MySQL.prepare.await(Query.SELECT_TRUNK, fakeColumn and { id, id } or { id })
end
```

{% endtab %}

{% tab title="ak47\_inventory" %}
Navigate to `mk_plates/config/config.lua` and make the following edits

`Config.FakeInstalled` Function

```lua
Config.FakeInstalled = function(playerSource, realPlate, fakePlate, vehicle, netId)
    local glovId = 'glovebox:'..realPlate
    local glovebox = exports['ak47_inventory']:GetInventory(glovId)
    if not glovebox then
        if exports['ak47_inventory']:LoadInventory(glovId) then
            glovebox = exports['ak47_inventory']:GetInventory(glovId)
        end
    end

    local trunkId = 'trunk:'..realPlate
    local trunk = exports['ak47_inventory']:GetInventory(trunkId)
    if not trunk then
        if exports['ak47_inventory']:LoadInventory(trunkId) then
            trunk = exports['ak47_inventory']:GetInventory(trunkId)
        end
    end

    if glovebox then
        local glovId2 = 'glovebox:'..fakePlate
        if not exports['ak47_inventory']:GetInventory(glovId2) then
            if exports['ak47_inventory']:LoadInventory(glovId2, {label = glovId2:upper(), maxWeight = glovebox.maxWeight, slots = glovebox.slots, type = 'glovebox'}) then
                exports['ak47_inventory']:SetInvItems(glovId2, glovebox.items)
                exports['ak47_inventory']:ClearInventory(glovId)
            end
        end
    end

    if trunk then
        local trunkId2 = 'trunk:'..fakePlate
        if not exports['ak47_inventory']:GetInventory(trunkId2) then
            if exports['ak47_inventory']:LoadInventory(trunkId2, {label = trunkId2:upper(), maxWeight = trunk.maxWeight, slots = trunk.slots, type = 'trunk'}) then
                exports['ak47_inventory']:SetInvItems(trunkId2, trunk.items)
                exports['ak47_inventory']:ClearInventory(trunkId)
            end
        end
    end

    local result = MySQL.update.await([[
    UPDATE `ak47_inventory`
    SET identifier = REPLACE(identifier, ?, ?),
        data = REPLACE(data, ?, ?)
    WHERE identifier = ? OR identifier LIKE ? OR identifier LIKE ? OR identifier LIKE ? OR identifier LIKE ?
    OR data = ? OR data LIKE ? OR data LIKE ? OR data LIKE ? OR data LIKE ?
    ]], {
        realPlate, fakePlate,  
        realPlate, fakePlate,  
        realPlate,             
        'glovebox ' .. realPlate, 'glovebox:' .. realPlate, 'trunk ' .. realPlate, 'trunk:' .. realPlate,  -- 4. Präfixe in identifier
        realPlate,             
        'glovebox ' .. realPlate, 'glovebox:' .. realPlate, 'trunk ' .. realPlate, 'trunk:' .. realPlate   
    })
end
```

`Config.FakeRemoved` Function

```lua
Config.FakeRemoved = function(playerSource, realPlate, fakePlate, vehicle, netId)
    local glovId = 'glovebox:'..fakePlate
    local glovebox = exports['ak47_inventory']:GetInventory(glovId)
    if not glovebox then
        if exports['ak47_inventory']:LoadInventory(glovId) then
            glovebox = exports['ak47_inventory']:GetInventory(glovId)
        end
    end

    local trunkId = 'trunk:'..fakePlate
    local trunk = exports['ak47_inventory']:GetInventory(trunkId)
    if not trunk then
        if exports['ak47_inventory']:LoadInventory(trunkId) then
            trunk = exports['ak47_inventory']:GetInventory(trunkId)
        end
    end

    if glovebox then
        local glovId2 = 'glovebox:'..realPlate
        if not exports['ak47_inventory']:GetInventory(glovId2) then
            if exports['ak47_inventory']:LoadInventory(glovId2, {label = glovId2:upper(), maxWeight = glovebox.maxWeight, slots = glovebox.slots, type = 'glovebox'}) then
                exports['ak47_inventory']:SetInvItems(glovId2, glovebox.items)
            end
        end
    end

    if trunk then
        local trunkId2 = 'trunk:'..realPlate
        if not exports['ak47_inventory']:GetInventory(trunkId2) then
            if exports['ak47_inventory']:LoadInventory(trunkId2, {label = trunkId2:upper(), maxWeight = trunk.maxWeight, slots = trunk.slots, type = 'trunk'}) then
                exports['ak47_inventory']:SetInvItems(trunkId2, trunk.items)
            end
        end
    end

    exports['ak47_inventory']:DeleteInventory(glovId)
    exports['ak47_inventory']:DeleteInventory(trunkId)

    local result = MySQL.update.await([[
        UPDATE `ak47_inventory`
        SET identifier = REPLACE(identifier, ?, ?),
            data = REPLACE(data, ?, ?)
        WHERE identifier = ? OR identifier LIKE ? OR identifier LIKE ? OR identifier LIKE ? OR identifier LIKE ?
        OR data = ? OR data LIKE ? OR data LIKE ? OR data LIKE ? OR data LIKE ?
    ]], {
        fakePlate, realPlate,  
        fakePlate, realPlate,  
        fakePlate,             
        'glovebox ' .. fakePlate, 'glovebox:' .. fakePlate, 'trunk ' .. fakePlate, 'trunk:' .. fakePlate,  
        fakePlate,             
        'glovebox ' .. fakePlate, 'glovebox:' .. fakePlate, 'trunk ' .. fakePlate, 'trunk:' .. fakePlate   
    })
end
```

{% endtab %}
{% endtabs %}
