1 -- Minetest: builtin/item.lua
3 local function copy_pointed_thing(pointed_thing)
5 type = pointed_thing.type,
6 above = vector.new(pointed_thing.above),
7 under = vector.new(pointed_thing.under),
8 ref = pointed_thing.ref,
13 -- Item definition helpers
16 function core.inventorycube(img1, img2, img3)
19 return "[inventorycube"
20 .. "{" .. img1:gsub("%^", "&")
21 .. "{" .. img2:gsub("%^", "&")
22 .. "{" .. img3:gsub("%^", "&")
25 function core.get_pointed_thing_position(pointed_thing, above)
26 if pointed_thing.type == "node" then
28 -- The position where a node would be placed
29 return pointed_thing.above
31 -- The position where a node would be dug
32 return pointed_thing.under
33 elseif pointed_thing.type == "object" then
34 return pointed_thing.ref and pointed_thing.ref:getpos()
38 function core.dir_to_facedir(dir, is6d)
39 --account for y if requested
40 if is6d and math.abs(dir.y) > math.abs(dir.x) and math.abs(dir.y) > math.abs(dir.z) then
44 if math.abs(dir.x) > math.abs(dir.z) then
60 if math.abs(dir.x) > math.abs(dir.z) then
75 --otherwise, place horizontally
76 elseif math.abs(dir.x) > math.abs(dir.z) then
91 -- Table of possible dirs
92 local facedir_to_dir = {
100 -- Mapping from facedir value to index in facedir_to_dir.
101 local facedir_to_dir_map = {
109 function core.facedir_to_dir(facedir)
110 return facedir_to_dir[facedir_to_dir_map[facedir]]
113 function core.dir_to_wallmounted(dir)
114 if math.abs(dir.y) > math.max(math.abs(dir.x), math.abs(dir.z)) then
120 elseif math.abs(dir.x) > math.abs(dir.z) then
135 -- table of dirs in wallmounted order
136 local wallmounted_to_dir = {
137 [0] = {x = 0, y = 1, z = 0},
138 {x = 0, y = -1, z = 0},
139 {x = 1, y = 0, z = 0},
140 {x = -1, y = 0, z = 0},
141 {x = 0, y = 0, z = 1},
142 {x = 0, y = 0, z = -1},
144 function core.wallmounted_to_dir(wallmounted)
145 return wallmounted_to_dir[wallmounted]
148 function core.get_node_drops(nodename, toolname)
149 local drop = ItemStack({name=nodename}):get_definition().drop
153 elseif type(drop) == "string" then
156 elseif drop.items == nil then
157 -- drop = {} to disable default drop
161 -- Extended drop table
165 for _, item in ipairs(drop.items) do
166 local good_rarity = true
167 local good_tool = true
168 if item.rarity ~= nil then
169 good_rarity = item.rarity < 1 or math.random(item.rarity) == 1
171 if item.tools ~= nil then
173 for _, tool in ipairs(item.tools) do
174 if tool:sub(1, 1) == '~' then
175 good_tool = toolname:find(tool:sub(2)) ~= nil
177 good_tool = toolname == tool
184 if good_rarity and good_tool then
185 got_count = got_count + 1
186 for _, add_item in ipairs(item.items) do
187 got_items[#got_items+1] = add_item
189 if drop.max_items ~= nil and got_count == drop.max_items then
197 function core.item_place_node(itemstack, placer, pointed_thing, param2)
198 local item = itemstack:peek_item()
199 local def = itemstack:get_definition()
200 if def.type ~= "node" or pointed_thing.type ~= "node" then
201 return itemstack, false
204 local under = pointed_thing.under
205 local oldnode_under = core.get_node_or_nil(under)
206 local above = pointed_thing.above
207 local oldnode_above = core.get_node_or_nil(above)
209 if not oldnode_under or not oldnode_above then
210 core.log("info", placer:get_player_name() .. " tried to place"
211 .. " node in unloaded position " .. core.pos_to_string(above))
212 return itemstack, false
215 local olddef_under = ItemStack({name=oldnode_under.name}):get_definition()
216 olddef_under = olddef_under or core.nodedef_default
217 local olddef_above = ItemStack({name=oldnode_above.name}):get_definition()
218 olddef_above = olddef_above or core.nodedef_default
220 if not olddef_above.buildable_to and not olddef_under.buildable_to then
221 core.log("info", placer:get_player_name() .. " tried to place"
222 .. " node in invalid position " .. core.pos_to_string(above)
223 .. ", replacing " .. oldnode_above.name)
224 return itemstack, false
227 -- Place above pointed node
228 local place_to = {x = above.x, y = above.y, z = above.z}
230 -- If node under is buildable_to, place into it instead (eg. snow)
231 if olddef_under.buildable_to then
232 core.log("info", "node under is buildable to")
233 place_to = {x = under.x, y = under.y, z = under.z}
236 if core.is_protected(place_to, placer:get_player_name()) and
237 not minetest.check_player_privs(placer, "protection_bypass") then
238 core.log("action", placer:get_player_name()
239 .. " tried to place " .. def.name
240 .. " at protected position "
241 .. core.pos_to_string(place_to))
242 core.record_protection_violation(place_to, placer:get_player_name())
246 core.log("action", placer:get_player_name() .. " places node "
247 .. def.name .. " at " .. core.pos_to_string(place_to))
249 local oldnode = core.get_node(place_to)
250 local newnode = {name = def.name, param1 = 0, param2 = param2}
252 -- Calculate direction for wall mounted stuff like torches and signs
253 if def.paramtype2 == 'wallmounted' and not param2 then
255 x = under.x - above.x,
256 y = under.y - above.y,
257 z = under.z - above.z
259 newnode.param2 = core.dir_to_wallmounted(dir)
260 -- Calculate the direction for furnaces and chests and stuff
261 elseif def.paramtype2 == 'facedir' and not param2 then
262 local placer_pos = placer:getpos()
265 x = above.x - placer_pos.x,
266 y = above.y - placer_pos.y,
267 z = above.z - placer_pos.z
269 newnode.param2 = core.dir_to_facedir(dir)
270 core.log("action", "facedir: " .. newnode.param2)
274 -- Check if the node is attached and if it can be placed there
275 if core.get_item_group(def.name, "attached_node") ~= 0 and
276 not check_attached_node(place_to, newnode) then
277 core.log("action", "attached node " .. def.name ..
278 " can not be placed at " .. core.pos_to_string(place_to))
279 return itemstack, false
282 -- Add node and update
283 core.add_node(place_to, newnode)
285 local take_item = true
288 if def.after_place_node then
289 -- Deepcopy place_to and pointed_thing because callback can modify it
290 local place_to_copy = {x=place_to.x, y=place_to.y, z=place_to.z}
291 local pointed_thing_copy = copy_pointed_thing(pointed_thing)
292 if def.after_place_node(place_to_copy, placer, itemstack,
293 pointed_thing_copy) then
300 for _, callback in ipairs(core.registered_on_placenodes) do
301 -- Deepcopy pos, node and pointed_thing because callback can modify them
302 local place_to_copy = {x=place_to.x, y=place_to.y, z=place_to.z}
303 local newnode_copy = {name=newnode.name, param1=newnode.param1, param2=newnode.param2}
304 local oldnode_copy = {name=oldnode.name, param1=oldnode.param1, param2=oldnode.param2}
305 local pointed_thing_copy = copy_pointed_thing(pointed_thing)
306 if callback(place_to_copy, newnode_copy, placer, oldnode_copy, itemstack, pointed_thing_copy) then
312 itemstack:take_item()
314 return itemstack, true
317 function core.item_place_object(itemstack, placer, pointed_thing)
318 local pos = core.get_pointed_thing_position(pointed_thing, true)
320 local item = itemstack:take_item()
321 core.add_item(pos, item)
326 function core.item_place(itemstack, placer, pointed_thing, param2)
327 -- Call on_rightclick if the pointed node defines it
328 if pointed_thing.type == "node" and placer and
329 not placer:get_player_control().sneak then
330 local n = core.get_node(pointed_thing.under)
332 if core.registered_nodes[nn] and core.registered_nodes[nn].on_rightclick then
333 return core.registered_nodes[nn].on_rightclick(pointed_thing.under, n,
334 placer, itemstack, pointed_thing) or itemstack, false
338 if itemstack:get_definition().type == "node" then
339 return core.item_place_node(itemstack, placer, pointed_thing, param2)
344 function core.item_secondary_use(itemstack, placer)
348 function core.item_drop(itemstack, dropper, pos)
349 if dropper and dropper:is_player() then
350 local v = dropper:get_look_dir()
351 local p = {x=pos.x, y=pos.y+1.2, z=pos.z}
352 local cs = itemstack:get_count()
353 if dropper:get_player_control().sneak then
356 local item = itemstack:take_item(cs)
357 local obj = core.add_item(p, item)
363 obj:get_luaentity().dropped_by = dropper:get_player_name()
368 if core.add_item(pos, itemstack) then
372 -- If we reach this, adding the object to the
373 -- environment failed
376 function core.do_item_eat(hp_change, replace_with_item, itemstack, user, pointed_thing)
377 for _, callback in pairs(core.registered_on_item_eats) do
378 local result = callback(hp_change, replace_with_item, itemstack, user, pointed_thing)
383 if itemstack:take_item() ~= nil then
384 user:set_hp(user:get_hp() + hp_change)
386 if replace_with_item then
387 if itemstack:is_empty() then
388 itemstack:add_item(replace_with_item)
390 local inv = user:get_inventory()
391 if inv:room_for_item("main", {name=replace_with_item}) then
392 inv:add_item("main", replace_with_item)
394 local pos = user:getpos()
395 pos.y = math.floor(pos.y + 0.5)
396 core.add_item(pos, replace_with_item)
404 function core.item_eat(hp_change, replace_with_item)
405 return function(itemstack, user, pointed_thing) -- closure
406 return core.do_item_eat(hp_change, replace_with_item, itemstack, user, pointed_thing)
410 function core.node_punch(pos, node, puncher, pointed_thing)
412 for _, callback in ipairs(core.registered_on_punchnodes) do
413 -- Copy pos and node because callback can modify them
414 local pos_copy = vector.new(pos)
415 local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
416 local pointed_thing_copy = pointed_thing and copy_pointed_thing(pointed_thing) or nil
417 callback(pos_copy, node_copy, puncher, pointed_thing_copy)
421 function core.handle_node_drops(pos, drops, digger)
422 -- Add dropped items to object's inventory
423 if digger:get_inventory() then
424 local _, dropped_item
425 for _, dropped_item in ipairs(drops) do
426 local left = digger:get_inventory():add_item("main", dropped_item)
427 if not left:is_empty() then
429 x = pos.x + math.random()/2-0.25,
430 y = pos.y + math.random()/2-0.25,
431 z = pos.z + math.random()/2-0.25,
433 core.add_item(p, left)
439 function core.node_dig(pos, node, digger)
440 local def = ItemStack({name=node.name}):get_definition()
441 if not def.diggable or (def.can_dig and not def.can_dig(pos,digger)) then
442 core.log("info", digger:get_player_name() .. " tried to dig "
443 .. node.name .. " which is not diggable "
444 .. core.pos_to_string(pos))
448 if core.is_protected(pos, digger:get_player_name()) and
449 not minetest.check_player_privs(digger, "protection_bypass") then
450 core.log("action", digger:get_player_name()
451 .. " tried to dig " .. node.name
452 .. " at protected position "
453 .. core.pos_to_string(pos))
454 core.record_protection_violation(pos, digger:get_player_name())
458 core.log('action', digger:get_player_name() .. " digs "
459 .. node.name .. " at " .. core.pos_to_string(pos))
461 local wielded = digger:get_wielded_item()
462 local drops = core.get_node_drops(node.name, wielded:get_name())
464 local wdef = wielded:get_definition()
465 local tp = wielded:get_tool_capabilities()
466 local dp = core.get_dig_params(def.groups, tp)
467 if wdef and wdef.after_use then
468 wielded = wdef.after_use(wielded, digger, node, dp) or wielded
471 if not core.setting_getbool("creative_mode") then
472 wielded:add_wear(dp.wear)
475 digger:set_wielded_item(wielded)
478 core.handle_node_drops(pos, drops, digger)
480 local oldmetadata = nil
481 if def.after_dig_node then
482 oldmetadata = core.get_meta(pos):to_table()
485 -- Remove node and update
486 core.remove_node(pos)
489 if def.after_dig_node then
490 -- Copy pos and node because callback can modify them
491 local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
492 local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
493 def.after_dig_node(pos_copy, node_copy, oldmetadata, digger)
498 for _, callback in ipairs(core.registered_on_dignodes) do
499 local origin = core.callback_origins[callback]
501 core.set_last_run_mod(origin.mod)
502 --print("Running " .. tostring(callback) ..
503 -- " (a " .. origin.name .. " callback in " .. origin.mod .. ")")
505 --print("No data associated with callback")
508 -- Copy pos and node because callback can modify them
509 local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
510 local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
511 callback(pos_copy, node_copy, digger)
515 -- This is used to allow mods to redefine core.item_place and so on
516 -- NOTE: This is not the preferred way. Preferred way is to provide enough
517 -- callbacks to not require redefining global functions. -celeron55
518 local function redef_wrapper(table, name)
520 return table[name](...)
525 -- Item definition defaults
528 core.nodedef_default = {
531 -- name intentionally not defined here
534 inventory_image = "",
536 wield_scale = {x=1,y=1,z=1},
539 liquids_pointable = false,
540 tool_capabilities = nil,
541 node_placement_prediction = nil,
543 -- Interaction callbacks
544 on_place = redef_wrapper(core, 'item_place'), -- core.item_place
545 on_drop = redef_wrapper(core, 'item_drop'), -- core.item_drop
549 on_punch = redef_wrapper(core, 'node_punch'), -- core.node_punch
551 on_dig = redef_wrapper(core, 'node_dig'), -- core.node_dig
553 on_receive_fields = nil,
555 on_metadata_inventory_move = core.node_metadata_inventory_move_allow_all,
556 on_metadata_inventory_offer = core.node_metadata_inventory_offer_allow_all,
557 on_metadata_inventory_take = core.node_metadata_inventory_take_allow_all,
562 -- Don't define these because otherwise the old tile_images and
563 -- special_materials wouldn't be read
566 -- {name="", backface_culling=true},
567 -- {name="", backface_culling=true},
570 post_effect_color = {a=0, r=0, g=0, b=0},
573 is_ground_content = true,
574 sunlight_propagates = false,
579 buildable_to = false,
582 liquid_alternative_flowing = "",
583 liquid_alternative_source = "",
584 liquid_viscosity = 0,
587 damage_per_second = 0,
588 selection_box = {type="regular"},
589 legacy_facedir_simple = false,
590 legacy_wallmounted = false,
593 core.craftitemdef_default = {
595 -- name intentionally not defined here
598 inventory_image = "",
600 wield_scale = {x=1,y=1,z=1},
602 liquids_pointable = false,
603 tool_capabilities = nil,
605 -- Interaction callbacks
606 on_place = redef_wrapper(core, 'item_place'), -- core.item_place
607 on_drop = redef_wrapper(core, 'item_drop'), -- core.item_drop
608 on_secondary_use = redef_wrapper(core, 'item_secondary_use'),
612 core.tooldef_default = {
614 -- name intentionally not defined here
617 inventory_image = "",
619 wield_scale = {x=1,y=1,z=1},
621 liquids_pointable = false,
622 tool_capabilities = nil,
624 -- Interaction callbacks
625 on_place = redef_wrapper(core, 'item_place'), -- core.item_place
626 on_secondary_use = redef_wrapper(core, 'item_secondary_use'),
627 on_drop = redef_wrapper(core, 'item_drop'), -- core.item_drop
631 core.noneitemdef_default = { -- This is used for the hand and unknown items
633 -- name intentionally not defined here
636 inventory_image = "",
638 wield_scale = {x=1,y=1,z=1},
640 liquids_pointable = false,
641 tool_capabilities = nil,
643 -- Interaction callbacks
644 on_place = redef_wrapper(core, 'item_place'),
645 on_secondary_use = redef_wrapper(core, 'item_secondary_use'),