Merge pull request #1 from DonBatman/master
Some changes I personally wouldn't have added the print statements, but that's okay, it only shows in a log anyway.
This commit is contained in:
commit
bda99cb9b2
76
init.lua
76
init.lua
@ -1,16 +1,18 @@
|
||||
kickafk = {}
|
||||
players = {}
|
||||
CPU_saver = 0
|
||||
local CPU_saver = 0
|
||||
|
||||
kickafk.version = "1.0"
|
||||
kickafk.path = minetest.get_modpath(minetest.get_current_modname())
|
||||
kickafk.first_flag = 0
|
||||
|
||||
kickafk.players = tonumber(minetest.setting_get('kickafk_number_of_players')) or 0
|
||||
local modpath = minetest.get_modpath("kickafk")
|
||||
|
||||
kickafk.players = tonumber(minetest.setting_get("kickafk_number_of_players")) or 10
|
||||
if kickafk.players < 0 or kickafk.players > 30 then
|
||||
kickafk.players = 15
|
||||
end
|
||||
kickafk.timer = tonumber(minetest.setting_get('kickafk_length_of_time')) or 0
|
||||
|
||||
kickafk.timer = tonumber(minetest.setting_get('kickafk_length_of_time')) or 2000
|
||||
if kickafk.timer < 0 or kickafk.timer > 1500 then
|
||||
kickafk.timer = 1500
|
||||
end
|
||||
@ -27,34 +29,44 @@ minetest.register_privilege("canafk",
|
||||
"Player can remain afk without being kicked")
|
||||
|
||||
minetest.register_globalstep(function(dtime)
|
||||
-- No reason to constantly be checking for players online.
|
||||
-- This function triggers the main function every ten-ish minutes.
|
||||
CPU_saver = CPU_saver + 1
|
||||
if CPU_saver >= kickafk.timer then
|
||||
CPU_saver = 0
|
||||
players_online = 0
|
||||
-- Loop through all connected players
|
||||
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
|
||||
players_online = players_online + 1
|
||||
if players_online >= kickafk.players then
|
||||
-- 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 minetest.check_player_privs(player_name, {canafk=false}) then
|
||||
minetest.kick_player(player_name, "Network Timeout")
|
||||
end
|
||||
end
|
||||
-- Record the players location
|
||||
if players[player_name]["last_pos"] ~= pos_hash then
|
||||
players[player_name]["last_pos"] = pos_hash
|
||||
CPU_saver = CPU_saver + dtime
|
||||
|
||||
if CPU_saver < kickafk.timer then
|
||||
return
|
||||
end
|
||||
|
||||
CPU_saver = 0
|
||||
|
||||
local players_online = 0
|
||||
|
||||
-- Loop through all connected players
|
||||
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
|
||||
minetest.kick_player(player_name, "Disconnected due to inactivity!")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
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
|
||||
end)
|
||||
|
@ -3,5 +3,5 @@
|
||||
kickafk_number_of_players (Number of players) int 10
|
||||
|
||||
# How often to check for AFK players.
|
||||
# This is in server ticks, not seconds.
|
||||
# This is in seconds.
|
||||
kickafk_length_of_time (How ofen to run) int 2000
|
||||
|
Loading…
x
Reference in New Issue
Block a user