From 1b652f04b2874fac96b45fc376a4aed1f78991ca Mon Sep 17 00:00:00 2001 From: LoneWolfHT Date: Sat, 28 Nov 2020 10:13:52 -0800 Subject: [PATCH] Use the now fixed on_hpchange() for melee bonuses --- mods/ctf/ctf_classes/melee.lua | 48 +++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/mods/ctf/ctf_classes/melee.lua b/mods/ctf/ctf_classes/melee.lua index 2db901f..9b6d3e0 100644 --- a/mods/ctf/ctf_classes/melee.lua +++ b/mods/ctf/ctf_classes/melee.lua @@ -1,22 +1,28 @@ -minetest.register_on_punchplayer(function(player, hitter, time_from_last_punch, tool_capabilities, dir, damage) - if tool_capabilities.damage_groups.nopunch then return end - if ctf_respawn_immunity.is_immune(player) then return true end - - local class = ctf_classes.get(hitter) - - if class.properties.melee_bonus and hitter:get_wielded_item():get_name():find("sword") then - local php = player:get_hp() - - if time_from_last_punch > 1 then - time_from_last_punch = 1 - elseif time_from_last_punch < 0.5 then - time_from_last_punch = 0.5 - end - - if php - damage > 0 then - minetest.after(0, function() - player:punch(hitter, 1, {damage_groups = {fleshy = time_from_last_punch*2, nopunch = 1}}, dir) - end) - end +minetest.register_on_player_hpchange(function(player, hp_change, reason) + if reason.type ~= "punch" or not reason.object or not reason.object:is_player() then + return hp_change end -end) + + local class = ctf_classes.get(reason.object) + + if class.properties.melee_bonus and reason.object:get_wielded_item():get_name():find("sword") then + local change = hp_change - class.properties.melee_bonus + + if player:get_hp() + change <= 0 and player:get_hp() + hp_change > 0 then + local wielded_item = reason.object:get_wielded_item() + + for i = 1, #ctf.registered_on_killedplayer do + ctf.registered_on_killedplayer[i]( + player:get_player_name(), + reason.object:get_player_name(), + wielded_item, + wielded_item:get_tool_capabilities() + ) + end + end + + return change + end + + return hp_change +end, true)