From 76d4396fa148a44f6aaee38d188d4c2cf8c90c7f Mon Sep 17 00:00:00 2001
From: ShadowNinja <shadowninja@minetest.net>
Date: Thu, 23 Jan 2014 19:21:01 -0500
Subject: [PATCH] Pass pointed_thing to on_punch and
 minetest.register_on_punchnode callbacks

---
 builtin/item.lua              | 45 +++++++++++++----------------------
 doc/lua_api.txt               |  8 +++----
 src/script/cpp_api/s_item.h   |  1 -
 src/script/cpp_api/s_node.cpp |  6 +++--
 src/script/cpp_api/s_node.h   |  2 +-
 src/script/lua_api/l_env.cpp  |  2 +-
 src/server.cpp                |  2 +-
 7 files changed, 28 insertions(+), 38 deletions(-)

diff --git a/builtin/item.lua b/builtin/item.lua
index 7a6bb6cf5..de546c2e7 100644
--- a/builtin/item.lua
+++ b/builtin/item.lua
@@ -1,5 +1,14 @@
 -- Minetest: builtin/item.lua
 
+local function copy_pointed_thing(pointed_thing)
+	return {
+		type  = pointed_thing.type,
+		above = vector.new(pointed_thing.above),
+		under = vector.new(pointed_thing.under),
+		ref   = pointed_thing.ref,
+	}
+end
+
 --
 -- Item definition helpers
 --
@@ -272,17 +281,7 @@ function minetest.item_place_node(itemstack, placer, pointed_thing, param2)
 	if def.after_place_node then
 		-- Deepcopy place_to and pointed_thing because callback can modify it
 		local place_to_copy = {x=place_to.x, y=place_to.y, z=place_to.z}
-		local pointed_thing_copy = {
-			type = pointed_thing.type,
-			under = {
-				x = pointed_thing.under.x,
-				y = pointed_thing.under.y,
-				z = pointed_thing.under.z},
-			above = {
-				x = pointed_thing.above.x,
-				y = pointed_thing.above.y,
-				z = pointed_thing.above.z}
-		}
+		local pointed_thing_copy = copy_pointed_thing(pointed_thing)
 		if def.after_place_node(place_to_copy, placer, itemstack,
 				pointed_thing_copy) then
 			take_item = false
@@ -292,22 +291,12 @@ function minetest.item_place_node(itemstack, placer, pointed_thing, param2)
 	-- Run script hook
 	local _, callback
 	for _, callback in ipairs(minetest.registered_on_placenodes) do
-		-- Deepcopy pos, node and poined_thing because callback can modify them
+		-- Deepcopy pos, node and pointed_thing because callback can modify them
 		local place_to_copy = {x=place_to.x, y=place_to.y, z=place_to.z}
 		local newnode_copy = {name=newnode.name, param1=newnode.param1, param2=newnode.param2}
 		local oldnode_copy = {name=oldnode.name, param1=oldnode.param1, param2=oldnode.param2}
-		local pointed_thing_copy = {
-			type = pointed_thing.type,
-			under = {
-				x = pointed_thing.under.x,
-				y = pointed_thing.under.y,
-				z = pointed_thing.under.z},
-			above = {
-				x = pointed_thing.above.x,
-				y = pointed_thing.above.y,
-				z = pointed_thing.above.z}
-		}
-		if callback(place_to_copy, newnode_copy, placer, oldnode_copy, itemstack, poined_thing_copy) then
+		local pointed_thing_copy = copy_pointed_thing(pointed_thing)
+		if callback(place_to_copy, newnode_copy, placer, oldnode_copy, itemstack, pointed_thing_copy) then
 			take_item = false
 		end
 	end
@@ -372,14 +361,14 @@ function minetest.item_eat(hp_change, replace_with_item)
 	end
 end
 
-function minetest.node_punch(pos, node, puncher)
+function minetest.node_punch(pos, node, puncher, pointed_thing)
 	-- Run script hook
-	local _, callback
 	for _, callback in ipairs(minetest.registered_on_punchnodes) do
 		-- Copy pos and node because callback can modify them
-		local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
+		local pos_copy = vector.new(pos)
 		local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
-		callback(pos_copy, node_copy, puncher)
+		local pointed_thing_copy = copy_pointed_thing(pointed_thing)
+		callback(pos_copy, node_copy, puncher, pointed_thing_copy)
 	end
 end
 
diff --git a/doc/lua_api.txt b/doc/lua_api.txt
index 4614ca062..736f6ca4f 100644
--- a/doc/lua_api.txt
+++ b/doc/lua_api.txt
@@ -1211,7 +1211,7 @@ minetest.register_on_dignode(func(pos, oldnode, digger))
 ^ Called when a node has been dug.
 ^ Not recommended: Use on_destruct or after_dig_node in node definition
 ^                  whenever possible
-minetest.register_on_punchnode(func(pos, node, puncher))
+minetest.register_on_punchnode(func(pos, node, puncher, pointed_thing))
 ^ Called when a node is punched
 minetest.register_on_generated(func(minp, maxp, blockseed))
 ^ Called after generating a piece of world. Modifying nodes inside the area
