feat(es_extended/server/modules/onesync): add optional vehicleType to SpawnVehicle

This commit is contained in:
Kenshin13
2025-04-28 18:01:48 +02:00
parent 5722873472
commit 372c8a224d
+44 -34
View File
@@ -78,57 +78,67 @@ function ESX.OneSync.GetClosestPlayer(source, maxDistance, ignore)
return getNearbyPlayers(source, true, maxDistance, ignore)
end
---@param model number|string
---@param vehicleModel number|string
---@param coords vector3|table
---@param heading number
---@param properties table
---@param vehicleProperties table
---@param cb? fun(netId: number)
---@param vehicleType string?
---@return number? netId
function ESX.OneSync.SpawnVehicle(model, coords, heading, properties, cb)
function ESX.OneSync.SpawnVehicle(vehicleModel, coords, heading, vehicleProperties, cb, vehicleType)
if cb and not ESX.IsFunctionReference(cb) then
error("Invalid callback function")
end
local vehicleModel = joaat(model)
local vehicleProperties = properties
vehicleModel = joaat(vehicleModel)
local promise = not cb and promise.new()
local function resolve(result)
if promise then
promise:resolve(result)
elseif cb then
cb(result)
end
end
local function reject(err)
if promise then
promise:reject(err)
end
error(err)
end
CreateThread(function()
local xPlayer = ESX.OneSync.GetClosestPlayer(coords, 300)
ESX.GetVehicleType(vehicleModel, xPlayer.id, function(vehicleType)
if not vehicleType then
if (promise) then
return promise:reject(("Tried to spawn invalid vehicle - ^5%s^7!"):format(model))
end
error(("Tried to spawn invalid vehicle - ^5%s^7!"):format(model))
if not vehicleType then
local xPlayer = ESX.OneSync.GetClosestPlayer(coords, 300)
if not xPlayer then
return reject("No players found nearby to check vehicle type!")
end
vehicleType = ESX.GetVehicleType(vehicleModel, xPlayer.id)
end
local createdVehicle = CreateVehicleServerSetter(vehicleModel, vehicleType, coords.x, coords.y, coords.z, heading)
local tries = 0
if not vehicleType then
return reject(("Tried to spawn invalid vehicle - ^5%s^7!"):format(vehicleModel))
end
while not createdVehicle or createdVehicle == 0 or NetworkGetEntityOwner(createdVehicle) == -1 do
Wait(200)
tries = tries + 1
if tries > 40 then
if promise then
return promise:reject(("Could not spawn vehicle - ^5%s^7!"):format(model))
end
error(("Could not spawn vehicle - ^5%s^7!"):format(model))
end
local createdVehicle = CreateVehicleServerSetter(vehicleModel, vehicleType, coords.x, coords.y, coords.z, heading)
local tries = 0
while not createdVehicle or createdVehicle == 0 or NetworkGetEntityOwner(createdVehicle) == -1 do
Wait(200)
tries = tries + 1
if tries > 40 then
return reject(("Could not spawn vehicle - ^5%s^7!"):format(vehicleModel))
end
end
-- luacheck: ignore
SetEntityOrphanMode(createdVehicle, 2)
local networkId = NetworkGetNetworkIdFromEntity(createdVehicle)
Entity(createdVehicle).state:set("VehicleProperties", vehicleProperties, true)
-- luacheck: ignore
SetEntityOrphanMode(createdVehicle, 2)
local networkId = NetworkGetNetworkIdFromEntity(createdVehicle)
Entity(createdVehicle).state:set("VehicleProperties", vehicleProperties, true)
if promise then
promise:resolve(networkId)
elseif cb then
cb(networkId)
end
end)
resolve(networkId)
end)
if promise then