2017-10-06 18:27:18 +00:00
|
|
|
--[[
|
|
|
|
Afk Kick mod for Minetest by GunshipPenguin
|
|
|
|
|
|
|
|
To the extent possible under law, the author(s)
|
|
|
|
have dedicated all copyright and related and neighboring rights
|
|
|
|
to this software to the public domain worldwide. This software is
|
|
|
|
distributed without any warranty.
|
|
|
|
]]
|
|
|
|
|
2017-11-06 23:50:31 +00:00
|
|
|
local MAX_INACTIVE_TIME = 120
|
2017-10-06 18:27:18 +00:00
|
|
|
local CHECK_INTERVAL = 1
|
|
|
|
local WARN_TIME = 20
|
|
|
|
|
|
|
|
local players = {}
|
|
|
|
local checkTimer = 0
|
|
|
|
|
2017-10-14 21:34:06 +00:00
|
|
|
minetest.register_privilege("canafk")
|
|
|
|
|
2017-10-06 18:27:18 +00:00
|
|
|
minetest.register_on_joinplayer(function(player)
|
|
|
|
local playerName = player:get_player_name()
|
|
|
|
players[playerName] = {
|
|
|
|
lastAction = minetest.get_gametime()
|
|
|
|
}
|
|
|
|
end)
|
|
|
|
|
|
|
|
minetest.register_on_leaveplayer(function(player)
|
|
|
|
local playerName = player:get_player_name()
|
|
|
|
players[playerName] = nil
|
|
|
|
end)
|
|
|
|
|
|
|
|
minetest.register_on_chat_message(function(playerName, message)
|
2020-05-08 06:21:22 +00:00
|
|
|
-- Verify that there is a player, and that the player is online
|
|
|
|
if not playerName or not minetest.get_player_by_name(playerName) then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2017-10-06 18:27:18 +00:00
|
|
|
players[playerName]["lastAction"] = minetest.get_gametime()
|
|
|
|
end)
|
|
|
|
|
|
|
|
minetest.register_globalstep(function(dtime)
|
|
|
|
local currGameTime = minetest.get_gametime()
|
2017-10-14 21:34:06 +00:00
|
|
|
|
2018-04-22 10:50:41 +00:00
|
|
|
--Check for inactivity once every CHECK_INTERVAL seconds
|
|
|
|
checkTimer = checkTimer + dtime
|
|
|
|
|
|
|
|
local checkNow = checkTimer >= CHECK_INTERVAL
|
|
|
|
if checkNow then
|
|
|
|
checkTimer = checkTimer - CHECK_INTERVAL
|
|
|
|
end
|
|
|
|
|
2017-10-06 18:27:18 +00:00
|
|
|
--Loop through each player in players
|
|
|
|
for playerName,_ in pairs(players) do
|
|
|
|
local player = minetest.get_player_by_name(playerName)
|
|
|
|
if player then
|
2018-04-22 10:50:41 +00:00
|
|
|
--Check if this player is doing an action
|
|
|
|
for _,keyPressed in pairs(player:get_player_control()) do
|
|
|
|
if keyPressed then
|
|
|
|
players[playerName]["lastAction"] = currGameTime
|
|
|
|
end
|
|
|
|
end
|
2017-10-14 21:34:06 +00:00
|
|
|
|
2018-04-22 10:50:41 +00:00
|
|
|
if checkNow and not minetest.check_player_privs(player, { canafk = true }) then
|
2017-10-06 18:27:18 +00:00
|
|
|
--Kick player if he/she has been inactive for longer than MAX_INACTIVE_TIME seconds
|
2017-10-14 21:34:06 +00:00
|
|
|
if players[playerName]["lastAction"] + MAX_INACTIVE_TIME < currGameTime then
|
2017-10-06 18:27:18 +00:00
|
|
|
minetest.kick_player(playerName, "Kicked for inactivity")
|
|
|
|
end
|
2017-10-14 21:34:06 +00:00
|
|
|
|
2017-10-06 18:27:18 +00:00
|
|
|
--Warn player if he/she has less than WARN_TIME seconds to move or be kicked
|
|
|
|
if players[playerName]["lastAction"] + MAX_INACTIVE_TIME - WARN_TIME < currGameTime then
|
2018-04-22 10:50:41 +00:00
|
|
|
minetest.chat_send_player(playerName, minetest.colorize("#FF8C00", "Warning, you have " ..
|
|
|
|
tostring(players[playerName]["lastAction"] + MAX_INACTIVE_TIME - currGameTime + 1) ..
|
|
|
|
" seconds to move or be kicked"))
|
2017-10-06 18:27:18 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|