local drop = ItemStack({name=nodename}):get_definition().drop
if drop == nil then
-- default drop
- return {ItemStack({name=nodename})}
+ return {nodename}
elseif type(drop) == "string" then
-- itemstring drop
- return {ItemStack(drop)}
+ return {drop}
elseif drop.items == nil then
-- drop = {} to disable default drop
return {}
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
end
end
+ -- Check if the node is attached and if it can be placed there
+ if minetest.get_item_group(def.name, "attached_node") ~= 0 and
+ 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 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)
local v = dropper:get_look_dir()
local p = {x=pos.x+v.x, y=pos.y+1.5+v.y, z=pos.z+v.z}
local obj = minetest.env:add_item(p, itemstack)
- v.x = v.x*2
- v.y = v.y*2 + 1
- v.z = v.z*2
- obj:setvelocity(v)
+ if obj then
+ v.x = v.x*2
+ v.y = v.y*2 + 1
+ v.z = v.z*2
+ obj:setvelocity(v)
+ end
else
minetest.env:add_item(pos, itemstack)
end
- return ""
+ return ItemStack("")
end
function minetest.item_eat(hp_change, replace_with_item)
end
end
+function minetest.handle_node_drops(pos, drops, digger)
+ -- Add dropped items to object's inventory
+ if digger:get_inventory() then
+ local _, dropped_item
+ for _, dropped_item in ipairs(drops) do
+ 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
+
function minetest.node_dig(pos, node, digger)
minetest.debug("node_dig")
local def = ItemStack({name=node.name}):get_definition()
- if not def.diggable or (def.can_dig and not def.can_dig(pos,digger)) then
+ -- Check if def ~= 0 because we always want to be able to remove unknown nodes
+ if #def ~= 0 and not def.diggable or (def.can_dig and not def.can_dig(pos,digger)) then
minetest.debug("not diggable")
minetest.log("info", digger:get_player_name() .. " tried to dig "
.. node.name .. " which is not diggable "
minetest.log('action', digger:get_player_name() .. " digs "
.. node.name .. " at " .. minetest.pos_to_string(pos))
- if not minetest.setting_getbool("creative_mode") then
- local wielded = digger:get_wielded_item()
- local drops = minetest.get_node_drops(node.name, wielded:get_name())
-
- -- Wear out tool
- 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)
-
- -- Add dropped items to object's inventory
- if digger:get_inventory() then
- local _, dropped_item
- for _, dropped_item in ipairs(drops) do
- digger:get_inventory():add_item("main", dropped_item)
- end
- end
- end
+ local wielded = digger:get_wielded_item()
+ local drops = minetest.get_node_drops(node.name, wielded:get_name())
+
+ -- Wear out tool
+ 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)
+ -- Handle drops
+ minetest.handle_node_drops(pos, drops, digger)
+
local oldmetadata = nil
if def.after_dig_node then
oldmetadata = minetest.env:get_meta(pos):to_table()
end
end
-function minetest.node_metadata_inventory_move_allow_all(pos, from_list,
- from_index, to_list, to_index, count, player)
- minetest.log("verbose", "node_metadata_inventory_move_allow_all")
- local meta = minetest.env:get_meta(pos)
- local inv = meta:get_inventory()
-
- local from_stack = inv:get_stack(from_list, from_index)
- local taken_items = from_stack:take_item(count)
- inv:set_stack(from_list, from_index, from_stack)
-
- local to_stack = inv:get_stack(to_list, to_index)
- to_stack:add_item(taken_items)
- inv:set_stack(to_list, to_index, to_stack)
-end
-
-function minetest.node_metadata_inventory_offer_allow_all(pos, listname, index, stack, player)
- minetest.log("verbose", "node_metadata_inventory_offer_allow_all")
- local meta = minetest.env:get_meta(pos)
- local inv = meta:get_inventory()
- local the_stack = inv:get_stack(listname, index)
- the_stack:add_item(stack)
- inv:set_stack(listname, index, the_stack)
- return ItemStack("")
-end
-
-function minetest.node_metadata_inventory_take_allow_all(pos, listname, index, count, player)
- minetest.log("verbose", "node_metadata_inventory_take_allow_all")
- local meta = minetest.env:get_meta(pos)
- local inv = meta:get_inventory()
- local the_stack = inv:get_stack(listname, index)
- local taken_items = the_stack:take_item(count)
- inv:set_stack(listname, index, the_stack)
- return taken_items
-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
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,
}