Framework

Framework Support

The script supports the following frameworks with an auto detection mode:

  • ESX

  • QBCore

  • Standalone (customizable) (Custom logic must be implemented)

Config.Framework = 'auto'   --- | 'esx' | 'qb-core' | 'standalone' |

Item Usage Handling

For each item defined in Ks_Ctu, the script registers it as a usable item:

  • QBCore: Uses QBCore.Functions.CreateUseableItem

  • ESX: Uses ESX.RegisterUsableItem

  • Standalone: Uses a RegisterCommand even

if Config.Framework == 'qb-core' then
    QBCore.Functions.CreateUseableItem(itemName, function(source, item)
        local Player = QBCore.Functions.GetPlayer(source)
        if not Player then return end
        TriggerEvent('Ks_Ctu:Internal:server:UseItem', source, itemName, itemData, categoryName)
    end)

elseif Config.Framework == 'esx' then
    ESX.RegisterUsableItem(itemName, function(source)
        local xPlayer = ESX.GetPlayerFromId(source)
        if not xPlayer then return end
        TriggerEvent('Ks_Ctu:Internal:server:UseItem', source, itemName, itemData, categoryName)
    end)

elseif Config.Framework == 'standalone' then
    RegisterCommand(itemName, function(source, args, rawCommand)
        TriggerEvent('Ks_Ctu:Internal:server:UseItem', source, itemName, itemData, categoryName)
    end)
end

For Standalone (It is recommended to implement your own logic) By default, items are registered as commands, but it is recommended that you implement your own logic to properly integrate item usage into your server.

Additionally we leave other functions on the server side

Handling Surprise Item Rewards

The system gives random items to players for surprise boxes.

-- @param player: Player ID
-- @param surprises: Table containing surprise items and their amounts {item = amount}
function ks_CTU_GiveSurprises(player, surprises)
    if Config.Framework == 'qb-core' then
        local QBCore = exports['qb-core']:GetCoreObject()
        local Player = QBCore.Functions.GetPlayer(player)
        if Player then
            for item, amount in pairs(surprises) do
                Player.Functions.AddItem(item, amount)
            end
        end

    elseif Config.Framework == 'esx' then
        local ESX = exports['es_extended']:getSharedObject()
        local Player = ESX.GetPlayerFromId(player)
        if Player then
            for item, amount in pairs(surprises) do
                Player.addInventoryItem(item, amount)
            end
        end

    elseif Config.Framework == 'standalone' then
        for item, amount in pairs(surprises) do

        -- ==================== INSERT YOUR LOGIC BELOW ====================
        --        Implement your custom logic to give the surprise items.
        --    Loop through the surprises table and add items to the player.
        -- ================================================================

        end
    end
end

Removing Items from Inventory

The system allows removing items from a player's inventory.

-- @param player: Player ID
-- @param item: The item to be removed
-- @param amount: The amount of the item to remove
-- @param cb: Callback function returning true if the item was removed, false otherwise
function ks_CTU_RemoveItem(player, item, amount, cb)
    if Config.Framework == 'qb-core' then
        local QBCore = exports['qb-core']:GetCoreObject()
        local Player = QBCore.Functions.GetPlayer(player)
        if Player then
            local hasItem = Player.Functions.GetItemByName(item)
            if hasItem and hasItem.amount >= amount then
                Player.Functions.RemoveItem(item, amount)
                cb(true)
            else
                cb(false)
            end
        else
            cb(false)
        end

    elseif Config.Framework == 'esx' then
        local ESX = exports['es_extended']:getSharedObject()
        local Player = ESX.GetPlayerFromId(player)
        if Player then
            local hasItem = Player.getInventoryItem(item)
            if hasItem and hasItem.count >= amount then
                Player.removeInventoryItem(item, amount)
                cb(true)
            else
                cb(false)
            end
        else
            cb(false)
        end

    elseif Config.Framework == 'standalone' then

        -- ==================== INSERT YOUR LOGIC BELOW ====================
        --         Implement your custom logic to remove the item.
        --     Ensure to handle the item validation and removal process.
        -- ================================================================
        cb(true)
    end
end

Last updated