1 -- Minetest: builtin/item.lua
4 -- Item definition helpers
7 function minetest.inventorycube(img1, img2, img3)
10 return "[inventorycube"
11 .. "{" .. img1:gsub("%^", "&")
12 .. "{" .. img2:gsub("%^", "&")
13 .. "{" .. img3:gsub("%^", "&")
16 function minetest.get_pointed_thing_position(pointed_thing, above)
17 if pointed_thing.type == "node" then
19 -- The position where a node would be placed
20 return pointed_thing.above
22 -- The position where a node would be dug
23 return pointed_thing.under
25 elseif pointed_thing.type == "object" then
26 obj = pointed_thing.ref
37 function minetest.dir_to_facedir(dir, is6d)
38 --account for y if requested
39 if is6d and math.abs(dir.y) > math.abs(dir.x) and math.abs(dir.y) > math.abs(dir.z) then
43 if math.abs(dir.x) > math.abs(dir.z) then
59 if math.abs(dir.x) > math.abs(dir.z) then
74 --otherwise, place horizontally
75 elseif math.abs(dir.x) > math.abs(dir.z) then
90 function minetest.facedir_to_dir(facedir)
91 --a table of possible dirs
92 return ({{x=0, y=0, z=1},
99 --indexed into by a table of correlating facedirs
107 --indexed into by the facedir in question
111 function minetest.dir_to_wallmounted(dir)
112 if math.abs(dir.y) > math.max(math.abs(dir.x), math.abs(dir.z)) then
118 elseif math.abs(dir.x) > math.abs(dir.z) then
133 function minetest.get_node_drops(nodename, toolname)
134 local drop = ItemStack({name=nodename}):get_definition().drop
138 elseif type(drop) == "string" then
141 elseif drop.items == nil then
142 -- drop = {} to disable default drop
146 -- Extended drop table
150 for _, item in ipairs(drop.items) do
151 local good_rarity = true
152 local good_tool = true
153 if item.rarity ~= nil then
154 good_rarity = item.rarity < 1 or math.random(item.rarity) == 1
156 if item.tools ~= nil then
158 for _, tool in ipairs(item.tools) do
159 if tool:sub(1, 1) == '~' then
160 good_tool = toolname:find(tool:sub(2)) ~= nil
162 good_tool = toolname == tool
169 if good_rarity and good_tool then
170 got_count = got_count + 1
171 for _, add_item in ipairs(item.items) do
172 got_items[#got_items+1] = add_item
174 if drop.max_items ~= nil and got_count == drop.max_items then
182 function minetest.item_place_node(itemstack, placer, pointed_thing, param2)
183 local item = itemstack:peek_item()
184 local def = itemstack:get_definition()
185 if def.type ~= "node" or pointed_thing.type ~= "node" then
186 return itemstack, false
189 local under = pointed_thing.under
190 local oldnode_under = minetest.get_node_or_nil(under)
191 local above = pointed_thing.above
192 local oldnode_above = minetest.get_node_or_nil(above)
194 if not oldnode_under or not oldnode_above then
195 minetest.log("info", placer:get_player_name() .. " tried to place"
196 .. " node in unloaded position " .. minetest.pos_to_string(above))
197 return itemstack, false
200 local olddef_under = ItemStack({name=oldnode_under.name}):get_definition()
201 olddef_under = olddef_under or minetest.nodedef_default
202 local olddef_above = ItemStack({name=oldnode_above.name}):get_definition()
203 olddef_above = olddef_above or minetest.nodedef_default
205 if not olddef_above.buildable_to and not olddef_under.buildable_to then
206 minetest.log("info", placer:get_player_name() .. " tried to place"
207 .. " node in invalid position " .. minetest.pos_to_string(above)
208 .. ", replacing " .. oldnode_above.name)
209 return itemstack, false
212 -- Place above pointed node
213 local place_to = {x = above.x, y = above.y, z = above.z}
215 -- If node under is buildable_to, place into it instead (eg. snow)
216 if olddef_under.buildable_to then
217 minetest.log("info", "node under is buildable to")
218 place_to = {x = under.x, y = under.y, z = under.z}
221 if minetest.is_protected(place_to, placer:get_player_name()) then
222 minetest.log("action", placer:get_player_name()
223 .. " tried to place " .. def.name
224 .. " at protected position "
225 .. minetest.pos_to_string(place_to))
226 minetest.record_protection_violation(place_to, placer:get_player_name())
230 minetest.log("action", placer:get_player_name() .. " places node "
231 .. def.name .. " at " .. minetest.pos_to_string(place_to))
233 local oldnode = minetest.get_node(place_to)
234 local newnode = {name = def.name, param1 = 0, param2 = param2}
236 -- Calculate direction for wall mounted stuff like torches and signs
237 if def.paramtype2 == 'wallmounted' and not param2 then
239 x = under.x - above.x,
240 y = under.y - above.y,
241 z = under.z - above.z
243 newnode.param2 = minetest.dir_to_wallmounted(dir)
244 -- Calculate the direction for furnaces and chests and stuff
245 elseif def.paramtype2 == 'facedir' and not param2 then
246 local placer_pos = placer:getpos()
249 x = above.x - placer_pos.x,
250 y = above.y - placer_pos.y,
251 z = above.z - placer_pos.z
253 newnode.param2 = minetest.dir_to_facedir(dir)
254 minetest.log("action", "facedir: " .. newnode.param2)
258 -- Check if the node is attached and if it can be placed there
259 if minetest.get_item_group(def.name, "attached_node") ~= 0 and
260 not check_attached_node(place_to, newnode) then
261 minetest.log("action", "attached node " .. def.name ..
262 " can not be placed at " .. minetest.pos_to_string(place_to))
263 return itemstack, false
266 -- Add node and update
267 minetest.add_node(place_to, newnode)
269 local take_item = true
272 if def.after_place_node then
273 -- Copy place_to because callback can modify it
274 local place_to_copy = {x=place_to.x, y=place_to.y, z=place_to.z}
275 if def.after_place_node(place_to_copy, placer, itemstack) then
282 for _, callback in ipairs(minetest.registered_on_placenodes) do
283 -- Copy pos and node because callback can modify them
284 local place_to_copy = {x=place_to.x, y=place_to.y, z=place_to.z}
285 local newnode_copy = {name=newnode.name, param1=newnode.param1, param2=newnode.param2}
286 local oldnode_copy = {name=oldnode.name, param1=oldnode.param1, param2=oldnode.param2}
287 if callback(place_to_copy, newnode_copy, placer, oldnode_copy, itemstack) then
293 itemstack:take_item()
295 return itemstack, true
298 function minetest.item_place_object(itemstack, placer, pointed_thing)
299 local pos = minetest.get_pointed_thing_position(pointed_thing, true)
301 local item = itemstack:take_item()
302 minetest.add_item(pos, item)
307 function minetest.item_place(itemstack, placer, pointed_thing, param2)
308 -- Call on_rightclick if the pointed node defines it
309 if pointed_thing.type == "node" and placer and
310 not placer:get_player_control().sneak then
311 local n = minetest.get_node(pointed_thing.under)
313 if minetest.registered_nodes[nn] and minetest.registered_nodes[nn].on_rightclick then
314 return minetest.registered_nodes[nn].on_rightclick(pointed_thing.under, n, placer, itemstack) or itemstack, false
318 if itemstack:get_definition().type == "node" then
319 return minetest.item_place_node(itemstack, placer, pointed_thing, param2)
324 function minetest.item_drop(itemstack, dropper, pos)
325 if dropper.get_player_name then
326 local v = dropper:get_look_dir()
327 local p = {x=pos.x+v.x, y=pos.y+1.5+v.y, z=pos.z+v.z}
328 local obj = minetest.add_item(p, itemstack)
336 minetest.add_item(pos, itemstack)
341 function minetest.item_eat(hp_change, replace_with_item)
342 return function(itemstack, user, pointed_thing) -- closure
343 if itemstack:take_item() ~= nil then
344 user:set_hp(user:get_hp() + hp_change)
345 itemstack:add_item(replace_with_item) -- note: replace_with_item is optional
351 function minetest.node_punch(pos, node, puncher)
354 for _, callback in ipairs(minetest.registered_on_punchnodes) do
355 -- Copy pos and node because callback can modify them
356 local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
357 local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
358 callback(pos_copy, node_copy, puncher)
362 function minetest.handle_node_drops(pos, drops, digger)
363 -- Add dropped items to object's inventory
364 if digger:get_inventory() then
365 local _, dropped_item
366 for _, dropped_item in ipairs(drops) do
367 local left = digger:get_inventory():add_item("main", dropped_item)
368 if not left:is_empty() then
370 x = pos.x + math.random()/2-0.25,
371 y = pos.y + math.random()/2-0.25,
372 z = pos.z + math.random()/2-0.25,
374 minetest.add_item(p, left)
380 function minetest.node_dig(pos, node, digger)
381 local def = ItemStack({name=node.name}):get_definition()
382 if not def.diggable or (def.can_dig and not def.can_dig(pos,digger)) then
383 minetest.log("info", digger:get_player_name() .. " tried to dig "
384 .. node.name .. " which is not diggable "
385 .. minetest.pos_to_string(pos))
389 if minetest.is_protected(pos, digger:get_player_name()) then
390 minetest.log("action", digger:get_player_name()
391 .. " tried to dig " .. node.name
392 .. " at protected position "
393 .. minetest.pos_to_string(pos))
394 minetest.record_protection_violation(pos, digger:get_player_name())
398 minetest.log('action', digger:get_player_name() .. " digs "
399 .. node.name .. " at " .. minetest.pos_to_string(pos))
401 local wielded = digger:get_wielded_item()
402 local drops = minetest.get_node_drops(node.name, wielded:get_name())
404 local wdef = wielded:get_definition()
405 local tp = wielded:get_tool_capabilities()
406 local dp = minetest.get_dig_params(def.groups, tp)
407 if wdef and wdef.after_use then
408 wielded = wdef.after_use(wielded, digger, node, dp) or wielded
411 if not minetest.setting_getbool("creative_mode") then
412 wielded:add_wear(dp.wear)
415 digger:set_wielded_item(wielded)
418 minetest.handle_node_drops(pos, drops, digger)
420 local oldmetadata = nil
421 if def.after_dig_node then
422 oldmetadata = minetest.get_meta(pos):to_table()
425 -- Remove node and update
426 minetest.remove_node(pos)
429 if def.after_dig_node then
430 -- Copy pos and node because callback can modify them
431 local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
432 local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
433 def.after_dig_node(pos_copy, node_copy, oldmetadata, digger)
438 for _, callback in ipairs(minetest.registered_on_dignodes) do
439 -- Copy pos and node because callback can modify them
440 local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
441 local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
442 callback(pos_copy, node_copy, digger)
446 -- This is used to allow mods to redefine minetest.item_place and so on
447 -- NOTE: This is not the preferred way. Preferred way is to provide enough
448 -- callbacks to not require redefining global functions. -celeron55
449 local function redef_wrapper(table, name)
451 return table[name](...)
456 -- Item definition defaults
459 minetest.nodedef_default = {
462 -- name intentionally not defined here
465 inventory_image = "",
467 wield_scale = {x=1,y=1,z=1},
470 liquids_pointable = false,
471 tool_capabilities = nil,
472 node_placement_prediction = nil,
474 -- Interaction callbacks
475 on_place = redef_wrapper(minetest, 'item_place'), -- minetest.item_place
476 on_drop = redef_wrapper(minetest, 'item_drop'), -- minetest.item_drop
480 on_punch = redef_wrapper(minetest, 'node_punch'), -- minetest.node_punch
482 on_dig = redef_wrapper(minetest, 'node_dig'), -- minetest.node_dig
484 on_receive_fields = nil,
486 on_metadata_inventory_move = minetest.node_metadata_inventory_move_allow_all,
487 on_metadata_inventory_offer = minetest.node_metadata_inventory_offer_allow_all,
488 on_metadata_inventory_take = minetest.node_metadata_inventory_take_allow_all,
493 -- Don't define these because otherwise the old tile_images and
494 -- special_materials wouldn't be read
497 -- {name="", backface_culling=true},
498 -- {name="", backface_culling=true},
501 post_effect_color = {a=0, r=0, g=0, b=0},
504 is_ground_content = true,
505 sunlight_propagates = false,
510 buildable_to = false,
512 liquid_alternative_flowing = "",
513 liquid_alternative_source = "",
514 liquid_viscosity = 0,
517 damage_per_second = 0,
518 selection_box = {type="regular"},
519 legacy_facedir_simple = false,
520 legacy_wallmounted = false,
523 minetest.craftitemdef_default = {
525 -- name intentionally not defined here
528 inventory_image = "",
530 wield_scale = {x=1,y=1,z=1},
532 liquids_pointable = false,
533 tool_capabilities = nil,
535 -- Interaction callbacks
536 on_place = redef_wrapper(minetest, 'item_place'), -- minetest.item_place
537 on_drop = redef_wrapper(minetest, 'item_drop'), -- minetest.item_drop
541 minetest.tooldef_default = {
543 -- name intentionally not defined here
546 inventory_image = "",
548 wield_scale = {x=1,y=1,z=1},
550 liquids_pointable = false,
551 tool_capabilities = nil,
553 -- Interaction callbacks
554 on_place = redef_wrapper(minetest, 'item_place'), -- minetest.item_place
555 on_drop = redef_wrapper(minetest, 'item_drop'), -- minetest.item_drop
559 minetest.noneitemdef_default = { -- This is used for the hand and unknown items
561 -- name intentionally not defined here
564 inventory_image = "",
566 wield_scale = {x=1,y=1,z=1},
568 liquids_pointable = false,
569 tool_capabilities = nil,
571 -- Interaction callbacks
572 on_place = redef_wrapper(minetest, 'item_place'),