builtin/item.lua: callbacks with copies of positions and nodes rather than recycle the same ones, which callbacks can modify

stable-0.4
Perttu Ahola 2012-07-24 17:46:17 +03:00
parent 0cf1ed544c
commit 96eac87d47
1 changed files with 92 additions and 78 deletions

View File

@ -124,7 +124,10 @@ end
function minetest.item_place_node(itemstack, placer, pointed_thing) function minetest.item_place_node(itemstack, placer, pointed_thing)
local item = itemstack:peek_item() local item = itemstack:peek_item()
local def = itemstack:get_definition() local def = itemstack:get_definition()
if def.type == "node" and pointed_thing.type == "node" then if def.type ~= "node" or pointed_thing.type ~= "node" then
return itemstack
end
local under = pointed_thing.under local under = pointed_thing.under
local oldnode_under = minetest.env:get_node(under) local oldnode_under = minetest.env:get_node(under)
local olddef_under = ItemStack({name=oldnode_under.name}):get_definition() local olddef_under = ItemStack({name=oldnode_under.name}):get_definition()
@ -183,17 +186,22 @@ function minetest.item_place_node(itemstack, placer, pointed_thing)
-- Run callback -- Run callback
if def.after_place_node then if def.after_place_node then
def.after_place_node(place_to, placer) -- Copy place_to because callback can modify it
local place_to_copy = {x=place_to.x, y=place_to.y, z=place_to.z}
def.after_place_node(place_to_copy, placer)
end end
-- Run script hook (deprecated) -- Run script hook
local _, callback local _, callback
for _, callback in ipairs(minetest.registered_on_placenodes) do for _, callback in ipairs(minetest.registered_on_placenodes) do
callback(place_to, newnode, placer, oldnode) -- Copy pos and node 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}
callback(place_to_copy, newnode_copy, placer, oldnode_copy)
end end
itemstack:take_item() itemstack:take_item()
end
return itemstack return itemstack
end end
@ -243,9 +251,11 @@ function minetest.node_punch(pos, node, puncher)
-- Run script hook -- Run script hook
local _, callback local _, callback
for _, callback in ipairs(minetest.registered_on_punchnodes) do for _, callback in ipairs(minetest.registered_on_punchnodes) do
callback(pos, node, puncher) -- Copy pos and node because callback can modify them
local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
callback(pos_copy, node_copy, puncher)
end end
end end
function minetest.node_dig(pos, node, digger) function minetest.node_dig(pos, node, digger)
@ -268,8 +278,8 @@ function minetest.node_dig(pos, node, digger)
local drops = minetest.get_node_drops(node.name, wielded:get_name()) local drops = minetest.get_node_drops(node.name, wielded:get_name())
-- Wear out tool -- Wear out tool
tp = wielded:get_tool_capabilities() local tp = wielded:get_tool_capabilities()
dp = minetest.get_dig_params(def.groups, tp) local dp = minetest.get_dig_params(def.groups, tp)
wielded:add_wear(dp.wear) wielded:add_wear(dp.wear)
digger:set_wielded_item(wielded) digger:set_wielded_item(wielded)
@ -282,10 +292,8 @@ function minetest.node_dig(pos, node, digger)
end end
end end
local oldnode = nil
local oldmetadata = nil local oldmetadata = nil
if def.after_dig_node then if def.after_dig_node then
oldnode = node;
oldmetadata = minetest.env:get_meta(pos):to_table() oldmetadata = minetest.env:get_meta(pos):to_table()
end end
@ -294,13 +302,19 @@ function minetest.node_dig(pos, node, digger)
-- Run callback -- Run callback
if def.after_dig_node then if def.after_dig_node then
def.after_dig_node(pos, oldnode, oldmetadata, digger) -- Copy pos and node because callback can modify them
local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
def.after_dig_node(pos_copy, node_copy, oldmetadata, digger)
end end
-- Run script hook (deprecated) -- Run script hook
local _, callback local _, callback
for _, callback in ipairs(minetest.registered_on_dignodes) do for _, callback in ipairs(minetest.registered_on_dignodes) do
callback(pos, node, digger) -- Copy pos and node because callback can modify them
local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
callback(pos_copy, node_copy, digger)
end end
end end