ESX

CreateThread(function()
    if Config.Framework ~= 'ESX' then return end
    
    ESX = exports['es_extended']:getSharedObject()
    
    -- GetPlayer
    function GetPlayer(source)
        return ESX.GetPlayerFromId(source)
    end
    
    -- CreateUseableItem
    function CreateUseableItem(itemName, callback)
        ESX.RegisterUsableItem(itemName, callback)
    end
    
    -- GetPlayerJobName
    function GetPlayerJobName(source)
        local xPlayer = GetPlayer(source)
        if xPlayer then
            local job = xPlayer.getJob()
            if job and job.name then
                return job.name
            end
        end
        return nil
    end
    
    -- GetPlayerJobGrade
    function GetPlayerJobGrade(source)
        local xPlayer = GetPlayer(source)
        if xPlayer then
            local job = xPlayer.getJob()
            if job and job.grade then
                return job.grade
            end
        end
        return nil
    end
    
    -- AddItem
    function AddItem(source, itemName, amount, info)
        local xPlayer = GetPlayer(source)
        if xPlayer then
            if info then
                xPlayer.addInventoryItem(itemName, amount, info)
            else
                xPlayer.addInventoryItem(itemName, amount)
            end
            return true
        end
        return false
    end
    
    -- RemoveItem
    function RemoveItem(source, itemName, amount, slot)
        local xPlayer = GetPlayer(source)
        if xPlayer then
            xPlayer.removeInventoryItem(itemName, amount)
            return true
        end
        return false
    end
    
    -- GetItemByName
    function GetItemByName(source, itemName)
        local xPlayer = GetPlayer(source)
        if xPlayer then
            return xPlayer.getInventoryItem(itemName)
        end
        return nil
    end
    
    -- GetItemsByName (ESX - returns array of items with metadata)
    function GetItemsByName(source, itemName)
        local xPlayer = GetPlayer(source)
        if xPlayer then
            local items = {}
            local inventory = xPlayer.getInventory()
            for i = 1, #inventory do
                if inventory[i].name == itemName then
                    table.insert(items, {
                        name = inventory[i].name,
                        count = inventory[i].count,
                        label = inventory[i].label,
                        slot = i,
                        info = inventory[i].metadata or {}
                    })
                end
            end
            return items
        end
        return {}
    end
    
    -- GetPlayerData for metadata access
    function GetPlayerData(source)
        local xPlayer = GetPlayer(source)
        if xPlayer then
            return {
                identifier = xPlayer.identifier,
                citizenid = xPlayer.identifier,
                source = source
            }
        end
        return nil
    end
    
    -- AddMoney
    function AddMoney(source, moneyType, amount)
        local xPlayer = GetPlayer(source)
        if xPlayer then
            if moneyType == 'cash' then
                xPlayer.addMoney(amount)
            elseif moneyType == 'bank' then
                xPlayer.addAccountMoney('bank', amount)
            end
            return true
        end
        return false
    end
    
    -- RemoveMoney
    function RemoveMoney(source, moneyType, amount)
        local xPlayer = GetPlayer(source)
        if xPlayer then
            if moneyType == 'cash' then
                xPlayer.removeMoney(amount)
            elseif moneyType == 'bank' then
                xPlayer.removeAccountMoney('bank', amount)
            end
            return true
        end
        return false
    end

    -- AddJobMoney
    function AddJobMoney(accountName, amount)
        if not accountName or accountName == '' then
            return false
        end
        TriggerEvent('esx_addonaccount:getSharedAccount', accountName, function(account)
            if account then
                account.addMoney(amount)
            else
                print('No shared account found: ' .. tostring(accountName))
            end
        end)
        return true
    end
    
    -- GetSharedItem
    function GetSharedItem(itemName)
        if ESX.GetItemLabel then
            return { label = ESX.GetItemLabel(itemName) }
        end
        return nil
    end
    
    -- Notify
    function Notify(source, message, theme, duration)
        TriggerClientEvent('esx:showNotification', source, message)
    end

    -- GetPlayerPermission
    function GetPlayerPermission(player)
        local xPlayer = ESX.GetPlayerFromId(player)
        if xPlayer then
            local group = xPlayer.getGroup()
            if group == 'admin' or group == 'god' or group == 'mod' then
                return true
            end
        end
        return false
    end
    
    -- GetPlayerMoney
    function GetPlayerMoney(source, moneyType)
        local xPlayer = GetPlayer(source)
        if xPlayer then
            if moneyType == 'cash' then
                return xPlayer.getMoney()
            elseif moneyType == 'bank' then
                return xPlayer.getAccount('bank').money
            end
        end
        return 0
    end
    
    -- Server event to retrieve player character info (firstname and lastname)
    RegisterServerEvent('peace-treasure:GetPlayerCharInfo')
    AddEventHandler('peace-treasure:GetPlayerCharInfo', function(identifier)
        local source = source
        MySQL.Async.fetchAll('SELECT * FROM users WHERE identifier = @identifier', {
            ['@identifier'] = identifier
        }, function(result)
            if result and #result > 0 then
                local firstname = result[1].firstname
                local lastname = result[1].lastname
                TriggerClientEvent('peace-treasure:ReceivePlayerCharInfo', source, { firstname = firstname, lastname = lastname })
            else
                TriggerClientEvent('peace-treasure:ReceivePlayerCharInfo', source, nil)
            end
        end)
    end)
end)

Last updated