Skip to content

Commit

Permalink
feat(server)deprecating certain vehicle spawning callbacks and functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Manason committed May 20, 2023
1 parent c889123 commit 683d64f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 55 deletions.
53 changes: 19 additions & 34 deletions server/events.lua
Original file line number Diff line number Diff line change
Expand Up @@ -224,32 +224,6 @@ RegisterNetEvent('QBCore:CallCommand', function(command, args)
end
end)

-- Use this for player vehicle spawning
-- Vehicle server-side spawning callback (netId)
-- use the netid on the client with the NetworkGetEntityFromNetworkId native
-- convert it to a vehicle via the NetToVeh native
---@param source Source
---@param cb fun(vehicleNetId: number)
---@param model string|number
---@param coords vector4
---@param warp boolean teleports player into vehicle if true
QBCore.Functions.CreateCallback('QBCore:Server:SpawnVehicle', function(source, cb, model, coords, warp)
local ped = GetPlayerPed(source)
model = type(model) == 'string' and joaat(model) or model
if not coords then coords = GetEntityCoords(ped) end
local veh = CreateVehicle(model, coords.x, coords.y, coords.z, coords.w, true, true)
while not DoesEntityExist(veh) do Wait(0) end
if warp then
while GetVehiclePedIsIn(ped, false) ~= veh do
Wait(0)
TaskWarpPedIntoVehicle(ped, veh, -1)
end
end
while NetworkGetEntityOwner(veh) ~= source do Wait(0) end
cb(NetworkGetNetworkIdFromEntity(veh))
end)

-- Use this for long distance vehicle spawning
-- vehicle server-side spawning callback (netId)
-- use the netid on the client with the NetworkGetEntityFromNetworkId native
-- convert it to a vehicle via the NetToVeh native but use a while loop before that to check if the vehicle exists first like this
Expand All @@ -261,18 +235,17 @@ end)
```
]]
-- If you don't use the above on the client, it will return 0 as the vehicle from the netid and 0 means no vehicle found because it doesn't exist so fast on the client
---@param source Source
---@param cb fun(vehicleNetId: number)
---@param source number
---@param model string|number
---@param coords vector4
---@param warp boolean
QBCore.Functions.CreateCallback('QBCore:Server:CreateVehicle', function(source, cb, model, coords, warp)
---@return number? vehNetId
local function createVehicle(source, model, coords, warp)
local ped = GetPlayerPed(source)
model = type(model) == 'string' and joaat(model) or model
if not coords then coords = GetEntityCoords(ped) end
if not CreateVehicleServerSetter then
error('^1CreateVehicleServerSetter is not available on your artifact, please use artifact 5904 or above to be able to use this^0')
cb(0)
return
end
local tempVehicle = CreateVehicle(model, 0, 0, 0, 0, true, true)
Expand All @@ -282,9 +255,21 @@ QBCore.Functions.CreateCallback('QBCore:Server:CreateVehicle', function(source,
local veh = CreateVehicleServerSetter(model, vehicleType, coords.x, coords.y, coords.z, coords.w)
while not DoesEntityExist(veh) do Wait(0) end
if warp then TaskWarpPedIntoVehicle(ped, veh, -1) end
cb(NetworkGetNetworkIdFromEntity(veh))
return NetworkGetNetworkIdFromEntity(veh)
end

lib.callback.register('qbx-core:server:createVehicle', createVehicle)

---@deprecated use 'qbx-core:server:createVehicle' via ox_lib callback instead.
QBCore.Functions.CreateCallback('QBCore:Server:SpawnVehicle', function(source, cb, model, coords, warp)
print(string.format("%s invoked deprecated callback QBCore:Server:SpawnVehicle. Use qbx-core:server:createVehicle via ox_lib callback instead.", GetInvokingResource()))
local netId = createVehicle(source, model, coords, warp)
if netId then cb(netId) end
end)

--QBCore.Functions.CreateCallback('QBCore:HasItem', function(source, cb, items, amount)
-- https://github.com/qbcore-framework/qb-inventory/blob/e4ef156d93dd1727234d388c3f25110c350b3bcf/server/main.lua#L2066
--end)
---@deprecated use 'qbx-core:server:createVehicle' via ox_lib callback instead.
QBCore.Functions.CreateCallback('QBCore:Server:CreateVehicle', function(source, cb, model, coords, warp)
print(string.format("%s invoked deprecated callback QBCore:Server:CreateVehicle. Use qbx-core:server:createVehicle via ox_lib callback instead.", GetInvokingResource()))
local netId = createVehicle(source, model, coords, warp)
if netId then cb(netId) end
end)
25 changes: 4 additions & 21 deletions server/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -201,27 +201,10 @@ function QBCore.Functions.GetEntitiesInBucket(bucket)
return curr_bucket_pool
end

-- Server side vehicle creation with optional callback
-- the CreateVehicle RPC still uses the client for creation so players must be near
---@param source Source
---@param model string|number
---@param coords vector4
---@param warp boolean
---@return number
---@deprecated Use QBCore.Functions.CreateVehicle instead.
function QBCore.Functions.SpawnVehicle(source, model, coords, warp)
local ped = GetPlayerPed(source)
model = type(model) == 'string' and joaat(model) or model
if not coords then coords = GetEntityCoords(ped) end
local veh = CreateVehicle(model, coords.x, coords.y, coords.z, coords.w, true, true)
while not DoesEntityExist(veh) do Wait(0) end
if warp then
while GetVehiclePedIsIn(ped, false) ~= veh do
Wait(0)
TaskWarpPedIntoVehicle(ped, veh, -1)
end
end
while NetworkGetEntityOwner(veh) ~= source do Wait(0) end
return veh
print(string.format("%s invoked deprecated server function QBCore.Functions.SpawnVehicle. Use QBCore.Functions.CreateVehicle instead.", GetInvokingResource()))
return QBCore.Functions.CreateVehicle(source, model, coords, warp)
end

-- Server side vehicle creation with optional callback
Expand Down Expand Up @@ -267,7 +250,7 @@ end

---@deprecated call a function instead
function QBCore.Functions.TriggerCallback(name, source, cb, ...)
print(string.format("%s invoked deprecated function TriggerCallback. Use ox_lib callback functions instead.", GetInvokingResource()))
print(string.format("%s invoked deprecated function TriggerCallback. Call a function instead.", GetInvokingResource()))
if not QBCore.ServerCallbacks[name] then return end
QBCore.ServerCallbacks[name](source, cb, ...)
end
Expand Down

0 comments on commit 683d64f

Please sign in to comment.