mirror of
https://github.com/jimathy/jim_bridge.git
synced 2026-08-17 05:56:02 +01:00
Quick tidy and optimizations of a few functions
This commit is contained in:
@@ -463,7 +463,7 @@ function PerformRaycast(startCoords, endCoords, entity, flags)
|
|||||||
local shapeTest = StartExpensiveSynchronousShapeTestLosProbe(
|
local shapeTest = StartExpensiveSynchronousShapeTestLosProbe(
|
||||||
startCoords.x, startCoords.y, startCoords.z,
|
startCoords.x, startCoords.y, startCoords.z,
|
||||||
endCoords.x, endCoords.y, endCoords.z,
|
endCoords.x, endCoords.y, endCoords.z,
|
||||||
flags or 4294967295, entity, 0
|
flags or 4294967295, entity, 4
|
||||||
)
|
)
|
||||||
|
|
||||||
return GetShapeTestResult(shapeTest)
|
return GetShapeTestResult(shapeTest)
|
||||||
|
|||||||
@@ -1320,7 +1320,7 @@ function hasItem(items, amount, src)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
for k, v in pairs(hasTable) do
|
for _, v in pairs(hasTable) do
|
||||||
if not v.hasItem then
|
if not v.hasItem then
|
||||||
return false, hasTable
|
return false, hasTable
|
||||||
end
|
end
|
||||||
@@ -1410,31 +1410,39 @@ end
|
|||||||
--- ```
|
--- ```
|
||||||
function createUseableItem(item, funct)
|
function createUseableItem(item, funct)
|
||||||
if doesItemExist(item) then
|
if doesItemExist(item) then
|
||||||
local itemResource = ""
|
local useableFunc = {
|
||||||
if isStarted(ESXExport) then
|
{ framework = ESXExport,
|
||||||
itemResource = ESXExport
|
func = function(item, funct)
|
||||||
while not ESX do Wait(0) end
|
while not ESX do Wait(0) end
|
||||||
ESX.RegisterUsableItem(item, funct)
|
ESX.RegisterUsableItem(item, funct)
|
||||||
|
end,
|
||||||
elseif isStarted(QBExport) and not isStarted(QBXExport) then
|
},
|
||||||
itemResource = QBExport
|
{ framework = QBXExport,
|
||||||
Core.Functions.CreateUseableItem(item, funct)
|
func = function(item, funct)
|
||||||
|
|
||||||
elseif isStarted(QBXExport) then
|
|
||||||
itemResource = QBXExport
|
|
||||||
exports[QBXExport]:CreateUseableItem(item, funct)
|
exports[QBXExport]:CreateUseableItem(item, funct)
|
||||||
|
end,
|
||||||
elseif isStarted(RSGExport) then
|
},
|
||||||
itemResource = RSGExport
|
{ framework = QBExport,
|
||||||
|
func = function(item, funct)
|
||||||
Core.Functions.CreateUseableItem(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
|
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")
|
debugPrint("^4ERROR^7: No supported framework detected for registering usable item: ^3"..item.."^7")
|
||||||
end
|
|
||||||
else
|
else
|
||||||
print("^1ERROR^7: ^1Tried to make item usable but it didn't exist^7: "..item)
|
print("^1ERROR^7: ^1Tried to make item usable but it didn't exist^7: "..item)
|
||||||
end
|
end
|
||||||
@@ -2312,6 +2320,7 @@ RegisterNetEvent(getScript()..":server:stashRemoveItem", stashRemoveItem)
|
|||||||
function stashhasItem(stashItems, items, amount)
|
function stashhasItem(stashItems, items, amount)
|
||||||
local invs = { OXInv, QSInv, CoreInv, CodeMInv, OrigenInv, TgiannInv, JPRInv, QBInv, PSInv, RSGInv }
|
local invs = { OXInv, QSInv, CoreInv, CodeMInv, OrigenInv, TgiannInv, JPRInv, QBInv, PSInv, RSGInv }
|
||||||
local foundInv = ""
|
local foundInv = ""
|
||||||
|
|
||||||
for _, inv in ipairs(invs) do
|
for _, inv in ipairs(invs) do
|
||||||
if isStarted(inv) then
|
if isStarted(inv) then
|
||||||
foundInv = inv:gsub("%-", "^7-^6"):gsub("%_", "^7_^6")
|
foundInv = inv:gsub("%-", "^7-^6"):gsub("%_", "^7_^6")
|
||||||
|
|||||||
@@ -152,17 +152,30 @@ end
|
|||||||
--- toggleDuty() -- Player receives a notification of their new duty status.
|
--- toggleDuty() -- Player receives a notification of their new duty status.
|
||||||
--- ```
|
--- ```
|
||||||
function toggleDuty()
|
function toggleDuty()
|
||||||
if isStarted(QBExport) or isStarted(QBXExport) then
|
local dutyFunc = {
|
||||||
|
{ framework = QBExport,
|
||||||
|
func = function()
|
||||||
TriggerServerEvent("QBCore:ToggleDuty")
|
TriggerServerEvent("QBCore:ToggleDuty")
|
||||||
Wait(100)
|
Wait(100)
|
||||||
onDuty = getPlayer().onDuty
|
onDuty = getPlayer().onDuty
|
||||||
|
end
|
||||||
elseif isStarted(RSGExport) then
|
},
|
||||||
|
{ framework = QBXExport,
|
||||||
|
func = function()
|
||||||
|
TriggerServerEvent("QBCore:ToggleDuty")
|
||||||
|
Wait(100)
|
||||||
|
onDuty = getPlayer().onDuty
|
||||||
|
end
|
||||||
|
},
|
||||||
|
{ framework = RSGExport,
|
||||||
|
func = function()
|
||||||
TriggerServerEvent("RSGCore:ToggleDuty")
|
TriggerServerEvent("RSGCore:ToggleDuty")
|
||||||
Wait(100)
|
Wait(100)
|
||||||
onDuty = getPlayer().onDuty
|
onDuty = getPlayer().onDuty
|
||||||
|
end
|
||||||
elseif isStarted(ESXExport) then
|
},
|
||||||
|
{ framework = ESXExport,
|
||||||
|
func = function()
|
||||||
local tempJob = ESX.GetPlayerData().job
|
local tempJob = ESX.GetPlayerData().job
|
||||||
tempJob.onDuty = not onDuty
|
tempJob.onDuty = not onDuty
|
||||||
ESX.SetPlayerData("job", tempJob)
|
ESX.SetPlayerData("job", tempJob)
|
||||||
@@ -172,16 +185,25 @@ function toggleDuty()
|
|||||||
else
|
else
|
||||||
triggerNotify(nil, "Now off duty", "success")
|
triggerNotify(nil, "Now off duty", "success")
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
else
|
for i = 1, #dutyFunc do
|
||||||
|
local framework = dutyFunc[i]
|
||||||
|
if isStarted(framework.framework) then
|
||||||
|
framework.func()
|
||||||
|
return
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- fallback
|
||||||
onDuty = not onDuty
|
onDuty = not onDuty
|
||||||
if onDuty then
|
if onDuty then
|
||||||
triggerNotify(nil, "Now on duty", "success")
|
triggerNotify(nil, "Now on duty", "success")
|
||||||
else
|
else
|
||||||
triggerNotify(nil, "Now off duty", "success")
|
triggerNotify(nil, "Now off duty", "success")
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Framework specific functions to keep duty status synced
|
-- Framework specific functions to keep duty status synced
|
||||||
|
|||||||
@@ -53,7 +53,10 @@ local polyCreation = {
|
|||||||
return zone
|
return zone
|
||||||
end,
|
end,
|
||||||
createCircle = function(data)
|
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)
|
zone:onPlayerInOut(function(isPointInside)
|
||||||
if isPointInside then
|
if isPointInside then
|
||||||
data.onEnter()
|
data.onEnter()
|
||||||
|
|||||||
@@ -22,7 +22,8 @@ local skillCheckFunc = {
|
|||||||
"2",
|
"2",
|
||||||
"3",
|
"3",
|
||||||
"4"
|
"4"
|
||||||
})
|
}
|
||||||
|
)
|
||||||
if Skillbar then
|
if Skillbar then
|
||||||
return true
|
return true
|
||||||
else
|
else
|
||||||
|
|||||||
@@ -105,17 +105,29 @@ end
|
|||||||
function getVehicleProperties(vehicle)
|
function getVehicleProperties(vehicle)
|
||||||
if not vehicle then return nil end
|
if not vehicle then return nil end
|
||||||
|
|
||||||
local properties = {}
|
local propertyFunc = {
|
||||||
if isStarted(QBExport) and not isStarted(QBXExport) then
|
{ framework = OXLibExport,
|
||||||
properties = Core.Functions.GetVehicleProperties(vehicle)
|
func = function(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]")
|
return lib.getVehicleProperties(vehicle)
|
||||||
|
end,
|
||||||
elseif isStarted(OXLibExport) then
|
},
|
||||||
properties = lib.getVehicleProperties(vehicle)
|
{ framework = QBExport,
|
||||||
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]")
|
func = function(vehicle)
|
||||||
end
|
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
|
return properties
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
--- Sets the properties of a given vehicle if changes are detected.
|
--- Sets the properties of a given vehicle if changes are detected.
|
||||||
--- It compares the current properties with the new ones and applies the update using the active framework.
|
--- It compares the current properties with the new ones and applies the update using the active framework.
|
||||||
@@ -228,7 +240,7 @@ function pushVehicle(entity)
|
|||||||
if entity ~= 0 and DoesEntityExist(entity) then
|
if entity ~= 0 and DoesEntityExist(entity) then
|
||||||
-- Request network control if not already controlled.
|
-- Request network control if not already controlled.
|
||||||
if not NetworkHasControlOfEntity(entity) then
|
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)
|
NetworkRequestControlOfEntity(entity)
|
||||||
local timeout = 2000
|
local timeout = 2000
|
||||||
while timeout > 0 and not NetworkHasControlOfEntity(entity) do
|
while timeout > 0 and not NetworkHasControlOfEntity(entity) do
|
||||||
@@ -236,13 +248,13 @@ function pushVehicle(entity)
|
|||||||
timeout = timeout - 100
|
timeout = timeout - 100
|
||||||
end
|
end
|
||||||
if NetworkHasControlOfEntity(entity) then
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Set as mission entity if not already set.
|
-- Set as mission entity if not already set.
|
||||||
if not IsEntityAMissionEntity(entity) then
|
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)
|
SetEntityAsMissionEntity(entity, true, true)
|
||||||
local timeout = 2000
|
local timeout = 2000
|
||||||
while timeout > 0 and not IsEntityAMissionEntity(entity) do
|
while timeout > 0 and not IsEntityAMissionEntity(entity) do
|
||||||
@@ -250,18 +262,23 @@ function pushVehicle(entity)
|
|||||||
timeout = timeout - 100
|
timeout = timeout - 100
|
||||||
end
|
end
|
||||||
if IsEntityAMissionEntity(entity) then
|
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
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- add entitty named version
|
||||||
|
function pushEnt(...) pushVehicle(...) end
|
||||||
|
|
||||||
|
|
||||||
--- Finds the closest vehicle to the specified coordinates.
|
--- Finds the closest vehicle to the specified coordinates.
|
||||||
--- The function uses different APIs based on whether a source is provided.
|
--- 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 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.
|
--- @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
|
--- @usage
|
||||||
--- ```lua
|
--- ```lua
|
||||||
@@ -271,9 +288,11 @@ function getClosestVehicle(coords, src)
|
|||||||
local ped, vehicles, closestDistance, closestVehicle
|
local ped, vehicles, closestDistance, closestVehicle
|
||||||
|
|
||||||
if src then
|
if src then
|
||||||
|
-- if checking server side cache src's ped and use server native
|
||||||
ped = GetPlayerPed(src)
|
ped = GetPlayerPed(src)
|
||||||
vehicles = GetAllVehicles()
|
vehicles = GetAllVehicles()
|
||||||
else
|
else
|
||||||
|
-- if checking client side cache local ped and use client native
|
||||||
ped = PlayerPedId()
|
ped = PlayerPedId()
|
||||||
vehicles = GetGamePool('CVehicle')
|
vehicles = GetGamePool('CVehicle')
|
||||||
end
|
end
|
||||||
@@ -290,7 +309,7 @@ function getClosestVehicle(coords, src)
|
|||||||
|
|
||||||
for i = 1, #vehicles, 1 do
|
for i = 1, #vehicles, 1 do
|
||||||
local vehicleCoords = GetEntityCoords(vehicles[i])
|
local vehicleCoords = GetEntityCoords(vehicles[i])
|
||||||
local distance = #(vehicleCoords - coords)
|
local distance = #(vehicleCoords - coords.xyz)
|
||||||
|
|
||||||
if closestDistance == -1 or distance < closestDistance then
|
if closestDistance == -1 or distance < closestDistance then
|
||||||
closestDistance = distance
|
closestDistance = distance
|
||||||
@@ -311,19 +330,46 @@ end
|
|||||||
--- local plate = "ABCD1234"
|
--- local plate = "ABCD1234"
|
||||||
--- local isVehicleOwned = isVehicleOwned(plate)
|
--- local isVehicleOwned = isVehicleOwned(plate)
|
||||||
--- ```
|
--- ```
|
||||||
|
|
||||||
local vehiclesOwned = {}
|
local vehiclesOwned = {}
|
||||||
|
|
||||||
function isVehicleOwned(plate)
|
function isVehicleOwned(plate)
|
||||||
vehDatabase = "player_vehicles"
|
-- If already checked, cache it to reduce database calls
|
||||||
if isStarted(ESXExport) then vehDatabase = "owned_vehicles"
|
|
||||||
elseif isStarted(OXCoreExport) then vehDatabase = "vehicles" end
|
|
||||||
|
|
||||||
if vehiclesOwned[plate] == true then
|
if vehiclesOwned[plate] == true then
|
||||||
return true
|
return true
|
||||||
else
|
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
|
if json.encode(result) ~= "[]" then
|
||||||
vehiclesOwned[plate] = true
|
vehiclesOwned[plate] = true -- Cache ownership for later checks
|
||||||
return true
|
return true
|
||||||
else
|
else
|
||||||
return false
|
return false
|
||||||
|
|||||||
Reference in New Issue
Block a user