Skip to content

Commit 08ee979

Browse files
JDiazrubenwardy
JDiaz
andauthoredJan 11, 2021
Implement on_rightclickplayer callback (#10775)
Co-authored-by: rubenwardy <rw@rubenwardy.com>
1 parent fcb3ed8 commit 08ee979

File tree

6 files changed

+28
-4
lines changed

6 files changed

+28
-4
lines changed
 

‎builtin/game/register.lua

+1
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,7 @@ core.registered_can_bypass_userlimit, core.register_can_bypass_userlimit = make_
617617
core.registered_on_modchannel_message, core.register_on_modchannel_message = make_registration()
618618
core.registered_on_player_inventory_actions, core.register_on_player_inventory_action = make_registration()
619619
core.registered_allow_player_inventory_actions, core.register_allow_player_inventory_action = make_registration()
620+
core.registered_on_rightclickplayers, core.register_on_rightclickplayer = make_registration()
620621

621622
--
622623
-- Compatibility for on_mapgen_init()

‎doc/lua_api.txt

+7-3
Original file line numberDiff line numberDiff line change
@@ -2113,7 +2113,7 @@ Examples
21132113
list[current_player;main;0,3.5;8,4;]
21142114
list[current_player;craft;3,0;3,3;]
21152115
list[current_player;craftpreview;7,1;1,1;]
2116-
2116+
21172117
Version History
21182118
---------------
21192119

@@ -4587,6 +4587,10 @@ Call these functions only at load time!
45874587
the puncher to the punched.
45884588
* `damage`: Number that represents the damage calculated by the engine
45894589
* should return `true` to prevent the default damage mechanism
4590+
* `minetest.register_on_rightclickplayer(function(player, clicker))`
4591+
* Called when a player is right-clicked
4592+
* `player`: ObjectRef - Player that was right-clicked
4593+
* `clicker`: ObjectRef - Object that right-clicked, may or may not be a player
45904594
* `minetest.register_on_player_hpchange(function(player, hp_change, reason), modifier)`
45914595
* Called when the player gets damaged or healed
45924596
* `player`: ObjectRef of the player
@@ -7641,12 +7645,12 @@ Used by `minetest.register_node`.
76417645
-- intensity: 1.0 = mid range of regular TNT.
76427646
-- If defined, called when an explosion touches the node, instead of
76437647
-- removing the node.
7644-
7648+
76457649
mod_origin = "modname",
76467650
-- stores which mod actually registered a node
76477651
-- if it can not find a source, returns "??"
76487652
-- useful for getting what mod truly registered something
7649-
-- example: if a node is registered as ":othermodname:nodename",
7653+
-- example: if a node is registered as ":othermodname:nodename",
76507654
-- nodename will show "othermodname", but mod_orgin will say "modname"
76517655
}
76527656

‎src/script/cpp_api/s_player.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,19 @@ bool ScriptApiPlayer::on_punchplayer(ServerActiveObject *player,
7777
return readParam<bool>(L, -1);
7878
}
7979

80+
void ScriptApiPlayer::on_rightclickplayer(ServerActiveObject *player,
81+
ServerActiveObject *clicker)
82+
{
83+
SCRIPTAPI_PRECHECKHEADER
84+
// Get core.registered_on_rightclickplayers
85+
lua_getglobal(L, "core");
86+
lua_getfield(L, -1, "registered_on_rightclickplayers");
87+
// Call callbacks
88+
objectrefGetOrCreate(L, player);
89+
objectrefGetOrCreate(L, clicker);
90+
runCallbacks(2, RUN_CALLBACKS_MODE_FIRST);
91+
}
92+
8093
s32 ScriptApiPlayer::on_player_hpchange(ServerActiveObject *player,
8194
s32 hp_change, const PlayerHPChangeReason &reason)
8295
{

‎src/script/cpp_api/s_player.h

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class ScriptApiPlayer : virtual public ScriptApiBase
4747
bool on_punchplayer(ServerActiveObject *player, ServerActiveObject *hitter,
4848
float time_from_last_punch, const ToolCapabilities *toolcap,
4949
v3f dir, s16 damage);
50+
void on_rightclickplayer(ServerActiveObject *player, ServerActiveObject *clicker);
5051
s32 on_player_hpchange(ServerActiveObject *player, s32 hp_change,
5152
const PlayerHPChangeReason &reason);
5253
void on_playerReceiveFields(ServerActiveObject *player,

‎src/server/player_sao.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,11 @@ u16 PlayerSAO::punch(v3f dir,
456456
return hitparams.wear;
457457
}
458458

459+
void PlayerSAO::rightClick(ServerActiveObject *clicker)
460+
{
461+
m_env->getScriptIface()->on_rightclickplayer(this, clicker);
462+
}
463+
459464
void PlayerSAO::setHP(s32 hp, const PlayerHPChangeReason &reason)
460465
{
461466
if (hp == (s32)m_hp)

‎src/server/player_sao.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ class PlayerSAO : public UnitSAO
111111

112112
u16 punch(v3f dir, const ToolCapabilities *toolcap, ServerActiveObject *puncher,
113113
float time_from_last_punch);
114-
void rightClick(ServerActiveObject *clicker) {}
114+
void rightClick(ServerActiveObject *clicker);
115115
void setHP(s32 hp, const PlayerHPChangeReason &reason);
116116
void setHPRaw(u16 hp) { m_hp = hp; }
117117
s16 readDamage();

0 commit comments

Comments
 (0)