Skip to content

Commit

Permalink
Merge pull request #677 from bitpredator/dev
Browse files Browse the repository at this point in the history
refactor(es_extended) general code refactor and await callbacks
  • Loading branch information
bitpredator authored Mar 25, 2024
2 parents a17b43c + 74b5ea5 commit fdbfda0
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 223 deletions.
135 changes: 48 additions & 87 deletions server-data/resources/[esx]/es_extended/client/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -212,8 +210,7 @@ AddEventHandler("esx:playerLoaded", function(xPlayer, _, skin)
end)


RegisterNetEvent("esx:onPlayerLogout")
AddEventHandler("esx:onPlayerLogout", function()
RegisterNetEvent("esx:onPlayerLogout", function()
ESX.PlayerLoaded = false
end)

Expand Down Expand Up @@ -278,7 +275,7 @@ AddStateBagChangeHandler("VehicleProperties", nil, function(bagName, _, value)

local netId = bagName:gsub("entity:", "")
local vehicle = NetToVeh(tonumber(netId))

ESX.Game.SetVehicleProperties(vehicle, value)
end)

Expand All @@ -295,8 +292,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)
Expand All @@ -310,8 +306,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)
Expand All @@ -325,46 +320,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)
Expand All @@ -379,33 +366,30 @@ 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)
Expand All @@ -414,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

Expand Down Expand Up @@ -456,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

Expand Down Expand Up @@ -517,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
Expand All @@ -527,17 +508,15 @@ AddEventHandler("esx:tpm", function()
local GetBlipInfoIdCoord = GetBlipInfoIdCoord
local GetVehiclePedIsIn = GetVehiclePedIsIn

ESX.TriggerServerCallback("esx:isUserAdmin", function(admin)
if not admin then
return
end
ESX.TriggerServerCallback('esx:isUserAdmin', function(isAdmin)
if not isAdmin then return end

local blipMarker = GetFirstBlipInfoId(8)
if not DoesBlipExist(blipMarker) then
ESX.ShowNotification(TranslateCap("tpm_nowaypoint"), true, false, 140)
return "marker"
end

-- Fade screen to hide how clients get teleported.
DoScreenFadeOut(650)
while not IsScreenFadedOut() do
Wait(0)
Expand All @@ -547,8 +526,6 @@ AddEventHandler("esx:tpm", function()
local vehicle = GetVehiclePedIsIn(ped, false)
local oldCoords = GetEntityCoords(ped)

-- 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)
Expand Down Expand Up @@ -578,7 +555,6 @@ AddEventHandler("esx:tpm", function()
Wait(0)
end

-- 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)
Expand All @@ -588,18 +564,14 @@ AddEventHandler("esx:tpm", function()
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)
end

-- If Z coord was found, set coords in found coords.
SetPedCoordsKeepVehicle(ped, x, y, groundZ)
ESX.ShowNotification(TranslateCap("tpm_success"), true, false, 140)
end)
Expand Down Expand Up @@ -650,12 +622,9 @@ 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()
ESX.TriggerServerCallback('esx:isUserAdmin', function(isAdmin)
if not isAdmin then return end

if not noclip then
noclip_pos = GetEntityCoords(ESX.PlayerData.ped, false)
Expand All @@ -671,13 +640,11 @@ AddEventHandler("esx:noclip", function()
end)
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)
Expand All @@ -686,18 +653,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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ local clientCallbacks = {}
ESX.TriggerServerCallback = function(eventName, callback, ...)
serverRequests[RequestId] = callback

TriggerServerEvent("esx:triggerServerCallback", eventName, RequestId, GetInvokingResource() or "unknown", ...)
TriggerServerEvent('esx:triggerServerCallback', eventName, RequestId, GetInvokingResource() or 'unknown', ...)

RequestId = RequestId + 1
end
Expand Down
2 changes: 1 addition & 1 deletion server-data/resources/[esx]/es_extended/fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,5 @@ dependencies {
'/native:0x6AE51D4B',
'/native:0xA61C8FC6',
'oxmysql',
'spawnmanager',
'spawnmanager'
}
Loading

0 comments on commit fdbfda0

Please sign in to comment.