Implement locked chest; add after_place_node and after_dig_node node callbacks
authorPerttu Ahola <celeron55@gmail.com>
Sat, 2 Jun 2012 10:20:30 +0000 (13:20 +0300)
committerPerttu Ahola <celeron55@gmail.com>
Sun, 3 Jun 2012 19:31:01 +0000 (22:31 +0300)
builtin/item.lua
builtin/misc_register.lua
doc/lua_api.txt
games/minimal/mods/default/init.lua

index eb8c556de87b418c26d42f2f16d30b5255df97ef..c4bd24204864f1f9277ecb38159b865ae1de6259 100644 (file)
@@ -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](...)
index 351a075e4950b86f510d37e676f23e08f6a11bbc..d400b09c39430d831972d5528425d3fa9c569740 100644 (file)
@@ -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
 
index 22c1824c0a692909aaf92d48d3d39b56aac48a0f..6de255c0d701d6c2d17694a6a3ec4b4faf323771 100644 (file)
@@ -678,8 +678,10 @@ minetest.register_globalstep(func(dtime))
 ^ Called every server step, usually interval of 0.05s
 minetest.register_on_placenode(func(pos, newnode, placer))
 ^ Called when a node has been placed
+^ Deprecated: Use on_construct or after_place_node in node definition instead
 minetest.register_on_dignode(func(pos, oldnode, digger))
 ^ Called when a node has been dug. digger can be nil.
+^ Deprecated: Use on_destruct or after_dig_node in node definition instead
 minetest.register_on_punchnode(func(pos, node, puncher))
 ^ Called when a node is punched
 minetest.register_on_generated(func(minp, maxp, blockseed))
@@ -1127,8 +1129,20 @@ Node definition (register_node)
     on_construct = func(pos),
     ^ Node constructor; always called after adding node
     ^ Can set up metadata and stuff like that
+    ^ default: nil
     on_destruct = func(pos),
     ^ Node destructor; always called before removing node
+    ^ default: nil
+
+    after_place_node = func(pos, placer),
+    ^ Called after constructing node when node was placed using
+      minetest.item_place_node
+    ^ default: nil
+    after_dig_node = func(pos, oldnode, oldmetadata, digger),
+    ^ oldmetadata is in table format
+    ^ Called after destructing node when node was dug using
+      minetest.node_dig
+    ^ default: nil
        
     on_punch = func(pos, node, puncher),
     ^ default: minetest.node_punch
index fc5eb0953c976093c1feb97bde8a9a1b4f87db0f..3d68e71e946ebe3f9e97d749bdeb974ed148a69e 100644 (file)
@@ -1143,12 +1143,38 @@ minetest.register_node("default:chest", {
                                "invsize[8,9;]"..
                                "list[current_name;main;0,0;8,4;]"..
                                "list[current_player;main;0,5;8,4;]")
-               meta:set_string("infotext", "Chest");
+               meta:set_string("infotext", "Chest")
                local inv = meta:get_inventory()
                inv:set_size("main", 8*4)
        end,
+    on_metadata_inventory_move = function(pos, from_list, from_index,
+                       to_list, to_index, count, player)
+               minetest.log("action", player:get_player_name()..
+                               " moves stuff in chest at "..minetest.pos_to_string(pos))
+               return minetest.node_metadata_inventory_move_allow_all(
+                               pos, from_list, from_index, to_list, to_index, count, player)
+       end,
+    on_metadata_inventory_offer = function(pos, listname, index, stack, player)
+               minetest.log("action", player:get_player_name()..
+                               " moves stuff to chest at "..minetest.pos_to_string(pos))
+               return minetest.node_metadata_inventory_offer_allow_all(
+                               pos, listname, index, stack, player)
+       end,
+    on_metadata_inventory_take = function(pos, listname, index, count, player)
+               minetest.log("action", player:get_player_name()..
+                               " takes stuff from chest at "..minetest.pos_to_string(pos))
+               return minetest.node_metadata_inventory_take_allow_all(
+                               pos, listname, index, count, player)
+       end,
 })
 
