From 35df1ee2536c6f8370914ecd62abd46501d2f86f Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Sat, 2 Jun 2012 13:20:30 +0300 Subject: Implement locked chest; add after_place_node and after_dig_node node callbacks --- builtin/item.lua | 23 +++++++++++++++++++++-- builtin/misc_register.lua | 14 ++++++++++++-- 2 files changed, 33 insertions(+), 4 deletions(-) (limited to 'builtin') diff --git a/builtin/item.lua b/builtin/item.lua index eb8c556de..c4bd24204 100644 --- a/builtin/item.lua +++ b/builtin/item.lua @@ -158,7 +158,12 @@ function minetest.item_place_node(itemstack, placer, pointed_thing) -- Add node and update minetest.env:add_node(pos, newnode) - -- Run script hook + -- Run callback + if def.after_place_node then + def.after_place_node(pos, placer) + end + + -- Run script hook (deprecated) local _, callback for _, callback in ipairs(minetest.registered_on_placenodes) do callback(pos, newnode, placer) @@ -251,11 +256,23 @@ function minetest.node_dig(pos, node, digger) digger:get_inventory():add_item("main", dropped_item) end end + + local oldnode = nil + local oldmetadata = nil + if def.after_dig_node then + oldnode = node; + oldmetadata = minetest.env:get_meta(pos):to_table() + end -- Remove node and update minetest.env:remove_node(pos) + + -- Run callback + if def.after_dig_node then + def.after_dig_node(pos, oldnode, oldmetadata, digger) + end - -- Run script hook + -- Run script hook (deprecated) local _, callback for _, callback in ipairs(minetest.registered_on_dignodes) do callback(pos, node, digger) @@ -298,6 +315,8 @@ function minetest.node_metadata_inventory_take_allow_all(pos, listname, index, c end -- This is used to allow mods to redefine minetest.item_place and so on +-- NOTE: This is not the preferred way. Preferred way is to provide enough +-- callbacks to not require redefining global functions. -celeron55 local function redef_wrapper(table, name) return function(...) return table[name](...) diff --git a/builtin/misc_register.lua b/builtin/misc_register.lua index 351a075e4..d400b09c3 100644 --- a/builtin/misc_register.lua +++ b/builtin/misc_register.lua @@ -301,8 +301,6 @@ end minetest.registered_on_chat_messages, minetest.register_on_chat_message = make_registration() minetest.registered_globalsteps, minetest.register_globalstep = make_registration() -minetest.registered_on_placenodes, minetest.register_on_placenode = make_registration() -minetest.registered_on_dignodes, minetest.register_on_dignode = make_registration() minetest.registered_on_punchnodes, minetest.register_on_punchnode = make_registration() minetest.registered_on_generateds, minetest.register_on_generated = make_registration() minetest.registered_on_newplayers, minetest.register_on_newplayer = make_registration() @@ -311,4 +309,16 @@ minetest.registered_on_respawnplayers, minetest.register_on_respawnplayer = make minetest.registered_on_joinplayers, minetest.register_on_joinplayer = make_registration() minetest.registered_on_leaveplayers, minetest.register_on_leaveplayer = make_registration() +minetest.registered_on_placenodes = {} +minetest.register_on_placenode = function(callback) + minetest.log("info", debug.traceback()) + minetest.log("info", "WARNING: minetest.register_on_placenode is deprecated. Use on_construct or after_place_node in node definition instead.") + table.insert(minetest.registered_on_placenodes, callback) +end +minetest.registered_on_dignodes = {} +minetest.register_on_dignode = function(callback) + minetest.log("info", debug.traceback()) + minetest.log("info", "WARNING: minetest.register_on_dignode is deprecated. Use on_destruct or after_dig_node in node definition instead.") + table.insert(minetest.registered_on_dignodes, callback) +end -- cgit v1.2.3