diff --git a/client/functions.lua b/client/functions.lua index 970a07ab..8d7df142 100644 --- a/client/functions.lua +++ b/client/functions.lua @@ -308,21 +308,16 @@ ESX.Game.GetPedMugshot = function(ped, transparent) end ESX.Game.Teleport = function(entity, coords, cb) + local vector = type(coords) == "vector4" and coords or type(coords) == "vector3" and vector4(coords, 0.0) or vec(coords.x, coords.y, coords.z, coords.heading or 0.0) + if DoesEntityExist(entity) then - RequestCollisionAtCoord(coords.x, coords.y, coords.z) - local timeout = 0 - - -- we can get stuck here if any of the axies are "invalid" - while not HasCollisionLoadedAroundEntity(entity) and timeout < 2000 do - Citizen.Wait(0) - timeout = timeout + 1 + RequestCollisionAtCoord(vector.xyz) + while not HasCollisionLoadedAroundEntity(entity) do + Wait(0) end - SetEntityCoords(entity, coords.x, coords.y, coords.z, false, false, false, false) - - if type(coords) == 'table' and coords.heading then - SetEntityHeading(entity, coords.heading) - end + SetEntityCoords(entity, vector.xyz, false, false, false, false) + SetEntityHeading(entity, vector.w) end if cb then @@ -330,14 +325,18 @@ ESX.Game.Teleport = function(entity, coords, cb) end end -ESX.Game.SpawnObject = function(model, coords, cb) - local model = (type(model) == 'number' and model or GetHashKey(model)) - - Citizen.CreateThread(function() +ESX.Game.SpawnObject = function(model, coords, cb, networked, dynamic) + local vector = type(coords) == "vector3" and coords or vec(coords.x, coords.y, coords.z) + networked = networked == nil and true or false + dynamic = dynamic ~= nil and true or false + + CreateThread(function() ESX.Streaming.RequestModel(model) - local obj = CreateObject(model, coords.x, coords.y, coords.z, true, false, true) - SetModelAsNoLongerNeeded(model) - + + -- The below has to be done just for CreateObject since for some reason CreateObjects model argument is set + -- as an Object instead of a hash so it doesn't automatically hash the item + model = type(model) == 'number' and model or GetHashKey(model) + local obj = CreateObject(model, vector.xyz, networked, false, dynamic) if cb then cb(obj) end @@ -345,17 +344,8 @@ ESX.Game.SpawnObject = function(model, coords, cb) end ESX.Game.SpawnLocalObject = function(model, coords, cb) - local model = (type(model) == 'number' and model or GetHashKey(model)) - - Citizen.CreateThread(function() - ESX.Streaming.RequestModel(model) - local obj = CreateObject(model, coords.x, coords.y, coords.z, false, false, true) - SetModelAsNoLongerNeeded(model) - - if cb then - cb(obj) - end - end) + -- Why have 2 separate functions for this? Just call the other one with an extra param + ESX.Game.SpawnObject(model, coords, cb, false) end ESX.Game.DeleteVehicle = function(vehicle) @@ -368,28 +358,25 @@ ESX.Game.DeleteObject = function(object) DeleteObject(object) end -ESX.Game.SpawnVehicle = function(modelName, coords, heading, cb) - local model = (type(modelName) == 'number' and modelName or GetHashKey(modelName)) - - Citizen.CreateThread(function() +ESX.Game.SpawnVehicle = function(model, coords, heading, cb, networked) + local vector = type(coords) == "vector3" and coords or vec(coords.x, coords.y, coords.z) + networked = networked == nil and true or false + CreateThread(function() ESX.Streaming.RequestModel(model) - local vehicle = CreateVehicle(model, coords.x, coords.y, coords.z, heading, true, false) - local networkId = NetworkGetNetworkIdFromEntity(vehicle) - local timeout = 0 + local vehicle = CreateVehicle(model, vector.xyz, heading, networked, false) + local id = NetworkGetNetworkIdFromEntity(vehicle) - SetNetworkIdCanMigrate(networkId, true) + SetNetworkIdCanMigrate(id, true) SetEntityAsMissionEntity(vehicle, true, false) SetVehicleHasBeenOwnedByPlayer(vehicle, true) SetVehicleNeedsToBeHotwired(vehicle, false) - SetVehRadioStation(vehicle, 'OFF') SetModelAsNoLongerNeeded(model) - RequestCollisionAtCoord(coords.x, coords.y, coords.z) + SetVehRadioStation(vehicle, 'OFF') - -- we can get stuck here if any of the axies are "invalid" - while not HasCollisionLoadedAroundEntity(vehicle) and timeout < 2000 do - Citizen.Wait(0) - timeout = timeout + 1 + RequestCollisionAtCoord(vector.xyz) + while not HasCollisionLoadedAroundEntity(vehicle) do + Wait(0) end if cb then @@ -398,32 +385,9 @@ ESX.Game.SpawnVehicle = function(modelName, coords, heading, cb) end) end -ESX.Game.SpawnLocalVehicle = function(modelName, coords, heading, cb) - local model = (type(modelName) == 'number' and modelName or GetHashKey(modelName)) - - Citizen.CreateThread(function() - ESX.Streaming.RequestModel(model) - - local vehicle = CreateVehicle(model, coords.x, coords.y, coords.z, heading, false, false) - local timeout = 0 - - SetEntityAsMissionEntity(vehicle, true, false) - SetVehicleHasBeenOwnedByPlayer(vehicle, true) - SetVehicleNeedsToBeHotwired(vehicle, false) - SetVehRadioStation(vehicle, 'OFF') - SetModelAsNoLongerNeeded(model) - RequestCollisionAtCoord(coords.x, coords.y, coords.z) - - -- we can get stuck here if any of the axies are "invalid" - while not HasCollisionLoadedAroundEntity(vehicle) and timeout < 2000 do - Citizen.Wait(0) - timeout = timeout + 1 - end - - if cb then - cb(vehicle) - end - end) +ESX.Game.SpawnLocalVehicle = function(model, coords, heading, cb) + -- Why have 2 separate functions for this? Just call the other one with an extra param + ESX.Game.SpawnVehicle(model, coords, heading, cb, false) end ESX.Game.IsVehicleEmpty = function(vehicle) @@ -1022,15 +986,22 @@ end) Citizen.CreateThread(function() while true do Citizen.Wait(0) + local letSleep = true local currTime = GetGameTimer() - for i=1, #ESX.TimeoutCallbacks, 1 do - if ESX.TimeoutCallbacks[i] then - if currTime >= ESX.TimeoutCallbacks[i].time then - ESX.TimeoutCallbacks[i].cb() - ESX.TimeoutCallbacks[i] = nil + if #ESX.TimeoutCallbacks > 0 then + letSleep = false + for i=1, #ESX.TimeoutCallbacks, 1 do + if ESX.TimeoutCallbacks[i] then + if currTime >= ESX.TimeoutCallbacks[i].time then + ESX.TimeoutCallbacks[i].cb() + ESX.TimeoutCallbacks[i] = nil + end end end end + if letSleep then + Citizen.Wait(500) + end end end) diff --git a/client/main.lua b/client/main.lua index 8e09eb81..751b5c33 100644 --- a/client/main.lua +++ b/client/main.lua @@ -3,8 +3,8 @@ local isPaused, isDead, pickups = false, false, {} Citizen.CreateThread(function() while NetworkIsPlayerActive(PlayerId()) do Citizen.Wait(5) - TriggerServerEvent('esx:onPlayerJoined') - break + TriggerServerEvent('esx:onPlayerJoined') + break end end) @@ -22,12 +22,6 @@ AddEventHandler('esx:playerLoaded', function(playerData, isNew) SetCanAttackFriendly(PlayerPedId(), true, false) NetworkSetFriendlyFireOption(true) end - - -- disable wanted level - if not Config.EnableWantedLevel then - ClearPlayerWantedLevel(PlayerId()) - SetMaxWantedLevel(0) - end if Config.EnableHud then for k,v in ipairs(playerData.accounts) do @@ -55,25 +49,28 @@ AddEventHandler('esx:playerLoaded', function(playerData, isNew) model = `mp_m_freemode_01`, skipFade = false }, function() - TriggerServerEvent('esx:onPlayerSpawn') TriggerEvent('esx:onPlayerSpawn') - TriggerEvent('playerSpawned') -- compatibility with old scripts, will be removed soon + TriggerEvent('playerSpawned') -- compatibility with old scripts, will be removed soon. TriggerEvent('esx:restoreLoadout') Citizen.Wait(4000) ShutdownLoadingScreen() ShutdownLoadingScreenNui() FreezeEntityPosition(PlayerPedId(), false) - DoScreenFadeIn(10000) StartServerSyncLoops() - if isNew then - -- Put your code for if you want to do something with new players. - else - -- If they aren't new put that code here. - end end) + if isNew then + TriggerEvent('esx_identity:showRegisterIdentity') + else + ESX.TriggerServerCallback('esx_skin:getPlayerSkin', function(skin, jobSkin) + TriggerEvent('skinchanger:loadSkin', skin) + end) + end + if Config.EnableHud then + ESX.UI.HUD.SetDisplay(1.0) + end - TriggerEvent('esx:loadingScreenOff') + TriggerEvent('esx:loadingScreenOff') -- compatibility with old scripts, will be removed soon. end) RegisterNetEvent('esx:setMaxWeight') @@ -172,65 +169,46 @@ end) RegisterNetEvent('esx:addWeapon') AddEventHandler('esx:addWeapon', function(weaponName, ammo) - local playerPed = PlayerPedId() - local weaponHash = GetHashKey(weaponName) - - GiveWeaponToPed(playerPed, weaponHash, ammo, false, false) + -- Removed PlayerPedId() from being stored in a variable, not needed + -- when it's only being used once, also doing it in a few + -- functions below this one + GiveWeaponToPed(PlayerPedId(), weaponName, ammo, false, false) end) RegisterNetEvent('esx:addWeaponComponent') AddEventHandler('esx:addWeaponComponent', function(weaponName, weaponComponent) - local playerPed = PlayerPedId() - local weaponHash = GetHashKey(weaponName) local componentHash = ESX.GetWeaponComponent(weaponName, weaponComponent).hash - - GiveWeaponComponentToPed(playerPed, weaponHash, componentHash) + GiveWeaponComponentToPed(PlayerPedId(), weaponName, componentHash) end) RegisterNetEvent('esx:setWeaponAmmo') AddEventHandler('esx:setWeaponAmmo', function(weaponName, weaponAmmo) - local playerPed = PlayerPedId() - local weaponHash = GetHashKey(weaponName) - - SetPedAmmo(playerPed, weaponHash, weaponAmmo) + SetPedAmmo(PlayerPedId(), weaponName, weaponAmmo) end) RegisterNetEvent('esx:setWeaponTint') AddEventHandler('esx:setWeaponTint', function(weaponName, weaponTintIndex) - local playerPed = PlayerPedId() - local weaponHash = GetHashKey(weaponName) - - SetPedWeaponTintIndex(playerPed, weaponHash, weaponTintIndex) + SetPedWeaponTintIndex(PlayerPedId(), weaponName, weaponTintIndex) end) RegisterNetEvent('esx:removeWeapon') AddEventHandler('esx:removeWeapon', function(weaponName) local playerPed = PlayerPedId() - local weaponHash = GetHashKey(weaponName) - - RemoveWeaponFromPed(playerPed, weaponHash) - SetPedAmmo(playerPed, weaponHash, 0) -- remove leftover ammo + RemoveWeaponFromPed(playerPed, weaponName) + SetPedAmmo(playerPed, weaponName, 0) end) RegisterNetEvent('esx:removeWeaponComponent') AddEventHandler('esx:removeWeaponComponent', function(weaponName, weaponComponent) - local playerPed = PlayerPedId() - local weaponHash = GetHashKey(weaponName) local componentHash = ESX.GetWeaponComponent(weaponName, weaponComponent).hash - - RemoveWeaponComponentFromPed(playerPed, weaponHash, componentHash) + RemoveWeaponComponentFromPed(PlayerPedId(), weaponName, componentHash) end) RegisterNetEvent('esx:teleport') AddEventHandler('esx:teleport', function(coords) - local playerPed = PlayerPedId() - - -- ensure decmial number - coords.x = coords.x + 0.0 - coords.y = coords.y + 0.0 - coords.z = coords.z + 0.0 - - ESX.Game.Teleport(playerPed, coords) + -- The coords x, y and z were having 0.0 added to them here to make them floats + -- Since we are forcing vectors in the teleport function now we don't need to do it + ESX.Game.Teleport(PlayerPedId(), coords) end) RegisterNetEvent('esx:setJob') @@ -245,21 +223,18 @@ AddEventHandler('esx:setJob', function(Job) end) RegisterNetEvent('esx:spawnVehicle') -AddEventHandler('esx:spawnVehicle', function(vehicleName) - local model = (type(vehicleName) == 'number' and vehicleName or GetHashKey(vehicleName)) - - if IsModelInCdimage(model) then +AddEventHandler('esx:spawnVehicle', function(vehicle) + if IsModelInCdimage(vehicle) then local playerPed = PlayerPedId() local playerCoords, playerHeading = GetEntityCoords(playerPed), GetEntityHeading(playerPed) - ESX.Game.SpawnVehicle(model, playerCoords, playerHeading, function(vehicle) + ESX.Game.SpawnVehicle(vehicle, playerCoords, playerHeading, function(vehicle) TaskWarpPedIntoVehicle(playerPed, vehicle, -1) end) else - TriggerEvent('chat:addMessage', {args = {'^1SYSTEM', 'Invalid vehicle model.'}}) + TriggerEvent('chat:addMessage', { args = { '^1SYSTEM', 'Invalid vehicle model.' } }) end end) - RegisterNetEvent('esx:createPickup') AddEventHandler('esx:createPickup', function(pickupId, label, coords, type, name, components, tintIndex) local function setObjectProperties(object) @@ -372,10 +347,6 @@ if Config.EnableHud then end end end) - - AddEventHandler('esx:loadingScreenOff', function() - ESX.UI.HUD.SetDisplay(1.0) - end) end function StartServerSyncLoops() @@ -384,11 +355,10 @@ function StartServerSyncLoops() while true do Citizen.Wait(1000) - if isDead then - Citizen.Wait(500) - else - local playerPed = PlayerPedId() + local letSleep = true + local playerPed = PlayerPedId() + if IsPedArmed(playerPed, 4) then if IsPedShooting(playerPed) then local _,weaponHash = GetCurrentPedWeapon(playerPed, true) local weapon = ESX.GetWeaponFromHash(weaponHash) @@ -399,6 +369,9 @@ function StartServerSyncLoops() end end end + if letSleep then + Citizen.Wait(500) + end end end) @@ -425,17 +398,21 @@ function StartServerSyncLoops() end) end -Citizen.CreateThread(function() - while Config.EnableDefaultInventory do - Citizen.Wait(9) - - if IsControlJustReleased(0, 289) then - if IsInputDisabled(0) and not isDead and not ESX.UI.Menu.IsOpen('default', 'es_extended', 'inventory') then - ESX.ShowInventory() - end +if Config.EnableDefaultInventory then + RegisterCommand('showinv', function() + if not isDead and not ESX.UI.Menu.IsOpen('default', 'es_extended', 'inventory') then + ESX.ShowInventory() end - end -end) + end) + + RegisterKeyMapping('showinv', _U('keymap_showinventory'), 'keyboard', 'F2') +end + +-- disable wanted level +if not Config.EnableWantedLevel then + ClearPlayerWantedLevel(PlayerId()) + SetMaxWantedLevel(0) +end Citizen.CreateThread(function() while true do diff --git a/client/modules/death.lua b/client/modules/death.lua index b7cd047a..baa2e727 100644 --- a/client/modules/death.lua +++ b/client/modules/death.lua @@ -2,13 +2,15 @@ Citizen.CreateThread(function() local isDead = false while true do - Citizen.Wait(150) + Citizen.Wait(0) + local letSleep = 0 local player = PlayerId() if NetworkIsPlayerActive(player) then local playerPed = PlayerPedId() if IsPedFatallyInjured(playerPed) and not isDead then + letSleep = false isDead = true local killerEntity, deathCause = GetPedSourceOfDeath(playerPed), GetPedCauseOfDeath(playerPed) @@ -20,10 +22,14 @@ Citizen.CreateThread(function() PlayerKilled(deathCause) end - elseif not IsPedFatallyInjured(playerPed) then + elseif not IsPedFatallyInjured(playerPed) and isDead then + letSleep = false isDead = false end end + if letSleep then + Citizen.Wait(500) + end end end) diff --git a/es_extended.sql b/es_extended.sql index 6bfa339e..977ed8de 100644 --- a/es_extended.sql +++ b/es_extended.sql @@ -2,7 +2,7 @@ CREATE DATABASE IF NOT EXISTS `es_extended`; USE `es_extended`; CREATE TABLE `users` ( - `identifier` VARCHAR(64) NOT NULL, + `identifier` VARCHAR(60) NOT NULL, `accounts` LONGTEXT NULL DEFAULT NULL, `group` VARCHAR(50) NULL DEFAULT 'user', `inventory` LONGTEXT NULL DEFAULT NULL, diff --git a/version.json b/version.json index 1762b8fa..3ff5d1a3 100644 --- a/version.json +++ b/version.json @@ -1,5 +1,5 @@ { "version": "legacy", - "commit" : "1.1.8", - "changelog": "Code Improvements; Redundant Code Removal" + "commit" : "1.1.9", + "changelog": "Fix esx_identity" }