diff options
author | savilli <78875209+savilli@users.noreply.github.com> | 2022-01-15 17:44:55 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-15 17:44:55 +0100 |
commit | 72b14bd994659163d4c2ea0d769d329df8a0f937 (patch) | |
tree | c293dd27aace5f2720fd301463e76a3dc4f2ffe2 /src/server/player_sao.cpp | |
parent | 76e97e85a08ce71fea7654c729787c6c4409e0d8 (diff) | |
download | minetest-72b14bd994659163d4c2ea0d769d329df8a0f937.tar.gz minetest-72b14bd994659163d4c2ea0d769d329df8a0f937.tar.bz2 minetest-72b14bd994659163d4c2ea0d769d329df8a0f937.zip |
Don't call on_dieplayer callback two times (#11874)
Diffstat (limited to 'src/server/player_sao.cpp')
-rw-r--r-- | src/server/player_sao.cpp | 37 |
1 files changed, 17 insertions, 20 deletions
diff --git a/src/server/player_sao.cpp b/src/server/player_sao.cpp index 83e17f830..d076d5783 100644 --- a/src/server/player_sao.cpp +++ b/src/server/player_sao.cpp @@ -463,36 +463,33 @@ void PlayerSAO::rightClick(ServerActiveObject *clicker) m_env->getScriptIface()->on_rightclickplayer(this, clicker); } -void PlayerSAO::setHP(s32 hp, const PlayerHPChangeReason &reason, bool send) +void PlayerSAO::setHP(s32 target_hp, const PlayerHPChangeReason &reason, bool from_client) { - if (hp == (s32)m_hp) - return; // Nothing to do + target_hp = rangelim(target_hp, 0, U16_MAX); - if (m_hp <= 0 && hp < (s32)m_hp) - return; // Cannot take more damage + if (target_hp == m_hp) + return; // Nothing to do - { - s32 hp_change = m_env->getScriptIface()->on_player_hpchange(this, hp - m_hp, reason); - if (hp_change == 0) - return; + s32 hp_change = m_env->getScriptIface()->on_player_hpchange(this, target_hp - (s32)m_hp, reason); - hp = m_hp + hp_change; - } + s32 hp = (s32)m_hp + std::min(hp_change, U16_MAX); // Protection against s32 overflow + hp = rangelim(hp, 0, U16_MAX); - s32 oldhp = m_hp; - hp = rangelim(hp, 0, m_prop.hp_max); + if (hp > m_prop.hp_max) + hp = m_prop.hp_max; - if (hp < oldhp && isImmortal()) - return; // Do not allow immortal players to be damaged - - m_hp = hp; + if (hp < m_hp && isImmortal()) + hp = m_hp; // Do not allow immortal players to be damaged // Update properties on death - if ((hp == 0) != (oldhp == 0)) + if ((hp == 0) != (m_hp == 0)) m_properties_sent = false; - if (send) - m_env->getGameDef()->SendPlayerHPOrDie(this, reason); + if (hp != m_hp) { + m_hp = hp; + m_env->getGameDef()->HandlePlayerHPChange(this, reason); + } else if (from_client) + m_env->getGameDef()->SendPlayerHP(this); } void PlayerSAO::setBreath(const u16 breath, bool send) |