diff --git a/client/afk.lua b/client/afk.lua index 3d7ee19f..7a42428d 100644 --- a/client/afk.lua +++ b/client/afk.lua @@ -1,17 +1,6 @@ local QBCore = exports['qb-core']:GetCoreObject() local isLoggedIn = LocalPlayer.state.isLoggedIn local checkUser = true -local prevPos, time = nil, nil -local timeMinutes = { - ['900'] = 'minutes', - ['600'] = 'minutes', - ['300'] = 'minutes', - ['150'] = 'minutes', - ['60'] = 'minutes', - ['30'] = 'seconds', - ['20'] = 'seconds', - ['10'] = 'seconds', -} local function updatePermissionLevel() QBCore.Functions.TriggerCallback('qb-afkkick:server:GetPermissions', function(userGroups) @@ -34,40 +23,30 @@ RegisterNetEvent('QBCore:Client:OnPlayerUnload', function() isLoggedIn = false end) -RegisterNetEvent('QBCore:Client:OnPermissionUpdate', function() - updatePermissionLevel() -end) - CreateThread(function() + local sleepTimer = 10000 while true do - Wait(10000) - local ped = PlayerPedId() - if isLoggedIn == true or Config.AFK.kickInCharMenu == true then - if checkUser then - local currPos = GetEntityCoords(ped, true) - if prevPos then - if currPos == prevPos then - if time then - if time > 0 then - local _type = timeMinutes[tostring(time)] - if _type == 'minutes' then - QBCore.Functions.Notify(Lang:t('afk.will_kick') .. math.ceil(time / 60) .. Lang:t('afk.time_minutes'), 'error', 10000) - elseif _type == 'seconds' then - QBCore.Functions.Notify(Lang:t('afk.will_kick') .. time .. Lang:t('afk.time_seconds'), 'error', 10000) - end - time -= 10 - else - TriggerServerEvent('KickForAFK') - end + if (isLoggedIn or Config.AFK.kickInCharMenu) and checkUser then + while GetTimeSinceLastInput(0) > sleepTimer do + local kickTimer = Config.AFK.minutesUntilKick * 60 * 1000 + local timeLeftSec = (kickTimer - GetTimeSinceLastInput(0)) / 1000 + for _, warning in pairs(Config.AFK.warnings) do + if not warning.sent and timeLeftSec < warning.timeSec then + if warning.timeSec >= 60 then + QBCore.Functions.Notify(Lang:t('afk.will_kick') .. math.ceil(warning.timeSec / 60) .. Lang:t('afk.time_minutes'), 'error', 15000) else - time = Config.AFK.secondsUntilKick + QBCore.Functions.Notify(Lang:t('afk.will_kick') .. warning.timeSec .. Lang:t('afk.time_seconds'), 'error', 15000) end - else - time = Config.AFK.secondsUntilKick + warning.sent = true + end + if GetTimeSinceLastInput(0) > kickTimer then + TriggerServerEvent('KickForAFK') end end - prevPos = currPos + Wait(1000) end + for _, warning in pairs(Config.AFK.warnings) do warning.sent = false end end + Wait(sleepTimer) end end) \ No newline at end of file diff --git a/config.lua b/config.lua index cf1076c3..0c5c0c22 100644 --- a/config.lua +++ b/config.lua @@ -11,8 +11,14 @@ Config.AFK = { ['admin'] = true, ['god'] = true }, - secondsUntilKick = 1000000, -- AFK Kick Time Limit (in seconds) - kickInCharMenu = false -- Set to true if you want to kick players for being AFK even when they are in the character menu. + warnings = { + {timeSec = 300, sent = false}, -- Will send a warning at 300 seconds (5 minutes) remaining on afk timer + {timeSec = 120, sent = false}, + {timeSec = 60, sent = false}, + {timeSec = 30, sent = false}, + }, + minutesUntilKick = 120, -- AFK Kick Time Limit (in minutes) + kickInCharMenu = false -- Set to true if you want to kick players for being AFK even when they are in the character menu. } Config.HandsUp = {