Standalone Tablet

You can configure this to register a usable item that fits your framework if enabled in the shared/config.lua file.

-- Purpose: Framework specific usable item creation. 
if Config.EnableStandaloneTablet and Config.TabletItemName then
    if Config.Framework == "qbcore" then
        Framework.Functions.CreateUseableItem(Config.TabletItemName, function(source)
            TriggerClientEvent("gs-trucking:client:openTablet", source)
        end)
    elseif Config.Framework == "esx" then
        Framework.RegisterUsableItem(Config.TabletItemName, function(source)
            TriggerClientEvent("gs-trucking:client:openTablet", source)
        end)
    end
end

Get Player

This is used to get framework specific data for the players source ID

-- Purpose: Function to get the player object from the source ID.
-- source [int]: Players source ID
function GetPlayer(source)
    if not source then return nil end
    if Config.Framework == "qbcore" then
        return Framework.Functions.GetPlayer(source)
    elseif Config.Framework == "esx" then
        return Framework.GetPlayerFromId(source)
    end
end

Get Identifier

This is used mostly in the reputation system as it stores reputation in the database connected to their character identifier. You can modify to your requirements if they are different from the default

-- Purpose: Function to get the players identifier from the source ID.
-- source [int]: Players source ID
function GetxPlayerIdentifier(source)
    local xPlayer = GetPlayer(source)
    if xPlayer then
        if Config.Framework == "qbcore" then
            return xPlayer.PlayerData.citizenid
        elseif Config.Framework == "esx" then
            return xPlayer.identifier
        end
    end
    return nil
end 

Get Players Money

Checks a players account as configured, mostly used for the truck rental system

-- Purpose: Function to get the players money from the source ID.
-- source [int]: Players source ID
function GetPlayerMoney(source)
    local xPlayer = GetPlayer(source)
    if xPlayer then
        if Config.Framework == "qbcore" then
            return xPlayer.Functions.GetMoney('bank')
        elseif Config.Framework == "esx" then
            return xPlayer.getAccount('bank').money
        end
    end
    return 0
end

Give Player Money

This is triggered when a job is completed or a rental truck is returned. It passes an extra argument to indicate whether or not a job is illegal or not. You can configure this however you need. (Does not get triggered if the payout is $0)

-- Purpose: Function to give the player money.
-- souce [int]: Players source ID
-- amount [int]: Amount of money to pay the player
-- illegal [bool]: Type of job the player is doing (always false if it's from rental return)
function GivePlayerMoney(source, amount, illegal)
    local xPlayer = GetPlayer(source)
    if xPlayer then
        if Config.Framework == "qbcore" then
            xPlayer.Functions.AddMoney((not illegal and 'bank' or 'crypto'), amount)
        elseif Config.Framework == "esx" then
            xPlayer.addAccountMoney((not illegal and 'bank' or 'black_money'), amount)
        end
    end
end

Remove Player Money

This is triggered when a player rents a truck and the rental fee is more than $0

-- Purpose: Function to remove the players money.
-- souce [int]: Players source ID
-- amount [int]: Amount of money to remove from the player
function RemovePlayerMoney(source, amount)
    local xPlayer = GetPlayer(source)
    if xPlayer then
        if Config.Framework == "qbcore" then
            xPlayer.Functions.RemoveMoney('bank', amount)
        elseif Config.Framework == "esx" then
            xPlayer.removeAccountMoney('bank', amount)
        end
    end
end

Player Reputation (Add/Remove)

These functions are only triggered when the reputation system is enabled. I added them incase you want to configure anything else such as the notification or for example QBCore has its own 'jobrep' metadata that you can also update here.

-- Purpose: Function triggered when player reputation has been added. (Doesnt trigger if reputation is disabled)
-- source [int]: Players source ID
-- added [int]: Amount of reputation to add to the player
-- total [int]: The new amount of reputation the player has
function AddedPlayerReputation(source, added, total)
    local xPlayer = GetPlayer(source)
    if xPlayer then
        --QBCore hass a metadata system to store player job reputation data. (Uncomment if you wish to use it)
        --if Config.Framework == "qbcore" then
        --    if Config.RequireJob then
        --        xPlayer.PlayerData.metadata.jobrep[Config.RequireJob] = total
        --        xPlayer.Functions.SetMetaData("jobrep", xPlayer.PlayerData.metadata.jobrep)
        --    end
        --end
        TriggerClientEvent('gs-trucking:client:notify', source, locale('earned_reputation', added, total), 'success') 
    end
end

-- Purpose: Function triggered when player reputation has been removed. (Doesnt trigger if reputation is disabled)  
-- source [int]: Players source ID
-- removed [int]: Amount of reputation to remove from the player
-- total [int]: The new amount of reputation the player has
function RemovedPlayerReputation(source, removed, total)
    local xPlayer = GetPlayer(source)
    if xPlayer then
        --QBCore hass a metadata system to store player job reputation data. (Uncomment if you wish to use it)
        --if Config.Framework == "qbcore" then
        --    if Config.RequireJob then
        --        xPlayer.PlayerData.metadata.jobrep[Config.RequireJob] = total
        --        xPlayer.Functions.SetMetaData("jobrep", xPlayer.PlayerData.metadata.jobrep)
        --    end
        --end
        TriggerClientEvent('gs-trucking:client:notify', source, locale('lost_reputation', removed, total), 'error') 
    end
end

Police Count

This is where you can configure the function that checks to see if there's enough police on duty for an illegal job to be started

-- Purpose: Function to check if there enough police on duty to do the job.
function PoliceCheck()
    local needed = Config.PoliceRequired
    local quickTable = {}

    if needed == 0 then return true end

    for _, jobName in ipairs(Config.PoliceJobs) do
        quickTable[jobName] = true
    end

    local found = 0

    for _, playerId in ipairs(GetPlayers()) do
        local xPlayer = GetPlayer(playerId)
        if xPlayer then
            if Config.Framework == "qbcore" then
                if quickTable[xPlayer.PlayerData.job.name] then
                    found = found + 1
                end
            elseif Config.Framework == "esx" then
                if quickTable[xPlayer.job.name] then
                    found = found + 1
                end
            end
            if found >= needed then
                return true
            end
        end
    end

    return false
end

Last updated