Skip to content

Commit 76d4396

Browse files
committedJan 24, 2014
Pass pointed_thing to on_punch and minetest.register_on_punchnode callbacks
1 parent cd7e837 commit 76d4396

File tree

7 files changed

+28
-38
lines changed

7 files changed

+28
-38
lines changed
 

‎builtin/item.lua

+17-28
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
-- Minetest: builtin/item.lua
22

3+
local function copy_pointed_thing(pointed_thing)
4+
return {
5+
type = pointed_thing.type,
6+
above = vector.new(pointed_thing.above),
7+
under = vector.new(pointed_thing.under),
8+
ref = pointed_thing.ref,
9+
}
10+
end
11+
312
--
413
-- Item definition helpers
514
--
@@ -272,17 +281,7 @@ function minetest.item_place_node(itemstack, placer, pointed_thing, param2)
272281
if def.after_place_node then
273282
-- Deepcopy place_to and pointed_thing because callback can modify it
274283
local place_to_copy = {x=place_to.x, y=place_to.y, z=place_to.z}
275-
local pointed_thing_copy = {
276-
type = pointed_thing.type,
277-
under = {
278-
x = pointed_thing.under.x,
279-
y = pointed_thing.under.y,
280-
z = pointed_thing.under.z},
281-
above = {
282-
x = pointed_thing.above.x,
283-
y = pointed_thing.above.y,
284-
z = pointed_thing.above.z}
285-
}
284+
local pointed_thing_copy = copy_pointed_thing(pointed_thing)
286285
if def.after_place_node(place_to_copy, placer, itemstack,
287286
pointed_thing_copy) then
288287
take_item = false
@@ -292,22 +291,12 @@ function minetest.item_place_node(itemstack, placer, pointed_thing, param2)
292291
-- Run script hook
293292
local _, callback
294293
for _, callback in ipairs(minetest.registered_on_placenodes) do
295-
-- Deepcopy pos, node and poined_thing because callback can modify them
294+
-- Deepcopy pos, node and pointed_thing because callback can modify them
296295
local place_to_copy = {x=place_to.x, y=place_to.y, z=place_to.z}
297296
local newnode_copy = {name=newnode.name, param1=newnode.param1, param2=newnode.param2}
298297
local oldnode_copy = {name=oldnode.name, param1=oldnode.param1, param2=oldnode.param2}
299-
local pointed_thing_copy = {
300-
type = pointed_thing.type,
301-
under = {
302-
x = pointed_thing.under.x,
303-
y = pointed_thing.under.y,
304-
z = pointed_thing.under.z},
305-
above = {
306-
x = pointed_thing.above.x,
307-
y = pointed_thing.above.y,
308-
z = pointed_thing.above.z}
309-
}
310-
if callback(place_to_copy, newnode_copy, placer, oldnode_copy, itemstack, poined_thing_copy) then
298+
local pointed_thing_copy = copy_pointed_thing(pointed_thing)
299+
if callback(place_to_copy, newnode_copy, placer, oldnode_copy, itemstack, pointed_thing_copy) then
311300
take_item = false
312301
end
313302
end
@@ -372,14 +361,14 @@ function minetest.item_eat(hp_change, replace_with_item)
372361
end
373362
end
374363

375-
function minetest.node_punch(pos, node, puncher)
364+
function minetest.node_punch(pos, node, puncher, pointed_thing)
376365
-- Run script hook
377-
local _, callback
378366
for _, callback in ipairs(minetest.registered_on_punchnodes) do
379367
-- Copy pos and node because callback can modify them
380-
local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
368+
local pos_copy = vector.new(pos)
381369
local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
382-
callback(pos_copy, node_copy, puncher)
370+
local pointed_thing_copy = copy_pointed_thing(pointed_thing)
371+
callback(pos_copy, node_copy, puncher, pointed_thing_copy)
383372
end
384373
end
385374

‎doc/lua_api.txt

+4-4
Original file line numberDiff line numberDiff line change
@@ -1211,7 +1211,7 @@ minetest.register_on_dignode(func(pos, oldnode, digger))
12111211
^ Called when a node has been dug.
12121212
^ Not recommended: Use on_destruct or after_dig_node in node definition
12131213
^ whenever possible
1214-
minetest.register_on_punchnode(func(pos, node, puncher))
1214+
minetest.register_on_punchnode(func(pos, node, puncher, pointed_thing))
12151215
^ Called when a node is punched
12161216
minetest.register_on_generated(func(minp, maxp, blockseed))
12171217
^ Called after generating a piece of world. Modifying nodes inside the area
@@ -1500,7 +1500,7 @@ minetest.item_eat(hp_change, replace_with_item)
15001500
^ Eat the item. replace_with_item can be nil.
15011501

