end
local under = pointed_thing.under
- local oldnode_under = minetest.env:get_node(under)
+ local oldnode_under = minetest.env:get_node_or_nil(under)
+ local above = pointed_thing.above
+ local oldnode_above = minetest.env:get_node_or_nil(above)
+
+ if not oldnode_under or not oldnode_above then
+ minetest.log("info", placer:get_player_name() .. " tried to place"
+ .. " node in unloaded position " .. minetest.pos_to_string(above))
+ return itemstack
+ end
+
local olddef_under = ItemStack({name=oldnode_under.name}):get_definition()
olddef_under = olddef_under or minetest.nodedef_default
- local above = pointed_thing.above
- local oldnode_above = minetest.env:get_node(above)
local olddef_above = ItemStack({name=oldnode_above.name}):get_definition()
olddef_above = olddef_above or minetest.nodedef_default
minetest.log("info", placer:get_player_name() .. " tried to place"
.. " node in invalid position " .. minetest.pos_to_string(above)
.. ", replacing " .. oldnode_above.name)
- return
+ return itemstack
end
-- Place above pointed node
not check_attached_node(place_to, newnode) then
minetest.log("action", "attached node " .. def.name ..
" can not be placed at " .. minetest.pos_to_string(place_to))
- return
+ return itemstack
end
-- 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
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
end
function minetest.item_place(itemstack, placer, pointed_thing)
+ -- Call on_rightclick if the pointed node defines it
+ if pointed_thing.type == "node" and placer and
+ not placer:get_player_control().sneak then
+ local n = minetest.env:get_node(pointed_thing.under)
+ local nn = n.name
+ if minetest.registered_nodes[nn] and minetest.registered_nodes[nn].on_rightclick then
+ return minetest.registered_nodes[nn].on_rightclick(pointed_thing.under, n, placer, itemstack) or itemstack
+ end
+ end
+
if itemstack:get_definition().type == "node" then
return minetest.item_place_node(itemstack, placer, pointed_thing)
- else
- return minetest.item_place_object(itemstack, placer, pointed_thing)
end
+ return itemstack
end
function minetest.item_drop(itemstack, dropper, pos)
else
minetest.env:add_item(pos, itemstack)
end
- return ""
+ return ItemStack("")
end
function minetest.item_eat(hp_change, replace_with_item)
if digger:get_inventory() then
local _, dropped_item
for _, dropped_item in ipairs(drops) do
- digger:get_inventory():add_item("main", dropped_item)
+ local left = digger:get_inventory():add_item("main", dropped_item)
+ if not left:is_empty() then
+ local p = {
+ x = pos.x + math.random()/2-0.25,
+ y = pos.y + math.random()/2-0.25,
+ z = pos.z + math.random()/2-0.25,
+ }
+ minetest.env:add_item(p, left)
+ end
end
end
end
can_dig = nil,
on_punch = redef_wrapper(minetest, 'node_punch'), -- minetest.node_punch
+ on_rightclick = nil,
on_dig = redef_wrapper(minetest, 'node_dig'), -- minetest.node_dig
on_receive_fields = nil,
tool_capabilities = nil,
-- Interaction callbacks
- on_place = nil,
+ on_place = redef_wrapper(minetest, 'item_place'),
on_drop = nil,
on_use = nil,
}