2016-04-20 17:02:21 -05:00
|
|
|
kickafk = {}
|
|
|
|
players = {}
|
2016-04-21 19:42:12 -07:00
|
|
|
local CPU_saver = 0
|
2016-04-20 17:02:21 -05:00
|
|
|
|
|
|
|
kickafk.version = "1.0"
|
|
|
|
kickafk.first_flag = 0
|
|
|
|
|
2016-04-21 19:42:12 -07:00
|
|
|
local modpath = minetest.get_modpath("kickafk")
|
|
|
|
|
|
|
|
kickafk.players = tonumber(minetest.setting_get("kickafk_number_of_players")) or 10
|
2016-04-20 17:02:21 -05:00
|
|
|
if kickafk.players < 0 or kickafk.players > 30 then
|
|
|
|
kickafk.players = 15
|
|
|
|
end
|
2016-04-21 19:42:12 -07:00
|
|
|
|
|
|
|
kickafk.timer = tonumber(minetest.setting_get('kickafk_length_of_time')) or 2000
|
2016-04-20 17:02:21 -05:00
|
|
|
if kickafk.timer < 0 or kickafk.timer > 1500 then
|
|
|
|
kickafk.timer = 1500
|
|
|
|
end
|
|
|
|
|
|
|
|
minetest.register_on_joinplayer(function(player)
|
|
|
|
players[player:get_player_name()] = {last_pos = 0,0,}
|
|
|
|
end)
|
|
|
|
|
|
|
|
minetest.register_on_leaveplayer(function(player)
|
|
|
|
players[player:get_player_name()] = nil
|
|
|
|
end)
|
|
|
|
|
|
|
|
minetest.register_privilege("canafk",
|
|
|
|
"Player can remain afk without being kicked")
|
|
|
|
|
|
|
|
minetest.register_globalstep(function(dtime)
|
2016-04-21 19:42:12 -07:00
|
|
|
|
|
|
|
CPU_saver = CPU_saver + dtime
|
|
|
|
|
|
|
|
if CPU_saver < kickafk.timer then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
CPU_saver = 0
|
|
|
|
|
|
|
|
local players_online = 0
|
|
|
|
|
2016-04-20 17:02:21 -05:00
|
|
|
-- Loop through all connected players
|
2016-04-21 19:42:12 -07:00
|
|
|
for _,player in ipairs(minetest.get_connected_players()) do
|
|
|
|
local player_name = player:get_player_name()
|
|
|
|
|
|
|
|
-- Only continue if the player has an entry in the players table
|
|
|
|
if players[player_name] then
|
|
|
|
print(players[player_name].last_pos)
|
|
|
|
players_online = players_online + 1
|
|
|
|
|
|
|
|
if players_online >= kickafk.players then
|
|
|
|
print("number of players is "..players_online)
|
|
|
|
|
|
|
|
-- Kick the player if their location hasn't changed.
|
|
|
|
local pos = player:getpos()
|
|
|
|
local pos_hash = math.floor(pos.x) .. ',' .. math.floor(pos.z)
|
|
|
|
|
|
|
|
if players[player_name].last_pos == pos_hash then
|
|
|
|
if not minetest.check_player_privs(player_name,"canafk") == true then
|
2016-04-21 20:42:23 -07:00
|
|
|
minetest.kick_player(player_name, "Disconnected due to inactivity!")
|
2016-04-20 17:02:21 -05:00
|
|
|
end
|
2016-04-21 19:42:12 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Record the players location
|
|
|
|
if players[player_name].last_pos ~= pos_hash then
|
|
|
|
players[player_name].last_pos = pos_hash
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-04-20 17:02:21 -05:00
|
|
|
end)
|