From 24fc76097de4f0bcd6572fdc8e9a7b5604d35707 Mon Sep 17 00:00:00 2001 From: Mycroft Date: Sun, 7 Aug 2022 09:43:34 +0100 Subject: [PATCH 1/4] feat: es_extended - basic Onesync infinity support --- [esx]/es_extended/client/functions.lua | 131 ++++++++++++++++--------- [esx]/es_extended/server/onesync.lua | 52 ++++++---- 2 files changed, 118 insertions(+), 65 deletions(-) diff --git a/[esx]/es_extended/client/functions.lua b/[esx]/es_extended/client/functions.lua index 5f490858..de705716 100644 --- a/[esx]/es_extended/client/functions.lua +++ b/[esx]/es_extended/client/functions.lua @@ -403,23 +403,14 @@ function ESX.UI.ShowInventoryItemNotification(add, item, count) end function ESX.Game.GetPedMugshot(ped, transparent) - if DoesEntityExist(ped) then - local mugshot + if not DoesEntityExist(ped) then return end + local mugshot = transparent and RegisterPedheadshotTransparent(ped) or RegisterPedheadshot(ped) - if transparent then - mugshot = RegisterPedheadshotTransparent(ped) - else - mugshot = RegisterPedheadshot(ped) - end - - while not IsPedheadshotReady(mugshot) do - Wait(0) - end - - return mugshot, GetPedheadshotTxdString(mugshot) - else - return + while not IsPedheadshotReady(mugshot) do + Wait(0) end + + return mugshot, GetPedheadshotTxdString(mugshot) end function ESX.Game.Teleport(entity, coords, cb) @@ -442,18 +433,34 @@ function ESX.Game.Teleport(entity, coords, cb) end 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 + 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() - ESX.Streaming.RequestModel(model) - - local obj = CreateObject(model, vector.xyz, networked, false, true) - if cb then - cb(obj) - end - end) + local obj = CreateObject(model, vector.xyz, networked, false, true) + if cb then + cb(obj) + end + end) + end end function ESX.Game.SpawnLocalObject(object, coords, cb) @@ -461,7 +468,7 @@ function ESX.Game.SpawnLocalObject(object, coords, cb) end function ESX.Game.DeleteVehicle(vehicle) - SetEntityAsMissionEntity(vehicle, false, true) + SetEntityAsMissionEntity(vehicle, true, true) DeleteVehicle(vehicle) 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 vector = type(coords) == "vector3" and coords or vec(coords.x, coords.y, coords.z) networked = networked == nil and true or networked - CreateThread(function() - ESX.Streaming.RequestModel(model) + if networked then + 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 - local id = NetworkGetNetworkIdFromEntity(vehicle) - SetNetworkIdCanMigrate(id, true) - SetEntityAsMissionEntity(vehicle, true, false) - end - SetVehicleHasBeenOwnedByPlayer(vehicle, true) - SetVehicleNeedsToBeHotwired(vehicle, false) - SetModelAsNoLongerNeeded(model) - SetVehRadioStation(vehicle, 'OFF') + if networked then + local id = NetworkGetNetworkIdFromEntity(vehicle) + SetNetworkIdCanMigrate(id, true) + SetEntityAsMissionEntity(vehicle, true, false) + end + SetVehicleHasBeenOwnedByPlayer(vehicle, true) + SetVehicleNeedsToBeHotwired(vehicle, false) + SetModelAsNoLongerNeeded(model) + SetVehRadioStation(vehicle, 'OFF') - RequestCollisionAtCoord(vector.xyz) - while not HasCollisionLoadedAroundEntity(vehicle) do - Wait(0) - end + RequestCollisionAtCoord(vector.xyz) + while not HasCollisionLoadedAroundEntity(vehicle) do + Wait(0) + end - if cb then - cb(vehicle) - end - end) + if cb then + cb(vehicle) + end + end) + end end function ESX.Game.SpawnLocalVehicle(vehicle, coords, heading, cb) @@ -549,6 +585,7 @@ function ESX.Game.GetPlayers(onlyOtherPlayers, returnKeyValue, returnPeds) return players end + function ESX.Game.GetClosestObject(coords, modelFilter) return ESX.Game.GetClosestEntity(ESX.Game.GetObjects(), false, coords, modelFilter) end @@ -665,7 +702,7 @@ function ESX.Game.GetVehicleProperties(vehicle) for extraId = 0, 12 do if DoesExtraExist(vehicle, extraId) then - local state = IsVehicleExtraTurnedOn(vehicle, extraId) == 1 + local state = IsVehicleExtraTurnedOn(vehicle, extraId) extras[tostring(extraId)] = state end end diff --git a/[esx]/es_extended/server/onesync.lua b/[esx]/es_extended/server/onesync.lua index 49ea47fc..b600c2b7 100644 --- a/[esx]/es_extended/server/onesync.lua +++ b/[esx]/es_extended/server/onesync.lua @@ -25,15 +25,14 @@ local function getNearbyPlayers(source, closest, distance, ignore) local coords = GetEntityCoords(entity) if not closest then - local dist = #(source - coords) + local dist = #(xPlayer.source - coords) if dist <= distance then 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 else - local dist = #(source - coords) - if dist <= (result.dist or distance) then - result = {id = xPlayer.source, ped = entity, coords = coords, dist = dist} + local dist = #(xPlayer.source - coords) + result = {id = xPlayer.source, ped = NetworkGetNetworkIdFromEntity(entity), coords = coords, dist = dist} if dist <= (result.dist or distance) then end end end @@ -60,12 +59,13 @@ end ---@param coords vector3|table ---@param heading number ---@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 - CreateThread(function() - local entity = Citizen.InvokeNative(`CREATE_AUTOMOBILE`, model, coords.x, coords.y, coords.z, heading) - while not DoesEntityExist(entity) do Wait(50) end - + local vector = type(coords) == "vector3" and coords or vec(coords.x, coords.y, coords.z) + if type(autoMobile) ~= 'boolean' then + 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 local ped = GetPedInVehicleSeat(entity, -1) if ped > 0 then @@ -77,22 +77,32 @@ function ESX.OneSync.SpawnVehicle(model, coords, heading, cb) 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) - end) + print(DoesEntityExist(entity) and NetworkGetNetworkIdFromEntity(entity) or false) + return DoesEntityExist(entity) and NetworkGetNetworkIdFromEntity(entity) or false end ---@param model number|string ---@param coords vector3|table ---@param heading number ---@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 + local vector = type(coords) == "vector3" and coords or vec(coords.x, coords.y, coords.z) CreateThread(function() local entity = CreateObject(model, coords, true, true) while not DoesEntityExist(entity) do Wait(50) end SetEntityHeading(entity, heading) - cb(entity) + return DoesEntityExist(entity) and NetworkGetNetworkIdFromEntity(entity) or false end) end @@ -105,7 +115,7 @@ function ESX.OneSync.SpawnPed(model, coords, heading, cb) CreateThread(function() local entity = CreatePed(0, model, coords.x, coords.y, coords.z, heading, true, true) while not DoesEntityExist(entity) do Wait(50) end - cb(entity) + return entity end) end @@ -118,7 +128,7 @@ function ESX.OneSync.SpawnPedInVehicle(model, vehicle, seat, cb) CreateThread(function() local entity = CreatePedInsideVehicle(vehicle, 1, model, seat, true, true) while not DoesEntityExist(entity) do Wait(50) end - cb(entity) + return entity end) end @@ -130,7 +140,7 @@ local function getNearbyEntities(entities, coords, modelFilter, maxDistance, isP if not modelFilter or modelFilter[GetEntityModel(entity)] then local entityCoords = GetEntityCoords(entity) if not maxDistance or #(coords - entityCoords) <= maxDistance then - nearbyEntities[#nearbyEntities+1] = {entity=entity, coords=entityCoords} + nearbyEntities[#nearbyEntities+1] = NetworkGetNetworkIdFromEntity(entity) end end end @@ -178,7 +188,7 @@ local function getClosestEntity(entities, coords, modelFilter, isPed) end end end - return closestEntity, distance, closestCoords + return NetworkGetNetworkIdFromEntity(closestEntity), distance, closestCoords end ---@param coords vector3 @@ -201,3 +211,9 @@ end function ESX.OneSync.GetClosestVehicle(coords, modelFilter) return getClosestEntity(GetAllVehicles(), coords, modelFilter) end + +for k,v in pairs(ESX.OneSync) do + ESX.RegisterServerCallback("esx:Onesync:"..k, function(source, cb, ...) + cb(v(...)) + end) +end \ No newline at end of file From a985ad37160076d0903829c3e4e5f9bc8c023ad7 Mon Sep 17 00:00:00 2001 From: Mycroft Date: Sun, 7 Aug 2022 09:44:06 +0100 Subject: [PATCH 2/4] fix config options, remove un-needed event --- [esx]/es_extended/client/main.lua | 66 ++++++------------------------- 1 file changed, 12 insertions(+), 54 deletions(-) diff --git a/[esx]/es_extended/client/main.lua b/[esx]/es_extended/client/main.lua index e12a820b..08d8e0cc 100644 --- a/[esx]/es_extended/client/main.lua +++ b/[esx]/es_extended/client/main.lua @@ -55,46 +55,41 @@ AddEventHandler('esx:playerLoaded', function(xPlayer, isNew, skin) NetworkSetFriendlyFireOption(true) end - if Config.DisableVehicleRewards then - DisablePlayerVehicleRewards(PlayerId()) - end - - if Config.DisableNPCDrops then - RemoveAllPickupsOfType(0xDF711959) -- carbine rifle - RemoveAllPickupsOfType(0xF9AFB48F) -- pistol - RemoveAllPickupsOfType(0xA9355DCD) -- pumpshotgun - end - CreateThread(function() while true do - local Sleep = 1500 - + Wait(0) if Config.DisableHealthRegeneration then SetPlayerHealthRechargeMultiplier(PlayerId(), 0.0) end if Config.DisableWeaponWheel then - Sleep = 0 BlockWeaponWheelThisFrame() DisableControlAction(0, 37,true) end if Config.DisableAimAssist then - if Sleep == 1500 then Sleep = 100 end if IsPedArmed(ESX.PlayerData.ped, 4) then SetPlayerLockonRangeOverride(PlayerId(), 2.0) end end + if Config.DisableVehicleRewards then + DisablePlayerVehicleRewards(PlayerId()) + end + + if Config.DisableNPCDrops then + RemoveAllPickupsOfType(0xDF711959) -- carbine rifle + RemoveAllPickupsOfType(0xF9AFB48F) -- pistol + RemoveAllPickupsOfType(0xA9355DCD) -- pumpshotgun + end + if Config.RemoveHudCommonents then - if Sleep ~= 0 then Sleep = 0 end for i=1, #(Config.RemoveHudCommonents) do if Config.RemoveHudCommonents[i] then HideHudComponentThisFrame(i) end end end - Wait(Sleep) end end) @@ -115,6 +110,7 @@ AddEventHandler('esx:playerLoaded', function(xPlayer, isNew, skin) }) end + SetDefaultVehicleNumberPlateTextPattern(-1, 'ESX.A111') FreezeEntityPosition(PlayerPedId(), false) StartServerSyncLoops() end) @@ -395,44 +391,6 @@ if not Config.OxInventory then end) end -RegisterNetEvent('esx:deleteVehicle') -AddEventHandler('esx:deleteVehicle', function(radius) - if radius and tonumber(radius) then - radius = tonumber(radius) + 0.01 - local vehicles = ESX.Game.GetVehiclesInArea(GetEntityCoords(ESX.PlayerData.ped), radius) - - for k,entity in ipairs(vehicles) do - local attempt = 0 - - while not NetworkHasControlOfEntity(entity) and attempt < 100 and DoesEntityExist(entity) do - Wait(100) - NetworkRequestControlOfEntity(entity) - attempt = attempt + 1 - end - - if DoesEntityExist(entity) and NetworkHasControlOfEntity(entity) then - ESX.Game.DeleteVehicle(entity) - end - end - else - local vehicle, attempt = ESX.Game.GetVehicleInDirection(), 0 - - if IsPedInAnyVehicle(ESX.PlayerData.ped, true) then - vehicle = GetVehiclePedIsIn(ESX.PlayerData.ped, false) - end - - while not NetworkHasControlOfEntity(vehicle) and attempt < 100 and DoesEntityExist(vehicle) do - Wait(100) - NetworkRequestControlOfEntity(vehicle) - attempt = attempt + 1 - end - - if DoesEntityExist(vehicle) and NetworkHasControlOfEntity(vehicle) then - ESX.Game.DeleteVehicle(vehicle) - end - end -end) - -- Pause menu disables HUD display if Config.EnableHud then CreateThread(function() From 64e23573c906a2df7bd8d0709fa0ad8b376d4f84 Mon Sep 17 00:00:00 2001 From: Mycroft Date: Sun, 7 Aug 2022 09:44:46 +0100 Subject: [PATCH 3/4] better DV/Cardel command --- [esx]/es_extended/server/commands.lua | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/[esx]/es_extended/server/commands.lua b/[esx]/es_extended/server/commands.lua index 2f99fdbd..6978a5ef 100644 --- a/[esx]/es_extended/server/commands.lua +++ b/[esx]/es_extended/server/commands.lua @@ -23,14 +23,19 @@ ESX.RegisterCommand('car', 'admin', function(xPlayer, args, showError) if not args.car then args.car = GameBuild >= 2699 and "DRAUGUR" or "Prototipo" end xPlayer.triggerEvent('esx:spawnVehicle', args.car) end, false, {help = _U('command_car'), validate = false, arguments = { - {name = 'car', help = _U('command_car_car'), type = 'any'} + {name = 'car',validate = false, help = _U('command_car_car'), type = 'string'} }}) ESX.RegisterCommand({'cardel', 'dv'}, 'admin', function(xPlayer, args, showError) - if not args.radius then args.radius = 4 end - xPlayer.triggerEvent('esx:deleteVehicle', args.radius) + local Vehicles = ESX.OneSync.GetVehiclesInArea(xPlayer.getCoords(true), tonumber(args.radius) or 4) + for i=1, #Vehicles do + local Vehicle = NetworkGetEntityFromNetworkId(Vehicles[i]) + if DoesEntityExist(Vehicle) then + DeleteEntity(Vehicle) + end + end end, false, {help = _U('command_cardel'), validate = false, arguments = { - {name = 'radius', help = _U('command_cardel_radius'), type = 'any'} + {name = 'radius',validate = false, help = _U('command_cardel_radius'), type = 'string'} }}) ESX.RegisterCommand('setaccountmoney', 'admin', function(xPlayer, args, showError) From 4eb998b0e53c59db1e52db665e070570f81f6326 Mon Sep 17 00:00:00 2001 From: Mycroft Date: Wed, 10 Aug 2022 06:14:08 +0100 Subject: [PATCH 4/4] better oneysnc infinity support --- [esx]/es_extended/client/functions.lua | 10 ++--- [esx]/es_extended/server/onesync.lua | 52 ++++++++++---------------- 2 files changed, 24 insertions(+), 38 deletions(-) diff --git a/[esx]/es_extended/client/functions.lua b/[esx]/es_extended/client/functions.lua index de705716..02b6fd25 100644 --- a/[esx]/es_extended/client/functions.lua +++ b/[esx]/es_extended/client/functions.lua @@ -440,6 +440,7 @@ function ESX.Game.SpawnObject(object, coords, cb, networked) local obj = NetworkGetEntityFromNetworkId(NetworkID) local Tries = 0 while not DoesEntityExist(obj) do + obj = NetworkGetEntityFromNetworkId(NetworkID) Wait(0) Tries += 1 if Tries > 250 then @@ -484,20 +485,19 @@ function ESX.Game.SpawnVehicle(vehicle, coords, heading, cb, networked) if networked then local isAutomobile = IsThisModelACar(model) if isAutomobile ~= false then isAutomobile = true end - ESX.TriggerServerCallback('esx:Onesync:SpawnVehicle', function(NetID) + 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 + vehicle = NetworkGetEntityFromNetworkId(NetID) Wait(0) Tries += 1 - if Tries > 300 then + if Tries > 250 then break end end - print("Spawned Vehicle: " .. vehicle) SetEntityAsMissionEntity(vehicle, true, true) SetVehicleHasBeenOwnedByPlayer(vehicle, true) SetVehicleNeedsToBeHotwired(vehicle, false) @@ -507,7 +507,7 @@ function ESX.Game.SpawnVehicle(vehicle, coords, heading, cb, networked) cb(vehicle) end end - end, vehicle, coords, heading, isAutomobile) + end, model, vector, heading, isAutomobile) else CreateThread(function() ESX.Streaming.RequestModel(model) diff --git a/[esx]/es_extended/server/onesync.lua b/[esx]/es_extended/server/onesync.lua index b600c2b7..6c841883 100644 --- a/[esx]/es_extended/server/onesync.lua +++ b/[esx]/es_extended/server/onesync.lua @@ -59,50 +59,28 @@ end ---@param coords vector3|table ---@param heading number ---@param cb function -function ESX.OneSync.SpawnVehicle(model, coords, heading, autoMobile) +function ESX.OneSync.SpawnVehicle(model, coords, heading, autoMobile, cb) if type(model) == 'string' then model = GetHashKey(model) end local vector = type(coords) == "vector3" and coords or vec(coords.x, coords.y, coords.z) if type(autoMobile) ~= 'boolean' then 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 - local ped = GetPedInVehicleSeat(entity, -1) - if ped > 0 then - for i = -1, 6 do - ped = GetPedInVehicleSeat(entity, i) - local popType = GetEntityPopulationType(ped) - if popType <= 5 or popType >= 1 then - DeleteEntity(ped) - 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 - - print(DoesEntityExist(entity) and NetworkGetNetworkIdFromEntity(entity) or false) - return DoesEntityExist(entity) and NetworkGetNetworkIdFromEntity(entity) or false + local Entity = autoMobile and Citizen.InvokeNative(`CREATE_AUTOMOBILE`, model, coords.x, coords.y, coords.z, heading) or CreateVehicle(model, coords, heading, true, true) + cb(NetworkGetNetworkIdFromEntity(Entity)) end ---@param model number|string ---@param coords vector3|table ---@param heading number ---@param cb function -function ESX.OneSync.SpawnObject(model, coords, heading) +function ESX.OneSync.SpawnObject(model, coords, heading, cb) 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() local entity = CreateObject(model, coords, true, true) while not DoesEntityExist(entity) do Wait(50) end SetEntityHeading(entity, heading) - return DoesEntityExist(entity) and NetworkGetNetworkIdFromEntity(entity) or false + cb(NetworkGetNetworkIdFromEntity(entity)) end) end @@ -169,7 +147,7 @@ end ---@param maxDistance number ---@param modelFilter table models to ignore, where the key is the model hash and the value is true ---@return table -function ESX.OneSync.GetVehiclesInArea(coords, maxDistance, modelFilter) +function ESX.OneSync.GetVehiclesInArea(coords, maxDistance, modelFilter, cb) return getNearbyEntities(GetAllVehicles(), coords, modelFilter, maxDistance) end @@ -212,8 +190,16 @@ function ESX.OneSync.GetClosestVehicle(coords, modelFilter) return getClosestEntity(GetAllVehicles(), coords, modelFilter) end -for k,v in pairs(ESX.OneSync) do - ESX.RegisterServerCallback("esx:Onesync:"..k, function(source, cb, ...) - cb(v(...)) - end) -end \ No newline at end of file +ESX.RegisterServerCallback("esx:Onesync:SpawnVehicle", function(source, cb, model, coords, heading, autoMobile) + ESX.OneSync.SpawnVehicle(model, coords, heading, autoMobile, cb) +end) + +ESX.RegisterServerCallback("esx:Onesync:SpawnObject", function(source, cb, model, coords, heading) + ESX.OneSync.SpawnObject(model, coords, heading, cb) +end) + +-- for k,v in pairs(ESX.OneSync) do +-- ESX.RegisterServerCallback("esx:Onesync:"..k, function(source, cb, ...) +-- cb(v(...)) +-- end) +-- end \ No newline at end of file