Skip to content

Commit

Permalink
refactor(es_extended/onesync): 'VehicleProperties' statebag
Browse files Browse the repository at this point in the history
  • Loading branch information
CsokiHUN committed Feb 9, 2023
1 parent 86d4e91 commit 36fcdf5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 24 deletions.
29 changes: 12 additions & 17 deletions [core]/es_extended/client/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -182,23 +182,18 @@ AddEventHandler('esx:restoreLoadout', function()
end
end)

AddStateBagChangeHandler('VehicleProperties', nil, function(bagName, key, value)
if value then
Wait(0)
local NetId = value.NetId
local Vehicle = NetworkGetEntityFromNetworkId(NetId)
local Tries = 0
while Vehicle == 0 do
Vehicle = NetworkGetEntityFromNetworkId(NetId)
Wait(100)
Tries = Tries + 1
if Tries > 300 then
break
end
end
if NetworkGetEntityOwner(Vehicle) == PlayerId() then
ESX.Game.SetVehicleProperties(Vehicle, value)
end
AddStateBagChangeHandler('VehicleProperties', nil, function(bagName, _, value)
if not value then
return
end

local vehicle = GetEntityFromStateBagName(bagName)
if not DoesEntityExist(vehicle) then
return
end

if NetworkGetEntityOwner(vehicle) == PlayerId() then
ESX.Game.SetVehicleProperties(vehicle, value)
end
end)

Expand Down
12 changes: 5 additions & 7 deletions [core]/es_extended/server/onesync.lua
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,12 @@ function ESX.OneSync.SpawnVehicle(model, coords, heading, properties, cb)
ESX.GetVehicleType(vehicleModel, xPlayer.id, function(vehicleType)
if vehicleType then
local createdVehicle = CreateVehicleServerSetter(vehicleModel, vehicleType, coords, heading)
if DoesEntityExist(createdVehicle) then
local networkId = NetworkGetNetworkIdFromEntity(createdVehicle)
vehicleProperties.NetId = networkId
Entity(createdVehicle).state:set('VehicleProperties', vehicleProperties, true)
cb(networkId)
else
print('[^1ERROR^7] Unfortunately, this vehicle has not spawned')
if not DoesEntityExist(createdVehicle) then
return print('[^1ERROR^7] Unfortunately, this vehicle has not spawned')
end

Entity(createdVehicle).state:set('VehicleProperties', vehicleProperties, true)
cb(NetworkGetNetworkIdFromEntity(createdVehicle))
else
print(('[^1ERROR^7] Tried to spawn invalid vehicle - ^5%s^7!'):format(model))
end
Expand Down

0 comments on commit 36fcdf5

Please sign in to comment.