15021502
Defaults for the on_punch and on_dig node definition callbacks:
1503-
minetest.node_punch(pos, node, puncher)
1503+
minetest.node_punch(pos, node, puncher, pointed_thing)
15041504
^ Calls functions registered by minetest.register_on_punchnode()
15051505
minetest.node_dig(pos, node, digger)
15061506
^ Checks if node can be dug, puts item into inventory, removes node
@@ -2273,9 +2273,9 @@ Node definition (register_node)
22732273
^ returns true if node can be dug, or false if not
22742274
^ default: nil
22752275

2276-
on_punch = func(pos, node, puncher),
2276+
on_punch = func(pos, node, puncher, pointed_thing),
22772277
^ default: minetest.node_punch
2278-
^ By default: does nothing
2278+
^ By default: Calls minetest.register_on_punchnode callbacks
22792279
on_rightclick = func(pos, node, clicker, itemstack, pointed_thing),
22802280
^ default: nil
22812281
^ if defined, itemstack will hold clicker's wielded item

‎src/script/cpp_api/s_item.h

-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ class ScriptApiItem
5252
friend class ModApiItemMod;
5353

5454
bool getItemCallback(const char *name, const char *callbackname);
55-
private:
5655
void pushPointedThing(const PointedThing& pointed);
5756

5857
};

‎src/script/cpp_api/s_node.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2424
#include "nodedef.h"
2525
#include "server.h"
2626
#include "environment.h"
27+
#include "util/pointedthing.h"
2728

2829

2930
struct EnumString ScriptApiNode::es_DrawType[] =
@@ -87,7 +88,7 @@ ScriptApiNode::~ScriptApiNode() {
8788
}
8889

8990
bool ScriptApiNode::node_on_punch(v3s16 p, MapNode node,
90-
ServerActiveObject *puncher)
91+
ServerActiveObject *puncher, PointedThing pointed)
9192
{
9293
SCRIPTAPI_PRECHECKHEADER
9394

@@ -104,7 +105,8 @@ bool ScriptApiNode::node_on_punch(v3s16 p, MapNode node,
104105
push_v3s16(L, p);
105106
pushnode(L, node, ndef);
106107
objectrefGetOrCreate(puncher);
107-
if(lua_pcall(L, 3, 0, errorhandler))
108+
pushPointedThing(pointed);
109+
if(lua_pcall(L, 4, 0, errorhandler))
108110
scriptError();
109111
lua_pop(L, 1); // Pop error handler
110112
return true;

‎src/script/cpp_api/s_node.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class ScriptApiNode
3838
virtual ~ScriptApiNode();
3939

4040
bool node_on_punch(v3s16 p, MapNode node,
41-
ServerActiveObject *puncher);
41+
ServerActiveObject *puncher, PointedThing pointed);
4242
bool node_on_dig(v3s16 p, MapNode node,
4343
ServerActiveObject *digger);
4444
void node_on_construct(v3s16 p, MapNode node);

‎src/script/lua_api/l_env.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ int ModApiEnvMod::l_punch_node(lua_State *L)
273273
}
274274
// Punch it with a NULL puncher (appears in Lua as a non-functional
275275
// ObjectRef)
276-
bool success = scriptIfaceNode->node_on_punch(pos, n, NULL);
276+
bool success = scriptIfaceNode->node_on_punch(pos, n, NULL, PointedThing());
277277
lua_pushboolean(L, success);
278278
return 1;
279279
}

‎src/server.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2918,7 +2918,7 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
29182918
m_emerge->enqueueBlockEmerge(peer_id, getNodeBlockPos(p_above), false);
29192919
}
29202920
if(n.getContent() != CONTENT_IGNORE)
2921-
m_script->node_on_punch(p_under, n, playersao);
2921+
m_script->node_on_punch(p_under, n, playersao, pointed);
29222922
// Cheat prevention
29232923
playersao->noCheatDigStart(p_under);
29242924
}

1 commit comments

Comments
 (1)

qwrwed commented on Jan 24, 2014

@qwrwed

Thank you!

Please sign in to comment.