Add tool callback
authorNovatux <nathanael.courant@laposte.net>
Mon, 6 May 2013 16:53:15 +0000 (18:53 +0200)
committerproller <proller@github.com>
Wed, 16 Oct 2013 17:58:29 +0000 (21:58 +0400)
builtin/item.lua
doc/lua_api.txt

index f07566b8c7176fec91b8b7b1eb9190d163387dc9..7f20d30c583aa8939af7aa8442d490c55da0b46c 100644 (file)
@@ -383,14 +383,19 @@ function minetest.node_dig(pos, node, digger)
 
        local wielded = digger:get_wielded_item()
        local drops = minetest.get_node_drops(node.name, wielded:get_name())
-
-       -- Wear out tool
-       if not minetest.setting_getbool("creative_mode") then
-               local tp = wielded:get_tool_capabilities()
-               local dp = minetest.get_dig_params(def.groups, tp)
-               wielded:add_wear(dp.wear)
-               digger:set_wielded_item(wielded)
+       
+       local wdef = wielded:get_definition()
+       local tp = wielded:get_tool_capabilities()
+       local dp = minetest.get_dig_params(def.groups, tp)
+       if wdef and wdef.after_use then
+               wielded = wdef.after_use(wielded, digger, node, dp) or wielded
+       else
+               -- Wear out tool
+               if not minetest.setting_getbool("creative_mode") then
+                       wielded:add_wear(dp.wear)
+               end
        end
+       digger:set_wielded_item(wielded)
        
        -- Handle drops
        minetest.handle_node_drops(pos, drops, digger)
index f89f71273a1597705a8ab4c9fbfc049a0d6e3ab6..cfea3b5c15eea441e6bf4402aab707c335d63898 100644 (file)
@@ -1980,6 +1980,15 @@ Item definition (register_node, register_craftitem, register_tool)
         eg. itemstack:take_item(); return itemstack
     ^ Otherwise, the function is free to do what it wants.
     ^ The default functions handle regular use cases.
+    after_use = func(itemstack, user, node, digparams),
+    ^  default: nil
+    ^ If defined, should return an itemstack and will be called instead of
+      wearing out the tool. If returns nil, does nothing.
+      If after_use doesn't exist, it is the same as:
+        function(itemstack, user, node, digparams)
+          itemstack:add_wear(digparams.wear)
+          return itemstack
+        end
 }
 
 Tile definition: