feat: es_extended - basic Onesync infinity support

This commit is contained in:
Mycroft
2022-08-07 09:43:34 +01:00
parent c5d6f14e46
commit 24fc76097d
2 changed files with 118 additions and 65 deletions
+84 -47
View File
@@ -403,23 +403,14 @@ function ESX.UI.ShowInventoryItemNotification(add, item, count)
end end
function ESX.Game.GetPedMugshot(ped, transparent) function ESX.Game.GetPedMugshot(ped, transparent)
if DoesEntityExist(ped) then if not DoesEntityExist(ped) then return end
local mugshot local mugshot = transparent and RegisterPedheadshotTransparent(ped) or RegisterPedheadshot(ped)
if transparent then while not IsPedheadshotReady(mugshot) do
mugshot = RegisterPedheadshotTransparent(ped) Wait(0)
else
mugshot = RegisterPedheadshot(ped)
end
while not IsPedheadshotReady(mugshot) do
Wait(0)
end
return mugshot, GetPedheadshotTxdString(mugshot)
else
return
end end
return mugshot, GetPedheadshotTxdString(mugshot)
end end
function ESX.Game.Teleport(entity, coords, cb) function ESX.Game.Teleport(entity, coords, cb)
@@ -442,18 +433,34 @@ function ESX.Game.Teleport(entity, coords, cb)
end end
function ESX.Game.SpawnObject(object, coords, cb, networked) function ESX.Game.SpawnObject(object, coords, cb, networked)
local model = type(object) == 'number' and object or GetHashKey(object)
local vector = type(coords) == "vector3" and coords or vec(coords.x, coords.y, coords.z)
networked = networked == nil and true or networked networked = networked == nil and true or networked
if networked then
ESX.TriggerServerCallback('esx:Onesync:SpawnObject', function(NetworkID)
if cb then
local obj = NetworkGetEntityFromNetworkId(NetworkID)
local Tries = 0
while not DoesEntityExist(obj) do
Wait(0)
Tries += 1
if Tries > 250 then
break
end
end
cb(obj)
end
end, object, coords, 0.0)
else
local model = type(object) == 'number' and object or GetHashKey(object)
local vector = type(coords) == "vector3" and coords or vec(coords.x, coords.y, coords.z)
CreateThread(function()
ESX.Streaming.RequestModel(model)
CreateThread(function() local obj = CreateObject(model, vector.xyz, networked, false, true)
ESX.Streaming.RequestModel(model) if cb then
cb(obj)
local obj = CreateObject(model, vector.xyz, networked, false, true) end
if cb then end)
cb(obj) end
end
end)
end end
function ESX.Game.SpawnLocalObject(object, coords, cb) function ESX.Game.SpawnLocalObject(object, coords, cb)
@@ -461,7 +468,7 @@ function ESX.Game.SpawnLocalObject(object, coords, cb)
end end
function ESX.Game.DeleteVehicle(vehicle) function ESX.Game.DeleteVehicle(vehicle)
SetEntityAsMissionEntity(vehicle, false, true) SetEntityAsMissionEntity(vehicle, true, true)
DeleteVehicle(vehicle) DeleteVehicle(vehicle)
end end
@@ -474,30 +481,59 @@ function ESX.Game.SpawnVehicle(vehicle, coords, heading, cb, networked)
local model = (type(vehicle) == 'number' and vehicle or GetHashKey(vehicle)) local model = (type(vehicle) == 'number' and vehicle or GetHashKey(vehicle))
local vector = type(coords) == "vector3" and coords or vec(coords.x, coords.y, coords.z) local vector = type(coords) == "vector3" and coords or vec(coords.x, coords.y, coords.z)
networked = networked == nil and true or networked networked = networked == nil and true or networked
CreateThread(function() if networked then
ESX.Streaming.RequestModel(model) local isAutomobile = IsThisModelACar(model)
if isAutomobile ~= false then isAutomobile = true end
ESX.TriggerServerCallback('esx:Onesync:SpawnVehicle', function(NetID)
print("Spawned Vehicle: " .. NetID)
if NetID then
Wait(250)
local vehicle = NetworkGetEntityFromNetworkId(NetID)
local Tries = 0
while not DoesEntityExist(vehicle) do
Wait(0)
Tries += 1
if Tries > 300 then
break
end
end
print("Spawned Vehicle: " .. vehicle)
SetEntityAsMissionEntity(vehicle, true, true)
SetVehicleHasBeenOwnedByPlayer(vehicle, true)
SetVehicleNeedsToBeHotwired(vehicle, false)
SetModelAsNoLongerNeeded(model)
SetVehRadioStation(vehicle, 'OFF')
if cb then
cb(vehicle)
end
end
end, vehicle, coords, heading, isAutomobile)
else
CreateThread(function()
ESX.Streaming.RequestModel(model)
local vehicle = CreateVehicle(model, vector.xyz, heading, networked, false) local vehicle = CreateVehicle(model, vector.xyz, heading, networked, false)
if networked then if networked then
local id = NetworkGetNetworkIdFromEntity(vehicle) local id = NetworkGetNetworkIdFromEntity(vehicle)
SetNetworkIdCanMigrate(id, true) SetNetworkIdCanMigrate(id, true)
SetEntityAsMissionEntity(vehicle, true, false) SetEntityAsMissionEntity(vehicle, true, false)
end end
SetVehicleHasBeenOwnedByPlayer(vehicle, true) SetVehicleHasBeenOwnedByPlayer(vehicle, true)
SetVehicleNeedsToBeHotwired(vehicle, false) SetVehicleNeedsToBeHotwired(vehicle, false)
SetModelAsNoLongerNeeded(model) SetModelAsNoLongerNeeded(model)
SetVehRadioStation(vehicle, 'OFF') SetVehRadioStation(vehicle, 'OFF')
RequestCollisionAtCoord(vector.xyz) RequestCollisionAtCoord(vector.xyz)
while not HasCollisionLoadedAroundEntity(vehicle) do while not HasCollisionLoadedAroundEntity(vehicle) do
Wait(0) Wait(0)
end end
if cb then if cb then
cb(vehicle) cb(vehicle)
end end
end) end)
end
end end
function ESX.Game.SpawnLocalVehicle(vehicle, coords, heading, cb) function ESX.Game.SpawnLocalVehicle(vehicle, coords, heading, cb)
@@ -549,6 +585,7 @@ function ESX.Game.GetPlayers(onlyOtherPlayers, returnKeyValue, returnPeds)
return players return players
end end
function ESX.Game.GetClosestObject(coords, modelFilter) function ESX.Game.GetClosestObject(coords, modelFilter)
return ESX.Game.GetClosestEntity(ESX.Game.GetObjects(), false, coords, modelFilter) return ESX.Game.GetClosestEntity(ESX.Game.GetObjects(), false, coords, modelFilter)
end end
@@ -665,7 +702,7 @@ function ESX.Game.GetVehicleProperties(vehicle)
for extraId = 0, 12 do for extraId = 0, 12 do
if DoesExtraExist(vehicle, extraId) then if DoesExtraExist(vehicle, extraId) then
local state = IsVehicleExtraTurnedOn(vehicle, extraId) == 1 local state = IsVehicleExtraTurnedOn(vehicle, extraId)
extras[tostring(extraId)] = state extras[tostring(extraId)] = state
end end
end end
+34 -18
View File
@@ -25,15 +25,14 @@ local function getNearbyPlayers(source, closest, distance, ignore)
local coords = GetEntityCoords(entity) local coords = GetEntityCoords(entity)
if not closest then if not closest then
local dist = #(source - coords) local dist = #(xPlayer.source - coords)
if dist <= distance then if dist <= distance then
count += 1 count += 1
result[count] = {id = xPlayer.source, ped = entity, coords = coords, dist = dist} result[count] = {id = xPlayer.source, ped = NetworkGetNetworkIdFromEntity(entity), coords = coords, dist = dist}
end end
else else
local dist = #(source - coords) local dist = #(xPlayer.source - coords)
if dist <= (result.dist or distance) then result = {id = xPlayer.source, ped = NetworkGetNetworkIdFromEntity(entity), coords = coords, dist = dist} if dist <= (result.dist or distance) then
result = {id = xPlayer.source, ped = entity, coords = coords, dist = dist}
end end
end end
end end
@@ -60,12 +59,13 @@ end
---@param coords vector3|table ---@param coords vector3|table
---@param heading number ---@param heading number
---@param cb function ---@param cb function
function ESX.OneSync.SpawnVehicle(model, coords, heading, cb) function ESX.OneSync.SpawnVehicle(model, coords, heading, autoMobile)
if type(model) == 'string' then model = GetHashKey(model) end if type(model) == 'string' then model = GetHashKey(model) end
CreateThread(function() local vector = type(coords) == "vector3" and coords or vec(coords.x, coords.y, coords.z)
local entity = Citizen.InvokeNative(`CREATE_AUTOMOBILE`, model, coords.x, coords.y, coords.z, heading) if type(autoMobile) ~= 'boolean' then
while not DoesEntityExist(entity) do Wait(50) end return
end
local entity = autoMobile and Citizen.InvokeNative(`CREATE_AUTOMOBILE`, model, coords.x, coords.y, coords.z, heading) or CreateVehicle(model, coords, heading, true, true)
-- Sometimes peds can spawn in the vehicle -- Sometimes peds can spawn in the vehicle
local ped = GetPedInVehicleSeat(entity, -1) local ped = GetPedInVehicleSeat(entity, -1)
if ped > 0 then if ped > 0 then
@@ -77,22 +77,32 @@ function ESX.OneSync.SpawnVehicle(model, coords, heading, cb)
end end
end end
end end
print('Spawned vehicle: ' .. entity)
local tries = 0
while not DoesEntityExist(entity) do
Wait(0)
tries = tries + 1
if tries > 250 then
break
end
end
cb(entity) print(DoesEntityExist(entity) and NetworkGetNetworkIdFromEntity(entity) or false)
end) return DoesEntityExist(entity) and NetworkGetNetworkIdFromEntity(entity) or false
end end
---@param model number|string ---@param model number|string
---@param coords vector3|table ---@param coords vector3|table
---@param heading number ---@param heading number
---@param cb function ---@param cb function
function ESX.OneSync.SpawnObject(model, coords, heading, cb) function ESX.OneSync.SpawnObject(model, coords, heading)
if type(model) == 'string' then model = GetHashKey(model) end if type(model) == 'string' then model = GetHashKey(model) end
local vector = type(coords) == "vector3" and coords or vec(coords.x, coords.y, coords.z)
CreateThread(function() CreateThread(function()
local entity = CreateObject(model, coords, true, true) local entity = CreateObject(model, coords, true, true)
while not DoesEntityExist(entity) do Wait(50) end while not DoesEntityExist(entity) do Wait(50) end
SetEntityHeading(entity, heading) SetEntityHeading(entity, heading)
cb(entity) return DoesEntityExist(entity) and NetworkGetNetworkIdFromEntity(entity) or false
end) end)
end end
@@ -105,7 +115,7 @@ function ESX.OneSync.SpawnPed(model, coords, heading, cb)
CreateThread(function() CreateThread(function()
local entity = CreatePed(0, model, coords.x, coords.y, coords.z, heading, true, true) local entity = CreatePed(0, model, coords.x, coords.y, coords.z, heading, true, true)
while not DoesEntityExist(entity) do Wait(50) end while not DoesEntityExist(entity) do Wait(50) end
cb(entity) return entity
end) end)
end end
@@ -118,7 +128,7 @@ function ESX.OneSync.SpawnPedInVehicle(model, vehicle, seat, cb)
CreateThread(function() CreateThread(function()
local entity = CreatePedInsideVehicle(vehicle, 1, model, seat, true, true) local entity = CreatePedInsideVehicle(vehicle, 1, model, seat, true, true)
while not DoesEntityExist(entity) do Wait(50) end while not DoesEntityExist(entity) do Wait(50) end
cb(entity) return entity
end) end)
end end
@@ -130,7 +140,7 @@ local function getNearbyEntities(entities, coords, modelFilter, maxDistance, isP
if not modelFilter or modelFilter[GetEntityModel(entity)] then if not modelFilter or modelFilter[GetEntityModel(entity)] then
local entityCoords = GetEntityCoords(entity) local entityCoords = GetEntityCoords(entity)
if not maxDistance or #(coords - entityCoords) <= maxDistance then if not maxDistance or #(coords - entityCoords) <= maxDistance then
nearbyEntities[#nearbyEntities+1] = {entity=entity, coords=entityCoords} nearbyEntities[#nearbyEntities+1] = NetworkGetNetworkIdFromEntity(entity)
end end
end end
end end
@@ -178,7 +188,7 @@ local function getClosestEntity(entities, coords, modelFilter, isPed)
end end
end end
end end
return closestEntity, distance, closestCoords return NetworkGetNetworkIdFromEntity(closestEntity), distance, closestCoords
end end
---@param coords vector3 ---@param coords vector3
@@ -201,3 +211,9 @@ end
function ESX.OneSync.GetClosestVehicle(coords, modelFilter) function ESX.OneSync.GetClosestVehicle(coords, modelFilter)
return getClosestEntity(GetAllVehicles(), coords, modelFilter) return getClosestEntity(GetAllVehicles(), coords, modelFilter)
end end
for k,v in pairs(ESX.OneSync) do
ESX.RegisterServerCallback("esx:Onesync:"..k, function(source, cb, ...)
cb(v(...))
end)
end