From aed4db306e13febe9f3c6ddacd304b05d05549b0 Mon Sep 17 00:00:00 2001 From: ElPumpo Date: Thu, 5 Apr 2018 12:32:36 +0200 Subject: [PATCH] Added blips for cops, closes #4 --- client/main.lua | 60 ++++++++++++++++++++++++++++++++++++++++++++++++- server/main.lua | 44 ++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+), 1 deletion(-) diff --git a/client/main.lua b/client/main.lua index 4f838d23..a1947c94 100644 --- a/client/main.lua +++ b/client/main.lua @@ -23,6 +23,8 @@ local CurrentActionData = {} local IsHandcuffed = false local IsDragged = false local CopPed = 0 +local hasAlreadyJoined = false +local blipsCops = {} ESX = nil GUI.Time = 0 @@ -1211,7 +1213,10 @@ end) RegisterNetEvent('esx:setJob') AddEventHandler('esx:setJob', function(job) - PlayerData.job = job + PlayerData.job = job + + Citizen.Wait(5000) + TriggerServerEvent('esx_policejob:forceBlip') end) RegisterNetEvent('esx_phone:loaded') @@ -1794,3 +1799,56 @@ Citizen.CreateThread(function() end end) + +function createBlip(id) + ped = GetPlayerPed(id) + blip = GetBlipFromEntity(ped) + + if not DoesBlipExist(blip) then -- Add blip and create head display on player + blip = AddBlipForEntity(ped) + SetBlipSprite(blip, 1) + Citizen.InvokeNative(0x5FBCA48327B914DF, blip, true) -- Player Blip indicator + SetBlipRotation(blip, math.ceil(GetEntityHeading(veh))) -- update rotation + SetBlipNameToPlayerName(blip, id) -- update blip name + SetBlipScale(blip, 0.85) -- set scale + SetBlipAsShortRange(blip, true) + + table.insert(blipsCops, blip) -- add blip to array so we can remove it later + end +end + +RegisterNetEvent('esx_policejob:updateBlip') +AddEventHandler('esx_policejob:updateBlip', function() + + -- Refresh all blips + for k, existingBlip in pairs(blipsCops) do + RemoveBlip(existingBlip) + end + + -- Clean the blip table + blipsCops = {} + + ESX.TriggerServerCallback('esx_policejob:isCop', function(isCop) + if isCop then + ESX.TriggerServerCallback('esx_society:getOnlinePlayers', function(players) + for i=1, #players, 1 do + if players[i].job.name == 'police' then + for id = 0, 32 do + if NetworkIsPlayerActive(id) and GetPlayerPed(id) ~= GetPlayerPed(-1) and GetPlayerName(id) == players[i].name then + createBlip(id) + end + end + end + end + end) + end + end) -- You don't need to specify source + +end) + +AddEventHandler('playerSpawned', function(spawn) + if not hasAlreadyJoined then + TriggerServerEvent('esx_policejob:spawned') + end + hasAlreadyJoined = true +end) diff --git a/server/main.lua b/server/main.lua index ddb9d615..d6994a7e 100644 --- a/server/main.lua +++ b/server/main.lua @@ -458,3 +458,47 @@ ESX.RegisterServerCallback('esx_policejob:getPlayerInventory', function(source, }) end) + +AddEventHandler('playerDropped', function() + -- Save the source in case we lose it (which happens a lot) + local _source = source + + -- Did the player ever join? + if _source ~= nil then + local xPlayer = ESX.GetPlayerFromId(_source) + + -- Is it worth telling all clients to refresh? + if xPlayer ~= nil and xPlayer.job ~= nil and xPlayer.job.name == 'police' then + Citizen.Wait(5000) + TriggerClientEvent('esx_policejob:updateBlip', -1) + end + end +end) + +RegisterServerEvent('esx_policejob:spawned') +AddEventHandler('esx_policejob:spawned', function() + local _source = source + local xPlayer = ESX.GetPlayerFromId(_source) + + if xPlayer ~= nil and xPlayer.job ~= nil and xPlayer.job.name == 'police' then + Citizen.Wait(5000) + TriggerClientEvent('esx_policejob:updateBlip', -1) + end +end) + +RegisterServerEvent('esx_policejob:forceBlip') +AddEventHandler('esx_policejob:forceBlip', function() + TriggerClientEvent('esx_policejob:updateBlip', -1) +end) + +ESX.RegisterServerCallback('esx_policejob:isCop', function(source, cb) + local xPlayer = ESX.GetPlayerFromId(source) + cb(xPlayer ~= nil and xPlayer.job ~= nil and xPlayer.job.name == 'police') +end) + +AddEventHandler('onResourceStart', function(resource) + if resource == GetCurrentResourceName() then + Citizen.Wait(5000) + TriggerClientEvent('esx_policejob:updateBlip', -1) + end +end) \ No newline at end of file