From 7acf79df2491b4f7caa0db66d7c5127a1d5db5ef Mon Sep 17 00:00:00 2001 From: ClobberXD Date: Mon, 31 Dec 2018 04:37:22 +0530 Subject: [PATCH] Improve vote_kick feature --- mods/vote/init.lua | 81 +------------------------- mods/vote/vote_kick.lua | 125 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 127 insertions(+), 79 deletions(-) create mode 100644 mods/vote/vote_kick.lua diff --git a/mods/vote/init.lua b/mods/vote/init.lua index 70f1b40..2106b4e 100644 --- a/mods/vote/init.lua +++ b/mods/vote/init.lua @@ -1,11 +1,8 @@ vote = { active = {}, queue = {}, - kick_cooldown = 600 } -local vlist = {} -- table storing player name, ip & lock status - function vote.new_vote(creator, voteset) local max_votes = tonumber(minetest.setting_get("vote.maximum_active")) or 1 local max_queue = tonumber(minetest.setting_get("vote.maximum_active")) or 0 @@ -217,11 +214,7 @@ function vote.update_hud(player) end end minetest.register_on_leaveplayer(function(player) - local name = player:get_player_name() - vote.hud.players[name] = nil - if not vlist[name].locked then - vlist[name] = nil - end + vote.hud.players[player:get_player_name()] = nil end) function vote.update_all_hud() @@ -317,75 +310,5 @@ minetest.register_chatcommand("abstain", { local set = minetest.setting_get("vote.kick_vote") if set == nil or minetest.is_yes(set) then - minetest.register_privilege("vote_kick", { - description = "Can (start) vote to kick a player", - }) - - minetest.register_chatcommand("vote_kick", { - privs = { - interact = true, - vote_kick = true, - }, - func = function(name, param) - if not minetest.get_player_by_name(param) then - minetest.chat_send_player(name, "There is no player called '" .. - param .. "'") - return - end - - return vote.new_vote(name, { - description = "Kick " .. param, - help = "/yes, /no or /abstain", - name = param, - duration = 60, - perc_needed = 0.8, - - on_result = function(self, result, results) - if result == "yes" then - minetest.chat_send_all("Vote passed, " .. - #results.yes .. " to " .. #results.no .. ", " .. - self.name .. " will be kicked.") - minetest.kick_player(self.name, - ("The vote to kick you passed.\n You have been temporarily banned" .. - " for %s minutes."):format(tostring(vote.kick_cooldown / 60))) - vlist[self.name].locked = true - minetest.after(vote.kick_cooldown, function() - vlist[self.name] = nil - end) - else - minetest.chat_send_all("Vote failed, " .. - #results.yes .. " to " .. #results.no .. ", " .. - self.name .. " remains ingame.") - end - end, - - on_vote = function(self, voter, value) - minetest.chat_send_all(voter .. " voted " .. value .. " to '" .. - self.description .. "'") - end - }) - end - }) + dofile(minetest.get_modpath("vote") .. "/vote_kick.lua") end - -minetest.register_on_joinplayer(function(player) - local name = player:get_player_name() - if not vlist[name] then - vlist[name] = { - ip = minetest.get_player_ip(name), - locked = false - } - end -end) -minetest.register_on_prejoinplayer(function(name, ip) - if vlist[name] and vlist[name].locked then - return "Please wait until the vote cool down period has elapsed before rejoining!" - else - for k, v in pairs(vlist) do - if v.ip == ip and v.locked then - return "This IP has been temporarily blocked.".. - " Please wait until the cool-down period has elapsed before rejoining." - end - end - end -end) diff --git a/mods/vote/vote_kick.lua b/mods/vote/vote_kick.lua new file mode 100644 index 0000000..5c4c0b5 --- /dev/null +++ b/mods/vote/vote_kick.lua @@ -0,0 +1,125 @@ +vote.kick_cooldown = 600 +local vlist = {} -- table storing player name, ip & lock status + +minetest.register_privilege("vote_kick", { + description = "Can (start) vote to kick a player", + on_grant = function(name, granter) + minetest.log("warning", "Player '" .. name .. "' has been" .. + " granted 'vote_kick' by '" .. granter .. "'") + end, + on_revoke = function(name, revoker) + minetest.log("warning", "Player '" .. name .. "' has been" .. + " revoked of 'vote_kick' by '" .. revoker .. "'") + end +}) + +minetest.register_chatcommand("vote_kick", { + params = "", + description = "Start a vote to kick a player", + privs = { + interact = true, + vote_kick = true, + }, + func = function(name, param) + param = param:trim() + if param == "" then + return false, "Please specify a player name to be vote-kicked!" + end + + if not minetest.get_player_by_name(param) then + return false, "There is no player called '" .. + param .. "'" + end + + if minetest.check_player_privs(param, {kick = true, ban = true}) then + return false, param .. " is a moderator, and can't be kicked!" + end + + minetest.log("warning", "Player '" .. name .. "' started a vote" .. + " to kick '" .. param .. "'") + return vote.new_vote(name, { + description = "Kick " .. param, + help = "/yes, /no or /abstain", + name = param, + duration = 60, + perc_needed = 0.8, + + on_result = function(self, result, results) + if result == "yes" then + minetest.chat_send_all("Vote passed, " .. + #results.yes .. " to " .. #results.no .. ", " .. + self.name .. " will be kicked.") + minetest.kick_player(self.name, + ("The vote to kick you passed.\n You have been temporarily banned" .. + " for %s minutes."):format(tostring(vote.kick_cooldown / 60))) + vlist[self.name].locked = true + minetest.after(vote.kick_cooldown, function() + vlist[self.name] = nil + end) + else + minetest.chat_send_all("Vote failed, " .. + #results.yes .. " to " .. #results.no .. ", " .. + self.name .. " remains ingame.") + end + end, + + on_vote = function(self, voter, value) + minetest.chat_send_all(voter .. " voted " .. value .. " to '" .. + self.description .. "'") + end + }) + end +}) + +minetest.register_chatcommand("unblock", { + params = "", + description = "Unblock a vote-kicked player before the cooldown expires", + privs = {kick = true, ban = true}, + func = function(name, param) + param = param:trim() + if param == "" then + return false, "Please specify a player name to be unblocked!" + end + + if not minetest.get_player_by_name(param) then + return false, "Can't find player '" .. param .. "'" + end + + if not vlist[name].locked then + return false, "Failed! " .. param .. " is not blocked" + end + + vlist[name].locked = false + return true, param .. " has been successfully unblocked!" + end +}) + +minetest.register_on_joinplayer(function(player) + local name = player:get_player_name() + if not vlist[name] then + vlist[name] = { + ip = minetest.get_player_ip(name), + locked = false + } + end +end) + +minetest.register_on_leaveplayer(function(player) + local name = player:get_player_name() + if not vlist[name].locked then + vlist[name] = nil + end +end) + +minetest.register_on_prejoinplayer(function(name, ip) + if vlist[name] and vlist[name].locked then + return "Please wait until the vote cool down period has elapsed before rejoining!" + else + for k, v in pairs(vlist) do + if v.ip == ip and v.locked then + return "This IP has been temporarily blocked.".. + " Please wait until the cool-down period has elapsed before rejoining." + end + end + end +end)