Skip to content

Commit

Permalink
add minetest.register_on_punchplayer
Browse files Browse the repository at this point in the history
  • Loading branch information
Bremaweb authored and TeTpaAka committed May 12, 2015
1 parent b4c3ff6 commit f3ba254
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 2 deletions.
1 change: 1 addition & 0 deletions builtin/game/register.lua
Expand Up @@ -431,6 +431,7 @@ core.registered_on_crafts, core.register_on_craft = make_registration()
core.registered_craft_predicts, core.register_craft_predict = make_registration()
core.registered_on_protection_violation, core.register_on_protection_violation = make_registration()
core.registered_on_item_eats, core.register_on_item_eat = make_registration()
core.registered_on_punchplayer, core.register_on_punchplayer = make_registration()

--
-- Compatibility for on_mapgen_init()
Expand Down
10 changes: 9 additions & 1 deletion doc/lua_api.txt
Expand Up @@ -2278,8 +2278,16 @@ These functions return the leftover itemstack.

* `minetest.forceload_free_block(pos)`
* stops forceloading the position `pos`

Please note that forceloaded areas are saved when the server restarts.
* `minetest.register_on_punchplayer(func(player, hitter, time_from_last_punch, tool_capabilities, dir))`
* Called when a player is punched
* `player` - ObjectRef - Player that was punched
* `hitter` - ObjectRef - Player that hit
* `time_from_last_punch`: Meant for disallowing spamming of clicks (can be nil)
* `tool_capabilities`: capability table of used tool (can be nil)
* `dir`: unit vector of direction of punch. Always defined. Points from
the puncher to the punched.
* should return `true` to prevent the default damage mechanism

minetest.global_exists(name)
^ Checks if a global variable has been set, without triggering a warning.
Expand Down
9 changes: 8 additions & 1 deletion src/content_sao.cpp
Expand Up @@ -1041,7 +1041,14 @@ int PlayerSAO::punch(v3f dir,
<<punchername<<", damage "<<hitparams.hp
<<" HP"<<std::endl;

setHP(getHP() - hitparams.hp);
PlayerSAO *playersao = m_player->getPlayerSAO();

bool damage_handled = m_env->getScriptIface()->on_punchplayer(playersao, puncher,
time_from_last_punch, toolcap, dir);

if (!damage_handled) {
setHP(getHP() - hitparams.hp);
}

return hitparams.wear;
}
Expand Down
1 change: 1 addition & 0 deletions src/script/cpp_api/s_internal.h
Expand Up @@ -61,3 +61,4 @@ bool* m_variable;
StackUnroller stack_unroller(L);

#endif /* S_INTERNAL_H_ */

23 changes: 23 additions & 0 deletions src/script/cpp_api/s_player.cpp
Expand Up @@ -19,6 +19,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,

#include "cpp_api/s_player.h"
#include "cpp_api/s_internal.h"
#include "common/c_converter.h"
#include "common/c_content.h"
#include "util/string.h"

void ScriptApiPlayer::on_newplayer(ServerActiveObject *player)
Expand All @@ -45,6 +47,27 @@ void ScriptApiPlayer::on_dieplayer(ServerActiveObject *player)
script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
}

bool ScriptApiPlayer::on_punchplayer(ServerActiveObject *player,
ServerActiveObject *hitter,
float time_from_last_punch,
const ToolCapabilities *toolcap,
v3f dir)
{
SCRIPTAPI_PRECHECKHEADER
// Get core.registered_on_dieplayers
lua_getglobal(L, "core");
lua_getfield(L, -1, "registered_on_punchplayer");
// Call callbacks
objectrefGetOrCreate(L, player);
objectrefGetOrCreate(L, hitter);
lua_pushnumber(L, time_from_last_punch);
push_tool_capabilities(L, *toolcap);
push_v3f(L, dir);
script_run_callbacks(L, 5, RUN_CALLBACKS_MODE_LAST);
bool damage_handled = lua_toboolean(L, -1);
return damage_handled;
}

bool ScriptApiPlayer::on_respawnplayer(ServerActiveObject *player)
{
SCRIPTAPI_PRECHECKHEADER
Expand Down
3 changes: 3 additions & 0 deletions src/script/cpp_api/s_player.h
Expand Up @@ -23,7 +23,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <map>

#include "cpp_api/s_base.h"
#include "irr_v3d.h"

struct ToolCapabilities;

class ScriptApiPlayer
: virtual public ScriptApiBase
Expand All @@ -38,6 +40,7 @@ class ScriptApiPlayer
void on_joinplayer(ServerActiveObject *player);
void on_leaveplayer(ServerActiveObject *player);
void on_cheat(ServerActiveObject *player, const std::string &cheat_type);
bool on_punchplayer(ServerActiveObject *player, ServerActiveObject *hitter,float time_from_last_punch, const ToolCapabilities *toolcap, v3f dir);

void on_playerReceiveFields(ServerActiveObject *player,
const std::string &formname,
Expand Down

0 comments on commit f3ba254

Please sign in to comment.