From 994c56d9948fb950ecd78ea301a79210c2956092 Mon Sep 17 00:00:00 2001 From: Solareon <769465+solareon@users.noreply.github.com> Date: Tue, 9 Jul 2024 13:57:10 +0200 Subject: [PATCH 1/2] fix(client/main): prevent duplicate blip/zone creation on job change --- client/main.lua | 133 ++++++++++++++++++++++++++---------------------- 1 file changed, 73 insertions(+), 60 deletions(-) diff --git a/client/main.lua b/client/main.lua index c35608b8..ada29756 100644 --- a/client/main.lua +++ b/client/main.lua @@ -1,7 +1,7 @@ QBCore = exports['qb-core']:GetCoreObject() PlayerData = {} inHuntingZone, inNoDispatchZone = false, false -huntingzone, nodispatchzone = nil , nil +local huntingZones, nodispatchZones, huntingBlips = {} , {}, {} local blips = {} local radius2 = {} @@ -16,7 +16,79 @@ local function toggleUI(bool) SendNUIMessage({ action = "setVisible", data = bool }) end +-- Zone Functions -- +local function removeZones() + -- Hunting Zone -- + for i = 1, #huntingZones do + huntingZones[i]:remove() + end + -- No Dispatch Zone -- + for i = 1, #nodispatchZones do + nodispatchZones[i]:remove() + end + -- Hunting Blips -- + for i = 1, #huntingBlips do + RemoveBlip(huntingBlips[i]) + end +end + +local function createZones() + -- Hunting Zone -- + if Config.Locations['HuntingZones'][1] then + for _, hunting in pairs(Config.Locations["HuntingZones"]) do + -- Creates the Blips + if Config.EnableHuntingBlip then + local blip = AddBlipForCoord(hunting.coords.x, hunting.coords.y, hunting.coords.z) + local huntingradius = AddBlipForRadius(hunting.coords.x, hunting.coords.y, hunting.coords.z, hunting.radius) + SetBlipSprite(blip, 442) + SetBlipAsShortRange(blip, true) + SetBlipScale(blip, 0.8) + SetBlipColour(blip, 0) + SetBlipColour(huntingradius, 0) + SetBlipAlpha(huntingradius, 40) + BeginTextCommandSetBlipName("STRING") + AddTextComponentString(hunting.label) + EndTextCommandSetBlipName(blip) + huntingBlips[#huntingBlips+1] = blip + huntingBlips[#huntingBlips+1] = huntingradius + end + -- Creates the Sphere -- + local huntingZone = lib.zones.sphere({ + coords = hunting.coords, + radius = hunting.radius, + debug = Config.Debug, + onEnter = function() + inHuntingZone = true + end, + onExit = function() + inHuntingZone = false + end + }) + HuntingZones[#HuntingZones+1] = huntingZone + end + end + -- No Dispatch Zone -- + if Config.Locations['NoDispatchZones'][1] then + for _, nodispatch in pairs(Config.Locations["NoDispatchZones"]) do + local nodispatchZone = lib.zones.box({ + coords = nodispatch.coords, + size = vec3(nodispatch.length, nodispatch.width, nodispatch.maxZ - nodispatch.minZ), + rotation = nodispatch.heading, + debug = Config.Debug, + onEnter = function() + inNoDispatchZone = true + end, + onExit = function() + inNoDispatchZone = false + end + }) + nodispatchZones[#nodispatchZones+1] = nodispatchZone + end + end +end + local function setupDispatch() + removeZones() local playerInfo = QBCore.Functions.GetPlayerData() local locales = lib.getLocales() PlayerData = { @@ -171,65 +243,6 @@ local function addBlip(data, blipData) end end --- Zone Functions -- -function createZones() - -- Hunting Zone -- - if Config.Locations['HuntingZones'][1] then - for _, hunting in pairs(Config.Locations["HuntingZones"]) do - -- Creates the Blips - if Config.EnableHuntingBlip then - local blip = AddBlipForCoord(hunting.coords.x, hunting.coords.y, hunting.coords.z) - local huntingradius = AddBlipForRadius(hunting.coords.x, hunting.coords.y, hunting.coords.z, hunting.radius) - SetBlipSprite(blip, 442) - SetBlipAsShortRange(blip, true) - SetBlipScale(blip, 0.8) - SetBlipColour(blip, 0) - SetBlipColour(huntingradius, 0) - SetBlipAlpha(huntingradius, 40) - BeginTextCommandSetBlipName("STRING") - AddTextComponentString(hunting.label) - EndTextCommandSetBlipName(blip) - end - -- Creates the Sphere -- - huntingzone = lib.zones.sphere({ - coords = hunting.coords, - radius = hunting.radius, - debug = Config.Debug, - onEnter = function() - inHuntingZone = true - end, - onExit = function() - inHuntingZone = false - end - }) - end - end - -- No Dispatch Zone -- - if Config.Locations['NoDispatchZones'][1] then - for _, nodispatch in pairs(Config.Locations["NoDispatchZones"]) do - nodispatchzone = lib.zones.box({ - coords = nodispatch.coords, - size = vec3(nodispatch.length, nodispatch.width, nodispatch.maxZ - nodispatch.minZ), - rotation = nodispatch.heading, - debug = Config.Debug, - onEnter = function() - inNoDispatchZone = true - end, - onExit = function() - inNoDispatchZone = false - end - }) - end - end -end - -local function removeZones() - -- Hunting Zone -- - huntingzone:remove() - -- No Dispatch Zone -- - nodispatchzone:remove() -end - -- Keybind local RespondToDispatch = lib.addKeybind({ name = 'RespondToDispatch', From ad6338fc39b4122d6279941ecabf2fc60709b56a Mon Sep 17 00:00:00 2001 From: Solareon <769465+solareon@users.noreply.github.com> Date: Tue, 9 Jul 2024 14:03:19 +0200 Subject: [PATCH 2/2] refactor(client/main): move createZone to OnPlayerLoaded --- client/main.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/client/main.lua b/client/main.lua index ada29756..13f87254 100644 --- a/client/main.lua +++ b/client/main.lua @@ -88,7 +88,6 @@ local function createZones() end local function setupDispatch() - removeZones() local playerInfo = QBCore.Functions.GetPlayerData() local locales = lib.getLocales() PlayerData = { @@ -109,8 +108,6 @@ local function setupDispatch() Wait(1000) - createZones() - SendNUIMessage({ action = "setupUI", data = { @@ -313,7 +310,10 @@ end) -- EventHandlers RegisterNetEvent("QBCore:Client:OnJobUpdate", setupDispatch) -AddEventHandler('QBCore:Client:OnPlayerLoaded', setupDispatch) +AddEventHandler('QBCore:Client:OnPlayerLoaded', function() + setupDispatch() + createZones() +end) AddEventHandler('QBCore:Client:OnPlayerUnload', removeZones)