@@ -1500,7 +1500,7 @@ minetest.item_eat(hp_change, replace_with_item)
 ^ Eat the item. replace_with_item can be nil.
 
 Defaults for the on_punch and on_dig node definition callbacks:
-minetest.node_punch(pos, node, puncher)
+minetest.node_punch(pos, node, puncher, pointed_thing)
 ^ Calls functions registered by minetest.register_on_punchnode()
 minetest.node_dig(pos, node, digger)
 ^ Checks if node can be dug, puts item into inventory, removes node
@@ -2273,9 +2273,9 @@ Node definition (register_node)
     ^ returns true if node can be dug, or false if not
     ^ default: nil
     
-    on_punch = func(pos, node, puncher),
+    on_punch = func(pos, node, puncher, pointed_thing),
     ^ default: minetest.node_punch
-    ^ By default: does nothing
+    ^ By default: Calls minetest.register_on_punchnode callbacks
     on_rightclick = func(pos, node, clicker, itemstack, pointed_thing),
     ^ default: nil
     ^ if defined, itemstack will hold clicker's wielded item
diff --git a/src/script/cpp_api/s_item.h b/src/script/cpp_api/s_item.h
index 4964dd5b4..cca1641fd 100644
--- a/src/script/cpp_api/s_item.h
+++ b/src/script/cpp_api/s_item.h
@@ -52,7 +52,6 @@ class ScriptApiItem
 	friend class ModApiItemMod;
 
 	bool getItemCallback(const char *name, const char *callbackname);
-private:
 	void pushPointedThing(const PointedThing& pointed);
 
 };
diff --git a/src/script/cpp_api/s_node.cpp b/src/script/cpp_api/s_node.cpp
index cd8451cf0..55db43584 100644
--- a/src/script/cpp_api/s_node.cpp
+++ b/src/script/cpp_api/s_node.cpp
@@ -24,6 +24,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "nodedef.h"
 #include "server.h"
 #include "environment.h"
+#include "util/pointedthing.h"
 
 
 struct EnumString ScriptApiNode::es_DrawType[] =
@@ -87,7 +88,7 @@ ScriptApiNode::~ScriptApiNode() {
 }
 
 bool ScriptApiNode::node_on_punch(v3s16 p, MapNode node,
-		ServerActiveObject *puncher)
+		ServerActiveObject *puncher, PointedThing pointed)
 {
 	SCRIPTAPI_PRECHECKHEADER
 
@@ -104,7 +105,8 @@ bool ScriptApiNode::node_on_punch(v3s16 p, MapNode node,
 	push_v3s16(L, p);
 	pushnode(L, node, ndef);
 	objectrefGetOrCreate(puncher);
-	if(lua_pcall(L, 3, 0, errorhandler))
+	pushPointedThing(pointed);
+	if(lua_pcall(L, 4, 0, errorhandler))
 		scriptError();
 	lua_pop(L, 1); // Pop error handler
 	return true;
diff --git a/src/script/cpp_api/s_node.h b/src/script/cpp_api/s_node.h
index 517b4b04e..b3a6c83b5 100644
--- a/src/script/cpp_api/s_node.h
+++ b/src/script/cpp_api/s_node.h
@@ -38,7 +38,7 @@ class ScriptApiNode
 	virtual ~ScriptApiNode();
 
 	bool node_on_punch(v3s16 p, MapNode node,
-			ServerActiveObject *puncher);
+			ServerActiveObject *puncher, PointedThing pointed);
 	bool node_on_dig(v3s16 p, MapNode node,
 			ServerActiveObject *digger);
 	void node_on_construct(v3s16 p, MapNode node);
diff --git a/src/script/lua_api/l_env.cpp b/src/script/lua_api/l_env.cpp
index f75334750..6447866cd 100644
--- a/src/script/lua_api/l_env.cpp
+++ b/src/script/lua_api/l_env.cpp
@@ -273,7 +273,7 @@ int ModApiEnvMod::l_punch_node(lua_State *L)
 	}
 	// Punch it with a NULL puncher (appears in Lua as a non-functional
 	// ObjectRef)
-	bool success = scriptIfaceNode->node_on_punch(pos, n, NULL);
+	bool success = scriptIfaceNode->node_on_punch(pos, n, NULL, PointedThing());
 	lua_pushboolean(L, success);
 	return 1;
 }
diff --git a/src/server.cpp b/src/server.cpp
index 50141f843..47b11d3da 100644
--- a/src/server.cpp
+++ b/src/server.cpp
@@ -2918,7 +2918,7 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
 					m_emerge->enqueueBlockEmerge(peer_id, getNodeBlockPos(p_above), false);
 				}
 				if(n.getContent() != CONTENT_IGNORE)
-					m_script->node_on_punch(p_under, n, playersao);
+					m_script->node_on_punch(p_under, n, playersao, pointed);
 				// Cheat prevention
 				playersao->noCheatDigStart(p_under);
 			}
-- 
GitLab