Add ability to change the itemstack in placenode callbacks
authorPilzAdam <adam-k@outlook.com>
Mon, 17 Dec 2012 18:49:18 +0000 (19:49 +0100)
committerPilzAdam <adam-k@outlook.com>
Mon, 17 Dec 2012 18:49:18 +0000 (19:49 +0100)
builtin/item.lua
doc/lua_api.txt

index 80c665c99b34a9999f7e2e6a8303b8366219d512..acc1e626232c33c62c8029cf6c0db847ff25b729 100644 (file)
@@ -192,11 +192,15 @@ function minetest.item_place_node(itemstack, placer, pointed_thing)
        -- Add node and update
        minetest.env:add_node(place_to, newnode)
 
+       local take_item = true
+
        -- Run callback
        if def.after_place_node then
                -- 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)
+               if def.after_place_node(place_to_copy, placer, itemstack) then
+                       take_item = false
+               end
        end
 
        -- Run script hook
@@ -206,10 +210,14 @@ function minetest.item_place_node(itemstack, placer, pointed_thing)
                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)
+               if callback(place_to_copy, newnode_copy, placer, oldnode_copy, itemstack) then
+                       take_item = false
+               end
        end
 
-       itemstack:take_item()
+       if take_item then
+               itemstack:take_item()
+       end
        return itemstack
 end
 
index 60d1b2cb991a7512fab1015ba64fa9c986fcf97c..a9944935fe0bda5d58f9371afbf008d578b5a7d4 100644 (file)
@@ -810,8 +810,9 @@ minetest.register_on_shutdown(func())
 ^ WARNING: If the server terminates abnormally (i.e. crashes), the registered
            callbacks WILL LIKELY NOT BE RUN.  Data should be saved at
            semi-frequent intervals as well as on server shutdown.
-minetest.register_on_placenode(func(pos, newnode, placer, oldnode))
+minetest.register_on_placenode(func(pos, newnode, placer, oldnode, itemstack))
 ^ Called when a node has been placed
+^ If return true no item is taken from itemstack
 ^ 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.
@@ -1405,9 +1406,10 @@ Node definition (register_node)
     ^ Node destructor; always called after removing node
     ^ default: nil
 
-    after_place_node = func(pos, placer),
+    after_place_node = func(pos, placer, itemstack),
     ^ Called after constructing node when node was placed using
       minetest.item_place_node / minetest.env:place_node
+    ^ If return true no item is taken from itemstack
     ^ default: nil
     after_dig_node = func(pos, oldnode, oldmetadata, digger),
     ^ oldmetadata is in table format