Skip to content

Commit

Permalink
refactor: (esx_policejob) await callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
bitpredator committed Mar 26, 2024
1 parent a09e4b3 commit f8616fd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 43 deletions.
37 changes: 15 additions & 22 deletions server-data/resources/[esx_addons]/esx_policejob/client/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -954,8 +954,7 @@ function OnPlayerData(k, v)
end
end

RegisterNetEvent('esx_phone:loaded')
AddEventHandler('esx_phone:loaded', function(phoneNumber, contacts)
RegisterNetEvent('esx_phone:loaded', function(phoneNumber, contacts)
local specialContact = {
name = TranslateCap('phone_police'),
number = 'police',
Expand All @@ -966,7 +965,7 @@ AddEventHandler('esx_phone:loaded', function(phoneNumber, contacts)
end)

-- don't show dispatches if the player isn't in service
AddEventHandler('esx_phone:cancelMessage', function(dispatchNumber)
RegisterNetEvent('esx_phone:cancelMessage', function(dispatchNumber)
if ESX.PlayerData.job and ESX.PlayerData.job.name == 'police' and ESX.PlayerData.job.name == dispatchNumber then
-- if esx_service is enabled
if Config.EnableESXService and not playerInService then
Expand All @@ -975,7 +974,7 @@ AddEventHandler('esx_phone:cancelMessage', function(dispatchNumber)
end
end)

AddEventHandler('esx_policejob:hasEnteredMarker', function(station, part, partNum)
RegisterNetEvent('esx_policejob:hasEnteredMarker', function(station, part, partNum)
if part == 'Cloakroom' then
CurrentAction = 'menu_cloakroom'
CurrentActionMsg = TranslateCap('open_cloackroom')
Expand All @@ -999,15 +998,15 @@ AddEventHandler('esx_policejob:hasEnteredMarker', function(station, part, partNu
end
end)

AddEventHandler('esx_policejob:hasExitedMarker', function(station, part, partNum)
RegisterNetEvent('esx_policejob:hasExitedMarker', function(station, part, partNum)
if not isInShopMenu then
ESX.CloseContext()
end

CurrentAction = nil
end)

AddEventHandler('esx_policejob:hasEnteredEntityZone', function(entity)
RegisterNetEvent('esx_policejob:hasEnteredEntityZone', function(entity)
local playerPed = PlayerPedId()

if ESX.PlayerData.job and ESX.PlayerData.job.name == 'police' and IsPedOnFoot(playerPed) then
Expand All @@ -1030,14 +1029,13 @@ AddEventHandler('esx_policejob:hasEnteredEntityZone', function(entity)
end
end)

AddEventHandler('esx_policejob:hasExitedEntityZone', function(entity)
RegisterNetEvent('esx_policejob:hasExitedEntityZone', function(entity)
if CurrentAction == 'remove_entity' then
CurrentAction = nil
end
end)

RegisterNetEvent('esx_policejob:handcuff')
AddEventHandler('esx_policejob:handcuff', function()
RegisterNetEvent('esx_policejob:handcuff', function()
isHandcuffed = not isHandcuffed
local playerPed = PlayerPedId()

Expand Down Expand Up @@ -1078,8 +1076,7 @@ AddEventHandler('esx_policejob:handcuff', function()
end
end)

RegisterNetEvent('esx_policejob:unrestrain')
AddEventHandler('esx_policejob:unrestrain', function()
RegisterNetEvent('esx_policejob:unrestrain', function()
if isHandcuffed then
local playerPed = PlayerPedId()
isHandcuffed = false
Expand All @@ -1098,8 +1095,7 @@ AddEventHandler('esx_policejob:unrestrain', function()
end
end)

RegisterNetEvent('esx_policejob:drag')
AddEventHandler('esx_policejob:drag', function(copId)
RegisterNetEvent('esx_policejob:drag', function(copId)
if isHandcuffed then
dragStatus.isDragged = not dragStatus.isDragged
dragStatus.CopId = copId
Expand Down Expand Up @@ -1136,8 +1132,7 @@ CreateThread(function()
end
end)

RegisterNetEvent('esx_policejob:putInVehicle')
AddEventHandler('esx_policejob:putInVehicle', function()
RegisterNetEvent('esx_policejob:putInVehicle', function()
if isHandcuffed then
local playerPed = PlayerPedId()
local vehicle, distance = ESX.Game.GetClosestVehicle()
Expand All @@ -1160,8 +1155,7 @@ AddEventHandler('esx_policejob:putInVehicle', function()
end
end)

RegisterNetEvent('esx_policejob:OutVehicle')
AddEventHandler('esx_policejob:OutVehicle', function()
RegisterNetEvent('esx_policejob:OutVehicle', function()
local GetVehiclePedIsIn = GetVehiclePedIsIn
local IsPedSittingInAnyVehicle = IsPedSittingInAnyVehicle
local TaskLeaveVehicle = TaskLeaveVehicle
Expand Down Expand Up @@ -1504,8 +1498,7 @@ function createBlip(id)
end
end

RegisterNetEvent('esx_policejob:updateBlip')
AddEventHandler('esx_policejob:updateBlip', function()
RegisterNetEvent('esx_policejob:updateBlip', function()

-- Refresh all blips
for k, existingBlip in pairs(blipsCops) do
Expand Down Expand Up @@ -1540,7 +1533,7 @@ AddEventHandler('esx_policejob:updateBlip', function()

end)

AddEventHandler('esx:onPlayerSpawn', function(spawn)
RegisterNetEvent('esx:onPlayerSpawn', function(spawn)
isDead = false
TriggerEvent('esx_policejob:unrestrain')

Expand All @@ -1550,11 +1543,11 @@ AddEventHandler('esx:onPlayerSpawn', function(spawn)
hasAlreadyJoined = true
end)

AddEventHandler('esx:onPlayerDeath', function(data)
RegisterNetEvent('esx:onPlayerDeath', function(data)
isDead = true
end)

AddEventHandler('onResourceStop', function(resource)
RegisterNetEvent('onResourceStop', function(resource)
if resource == GetCurrentResourceName() then
TriggerEvent('esx_policejob:unrestrain')
TriggerEvent('esx_phone:removeSpecialContact', 'police')
Expand Down
33 changes: 12 additions & 21 deletions server-data/resources/[esx_addons]/esx_policejob/server/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ end
TriggerEvent('esx_phone:registerNumber', 'police', TranslateCap('alert_police'), true, true)
TriggerEvent('esx_society:registerSociety', 'police', TranslateCap('society_police'), 'society_police', 'society_police', 'society_police', {type = 'public'})

RegisterNetEvent('esx_policejob:confiscatePlayerItem')
AddEventHandler('esx_policejob:confiscatePlayerItem', function(target, itemType, itemName, amount)
RegisterNetEvent('esx_policejob:confiscatePlayerItem', function(target, itemType, itemName, amount)
local source = source
local sourceXPlayer = ESX.GetPlayerFromId(source)
local targetXPlayer = ESX.GetPlayerFromId(target)
Expand Down Expand Up @@ -68,8 +67,7 @@ AddEventHandler('esx_policejob:confiscatePlayerItem', function(target, itemType,
end
end)

RegisterNetEvent('esx_policejob:handcuff')
AddEventHandler('esx_policejob:handcuff', function(target)
RegisterNetEvent('esx_policejob:handcuff', function(target)
local xPlayer = ESX.GetPlayerFromId(source)

if xPlayer.job.name == 'police' then
Expand All @@ -79,8 +77,7 @@ AddEventHandler('esx_policejob:handcuff', function(target)
end
end)

RegisterNetEvent('esx_policejob:drag')
AddEventHandler('esx_policejob:drag', function(target)
RegisterNetEvent('esx_policejob:drag', function(target)
local xPlayer = ESX.GetPlayerFromId(source)

if xPlayer.job.name == 'police' then
Expand All @@ -90,8 +87,7 @@ AddEventHandler('esx_policejob:drag', function(target)
end
end)

RegisterNetEvent('esx_policejob:putInVehicle')
AddEventHandler('esx_policejob:putInVehicle', function(target)
RegisterNetEvent('esx_policejob:putInVehicle', function(target)
local xPlayer = ESX.GetPlayerFromId(source)

if xPlayer.job.name == 'police' then
Expand All @@ -101,8 +97,7 @@ AddEventHandler('esx_policejob:putInVehicle', function(target)
end
end)

RegisterNetEvent('esx_policejob:OutVehicle')
AddEventHandler('esx_policejob:OutVehicle', function(target)
RegisterNetEvent('esx_policejob:OutVehicle', function(target)
local xPlayer = ESX.GetPlayerFromId(source)

if xPlayer.job.name == 'police' then
Expand All @@ -112,8 +107,7 @@ AddEventHandler('esx_policejob:OutVehicle', function(target)
end
end)

RegisterNetEvent('esx_policejob:getStockItem')
AddEventHandler('esx_policejob:getStockItem', function(itemName, count)
RegisterNetEvent('esx_policejob:getStockItem', function(itemName, count)
local source = source
local xPlayer = ESX.GetPlayerFromId(source)

Expand All @@ -137,8 +131,7 @@ AddEventHandler('esx_policejob:getStockItem', function(itemName, count)
end)
end)

RegisterNetEvent('esx_policejob:putStockItems')
AddEventHandler('esx_policejob:putStockItems', function(itemName, count)
RegisterNetEvent('esx_policejob:putStockItems', function(itemName, count)
local xPlayer = ESX.GetPlayerFromId(source)
local sourceItem = xPlayer.getInventoryItem(itemName)

Expand Down Expand Up @@ -426,7 +419,7 @@ ESX.RegisterServerCallback('esx_policejob:getPlayerInventory', function(source,
cb({items = items})
end)

AddEventHandler('playerDropped', function()
RegisterNetEvent('playerDropped', function()
local playerId = source
if playerId then
local xPlayer = ESX.GetPlayerFromId(playerId)
Expand All @@ -438,8 +431,7 @@ AddEventHandler('playerDropped', function()
end
end)

RegisterNetEvent('esx_policejob:spawned')
AddEventHandler('esx_policejob:spawned', function()
RegisterNetEvent('esx_policejob:spawned', function()
local playerId = source
local xPlayer = ESX.GetPlayerFromId(playerId)

Expand All @@ -449,14 +441,13 @@ AddEventHandler('esx_policejob:spawned', function()
end
end)

RegisterNetEvent('esx_policejob:forceBlip')
AddEventHandler('esx_policejob:forceBlip', function()
RegisterNetEvent('esx_policejob:forceBlip', function()
for _, xPlayer in pairs(ESX.GetExtendedPlayers('job', 'police')) do
TriggerClientEvent('esx_policejob:updateBlip', xPlayer.source)
end
end)

AddEventHandler('onResourceStart', function(resource)
RegisterNetEvent('onResourceStart', function(resource)
if resource == GetCurrentResourceName() then
Wait(5000)
for _, xPlayer in pairs(ESX.GetExtendedPlayers('job', 'police')) do
Expand All @@ -465,7 +456,7 @@ AddEventHandler('onResourceStart', function(resource)
end
end)

AddEventHandler('onResourceStop', function(resource)
RegisterNetEvent('onResourceStop', function(resource)
if resource == GetCurrentResourceName() then
TriggerEvent('esx_phone:removeNumber', 'police')
end
Expand Down

0 comments on commit f8616fd

Please sign in to comment.