From 1f621760bf09cff51bb42343bd35d52f1f6d76e2 Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Tue, 7 Nov 2017 01:07:49 +0000 Subject: [PATCH] Add immunity end on enemy attack, and fix potential crash --- mods/respawn_immunity/depends.txt | 1 + mods/respawn_immunity/init.lua | 44 ++++++++++++++++++------------- 2 files changed, 27 insertions(+), 18 deletions(-) create mode 100644 mods/respawn_immunity/depends.txt diff --git a/mods/respawn_immunity/depends.txt b/mods/respawn_immunity/depends.txt new file mode 100644 index 0000000..0dcc91d --- /dev/null +++ b/mods/respawn_immunity/depends.txt @@ -0,0 +1 @@ +ctf diff --git a/mods/respawn_immunity/init.lua b/mods/respawn_immunity/init.lua index 99f32c7..d85d2bb 100644 --- a/mods/respawn_immunity/init.lua +++ b/mods/respawn_immunity/init.lua @@ -8,6 +8,22 @@ function respawn_immunity.is_immune(player) return immune_players[player:get_player_name()] end +function respawn_immunity.set_immune(player) + immune_serial = immune_serial + 1 + immune_players[player:get_player_name()] = immune_serial + minetest.after(1, function() + respawn_immunity.update_effects(player) + end) + + -- Set time out + minetest.after(IMMUNE_TIME, function(name, id) + if immune_players[name] == id then + immune_players[name] = nil + respawn_immunity.update_effects(minetest.get_player_by_name(name)) + end + end, player:get_player_name(), immune_serial) +end + function respawn_immunity.update_effects(player) -- TODO: transparent player when immune -- @@ -18,29 +34,21 @@ function respawn_immunity.update_effects(player) -- end end -function respawn_immunity.set_immune(player) - immune_serial = immune_serial + 1 - immune_players[player:get_player_name()] = immune_serial - minetest.after(1, function() - respawn_immunity.update_effects(player) - end) - - -- Set time out - minetest.after(IMMUNE_TIME, function(id) - if immune_players[player:get_player_name()] == id then - immune_players[player:get_player_name()] = nil - respawn_immunity.update_effects(player) - end - end, immune_serial) -end - -table.insert(minetest.registered_on_punchplayers, 1, function(player, hitter, +minetest.register_on_punchplayer(function(player, hitter, time_from_last_punch, tool_capabilities, dir, damage) if player and respawn_immunity.is_immune(player) then minetest.chat_send_player(hitter:get_player_name(), - player:get_player_name() .. " just respawned or joined, and is immune to attacks!") + player:get_player_name() .. + " just respawned or joined, and is immune to attacks!") return true end + + if hitter and respawn_immunity.is_immune(hitter) then + minetest.chat_send_player(hitter:get_player_name(), + "Your immunity has ended because you attacked a player") + immune_players[hitter:get_player_name()] = nil + respawn_immunity.update_effects(hitter) + end end) minetest.register_on_joinplayer(respawn_immunity.set_immune)