ESX

Default ESX framework installation (axr_octastore/server/framework/esx.lua)

if Config.Framework ~= 'esx' then
    return
end

ESX = exports['es_extended']:getSharedObject()

Framework = {}

---@param identifier integer
---@return boolean
Framework.canAccesMenu = function(identifier)
    return true
end

---@param source integer
---@return integer
Framework.getPlayerIdentifier = function(source)
    local xPlayer = ESX.GetPlayerFromId(source)
    return xPlayer and xPlayer.identifier or nil
end

---@param identifier string
---@return integer
Framework.getIdentifierSource = function(identifier)
    local players = ESX.GetPlayers()
    for _, src in ipairs(players) do
        local xPlayer = ESX.GetPlayerFromId(src)
        if xPlayer and xPlayer.identifier == identifier then
            return src
        end
    end
    return nil
end

---@param identifier string
---@return table
Framework.getPlayerIdentity = function(identifier)
    local src = Framework.getIdentifierSource(identifier)
    local xPlayer = ESX.GetPlayerFromId(src)
    if xPlayer then
        return {
            firstname = xPlayer.get('firstName'),
            secondname = xPlayer.get('lastName'),
            phone_numner = xPlayer.get('phoneNumber') or 'unknown'
        }
    end
    return nil
end

---@param identifier string
---@param price integer
---@return boolean
Framework.tryPaymentPlan = function(identifier, price)
    local src = Framework.getIdentifierSource(identifier)
    local xPlayer = ESX.GetPlayerFromId(src)
    if xPlayer and xPlayer.getAccount('bank').money >= price then
        xPlayer.removeAccountMoney('bank', price)
        return true
    end
    return false
end

---@param identifier string
---@param price integer
---@return boolean
Framework.tryPayProductsOnline = function(identifier, price)
    local src = Framework.getIdentifierSource(identifier)
    local xPlayer = ESX.GetPlayerFromId(src)
    if xPlayer and xPlayer.getAccount('bank').money >= price then
        xPlayer.removeAccountMoney('bank', price)
        return true
    end
    return false
end

---@param identifier string
---@param amount integer
Framework.paySeller = function(identifier, amount)
    local src = Framework.getIdentifierSource(identifier)
    local xPlayer = ESX.GetPlayerFromId(src)
    if xPlayer then
        xPlayer.addAccountMoney('bank', amount)
    else
        exports[Config.Sql]:execute(
            "UPDATE users SET accounts = JSON_SET(accounts, '$.bank', JSON_EXTRACT(accounts, '$.bank') + @amount) WHERE identifier = @identifier",
            {
                ['@amount'] = amount,
                ['@identifier'] = identifier
            })
    end
end

---@param item string
---@param callback function
Framework.createItem = function(item, callback)
    -- ESX doesn't use item registration this way
end

---@param identifier string
---@param item string
---@param amount integer
---@return boolean
Framework.hasItem = function(identifier, item, amount)
    if Config.Inventory == 'esx' then
        local src = Framework.getIdentifierSource(identifier)
        local xPlayer = ESX.GetPlayerFromId(src)
        if xPlayer then
            local invItem = xPlayer.getInventoryItem(item)
            if invItem then
                if amount ~= nil then
                    return invItem.count >= amount
                else
                    return invItem.count > 0
                end
            end
        end
        return false
    end
end

---@param identifier string
---@return table
Framework.getPlayerItems = function(identifier)
    if Config.Inventory == 'esx' then
        local src = Framework.getIdentifierSource(identifier)
        local xPlayer = ESX.GetPlayerFromId(src)
        local items = {}
        if xPlayer then
            for _, v in pairs(xPlayer.inventory) do
                if v.count > 0 and not Config.blacklistedItems[v.name] then
                    items[v.name] = {
                        amount = v.count,
                        img = exports.axr_octastore:getItemImagePath(v.name),
                        name = v.label
                    }
                end
            end
        end
        return items
    end
end

---@param identifier string
---@param items table
---@return boolean
Framework.hasSpaceForItems = function(identifier, items)
    return true -- ESX nu are sistem nativ de greutate strict, se poate personaliza
end

---@param identifier string
---@param item string
---@param amount integer
Framework.giveInventoryItem = function(identifier, item, amount)
    if Config.Inventory == 'esx' then
        local src = Framework.getIdentifierSource(identifier)
        local xPlayer = ESX.GetPlayerFromId(src)
        if xPlayer then
            xPlayer.addInventoryItem(item, amount)
        end
    end
end

---@param identifier string
---@param item string
---@param amount integer
Framework.removeInventoryItem = function(identifier, item, amount)
    if Config.Inventory == 'esx' then
        local src = Framework.getIdentifierSource(identifier)
        local xPlayer = ESX.GetPlayerFromId(src)
        if xPlayer then
            xPlayer.removeInventoryItem(item, amount)
        end
    end
end

---@param item string
---@return string
Framework.getItemName = function(item)
    if Config.Inventory == 'esx' then
        local itemData = ESX.Items[item]
        return itemData and itemData.label or item
    end
end

---@param identifier string
---@param lockerItems table
Framework.giveLockerItems = function(identifier, lockerItems)
    local src = Framework.getIdentifierSource(identifier)
    if Framework.hasSpaceForItems(identifier, lockerItems) then
        for _, itemData in pairs(lockerItems) do
            Framework.giveInventoryItem(identifier, itemData.item, itemData.amount)
        end
        Framework.notify(src, Lang.notify['receive_locker_product'].message, Lang.notify['receive_locker_product'].type)
    else
        Framework.notify(src, Lang.notify['no_inventory_weight'].message, Lang.notify['no_inventory_weight'].type)
    end
end

---@param source integer
---@param notify string
---@param type string
Framework.notify = function(source, notify, type)
    TriggerClientEvent('esx:showNotification', source, notify)
end

Framework.playerSpawn = function()
    AddEventHandler('esx:playerLoaded', function(source, xPlayer)
        local identifier = xPlayer.identifier
        if exports.axr_octastore:getPlayerPlan(identifier) > 0 then
            exports.axr_octastore:checkPlayerRemaningTimePlan(identifier)
        end
    end)
end

Framework.playerSpawn()

Client

if Config.Framework ~= 'esx' then
    return
end

Last updated