2015-01-05 22:31:54 -08: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 .
] ]
2015-03-25 19:38:08 -07:00
local MAX_INACTIVE_TIME = 300
2015-01-05 22:31:54 -08:00
local CHECK_INTERVAL = 1
local WARN_TIME = 20
local players = { }
local checkTimer = 0
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 )
2015-01-06 17:50:59 -08:00
minetest.register_on_chat_message ( function ( playerName , message )
players [ playerName ] [ " lastAction " ] = minetest.get_gametime ( )
end )
2015-01-05 22:31:54 -08:00
minetest.register_globalstep ( function ( dtime )
local currGameTime = minetest.get_gametime ( )
--Loop through each player in players
for playerName , _ in pairs ( players ) do
local player = minetest.get_player_by_name ( playerName )
2015-03-25 19:36:43 -07:00
if player then
2015-01-05 22:31:54 -08:00
2015-03-25 19:36:43 -07:00
--Check for inactivity once every CHECK_INTERVAL seconds
checkTimer = checkTimer + dtime
if checkTimer > CHECK_INTERVAL then
checkTimer = 0
--Kick player if he/she has been inactive for longer than MAX_INACTIVE_TIME seconds
if players [ playerName ] [ " lastAction " ] + MAX_INACTIVE_TIME < currGameTime then
minetest.kick_player ( playerName , " Kicked for inactivity " )
end
--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
minetest.chat_send_player ( playerName , " Warning, you have " .. tostring ( players [ playerName ] [ " lastAction " ] + MAX_INACTIVE_TIME - currGameTime ) .. " seconds to move or be kicked " )
end
2015-01-05 22:31:54 -08:00
end
2015-03-25 19:36:43 -07: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
2015-01-05 22:31:54 -08:00
end
end
end
end )