Skip to content

Commit 0727bb3

Browse files
red-001nerzhul
authored andcommittedMar 13, 2017
[CSM] Add on_punchnode callback
1 parent 37df9cb commit 0727bb3

File tree

6 files changed

+46
-11
lines changed

6 files changed

+46
-11
lines changed
 

‎builtin/client/register.lua

+1-2
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,4 @@ core.registered_on_hp_modification, core.register_on_hp_modification = make_regi
6767
core.registered_on_damage_taken, core.register_on_damage_taken = make_registration()
6868
core.registered_on_formspec_input, core.register_on_formspec_input = make_registration()
6969
core.registered_on_dignode, core.register_on_dignode = make_registration()
70-
71-
70+
core.registered_on_punchnode, core.register_on_punchnode = make_registration()

‎clientmods/preview/init.lua

+8
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ core.after(2, function()
5252
end)
5353

5454
core.register_on_dignode(function(pos, node)
55+
print("The local player dug a node!")
56+
print("pos:" .. dump(pos))
57+
print("node:" .. dump(node))
58+
return false
59+
end)
60+
61+
core.register_on_punchnode(function(pos, node)
62+
print("The local player punched a node!")
5563
print("pos:" .. dump(pos))
5664
print("node:" .. dump(node))
5765
return false

‎doc/client_lua_api.txt

+8-4
Original file line numberDiff line numberDiff line change
@@ -694,19 +694,23 @@ Call these functions only at load time!
694694
* `minetest.register_chatcommand(cmd, chatcommand definition)`
695695
* Adds definition to minetest.registered_chatcommands
696696
* `minetest.register_on_death(func())`
697-
* Called when player die
697+
* Called when the local player dies
698698
* `minetest.register_on_hp_modification(func(hp))`
699699
* Called when server modified player's HP
700700
* `minetest.register_on_damage_taken(func(hp))`
701-
* Called when player take damages
701+
* Called when the local player take damages
702702
* `minetest.register_on_formspec_input(func(formname, fields))`
703-
* Called when a button is pressed in player's inventory form
703+
* Called when a button is pressed in the local player's inventory form
704704
* Newest functions are called first
705705
* If function returns `true`, remaining functions are not called
706706
* `minetest.register_on_dignode(func(pos, node))`
707-
* Called when a player digs a node
707+
* Called when the local player digs a node
708708
* Newest functions are called first
709709
* If any function returns true, the node isn't dug
710+
* `minetest.register_on_punchnode(func(pos, node))`
711+
* Called when the local player punches a node
712+
* Newest functions are called first
713+
* If any function returns true, the punch is ignored
710714
### Sounds
711715
* `minetest.sound_play(spec, parameters)`: returns a handle
712716
* `spec` is a `SimpleSoundSpec`

‎src/game.cpp

+7-4
Original file line numberDiff line numberDiff line change
@@ -3895,17 +3895,20 @@ void Game::handleDigging(GameRunData *runData,
38953895
const PointedThing &pointed, const v3s16 &nodepos,
38963896
const ToolCapabilities &playeritem_toolcap, f32 dtime)
38973897
{
3898+
3899+
LocalPlayer *player = client->getEnv().getLocalPlayer();
3900+
ClientMap &map = client->getEnv().getClientMap();
3901+
MapNode n = client->getEnv().getClientMap().getNodeNoEx(nodepos);
3902+
38983903
if (!runData->digging) {
38993904
infostream << "Started digging" << std::endl;
3905+
if (client->getScript()->on_punchnode(nodepos, n))
3906+
return;
39003907
client->interact(0, pointed);
39013908
runData->digging = true;
39023909
runData->ldown_for_dig = true;
39033910
}
39043911

3905-
LocalPlayer *player = client->getEnv().getLocalPlayer();
3906-
ClientMap &map = client->getEnv().getClientMap();
3907-
MapNode n = client->getEnv().getClientMap().getNodeNoEx(nodepos);
3908-
39093912
// NOTE: Similar piece of code exists on the server side for
39103913
// cheat detection.
39113914
// Get digging parameters

‎src/script/cpp_api/s_client.cpp

+21-1
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,24 @@ bool ScriptApiClient::on_dignode(v3s16 p, MapNode node)
157157
runCallbacks(2, RUN_CALLBACKS_MODE_OR);
158158
bool blocked = lua_toboolean(L, -1);
159159
return blocked;
160-
}
160+
}
161+
162+
bool ScriptApiClient::on_punchnode(v3s16 p, MapNode node)
163+
{
164+
SCRIPTAPI_PRECHECKHEADER
165+
166+
INodeDefManager *ndef = getClient()->ndef();
167+
168+
// Get core.registered_on_punchgnode
169+
lua_getglobal(L, "core");
170+
lua_getfield(L, -1, "registered_on_punchnode");
171+
172+
// Push data
173+
push_v3s16(L, p);
174+
pushnode(L, node, ndef);
175+
176+
// Call functions
177+
runCallbacks(2, RUN_CALLBACKS_MODE_OR);
178+
bool blocked = lua_toboolean(L, -1);
179+
return blocked;
180+
}

‎src/script/cpp_api/s_client.h

+1
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,6 @@ class ScriptApiClient: virtual public ScriptApiBase
4646
void on_formspec_input(const std::string &formname, const StringMap &fields);
4747

4848
bool on_dignode(v3s16 p, MapNode node);
49+
bool on_punchnode(v3s16 p, MapNode node);
4950
};
5051
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.