From 08b61ae3203b89c22046d336f25eae9e6d0ab67c Mon Sep 17 00:00:00 2001 From: Charlie Date: Mon, 16 Jan 2023 04:44:01 +0000 Subject: [PATCH 1/4] Refactor AFK function - Reinstates locale message as it was missing - Includes AFK for logged out users (ie. qb-multicharacter) - Adds config option for enable/disable Co-authored-by: Charlie Co-authored-by: Duo <122633389+DuoTheDev@users.noreply.github.com> --- client/afk.lua | 81 ++++++++++++++++++++++++++------------------------ config.lua | 3 +- locales/en.lua | 3 +- locales/pt.lua | 7 ++++- server/afk.lua | 2 +- 5 files changed, 53 insertions(+), 43 deletions(-) diff --git a/client/afk.lua b/client/afk.lua index c1562a63..a3d2030c 100644 --- a/client/afk.lua +++ b/client/afk.lua @@ -1,17 +1,9 @@ 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 prevPos = nil +local checkInterval = 10 +local notificationDuration = (checkInterval - 2) * 1000 local function updatePermissionLevel() QBCore.Functions.TriggerCallback('qb-afkkick:server:GetPermissions', function(userGroups) @@ -38,36 +30,47 @@ RegisterNetEvent('QBCore:Client:OnPermissionUpdate', function() updatePermissionLevel() end) -CreateThread(function() - while true do - Wait(10000) - local playerPed = PlayerPedId() - if isLoggedIn then - if checkUser then +function getTimeUnit(time) + return time >= 60 and 'minutes' or 'seconds' +end +if Config.AFK.enabled then + CreateThread(function() + while true do + Wait(checkInterval * 1000) + local playerPed = PlayerPedId() + if not time then + time = Config.AFK.secondsUntilKick + end + + if not checkUser then + goto skip + end + + if isLoggedIn then local currentPos = GetEntityCoords(playerPed, true) - if prevPos then - if currentPos == 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 - else - time = Config.AFK.secondsUntilKick - end - else - time = Config.AFK.secondsUntilKick - end + if currentPos ~= prevPos then + time = Config.AFK.secondsUntilKick end prevPos = currentPos end + + if time > 0 then + if time < Config.AFK.secondsUntilKick / 2 then + local timeString = math.ceil(time / 60) .. Lang:t('afk.time_minutes') + + if getTimeUnit(time) == 'seconds' then + timeString = time .. Lang:t('afk.time_seconds') + end + + QBCore.Functions.Notify(Lang:t('afk.will_kick') .. timeString, 'error', notificationDuration) + end + + time -= checkInterval + else + TriggerServerEvent('KickForAFK') + end + + ::skip:: end - end -end) + end) +end \ No newline at end of file diff --git a/config.lua b/config.lua index ed87c544..5bf220c7 100644 --- a/config.lua +++ b/config.lua @@ -4,12 +4,13 @@ Config.UseTarget = GetConvar('UseTarget', 'false') == 'true' -- Use qb-target in -- To make this simple. Everything you need is in the config, except for discord, weapon drops, vehicle classes for cruise, hands up disabled keys, and recoil Config.AFK = { + enabled = true, -- Enable or disable AFK Kick ignoredGroups = { ['mod'] = true, ['admin'] = true, ['god'] = true }, - secondsUntilKick = 1800 -- AFK Kick Time Limit (in seconds) + secondsUntilKick = 1800, -- AFK Kick Time Limit (in seconds) } Config.Binoculars = { diff --git a/locales/en.lua b/locales/en.lua index 6e02e60c..64f43119 100644 --- a/locales/en.lua +++ b/locales/en.lua @@ -2,7 +2,8 @@ local Translations = { afk = { will_kick = 'You are AFK and will be kicked in ', time_seconds = ' seconds!', - time_minutes = ' minute(s)!' + time_minutes = ' minute(s)!', + kick_message = 'You were kicked for being AFK', }, wash = { in_progress = "Vehicle is being washed ..", diff --git a/locales/pt.lua b/locales/pt.lua index 76bd8207..4d243ebc 100644 --- a/locales/pt.lua +++ b/locales/pt.lua @@ -1,4 +1,10 @@ local Translations = { + afk = { + ["will_kick"] = "You will be kicked for being AFK in ", + ["time_minutes"] = " minutes", + ["time_seconds"] = " seconds", + ["kick_message"] = "Foste Kickado Por Estares AFK", + }, error = { ["afk_time_left_minutes"] = "Estás AFK e vais ser kickado em %{timeLeft} minutos!", ["afk_time_left_minute"] = "Estás AFK e vais ser kickado em %{timeLeft} minuto!", @@ -22,7 +28,6 @@ local Translations = { ["remove_race_harness"] = "A remover Arnês de Corrida", }, text = { - ["afk_kick_message"] = "Foste Kickado Por Estares AFK", ["car_wash_text"] = "~g~E~w~ - Lavar Veículo (%{price}€)", ["car_wash_not_available"] = "A estação de lavagem não está disponível...", ["time_until_firework"] = "Fogo de Artifício em ~r~%{time}", diff --git a/server/afk.lua b/server/afk.lua index 0443e4ee..2c0ae135 100644 --- a/server/afk.lua +++ b/server/afk.lua @@ -1,7 +1,7 @@ local QBCore = exports['qb-core']:GetCoreObject() RegisterNetEvent('KickForAFK', function() - DropPlayer(source, Lang:t("text.afk_kick_message")) + DropPlayer(source, Lang:t("afk.kick_message")) end) QBCore.Functions.CreateCallback('qb-afkkick:server:GetPermissions', function(source, cb) From 9d170a1961e87a81d3aee9d517b9e9fac1df6de1 Mon Sep 17 00:00:00 2001 From: Charlie Date: Mon, 16 Jan 2023 14:58:36 +0000 Subject: [PATCH 2/4] Fix linting issues --- client/afk.lua | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/client/afk.lua b/client/afk.lua index a3d2030c..293aaacb 100644 --- a/client/afk.lua +++ b/client/afk.lua @@ -1,7 +1,6 @@ local QBCore = exports['qb-core']:GetCoreObject() local isLoggedIn = LocalPlayer.state.isLoggedIn local checkUser = true -local prevPos = nil local checkInterval = 10 local notificationDuration = (checkInterval - 2) * 1000 @@ -35,6 +34,9 @@ function getTimeUnit(time) end if Config.AFK.enabled then CreateThread(function() + local time = nil + local prevPos = nil + while true do Wait(checkInterval * 1000) local playerPed = PlayerPedId() @@ -73,4 +75,4 @@ if Config.AFK.enabled then ::skip:: end end) -end \ No newline at end of file +end From b1fb92a901399a1c2bcbfbd9b13a42987e515bbe Mon Sep 17 00:00:00 2001 From: Seige Date: Thu, 18 May 2023 02:44:14 +0100 Subject: [PATCH 3/4] Update pt.lua --- locales/pt.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/locales/pt.lua b/locales/pt.lua index 9e30d4a6..0318d1ae 100644 --- a/locales/pt.lua +++ b/locales/pt.lua @@ -1,8 +1,8 @@ local Translations = { afk = { ["will_kick"] = "Estás AFK e vais ser kickado em ", - ["time_minutes"] = " minutes", - ["time_seconds"] = " seconds", + ["time_minutes"] = " minutos", + ["time_seconds"] = " segundos", ["kick_message"] = "Foste Kickado Por Estares AFK", }, error = { From 2773e5f0202f3b24189aeee03acae72fc248e127 Mon Sep 17 00:00:00 2001 From: Seige Date: Thu, 18 May 2023 02:44:49 +0100 Subject: [PATCH 4/4] Update afk.lua --- client/afk.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/client/afk.lua b/client/afk.lua index 293aaacb..972e6a7d 100644 --- a/client/afk.lua +++ b/client/afk.lua @@ -32,6 +32,7 @@ end) function getTimeUnit(time) return time >= 60 and 'minutes' or 'seconds' end + if Config.AFK.enabled then CreateThread(function() local time = nil