server/convert.lua

-- with this command you can integrate your qb inventories into the new system
-- if you write a code for different inventories please share it with us
-- Commands only available from cmd/live consol

local function convertItems(itemList)
    if not itemList then return "[]" end
    local items = json.decode(itemList)
    if not items then return "[]" end
    for _, itemData in pairs(items) do
        if itemData then
            itemData.info = itemData.metadata or itemData.info
            itemData.amount = itemData.count or itemData.amount
        end
    end
    return json.encode(items)
end

local function checkTableExist(table)
    return MySQL.single.await("SHOW TABLES LIKE ?", { table })
end

RegisterCommand("convertqb", function(source)
    if source > 0 then return print("Pls use on server cmd") end

    local newQb = checkTableExist("inventories")
    if newQb then
        local result = MySQL.query.await('SELECT * FROM inventories')
        if result and #result > 0 then
            for i = 1, #result do
                local inventory = result[i]
                local items = inventory.items
                local identifier = inventory.identifier
                if identifier:find('trunk-') then
                    MySQL.insert.await('INSERT INTO `tgiann_inventory_trunkitems` (plate, items) VALUES (?, ?)', {
                        string.gsub(identifier, "trunk-", ""), items
                    })
                elseif identifier:find('glovebox-') then
                    MySQL.insert.await('INSERT INTO `tgiann_inventory_gloveboxitems` (plate, items) VALUES (?, ?)', {
                        string.gsub(identifier, "glovebox-", ""), items
                    })
                else
                    MySQL.insert.await('INSERT INTO `tgiann_inventory_stashitems` (stash, items) VALUES (?, ?)', {
                        identifier, items
                    })
                end
            end
        end
        print("New Qb inventories converted")
    else
        local response = MySQL.query.await('SELECT * FROM `stashitems`')
        if response and next(response) then
            for i = 1, #response do
                MySQL.insert.await('INSERT INTO `tgiann_inventory_stashitems` (stash, items) VALUES (?, ?)', {
                    response[i].stash, response[i].items
                })
            end
            print("stashitems converted")
        end

        local response = MySQL.query.await('SELECT * FROM `trunkitems`')
        if response and next(response) then
            for i = 1, #response do
                MySQL.insert.await('INSERT INTO `tgiann_inventory_trunkitems` (plate, items) VALUES (?, ?)', {
                    response[i].plate, response[i].items
                })
            end
            print("trunkitems converted")
        end

        local response = MySQL.query.await('SELECT * FROM `gloveboxitems`')
        if response and next(response) then
            for i = 1, #response do
                MySQL.insert.await('INSERT INTO `tgiann_inventory_gloveboxitems` (plate, items) VALUES (?, ?)', {
                    response[i].plate, response[i].items
                })
            end
            print("gloveboxitems converted")
        end
    end

    if checkTableExist("players") then
        local response = MySQL.query.await('SELECT citizenid, inventory FROM `players`')
        if response and next(response) then
            for i = 1, #response do
                if response[i].inventory then
                    MySQL.insert.await('INSERT INTO `tgiann_inventory_player` (citizenid, inventory, clotheinventory) VALUES (?, ?, ?)', {
                        response[i].citizenid, response[i].inventory, "[]"
                    })
                end
            end
            print("Player inventorys converted")
        end
    end

    print("Finished")
end)

-- Ox Inventory
-- Not Tested!
RegisterCommand("convertox", function(source)
    if source > 0 then return print("Pls use on server cmd") end
    if not checkTableExist("ox_inventory") then
        return print("Ox Inventory not found")
    end

    local response = MySQL.query.await('SELECT name, data FROM `ox_inventory`')
    if response and next(response) then
        for i = 1, #response do
            local owner = response[i].owner or ""
            MySQL.insert.await('INSERT INTO `tgiann_inventory_stashitems` (stash, items) VALUES (?, ?)', {
                response[i].name .. "_" .. owner, convertItems(response[i].data)
            })
            if response[i].owner then
                print(string.format("%s Converted but u need the edit open event! When opening the stash you need to send the player id in the stash name! Example: TriggerServerEvent('inventory:server:OpenInventory', 'stash', %s_PlayerData.citizenid)", response[i].name, response[i].name))
            end
        end
        print("stashitems converted")
    end

    if config.framework == "qb" then
        local response = MySQL.query.await('SELECT glovebox, trunk, plate FROM `player_vehicles`')
        if response and next(response) then
            for i = 1, #response do
                MySQL.insert.await('INSERT INTO `tgiann_inventory_gloveboxitems` (plate, items) VALUES (?, ?)', {
                    response[i].plate, convertItems(response[i].glovebox)
                })
                MySQL.insert.await('INSERT INTO `tgiann_inventory_trunkitems` (plate, items) VALUES (?, ?)', {
                    response[i].plate, convertItems(response[i].trunk)
                })
            end
            print("gloveboxitems and trunkitems converted")
        end
    else
        local response = MySQL.query.await('SELECT glovebox, trunk, plate FROM `owned_vehicles`')
        if response and next(response) then
            for i = 1, #response do
                MySQL.insert.await('INSERT INTO `tgiann_inventory_gloveboxitems` (plate, items) VALUES (?, ?)', {
                    response[i].plate, convertItems(response[i].glovebox)
                })
                MySQL.insert.await('INSERT INTO `tgiann_inventory_trunkitems` (plate, items) VALUES (?, ?)', {
                    response[i].plate, convertItems(response[i].trunk)
                })
            end
            print("gloveboxitems and trunkitems converted")
        end
    end

    if config.framework == "qb" then
        local response = MySQL.query.await('SELECT citizenid, inventory  FROM `players`')
        if response and next(response) then
            for i = 1, #response do
                if response[i].inventory then
                    MySQL.insert.await('INSERT INTO `tgiann_inventory_player` (citizenid, inventory, clotheinventory) VALUES (?, ?, ?)', {
                        response[i].citizenid, convertItems(response[i].inventory), "[]"
                    })
                end
            end
            print("Player inventorys converted")
        end
    else
        local response = MySQL.query.await('SELECT identifier, inventory  FROM `users`')
        if response and next(response) then
            for i = 1, #response do
                if response[i].inventory then
                    MySQL.insert.await('INSERT INTO `tgiann_inventory_player` (citizenid, inventory, clotheinventory) VALUES (?, ?, ?)', {
                        response[i].identifier, convertItems(response[i].inventory), "[]"
                    })
                end
            end
            print("Player inventorys converted")
        end
    end


    print("Finished")
end)

Last updated