From 2e5d1111c87e86ae71e9d922ffb5875247742cdf Mon Sep 17 00:00:00 2001 From: Thekuca Date: Thu, 1 Feb 2024 23:15:48 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8Frefactor(es=5Fextended/client?= =?UTF-8?q?/main)=20general=20code=20refactor=20and=20await=20callbacks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- [core]/es_extended/client/main.lua | 252 ++++++++++++----------------- 1 file changed, 105 insertions(+), 147 deletions(-) diff --git a/[core]/es_extended/client/main.lua b/[core]/es_extended/client/main.lua index f161f1f44..4df316dda 100644 --- a/[core]/es_extended/client/main.lua +++ b/[core]/es_extended/client/main.lua @@ -14,9 +14,7 @@ CreateThread(function() end end) -RegisterNetEvent("esx:requestModel", function(model) - ESX.Streaming.RequestModel(model) -end) +RegisterNetEvent("esx:requestModel", ESX.Streaming.RequestModel) function ESX.SpawnPlayer(skin, coords, cb) local p = promise.new() @@ -71,7 +69,7 @@ AddEventHandler("esx:playerLoaded", function(xPlayer, _, skin) local timer = GetGameTimer() while not HaveAllStreamingRequestsCompleted(ESX.PlayerData.ped) and (GetGameTimer() - timer) < 2000 do Wait(0) - end + end if Config.EnablePVP then SetCanAttackFriendly(ESX.PlayerData.ped, true, false) @@ -211,8 +209,7 @@ AddEventHandler("esx:playerLoaded", function(xPlayer, _, skin) end) -RegisterNetEvent("esx:onPlayerLogout") -AddEventHandler("esx:onPlayerLogout", function() +RegisterNetEvent("esx:onPlayerLogout", function() ESX.PlayerLoaded = false end) @@ -277,9 +274,9 @@ AddStateBagChangeHandler("VehicleProperties", nil, function(bagName, _, value) local netId = bagName:gsub("entity:", "") local vehicle = NetToVeh(tonumber(netId)) - + ESX.Game.SetVehicleProperties(vehicle, value) -end) +end) RegisterNetEvent("esx:setAccountMoney") AddEventHandler("esx:setAccountMoney", function(account) @@ -294,8 +291,7 @@ AddEventHandler("esx:setAccountMoney", function(account) end) if not Config.OxInventory then - RegisterNetEvent("esx:addInventoryItem") - AddEventHandler("esx:addInventoryItem", function(item, count, showNotification) + RegisterNetEvent("esx:addInventoryItem", function(item, count, showNotification) for k, v in ipairs(ESX.PlayerData.inventory) do if v.name == item then ESX.UI.ShowInventoryItemNotification(true, v.label, count - v.count) @@ -309,8 +305,7 @@ if not Config.OxInventory then end end) - RegisterNetEvent("esx:removeInventoryItem") - AddEventHandler("esx:removeInventoryItem", function(item, count, showNotification) + RegisterNetEvent("esx:removeInventoryItem", function(item, count, showNotification) for i = 1, #ESX.PlayerData.inventory do if ESX.PlayerData.inventory[i].name == item then ESX.UI.ShowInventoryItemNotification(false, ESX.PlayerData.inventory[i].label, ESX.PlayerData.inventory[i].count - count) @@ -324,46 +319,38 @@ if not Config.OxInventory then end end) - RegisterNetEvent("esx:addWeapon") - AddEventHandler("esx:addWeapon", function() + RegisterNetEvent("esx:addWeapon", function() print("[^1ERROR^7] event ^5'esx:addWeapon'^7 Has Been Removed. Please use ^5xPlayer.addWeapon^7 Instead!") end) - RegisterNetEvent("esx:addWeaponComponent") - AddEventHandler("esx:addWeaponComponent", function() + RegisterNetEvent("esx:addWeaponComponent", function() print("[^1ERROR^7] event ^5'esx:addWeaponComponent'^7 Has Been Removed. Please use ^5xPlayer.addWeaponComponent^7 Instead!") end) - RegisterNetEvent("esx:setWeaponAmmo") - AddEventHandler("esx:setWeaponAmmo", function() + RegisterNetEvent("esx:setWeaponAmmo", function() print("[^1ERROR^7] event ^5'esx:setWeaponAmmo'^7 Has Been Removed. Please use ^5xPlayer.addWeaponAmmo^7 Instead!") end) - RegisterNetEvent("esx:setWeaponTint") - AddEventHandler("esx:setWeaponTint", function(weapon, weaponTintIndex) + RegisterNetEvent("esx:setWeaponTint", function(weapon, weaponTintIndex) SetPedWeaponTintIndex(ESX.PlayerData.ped, joaat(weapon), weaponTintIndex) end) - RegisterNetEvent("esx:removeWeapon") - AddEventHandler("esx:removeWeapon", function() + RegisterNetEvent("esx:removeWeapon", function() print("[^1ERROR^7] event ^5'esx:removeWeapon'^7 Has Been Removed. Please use ^5xPlayer.removeWeapon^7 Instead!") end) - RegisterNetEvent("esx:removeWeaponComponent") - AddEventHandler("esx:removeWeaponComponent", function(weapon, weaponComponent) + RegisterNetEvent("esx:removeWeaponComponent", function(weapon, weaponComponent) local componentHash = ESX.GetWeaponComponent(weapon, weaponComponent).hash RemoveWeaponComponentFromPed(ESX.PlayerData.ped, joaat(weapon), componentHash) end) end -RegisterNetEvent("esx:setJob") -AddEventHandler("esx:setJob", function(Job) +RegisterNetEvent("esx:setJob", function(Job) ESX.SetPlayerData("job", Job) end) if not Config.OxInventory then - RegisterNetEvent("esx:createPickup") - AddEventHandler("esx:createPickup", function(pickupId, label, coords, itemType, name, components, tintIndex) + RegisterNetEvent("esx:createPickup", function(pickupId, label, coords, itemType, name, components, tintIndex) local function setObjectProperties(object) SetEntityAsMissionEntity(object, true, false) PlaceObjectOnGroundProperly(object) @@ -378,33 +365,31 @@ if not Config.OxInventory then } end - if itemType == "item_weapon" then - local weaponHash = joaat(name) - ESX.Streaming.RequestWeaponAsset(weaponHash) - local pickupObject = CreateWeaponObject(weaponHash, 50, coords.x, coords.y, coords.z, true, 1.0, 0) - SetWeaponObjectTintIndex(pickupObject, tintIndex) + if itemType ~= 'item_weapon' then + return ESX.Game.SpawnLocalObject('prop_money_bag_01', coords, setObjectProperties) + end - for _, v in ipairs(components) do - local component = ESX.GetWeaponComponent(name, v) - GiveWeaponComponentToWeaponObject(pickupObject, component.hash) - end + local weaponHash = joaat(name) + ESX.Streaming.RequestWeaponAsset(weaponHash) + local pickupObject = CreateWeaponObject(weaponHash, 50, coords.x, coords.y, coords.z, true, 1.0, 0) + SetWeaponObjectTintIndex(pickupObject, tintIndex) - setObjectProperties(pickupObject) - else - ESX.Game.SpawnLocalObject("prop_money_bag_01", coords, setObjectProperties) + for _, v in ipairs(components) do + local component = ESX.GetWeaponComponent(name, v) + GiveWeaponComponentToWeaponObject(pickupObject, component.hash) end + + setObjectProperties(pickupObject) end) - RegisterNetEvent("esx:createMissingPickups") - AddEventHandler("esx:createMissingPickups", function(missingPickups) + RegisterNetEvent("esx:createMissingPickups", function(missingPickups) for pickupId, pickup in pairs(missingPickups) do TriggerEvent("esx:createPickup", pickupId, pickup.label, vector3(pickup.coords.x, pickup.coords.y, pickup.coords.z - 1.0), pickup.type, pickup.name, pickup.components, pickup.tintIndex) end end) end -RegisterNetEvent("esx:registerSuggestions") -AddEventHandler("esx:registerSuggestions", function(registeredCommands) +RegisterNetEvent("esx:registerSuggestions", function(registeredCommands) for name, command in pairs(registeredCommands) do if command.suggestion then TriggerEvent("chat:addSuggestion", ("/%s"):format(name), command.suggestion.help, command.suggestion.arguments) @@ -413,12 +398,11 @@ AddEventHandler("esx:registerSuggestions", function(registeredCommands) end) if not Config.OxInventory then - RegisterNetEvent("esx:removePickup") - AddEventHandler("esx:removePickup", function(pickupId) - if pickups[pickupId] and pickups[pickupId].obj then - ESX.Game.DeleteObject(pickups[pickupId].obj) - pickups[pickupId] = nil - end + RegisterNetEvent("esx:removePickup", function(pickupId) + if not pickups[pickupId] and not pickups[pickupId].obj then return end + + ESX.Game.DeleteObject(pickups[pickupId].obj) + pickups[pickupId] = nil end) end @@ -455,9 +439,8 @@ end if not Config.OxInventory and Config.EnableDefaultInventory then ESX.RegisterInput("showinv", TranslateCap("keymap_showinventory"), "keyboard", "F2", function() - if not ESX.PlayerData.dead then - ESX.ShowInventory() - end + if ESX.PlayerData.dead then return end + ESX.ShowInventory() end) end @@ -516,8 +499,7 @@ end ----- Admin commands from esx_adminplus -RegisterNetEvent("esx:tpm") -AddEventHandler("esx:tpm", function() +RegisterNetEvent("esx:tpm", function() local GetEntityCoords = GetEntityCoords local GetGroundZFor_3dCoord = GetGroundZFor_3dCoord local GetFirstBlipInfoId = GetFirstBlipInfoId @@ -526,82 +508,71 @@ AddEventHandler("esx:tpm", function() local GetBlipInfoIdCoord = GetBlipInfoIdCoord local GetVehiclePedIsIn = GetVehiclePedIsIn - ESX.TriggerServerCallback("esx:isUserAdmin", function(admin) - if not admin then - return - end - local blipMarker = GetFirstBlipInfoId(8) - if not DoesBlipExist(blipMarker) then - ESX.ShowNotification(TranslateCap("tpm_nowaypoint"), true, false, 140) - return "marker" - end + if not ESX.TriggerServerCallback.Await('esx:isUserAdmin') then return end - -- Fade screen to hide how clients get teleported. - DoScreenFadeOut(650) - while not IsScreenFadedOut() do - Wait(0) - end + local blipMarker = GetFirstBlipInfoId(8) + if not DoesBlipExist(blipMarker) then + ESX.ShowNotification(TranslateCap("tpm_nowaypoint"), true, false, 140) + return "marker" + end - local ped, coords = ESX.PlayerData.ped, GetBlipInfoIdCoord(blipMarker) - local vehicle = GetVehiclePedIsIn(ped, false) - local oldCoords = GetEntityCoords(ped) + DoScreenFadeOut(650) + while not IsScreenFadedOut() do + Wait(0) + end - -- Unpack coords instead of having to unpack them while iterating. - -- 825.0 seems to be the max a player can reach while 0.0 being the lowest. - local x, y, groundZ, Z_START = coords["x"], coords["y"], 850.0, 950.0 - local found = false - FreezeEntityPosition(vehicle > 0 and vehicle or ped, true) + local ped, coords = ESX.PlayerData.ped, GetBlipInfoIdCoord(blipMarker) + local vehicle = GetVehiclePedIsIn(ped, false) + local oldCoords = GetEntityCoords(ped) - for i = Z_START, 0, -25.0 do - local z = i - if (i % 2) ~= 0 then - z = Z_START - i - end + local x, y, groundZ, Z_START = coords["x"], coords["y"], 850.0, 950.0 + local found = false + FreezeEntityPosition(vehicle > 0 and vehicle or ped, true) - NewLoadSceneStart(x, y, z, x, y, z, 50.0, 0) - local curTime = GetGameTimer() - while IsNetworkLoadingScene() do - if GetGameTimer() - curTime > 1000 then - break - end - Wait(0) - end - NewLoadSceneStop() - SetPedCoordsKeepVehicle(ped, x, y, z) + for i = Z_START, 0, -25.0 do + local z = i + if (i % 2) ~= 0 then + z = Z_START - i + end - while not HasCollisionLoadedAroundEntity(ped) do - RequestCollisionAtCoord(x, y, z) - if GetGameTimer() - curTime > 1000 then - break - end - Wait(0) + NewLoadSceneStart(x, y, z, x, y, z, 50.0, 0) + local curTime = GetGameTimer() + while IsNetworkLoadingScene() do + if GetGameTimer() - curTime > 1000 then + break end + Wait(0) + end + NewLoadSceneStop() + SetPedCoordsKeepVehicle(ped, x, y, z) - -- Get ground coord. As mentioned in the natives, this only works if the client is in render distance. - found, groundZ = GetGroundZFor_3dCoord(x, y, z, false) - if found then - Wait(0) - SetPedCoordsKeepVehicle(ped, x, y, groundZ) + while not HasCollisionLoadedAroundEntity(ped) do + RequestCollisionAtCoord(x, y, z) + if GetGameTimer() - curTime > 1000 then break end Wait(0) end - -- Remove black screen once the loop has ended. - DoScreenFadeIn(650) - FreezeEntityPosition(vehicle > 0 and vehicle or ped, false) - - if not found then - -- If we can't find the coords, set the coords to the old ones. - -- We don't unpack them before since they aren't in a loop and only called once. - SetPedCoordsKeepVehicle(ped, oldCoords["x"], oldCoords["y"], oldCoords["z"] - 1.0) - ESX.ShowNotification(TranslateCap("tpm_success"), true, false, 140) + found, groundZ = GetGroundZFor_3dCoord(x, y, z, false) + if found then + Wait(0) + SetPedCoordsKeepVehicle(ped, x, y, groundZ) + break end + Wait(0) + end + + DoScreenFadeIn(650) + FreezeEntityPosition(vehicle > 0 and vehicle or ped, false) - -- If Z coord was found, set coords in found coords. - SetPedCoordsKeepVehicle(ped, x, y, groundZ) + if not found then + SetPedCoordsKeepVehicle(ped, oldCoords["x"], oldCoords["y"], oldCoords["z"] - 1.0) ESX.ShowNotification(TranslateCap("tpm_success"), true, false, 140) - end) + end + + SetPedCoordsKeepVehicle(ped, x, y, groundZ) + ESX.ShowNotification(TranslateCap("tpm_success"), true, false, 140) end) local noclip = false @@ -649,34 +620,27 @@ local function noclipThread() end end -RegisterNetEvent("esx:noclip") -AddEventHandler("esx:noclip", function() - ESX.TriggerServerCallback("esx:isUserAdmin", function(admin) - if not admin then - return - end +RegisterNetEvent("esx:noclip", function() + if not ESX.TriggerServerCallback.Await('esx:isUserAdmin') then return end - if not noclip then - noclip_pos = GetEntityCoords(ESX.PlayerData.ped, false) - heading = GetEntityHeading(ESX.PlayerData.ped) - end + if not noclip then + noclip_pos = GetEntityCoords(ESX.PlayerData.ped, false) + heading = GetEntityHeading(ESX.PlayerData.ped) + end - noclip = not noclip - if noclip then - CreateThread(noclipThread) - end + noclip = not noclip + if noclip then + CreateThread(noclipThread) + end - ESX.ShowNotification(TranslateCap("noclip_message", noclip and Translate("enabled") or Translate("disabled")), true, false, 140) - end) + ESX.ShowNotification(TranslateCap("noclip_message", noclip and Translate("enabled") or Translate("disabled")), true, false, 140) end) -RegisterNetEvent("esx:killPlayer") -AddEventHandler("esx:killPlayer", function() +RegisterNetEvent("esx:killPlayer", function() SetEntityHealth(ESX.PlayerData.ped, 0) end) -RegisterNetEvent("esx:repairPedVehicle") -AddEventHandler("esx:repairPedVehicle", function() +RegisterNetEvent("esx:repairPedVehicle", function() local ped = ESX.PlayerData.ped local vehicle = GetVehiclePedIsIn(ped, false) SetVehicleEngineHealth(vehicle, 1000) @@ -685,18 +649,12 @@ AddEventHandler("esx:repairPedVehicle", function() SetVehicleDirtLevel(vehicle, 0) end) -RegisterNetEvent("esx:freezePlayer") -AddEventHandler("esx:freezePlayer", function(input) - local player = PlayerId() - if input == "freeze" then - SetEntityCollision(ESX.PlayerData.ped, false) - FreezeEntityPosition(ESX.PlayerData.ped, true) - SetPlayerInvincible(player, true) - elseif input == "unfreeze" then - SetEntityCollision(ESX.PlayerData.ped, true) - FreezeEntityPosition(ESX.PlayerData.ped, false) - SetPlayerInvincible(player, false) - end +RegisterNetEvent("esx:freezePlayer", function(input) + local state = input == 'freeze' + + SetEntityCollision(ESX.PlayerData.ped, not state) + FreezeEntityPosition(ESX.PlayerData.ped, state) + SetPlayerInvincible(PlayerId(), state) end) ESX.RegisterClientCallback("esx:GetVehicleType", function(cb, model)