From 2328870daebe82243dfff0bd9046d500d66b47a4 Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Tue, 14 Jul 2015 14:43:52 +0100 Subject: [PATCH] ctf_flash Remove player from team on leaveplayer --- mods/capturetheflag/ctf/teams.lua | 16 +++++++++++++++- mods/capturetheflag/ctf_flash/init.lua | 26 ++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/mods/capturetheflag/ctf/teams.lua b/mods/capturetheflag/ctf/teams.lua index 65e240e..8410005 100644 --- a/mods/capturetheflag/ctf/teams.lua +++ b/mods/capturetheflag/ctf/teams.lua @@ -23,7 +23,10 @@ function ctf.team(name) return ctf.teams[name.name] else if not ctf.teams[name] then - ctf.warning("team", dump(name) .. " does not exist!") + if name then + ctf.warning("team", dump(name) .. " does not exist!") + end + return nil end return ctf.teams[name] end @@ -97,6 +100,17 @@ function ctf.player_or_nil(name) return ctf.players[name] end +function ctf.remove_player(name) + local player = ctf.players[name] + if player then + local team = ctf.team(ctf.players.team) + if team then + team.players[name] = nil + end + ctf.players[name] = nil + end +end + -- Player joins team -- Called by /join, /team join or auto allocate. function ctf.join(name, team, force, by) diff --git a/mods/capturetheflag/ctf_flash/init.lua b/mods/capturetheflag/ctf_flash/init.lua index 485c54f..bcead7a 100644 --- a/mods/capturetheflag/ctf_flash/init.lua +++ b/mods/capturetheflag/ctf_flash/init.lua @@ -16,6 +16,32 @@ for i, flag in pairs(ctf_flag.claimed) do end ctf_flag.collect_claimed() +minetest.register_on_joinplayer(function(player) + if ctf.team(ctf.player(player:get_player_name()).team) then + return + end + + local alloc_mode = tonumber(ctf.setting("allocate_mode")) + if alloc_mode == 0 then + return + end + local name = player:get_player_name() + local team = ctf.autoalloc(name, alloc_mode) + if team then + ctf.log("autoalloc", name .. " was allocated to " .. team) + ctf.join(name, team) + + local spawn = ctf.get_spawn(team) + if spawn then + player:moveto(spawn, false) + end + end +end) + +minetest.register_on_leaveplayer(function(player) + ctf.remove_player(player:get_player_name()) +end) + ctf.register_on_new_game(function() ctf.log("flash", "Setting up new game!")