[CSM] Add `on_punchnode` callback
authorred-001 <red-001@outlook.ie>
Sun, 29 Jan 2017 18:28:38 +0000 (18:28 +0000)
committerLoïc Blot <nerzhul@users.noreply.github.com>
Mon, 13 Mar 2017 22:56:05 +0000 (23:56 +0100)
builtin/client/register.lua
clientmods/preview/init.lua
doc/client_lua_api.txt
src/game.cpp
src/script/cpp_api/s_client.cpp
src/script/cpp_api/s_client.h

index c8a5ddbda6b389a5082661cc9a275a10a7a3776b..1c3966eda335db3a3b24451a199fbfb5dd86b443 100644 (file)
@@ -67,5 +67,4 @@ core.registered_on_hp_modification, core.register_on_hp_modification = make_regi
 core.registered_on_damage_taken, core.register_on_damage_taken = make_registration()
 core.registered_on_formspec_input, core.register_on_formspec_input = make_registration()
 core.registered_on_dignode, core.register_on_dignode = make_registration()
-
-
+core.registered_on_punchnode, core.register_on_punchnode = make_registration()
index 91b12d81047c9dd8bdad958a8d878765426adcd4..db4f283509f9257dd4ad83c5ea7a9ab0a8610665 100644 (file)
@@ -52,6 +52,14 @@ core.after(2, function()
 end)
 
 core.register_on_dignode(function(pos, node)
+       print("The local player dug a node!")
+       print("pos:" .. dump(pos))
+       print("node:" .. dump(node))
+       return false
+end)
+
+core.register_on_punchnode(function(pos, node)
+       print("The local player punched a node!")
        print("pos:" .. dump(pos))
        print("node:" .. dump(node))
        return false
index 7b24452e83ebf4aed8d5775db9ccbf6244f3e37b..5ffffe938a2bcfa3d480d461e761bfcd34f6fcd5 100644 (file)
@@ -694,19 +694,23 @@ Call these functions only at load time!
 * `minetest.register_chatcommand(cmd, chatcommand definition)`
     * Adds definition to minetest.registered_chatcommands
 * `minetest.register_on_death(func())`
-    * Called when player die
+    * Called when the local player dies
 * `minetest.register_on_hp_modification(func(hp))`
     * Called when server modified player's HP
 * `minetest.register_on_damage_taken(func(hp))`
-    * Called when player take damages
+    * Called when the local player take damages
 * `minetest.register_on_formspec_input(func(formname, fields))`
-    * Called when a button is pressed in player's inventory form
+    * Called when a button is pressed in the local player's inventory form
     * Newest functions are called first
     * If function returns `true`, remaining functions are not called
 * `minetest.register_on_dignode(func(pos, node))`
-    * Called when a player digs a node
+    * Called when the local player digs a node
     * Newest functions are called first
     * If any function returns true, the node isn't dug
+* `minetest.register_on_punchnode(func(pos, node))`
+    * Called when the local player punches a node
+    * Newest functions are called first
+    * If any function returns true, the punch is ignored
 ### Sounds
 * `minetest.sound_play(spec, parameters)`: returns a handle
     * `spec` is a `SimpleSoundSpec`
index 66c8859c8298a2ee8876ad4eba4e9b1e6bc186a1..612bd25363d3c95c5378bd811be17b4d5cdf1e70 100644 (file)
@@ -3895,17 +3895,20 @@ void Game::handleDigging(GameRunData *runData,
                const PointedThing &pointed, const v3s16 &nodepos,
                const ToolCapabilities &playeritem_toolcap, f32 dtime)
 {
+
+       LocalPlayer *player = client->getEnv().getLocalPlayer();
+       ClientMap &map = client->getEnv().getClientMap();
+       MapNode n = client->getEnv().getClientMap().getNodeNoEx(nodepos);
+
        if (!runData->digging) {
                infostream << "Started digging" << std::endl;
+               if (client->getScript()->on_punchnode(nodepos, n))
+                       return;
                client->interact(0, pointed);
                runData->digging = true;
                runData->ldown_for_dig = true;
        }
 
-       LocalPlayer *player = client->getEnv().getLocalPlayer();
-       ClientMap &map = client->getEnv().getClientMap();
-       MapNode n = client->getEnv().getClientMap().getNodeNoEx(nodepos);
-
        // NOTE: Similar piece of code exists on the server side for
        // cheat detection.
        // Get digging parameters
index 2c8fee334682590bbf55a0276ce3ce2078474143..8c5e3796b1620094b0b66688737d15b8a490c8a0 100644 (file)
@@ -157,4 +157,24 @@ bool ScriptApiClient::on_dignode(v3s16 p, MapNode node)
        runCallbacks(2, RUN_CALLBACKS_MODE_OR);
        bool blocked = lua_toboolean(L, -1);
        return blocked;
-}
\ No newline at end of file
+}
+
+bool ScriptApiClient::on_punchnode(v3s16 p, MapNode node)
+{
+       SCRIPTAPI_PRECHECKHEADER
+
+       INodeDefManager *ndef = getClient()->ndef();
+
+       // Get core.registered_on_punchgnode
+       lua_getglobal(L, "core");
+       lua_getfield(L, -1, "registered_on_punchnode");
+
+       // Push data
+       push_v3s16(L, p);
+       pushnode(L, node, ndef);
+
+       // Call functions
+       runCallbacks(2, RUN_CALLBACKS_MODE_OR);
+       bool blocked = lua_toboolean(L, -1);
+       return blocked;
+}
index 09fd3a69150a4691c3649bd47fb4fe51fcb66558..93e9558f23792bb852e622d96871b000ed112f90 100644 (file)
@@ -46,5 +46,6 @@ public:
        void on_formspec_input(const std::string &formname, const StringMap &fields);
 
        bool on_dignode(v3s16 p, MapNode node);
+       bool on_punchnode(v3s16 p, MapNode node);
 };
 #endif