Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(es_extended): add promise support for vehicle spawn functions #1539

Merged
merged 5 commits into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions [core]/es_extended/client/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -530,10 +530,14 @@ end
---@param vehicleModel integer | string The vehicle to spawn
---@param coords table | vector3 The coords to spawn the vehicle at
---@param heading number The heading of the vehicle
---@param cb? function The callback function
---@param cb? fun(vehicle: number) The callback function
---@param networked? boolean Whether the vehicle should be networked
---@return nil
---@return number? vehicle
function ESX.Game.SpawnVehicle(vehicleModel, coords, heading, cb, networked)
if cb and not ESX.IsFunctionReference(cb) then
error("Invalid callback function")
end

local model = type(vehicleModel) == "number" and vehicleModel or joaat(vehicleModel)
local vector = type(coords) == "vector3" and coords or vec(coords.x, coords.y, coords.z)
local isNetworked = networked == nil or networked
Expand All @@ -549,8 +553,15 @@ function ESX.Game.SpawnVehicle(vehicleModel, coords, heading, cb, networked)
return error(("Resource ^5%s^1 Tried to spawn vehicle on the client but the position is too far away (Out of onesync range)."):format(executingResource))
end

local promise = not cb and promise.new()
CreateThread(function()
ESX.Streaming.RequestModel(model)
local modelHash = ESX.Streaming.RequestModel(model)
if not modelHash 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))
end

local vehicle = CreateVehicle(model, vector.x, vector.y, vector.z, heading, isNetworked, true)

Expand All @@ -569,10 +580,16 @@ function ESX.Game.SpawnVehicle(vehicleModel, coords, heading, cb, networked)
Wait(0)
end

if cb then
if promise then
promise:resolve(vehicle)
elseif cb then
cb(vehicle)
end
end)

if promise then
return Citizen.Await(promise)
end
end

---@param vehicle integer The vehicle to spawn
Expand Down
54 changes: 38 additions & 16 deletions [core]/es_extended/server/modules/onesync.lua
Original file line number Diff line number Diff line change
Expand Up @@ -82,35 +82,57 @@ end
---@param coords vector3|table
---@param heading number
---@param properties table
---@param cb function
---@param cb? fun(netId: number)
---@return number? netId
function ESX.OneSync.SpawnVehicle(model, coords, heading, properties, cb)
if cb and not ESX.IsFunctionReference(cb) then
error("Invalid callback function")
end


local vehicleModel = joaat(model)
local vehicleProperties = properties

local promise = not cb and promise.new()
CreateThread(function()
local xPlayer = ESX.OneSync.GetClosestPlayer(coords, 300)
ESX.GetVehicleType(vehicleModel, xPlayer.id, function(vehicleType)
if vehicleType then
local createdVehicle = CreateVehicleServerSetter(vehicleModel, vehicleType, coords.x, coords.y, coords.z, heading)
local tries = 0

while not createdVehicle or createdVehicle == 0 or not GetEntityCoords(createdVehicle) do
Wait(200)
tries = tries + 1
if tries > 20 then
return error(("Could not spawn vehicle - ^5%s^7!"):format(model))
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))
end

local createdVehicle = CreateVehicleServerSetter(vehicleModel, vehicleType, coords.x, coords.y, coords.z, heading)
local tries = 0

while not createdVehicle or createdVehicle == 0 or not GetEntityCoords(createdVehicle) do
Wait(200)
tries = tries + 1
if tries > 20 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
-- luacheck: ignore
SetEntityOrphanMode(createdVehicle, 2)
local networkId = NetworkGetNetworkIdFromEntity(createdVehicle)
Entity(createdVehicle).state:set("VehicleProperties", vehicleProperties, true)
end

-- 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)
else
error(("Tried to spawn invalid vehicle - ^5%s^7!"):format(model))
end
end)
end)

if promise then
return Citizen.Await(promise)
end
end

---@param model number|string
Expand Down
Loading