Add player invulnerability & fix not continuously damaging players when holding the attack key

Player invulnerability is the same as Minecraft's Damage Immunity https://minecraft.wiki/w/Damage#Immunity

The old code for some reason only allows a few damage by holding and does not continuously damage other players after a few hits
This commit is contained in:
Eliy21 2023-12-13 17:34:42 +00:00
parent ebd733be82
commit a8806fe04e

@ -663,6 +663,8 @@ minetest.register_on_joinplayer(function(player)
lastPos = nil, lastPos = nil,
swimDistance = 0, swimDistance = 0,
jump_cooldown = -1, -- Cooldown timer for jumping, we need this to prevent the jump exhaustion to increase rapidly jump_cooldown = -1, -- Cooldown timer for jumping, we need this to prevent the jump exhaustion to increase rapidly
last_damage = 0,
invul_timestamp = 0,
} }
mcl_playerplus.elytra[player] = {active = false, rocketing = 0, speed = 0} mcl_playerplus.elytra[player] = {active = false, rocketing = 0, speed = 0}
@ -727,19 +729,35 @@ mcl_damage.register_modifier(function(obj, damage, reason)
end end
end, -200) end, -200)
-- damage invulnerability minetest.register_on_punchplayer(function(player, hitter, time_from_last_punch, tool_capabilities, dir, damage)
mcl_damage.register_modifier(function(obj, damage, reason) -- damage invulnerability
local invul = obj:get_meta():get_int("mcl_damage:invulnerable") if hitter then
if invul > 0 then local name = player:get_player_name()
return 0 local time_now = minetest.get_us_time()
else local invul_timestamp = mcl_playerplus_internal[name].invul_timestamp
obj:get_meta():set_int("mcl_damage:invulnerable", 1) local time_diff = time_now - invul_timestamp
minetest.after(0.5, function() -- check for invulnerability time in microseconds (0.5 second)
obj:get_meta():set_int("mcl_damage:invulnerable", 0) if time_diff <= 500000 and time_diff >= 0 then
end) damage = damage - mcl_playerplus_internal[name].last_damage
return damage if damage < 0 then
damage = 0
end
return damage
else
mcl_playerplus_internal[name].last_damage = damage
mcl_playerplus_internal[name].invul_timestamp = time_now
end
end end
end, -1000) -- attack reach limit
if hitter and hitter:is_player() then
local player_pos = player:get_pos()
local hitter_pos = hitter:get_pos()
if vector.distance(player_pos, hitter_pos) > 3 then
damage = 0
return damage
end
end
end)
minetest.register_on_respawnplayer(function(player) minetest.register_on_respawnplayer(function(player)
local pos = player:get_pos() local pos = player:get_pos()