diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/content_sao.cpp | 6 | ||||
-rw-r--r-- | src/script/cpp_api/s_player.cpp | 19 | ||||
-rw-r--r-- | src/script/cpp_api/s_player.h | 1 |
3 files changed, 26 insertions, 0 deletions
diff --git a/src/content_sao.cpp b/src/content_sao.cpp index 1a0fce0bb..318dd367a 100644 --- a/src/content_sao.cpp +++ b/src/content_sao.cpp @@ -1125,6 +1125,12 @@ void PlayerSAO::setHP(s16 hp) { s16 oldhp = m_player->hp; + s16 hp_change = m_env->getScriptIface()->on_player_hpchange(this, + hp - oldhp); + if (hp_change == 0) + return; + hp = oldhp + hp_change; + if (hp < 0) hp = 0; else if (hp > PLAYER_MAX_HP) diff --git a/src/script/cpp_api/s_player.cpp b/src/script/cpp_api/s_player.cpp index 9b2c3753c..a499130c7 100644 --- a/src/script/cpp_api/s_player.cpp +++ b/src/script/cpp_api/s_player.cpp @@ -69,6 +69,25 @@ bool ScriptApiPlayer::on_punchplayer(ServerActiveObject *player, return lua_toboolean(L, -1); } +s16 ScriptApiPlayer::on_player_hpchange(ServerActiveObject *player, + s16 hp_change) +{ + SCRIPTAPI_PRECHECKHEADER + + // Get core.registered_on_player_hpchange + lua_getglobal(L, "core"); + lua_getfield(L, -1, "registered_on_player_hpchange"); + lua_remove(L, -2); + + objectrefGetOrCreate(L, player); + lua_pushnumber(L, hp_change); + if (lua_pcall(L, 2, 1, m_errorhandler)) + scriptError(); + hp_change = lua_tointeger(L, -1); + lua_pop(L, -1); + return hp_change; +} + bool ScriptApiPlayer::on_respawnplayer(ServerActiveObject *player) { SCRIPTAPI_PRECHECKHEADER diff --git a/src/script/cpp_api/s_player.h b/src/script/cpp_api/s_player.h index 9c8d1d1ad..2e4dc2222 100644 --- a/src/script/cpp_api/s_player.h +++ b/src/script/cpp_api/s_player.h @@ -43,6 +43,7 @@ public: bool on_punchplayer(ServerActiveObject *player, ServerActiveObject *hitter, float time_from_last_punch, const ToolCapabilities *toolcap, v3f dir, s16 damage); + s16 on_player_hpchange(ServerActiveObject *player, s16 hp_change); void on_playerReceiveFields(ServerActiveObject *player, const std::string &formname, const StringMap &fields); }; |