+local function has_locked_chest_privilege(meta, player)
+       if player:get_player_name() ~= meta:get_string("owner") then
+               return false
+       end
+       return true
+end
+
 minetest.register_node("default:chest_locked", {
        description = "Locked Chest",
        tile_images = {"default_chest_top.png", "default_chest_top.png", "default_chest_side.png",
@@ -1157,16 +1183,66 @@ minetest.register_node("default:chest_locked", {
        groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2},
        legacy_facedir_simple = true,
        sounds = default.node_sound_wood_defaults(),
+       after_place_node = function(pos, placer)
+               local meta = minetest.env:get_meta(pos)
+               meta:set_string("owner", placer:get_player_name() or "")
+               meta:set_string("infotext", "Locked Chest (owned by "..
+                               meta:get_string("owner")..")")
+       end,
        on_construct = function(pos)
                local meta = minetest.env:get_meta(pos)
                meta:set_string("formspec",
                                "invsize[8,9;]"..
                                "list[current_name;main;0,0;8,4;]"..
                                "list[current_player;main;0,5;8,4;]")
-               meta:set_string("infotext", "Locked Chest");
+               meta:set_string("infotext", "Locked Chest")
+               meta:set_string("owner", "")
                local inv = meta:get_inventory()
                inv:set_size("main", 8*4)
        end,
+    on_metadata_inventory_move = function(pos, from_list, from_index,
+                       to_list, to_index, count, player)
+               local meta = minetest.env:get_meta(pos)
+               if not has_locked_chest_privilege(meta, player) then
+                       minetest.log("action", player:get_player_name()..
+                                       " tried to access a locked chest belonging to "..
+                                       meta:get_string("owner").." at "..
+                                       minetest.pos_to_string(pos))
+                       return
+               end
+               minetest.log("action", player:get_player_name()..
+                               " moves stuff in locked chest at "..minetest.pos_to_string(pos))
+               return minetest.node_metadata_inventory_move_allow_all(
+                               pos, from_list, from_index, to_list, to_index, count, player)
+       end,
+    on_metadata_inventory_offer = function(pos, listname, index, stack, player)
+               local meta = minetest.env:get_meta(pos)
+               if not has_locked_chest_privilege(meta, player) then
+                       minetest.log("action", player:get_player_name()..
+                                       " tried to access a locked chest belonging to "..
+                                       meta:get_string("owner").." at "..
+                                       minetest.pos_to_string(pos))
+                       return
+               end
+               minetest.log("action", player:get_player_name()..
+                               " moves stuff to locked chest at "..minetest.pos_to_string(pos))
+               return minetest.node_metadata_inventory_offer_allow_all(
+                               pos, listname, index, stack, player)
+       end,
+    on_metadata_inventory_take = function(pos, listname, index, count, player)
+               local meta = minetest.env:get_meta(pos)
+               if not has_locked_chest_privilege(meta, player) then
+                       minetest.log("action", player:get_player_name()..
+                                       " tried to access a locked chest belonging to "..
+                                       meta:get_string("owner").." at "..
+                                       minetest.pos_to_string(pos))
+                       return
+               end
+               minetest.log("action", player:get_player_name()..
+                               " takes stuff from locked chest at "..minetest.pos_to_string(pos))
+               return minetest.node_metadata_inventory_take_allow_all(
+                               pos, listname, index, count, player)
+       end,
 })
 
 minetest.register_node("default:furnace", {
@@ -1185,7 +1261,7 @@ minetest.register_node("default:furnace", {
                        "list[current_name;src;2,1;1,1;]"..
                        "list[current_name;dst;5,1;2,2;]"..
                        "list[current_player;main;0,5;8,4;]")
-               meta:set_string("infotext", "Furnace");
+               meta:set_string("infotext", "Furnace")
                local inv = meta:get_inventory()
                inv:set_size("fuel", 1)
                inv:set_size("src", 1)