From fffef0c2285bdecd1197a8c15a6fd074c7eb79d1 Mon Sep 17 00:00:00 2001 From: Jim Shield Date: Fri, 5 Sep 2025 14:37:39 +0100 Subject: [PATCH] Quick tidy and optimizations of a few functions --- shared/helpers.lua | 2 +- shared/itemcontrol.lua | 57 +++++++++++++++----------- shared/jobfunctions.lua | 72 +++++++++++++++++++++------------ shared/polyZone.lua | 5 ++- shared/skillcheck.lua | 3 +- shared/vehicles.lua | 88 +++++++++++++++++++++++++++++++---------- 6 files changed, 154 insertions(+), 73 deletions(-) diff --git a/shared/helpers.lua b/shared/helpers.lua index 42ae696..fdd203c 100644 --- a/shared/helpers.lua +++ b/shared/helpers.lua @@ -463,7 +463,7 @@ function PerformRaycast(startCoords, endCoords, entity, flags) local shapeTest = StartExpensiveSynchronousShapeTestLosProbe( startCoords.x, startCoords.y, startCoords.z, endCoords.x, endCoords.y, endCoords.z, - flags or 4294967295, entity, 0 + flags or 4294967295, entity, 4 ) return GetShapeTestResult(shapeTest) diff --git a/shared/itemcontrol.lua b/shared/itemcontrol.lua index ad7b803..e28dfc0 100644 --- a/shared/itemcontrol.lua +++ b/shared/itemcontrol.lua @@ -1320,7 +1320,7 @@ function hasItem(items, amount, src) end end end - for k, v in pairs(hasTable) do + for _, v in pairs(hasTable) do if not v.hasItem then return false, hasTable end @@ -1410,31 +1410,39 @@ end --- ``` function createUseableItem(item, funct) if doesItemExist(item) then - local itemResource = "" - if isStarted(ESXExport) then - itemResource = ESXExport - while not ESX do Wait(0) end - ESX.RegisterUsableItem(item, funct) - - elseif isStarted(QBExport) and not isStarted(QBXExport) then - itemResource = QBExport - Core.Functions.CreateUseableItem(item, funct) - - elseif isStarted(QBXExport) then - itemResource = QBXExport - exports[QBXExport]:CreateUseableItem(item, funct) - - elseif isStarted(RSGExport) then - itemResource = RSGExport - Core.Functions.CreateUseableItem(item, funct) + local useableFunc = { + { framework = ESXExport, + func = function(item, funct) + while not ESX do Wait(0) end + ESX.RegisterUsableItem(item, funct) + end, + }, + { framework = QBXExport, + func = function(item, funct) + exports[QBXExport]:CreateUseableItem(item, funct) + end, + }, + { framework = QBExport, + func = function(item, funct) + Core.Functions.CreateUseableItem(item, funct) + end, + }, + { framework = RSGExport, + func = function(item, funct) + Core.Functions.CreateUseableItem(item, funct) + end, + }, + } + for i = 1, #useableFunc do + local framework = useableFunc[i] + if isStarted(framework.framework) then + debugPrint("^6Bridge^7: ^2Registering ^3UsableItem^2 with ^4"..framework.framework.."^7:", item) + framework.func(item, funct) + return + end end - - if itemResource ~= "" then - debugPrint("^6Bridge^7: ^2Registering ^3UsableItem^2 with ^4"..itemResource.."^7:", item) - else - debugPrint("^4ERROR^7: No supported framework detected for registering usable item: ^3"..item.."^7") - end + debugPrint("^4ERROR^7: No supported framework detected for registering usable item: ^3"..item.."^7") else print("^1ERROR^7: ^1Tried to make item usable but it didn't exist^7: "..item) end @@ -2312,6 +2320,7 @@ RegisterNetEvent(getScript()..":server:stashRemoveItem", stashRemoveItem) function stashhasItem(stashItems, items, amount) local invs = { OXInv, QSInv, CoreInv, CodeMInv, OrigenInv, TgiannInv, JPRInv, QBInv, PSInv, RSGInv } local foundInv = "" + for _, inv in ipairs(invs) do if isStarted(inv) then foundInv = inv:gsub("%-", "^7-^6"):gsub("%_", "^7_^6") diff --git a/shared/jobfunctions.lua b/shared/jobfunctions.lua index e42537b..ad43a5c 100644 --- a/shared/jobfunctions.lua +++ b/shared/jobfunctions.lua @@ -152,35 +152,57 @@ end --- toggleDuty() -- Player receives a notification of their new duty status. --- ``` function toggleDuty() - if isStarted(QBExport) or isStarted(QBXExport) then - TriggerServerEvent("QBCore:ToggleDuty") - Wait(100) - onDuty = getPlayer().onDuty + local dutyFunc = { + { framework = QBExport, + func = function() + TriggerServerEvent("QBCore:ToggleDuty") + Wait(100) + onDuty = getPlayer().onDuty + end + }, + { framework = QBXExport, + func = function() + TriggerServerEvent("QBCore:ToggleDuty") + Wait(100) + onDuty = getPlayer().onDuty + end + }, + { framework = RSGExport, + func = function() + TriggerServerEvent("RSGCore:ToggleDuty") + Wait(100) + onDuty = getPlayer().onDuty + end + }, + { framework = ESXExport, + func = function() + local tempJob = ESX.GetPlayerData().job + tempJob.onDuty = not onDuty + ESX.SetPlayerData("job", tempJob) + onDuty = getPlayer().onDuty + if onDuty then + triggerNotify(nil, "Now on duty", "success") + else + triggerNotify(nil, "Now off duty", "success") + end + end + }, + } - elseif isStarted(RSGExport) then - TriggerServerEvent("RSGCore:ToggleDuty") - Wait(100) - onDuty = getPlayer().onDuty - - elseif isStarted(ESXExport) then - local tempJob = ESX.GetPlayerData().job - tempJob.onDuty = not onDuty - ESX.SetPlayerData("job", tempJob) - onDuty = getPlayer().onDuty - if onDuty then - triggerNotify(nil, "Now on duty", "success") - else - triggerNotify(nil, "Now off duty", "success") + for i = 1, #dutyFunc do + local framework = dutyFunc[i] + if isStarted(framework.framework) then + framework.func() + return end + end + -- fallback + onDuty = not onDuty + if onDuty then + triggerNotify(nil, "Now on duty", "success") else - onDuty = not onDuty - if onDuty then - triggerNotify(nil, "Now on duty", "success") - else - triggerNotify(nil, "Now off duty", "success") - end - + triggerNotify(nil, "Now off duty", "success") end end diff --git a/shared/polyZone.lua b/shared/polyZone.lua index faf901a..f24f432 100644 --- a/shared/polyZone.lua +++ b/shared/polyZone.lua @@ -53,7 +53,10 @@ local polyCreation = { return zone end, createCircle = function(data) - local zone = CircleZone:Create(data.coords, data.radius, { name = data.name, debugPoly = debugMode }) + local zone = CircleZone:Create(data.coords, data.radius, { + name = data.name, + debugPoly = data.debug + }) zone:onPlayerInOut(function(isPointInside) if isPointInside then data.onEnter() diff --git a/shared/skillcheck.lua b/shared/skillcheck.lua index ebf1efd..dd517c7 100644 --- a/shared/skillcheck.lua +++ b/shared/skillcheck.lua @@ -22,7 +22,8 @@ local skillCheckFunc = { "2", "3", "4" - }) + } + ) if Skillbar then return true else diff --git a/shared/vehicles.lua b/shared/vehicles.lua index b6a2337..634e6f3 100644 --- a/shared/vehicles.lua +++ b/shared/vehicles.lua @@ -105,16 +105,28 @@ end function getVehicleProperties(vehicle) if not vehicle then return nil end - local properties = {} - if isStarted(QBExport) and not isStarted(QBXExport) then - properties = Core.Functions.GetVehicleProperties(vehicle) - debugPrint("^6Bridge^7: ^2Getting Vehicle Properties ^7[^6"..QBExport.."^7] - [^3"..vehicle.."^7] - [^3"..GetEntityModel(vehicle).."^7/^3"..properties.model.."^7] - [^3"..properties.plate.."^7]") - - elseif isStarted(OXLibExport) then - properties = lib.getVehicleProperties(vehicle) - debugPrint("^6Bridge^7: ^2Getting Vehicle Properties ^7[^6"..OXLibExport.."^7] - [^3"..vehicle.."^7] - [^3"..GetEntityModel(vehicle).."^7/^3"..properties.model.."^7] - [^3"..properties.plate.."^7]") + local propertyFunc = { + { framework = OXLibExport, + func = function(vehicle) + return lib.getVehicleProperties(vehicle) + end, + }, + { framework = QBExport, + func = function(vehicle) + return Core.Functions.GetVehicleProperties(vehicle) + end, + }, + } + for i = 1, #propertyFunc do + local prop = propertyFunc[i] + if isStarted(prop.framework) then + local properties = prop.func(vehicle) + debugPrint("^6Bridge^7: ^2Getting Vehicle Properties ^7[^6"..prop.framework.."^7] - [^3"..vehicle.."^7] - [^3"..GetEntityModel(vehicle).."^7/^3"..properties.model.."^7] - [^3"..properties.plate.."^7]") + return properties + end end - return properties + + return nil end --- Sets the properties of a given vehicle if changes are detected. @@ -228,7 +240,7 @@ function pushVehicle(entity) if entity ~= 0 and DoesEntityExist(entity) then -- Request network control if not already controlled. if not NetworkHasControlOfEntity(entity) then - debugPrint("^6Bridge^7: ^3pushVehicle^7: ^2Requesting network control of vehicle^7.") + debugPrint("^6Bridge^7: ^3pushEnt^7: ^2Requesting network control of vehicle^7.") NetworkRequestControlOfEntity(entity) local timeout = 2000 while timeout > 0 and not NetworkHasControlOfEntity(entity) do @@ -236,13 +248,13 @@ function pushVehicle(entity) timeout = timeout - 100 end if NetworkHasControlOfEntity(entity) then - debugPrint("^6Bridge^7: ^3pushVehicle^7: ^2Network now has control of the entity^7.") + debugPrint("^6Bridge^7: ^3pushEnt^7: ^2Network now has control of the entity^7.") end end -- Set as mission entity if not already set. if not IsEntityAMissionEntity(entity) then - debugPrint("^6Bridge^7: ^3pushVehicle^7: ^2Setting vehicle as a ^7'^2mission^7' ^2entity^7.") + debugPrint("^6Bridge^7: ^3pushEnt^7: ^2Setting vehicle as a ^7'^2mission^7' ^2entity^7.") SetEntityAsMissionEntity(entity, true, true) local timeout = 2000 while timeout > 0 and not IsEntityAMissionEntity(entity) do @@ -250,18 +262,23 @@ function pushVehicle(entity) timeout = timeout - 100 end if IsEntityAMissionEntity(entity) then - debugPrint("^6Bridge^7: ^3pushVehicle^7: ^2Vehicle is a ^7'^2mission^7'^2 entity^7.") + debugPrint("^6Bridge^7: ^3pushEnt^7: ^2Vehicle is a ^7'^2mission^7'^2 entity^7.") end end end end +-- add entitty named version +function pushEnt(...) pushVehicle(...) end + + --- Finds the closest vehicle to the specified coordinates. --- The function uses different APIs based on whether a source is provided. --- --- @param coords table|vector3 (Optional) The reference coordinates. If nil, uses the player's position. --- @param src boolean (Optional) If true, uses GetPlayerPed(source) and GetAllVehicles. ---- @return number|number closestVehicle|closestDistance The closest vehicle entity and its distance. +--- @return number closestVehicle The closest vehicle entity and its distance. +--- @return number closestDistance The distance of the closest vehicle. --- --- @usage --- ```lua @@ -271,9 +288,11 @@ function getClosestVehicle(coords, src) local ped, vehicles, closestDistance, closestVehicle if src then + -- if checking server side cache src's ped and use server native ped = GetPlayerPed(src) vehicles = GetAllVehicles() else + -- if checking client side cache local ped and use client native ped = PlayerPedId() vehicles = GetGamePool('CVehicle') end @@ -290,7 +309,7 @@ function getClosestVehicle(coords, src) for i = 1, #vehicles, 1 do local vehicleCoords = GetEntityCoords(vehicles[i]) - local distance = #(vehicleCoords - coords) + local distance = #(vehicleCoords - coords.xyz) if closestDistance == -1 or distance < closestDistance then closestDistance = distance @@ -311,19 +330,46 @@ end --- local plate = "ABCD1234" --- local isVehicleOwned = isVehicleOwned(plate) --- ``` + local vehiclesOwned = {} function isVehicleOwned(plate) - vehDatabase = "player_vehicles" - if isStarted(ESXExport) then vehDatabase = "owned_vehicles" - elseif isStarted(OXCoreExport) then vehDatabase = "vehicles" end - + -- If already checked, cache it to reduce database calls if vehiclesOwned[plate] == true then return true else - local result = MySQL.query.await("SELECT 1 from "..vehDatabase.." WHERE plate = ?", { plate }) + -- Find frameworks vehicle table and search sql for if vehicle plate is owned + local sqlTable = "player_vehicles" + local vehDatabase = { + + { framework = ESXExport, + sqlTable = "owned_vehicles" + }, + + { framework = QBExport, + sqlTable = "player_vehicles" + }, + + { framework = QBXExport, + sqlTable = "player_vehicles" + }, + + { framework = OXCoreExport, + sqlTable = "owned_vehicles" + }, + + } + + for i = 1, #vehDatabase do + local framework = vehDatabase[i] + if isStarted(framework.framework) then + sqlTable = framework.sqlTable + end + end + + local result = MySQL.query.await("SELECT 1 from "..sqlTable.." WHERE plate = ?", { plate }) if json.encode(result) ~= "[]" then - vehiclesOwned[plate] = true + vehiclesOwned[plate] = true -- Cache ownership for later checks return true else return false