From 9adb33b970f16c92aaa2fe6d341ec23a716e0de6 Mon Sep 17 00:00:00 2001 From: Kakarot <57848836+GhzGarage@users.noreply.github.com> Date: Sat, 25 Jan 2025 00:36:31 -0600 Subject: [PATCH] Core object filtering + exported functions/data Maintains backwards compatibility and allows users to only get what they need from the core instead of importing it all --- client/functions.lua | 9 ++++++ client/main.lua | 48 ++++++++++++++++++++++++++++---- fxmanifest.lua | 2 +- server/functions.lua | 66 ++++++++++++++++++++++++-------------------- server/main.lua | 48 ++++++++++++++++++++++++++++---- 5 files changed, 130 insertions(+), 43 deletions(-) diff --git a/client/functions.lua b/client/functions.lua index 737b12c..f6bca00 100644 --- a/client/functions.lua +++ b/client/functions.lua @@ -1094,3 +1094,12 @@ function QBCore.Functions.GetGroundHash(entity) local retval, success, endCoords, surfaceNormal, materialHash, entityHit = GetShapeTestResultEx(num) return materialHash, entityHit, surfaceNormal, endCoords, success, retval end + +for functionName, func in pairs(QBCore.Functions) do + if type(func) == 'function' then + exports(functionName, func) + end +end + +-- Access a specific function directly: +-- exports['qb-core']:Notify('Hello Player!') diff --git a/client/main.lua b/client/main.lua index 7407a6d..05d3b68 100644 --- a/client/main.lua +++ b/client/main.lua @@ -5,10 +5,46 @@ QBCore.Shared = QBShared QBCore.ClientCallbacks = {} QBCore.ServerCallbacks = {} -exports('GetCoreObject', function() - return QBCore -end) +-- Get the full QBCore object (default behavior): +-- local QBCore = GetCoreObject() --- To use this export in a script instead of manifest method --- Just put this line of code below at the very top of the script --- local QBCore = exports['qb-core']:GetCoreObject() +-- Get only specific parts of QBCore: +-- local QBCore = GetCoreObject({'Players', 'Config'}) + +local function GetCoreObject(filters) + if not filters then return QBCore end + local results = {} + for i = 1, #filters do + local key = filters[i] + if QBCore[key] then + results[key] = QBCore[key] + end + end + return results +end +exports('GetCoreObject', GetCoreObject) + +local function GetSharedItems() + return QBShared.Items +end +exports('GetSharedItems', GetSharedItems) + +local function GetSharedVehicles() + return QBShared.Vehicles +end +exports('GetSharedVehicles', GetSharedVehicles) + +local function GetSharedWeapons() + return QBShared.Weapons +end +exports('GetSharedWeapons', GetSharedWeapons) + +local function GetSharedJobs() + return QBShared.Jobs +end +exports('GetSharedJobs', GetSharedJobs) + +local function GetSharedGangs() + return QBShared.Gangs +end +exports('GetSharedGangs', GetSharedGangs) diff --git a/fxmanifest.lua b/fxmanifest.lua index bc0da07..924a561 100644 --- a/fxmanifest.lua +++ b/fxmanifest.lua @@ -3,7 +3,7 @@ game 'gta5' lua54 'yes' author 'Kakarot' description 'Core resource for the framework, contains all the core functionality and features' -version '1.2.6' +version '1.3.0' shared_scripts { 'config.lua', diff --git a/server/functions.lua b/server/functions.lua index d9bfc57..8f81093 100644 --- a/server/functions.lua +++ b/server/functions.lua @@ -394,33 +394,30 @@ function QBCore.Functions.CreateVehicle(source, model, vehtype, coords, warp) return veh end ----Paychecks (standalone - don't touch) function PaycheckInterval() - if next(QBCore.Players) then - for _, Player in pairs(QBCore.Players) do - if Player then - local payment = QBShared.Jobs[Player.PlayerData.job.name]['grades'][tostring(Player.PlayerData.job.grade.level)].payment - if not payment then payment = Player.PlayerData.job.payment end - if Player.PlayerData.job and payment > 0 and (QBShared.Jobs[Player.PlayerData.job.name].offDutyPay or Player.PlayerData.job.onduty) then - if QBCore.Config.Money.PayCheckSociety then - local account = exports['qb-banking']:GetAccountBalance(Player.PlayerData.job.name) - if account ~= 0 then -- Checks if player is employed by a society - if account < payment then -- Checks if company has enough money to pay society - TriggerClientEvent('QBCore:Notify', Player.PlayerData.source, Lang:t('error.company_too_poor'), 'error') - else - Player.Functions.AddMoney('bank', payment, 'paycheck') - exports['qb-banking']:RemoveMoney(Player.PlayerData.job.name, payment, 'Employee Paycheck') - TriggerClientEvent('QBCore:Notify', Player.PlayerData.source, Lang:t('info.received_paycheck', { value = payment })) - end - else - Player.Functions.AddMoney('bank', payment, 'paycheck') - TriggerClientEvent('QBCore:Notify', Player.PlayerData.source, Lang:t('info.received_paycheck', { value = payment })) - end + if not next(QBCore.Players) then return end + for _, Player in pairs(QBCore.Players) do + if not Player then return end + local payment = QBShared.Jobs[Player.PlayerData.job.name]['grades'][tostring(Player.PlayerData.job.grade.level)].payment + if not payment then payment = Player.PlayerData.job.payment end + if Player.PlayerData.job and payment > 0 and (QBShared.Jobs[Player.PlayerData.job.name].offDutyPay or Player.PlayerData.job.onduty) then + if QBCore.Config.Money.PayCheckSociety then + local account = exports['qb-banking']:GetAccountBalance(Player.PlayerData.job.name) + if account ~= 0 then + if account < payment then + TriggerClientEvent('QBCore:Notify', Player.PlayerData.source, Lang:t('error.company_too_poor'), 'error') else Player.Functions.AddMoney('bank', payment, 'paycheck') + exports['qb-banking']:RemoveMoney(Player.PlayerData.job.name, payment, 'Employee Paycheck') TriggerClientEvent('QBCore:Notify', Player.PlayerData.source, Lang:t('info.received_paycheck', { value = payment })) end + else + Player.Functions.AddMoney('bank', payment, 'paycheck') + TriggerClientEvent('QBCore:Notify', Player.PlayerData.source, Lang:t('info.received_paycheck', { value = payment })) end + else + Player.Functions.AddMoney('bank', payment, 'paycheck') + TriggerClientEvent('QBCore:Notify', Player.PlayerData.source, Lang:t('info.received_paycheck', { value = payment })) end end end @@ -471,7 +468,7 @@ end function QBCore.Functions.CreateUseableItem(item, data) local rawFunc = nil - if type(data) == "table" then + if type(data) == 'table' then if rawget(data, '__cfx_functionReference') then rawFunc = data elseif data.cb and rawget(data.cb, '__cfx_functionReference') then @@ -479,7 +476,7 @@ function QBCore.Functions.CreateUseableItem(item, data) elseif data.callback and rawget(data.callback, '__cfx_functionReference') then rawFunc = data.callback end - elseif type(data) == "function" then + elseif type(data) == 'function' then rawFunc = data end @@ -654,23 +651,23 @@ end function QBCore.Functions.GetDatabaseInfo() local details = { exists = false, - database = "", + database = '', } - local connectionString = GetConvar("mysql_connection_string", "") + local connectionString = GetConvar('mysql_connection_string', '') - if connectionString == "" then + if connectionString == '' then return details - elseif connectionString:find("mysql://") then + elseif connectionString:find('mysql://') then connectionString = connectionString:sub(9, -1) - details.database = connectionString:sub(connectionString:find("/") + 1, -1):gsub("[%?]+[%w%p]*$", "") + details.database = connectionString:sub(connectionString:find('/') + 1, -1):gsub('[%?]+[%w%p]*$', '') details.exists = true return details else - connectionString = { string.strsplit(";", connectionString) } + connectionString = { string.strsplit(';', connectionString) } for i = 1, #connectionString do local v = connectionString[i] - if v:match("database") then + if v:match('database') then details.database = v:sub(10, #v) details.exists = true return details @@ -728,3 +725,12 @@ function QBCore.Functions.PrepForSQL(source, data, pattern) end return true end + +for functionName, func in pairs(QBCore.Functions) do + if type(func) == 'function' then + exports(functionName, func) + end +end + +-- Access a specific function directly: +-- exports['qb-core']:Notify(source, 'Hello Player!') diff --git a/server/main.lua b/server/main.lua index 368dc05..d6228e5 100644 --- a/server/main.lua +++ b/server/main.lua @@ -4,10 +4,46 @@ QBCore.Shared = QBShared QBCore.ClientCallbacks = {} QBCore.ServerCallbacks = {} -exports('GetCoreObject', function() - return QBCore -end) +-- Get the full QBCore object (default behavior): +-- local QBCore = GetCoreObject() --- To use this export in a script instead of manifest method --- Just put this line of code below at the very top of the script --- local QBCore = exports['qb-core']:GetCoreObject() +-- Get only specific parts of QBCore: +-- local QBCore = GetCoreObject({'Players', 'Config'}) + +local function GetCoreObject(filters) + if not filters then return QBCore end + local results = {} + for i = 1, #filters do + local key = filters[i] + if QBCore[key] then + results[key] = QBCore[key] + end + end + return results +end +exports('GetCoreObject', GetCoreObject) + +local function GetSharedItems() + return QBShared.Items +end +exports('GetSharedItems', GetSharedItems) + +local function GetSharedVehicles() + return QBShared.Vehicles +end +exports('GetSharedVehicles', GetSharedVehicles) + +local function GetSharedWeapons() + return QBShared.Weapons +end +exports('GetSharedWeapons', GetSharedWeapons) + +local function GetSharedJobs() + return QBShared.Jobs +end +exports('GetSharedJobs', GetSharedJobs) + +local function GetSharedGangs() + return QBShared.Gangs +end +exports('GetSharedGangs', GetSharedGangs)