local QBCore = exports['qb-core']:GetCoreObject() local isJobMenuOpen = false local lastJobCheck = 0 local checkInterval = 60000 -- Check for new jobs every 60 seconds local function GetJobs() local p = promise.new() QBCore.Functions.TriggerCallback('ps-multijob:getJobs', function(result) p:resolve(result) end) return Citizen.Await(p) end local function OpenUI() local job = QBCore.Functions.GetPlayerData().job SetNuiFocus(true, true) isJobMenuOpen = true SendNUIMessage({ action = 'sendjobs', activeJob = job["name"], onDuty = job["onduty"], jobs = GetJobs(), side = Config.Side, }) lastJobCheck = GetGameTimer() -- Reset the timer when we open the UI end -- Function to check for job updates local function CheckForJobUpdates() if isJobMenuOpen and (GetGameTimer() - lastJobCheck) > checkInterval then lastJobCheck = GetGameTimer() local jobs = GetJobs() SendNUIMessage({ action = 'updateJobList', jobs = jobs }) end end RegisterNUICallback('selectjob', function(data, cb) TriggerServerEvent("ps-multijob:changeJob", data["name"], data["grade"]) local onDuty = false if data["name"] ~= "police" then onDuty = QBCore.Shared.Jobs[data["name"]].defaultDuty end cb({onDuty = onDuty}) end) RegisterNUICallback('closemenu', function(data, cb) cb({}) SetNuiFocus(false, false) isJobMenuOpen = false end) RegisterNUICallback('removejob', function(data, cb) TriggerServerEvent("ps-multijob:removeJob", data["name"], data["grade"]) local jobs = GetJobs() jobs[data["name"]] = nil cb(jobs) end) RegisterNUICallback('toggleduty', function(data, cb) cb({}) local job = QBCore.Functions.GetPlayerData().job.name if Config.DenyDuty[job] then TriggerEvent("QBCore:Notify", 'Not allowed to use this station for clock-in.', 'error') return end TriggerServerEvent("QBCore:ToggleDuty") end) RegisterNetEvent('QBCore:Client:OnJobUpdate', function(JobInfo) SendNUIMessage({ action = 'updatejob', name = JobInfo["name"], label = JobInfo["label"], onDuty = JobInfo["onduty"], gradeLabel = JobInfo["grade"].name, grade = JobInfo["grade"].level, salary = JobInfo["payment"], isWhitelist = Config.WhitelistJobs[JobInfo["name"]] or false, description = Config.Descriptions[JobInfo["name"]] or "", icon = Config.FontAwesomeIcons[JobInfo["name"]] or "", }) end) RegisterCommand("jobmenu", OpenUI, false) RegisterKeyMapping('jobmenu', "Show Job Management", "keyboard", "J") TriggerEvent('chat:removeSuggestion', '/jobmenu') RegisterNetEvent('ps-multijob:refreshJobs', function() if isJobMenuOpen then OpenUI() -- Refresh the UI with updated job data end end) -- Request job updates from server periodically Citizen.CreateThread(function() while true do Citizen.Wait(checkInterval) TriggerServerEvent('ps-multijob:requestJobUpdate') end end) -- Check for job updates while menu is open Citizen.CreateThread(function() while true do Citizen.Wait(1000) -- Check every second if we need to update CheckForJobUpdates() end end)