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)
38 if math.abs(dir.x) > math.abs(dir.z) then
53 function minetest.dir_to_wallmounted(dir)
54 if math.abs(dir.y) > math.max(math.abs(dir.x), math.abs(dir.z)) then
60 elseif math.abs(dir.x) > math.abs(dir.z) then
75 function minetest.get_node_drops(nodename, toolname)
76 local drop = ItemStack({name=nodename}):get_definition().drop
79 return {ItemStack({name=nodename})}
80 elseif type(drop) == "string" then
82 return {ItemStack(drop)}
83 elseif drop.items == nil then
84 -- drop = {} to disable default drop
88 -- Extended drop table
92 for _, item in ipairs(drop.items) do
93 local good_rarity = true
94 local good_tool = true
95 if item.rarity ~= nil then
96 good_rarity = item.rarity < 1 or math.random(item.rarity) == 1
98 if item.tools ~= nil then
100 for _, tool in ipairs(item.tools) do
101 if tool:sub(1, 1) == '~' then
102 good_tool = toolname:find(tool:sub(2)) ~= nil
104 good_tool = toolname == tool
111 if good_rarity and good_tool then
112 got_count = got_count + 1
113 for _, add_item in ipairs(item.items) do
114 got_items[#got_items+1] = add_item
116 if drop.max_items ~= nil and got_count == drop.max_items then
124 function minetest.item_place_node(itemstack, placer, pointed_thing)
125 local item = itemstack:peek_item()
126 local def = itemstack:get_definition()
127 if def.type == "node" and pointed_thing.type == "node" then
128 local under = pointed_thing.under
129 local oldnode_under = minetest.env:get_node(under)
130 local olddef_under = ItemStack({name=oldnode_under.name}):get_definition()
131 olddef_under = olddef_under or minetest.nodedef_default
132 local above = pointed_thing.above
133 local oldnode_above = minetest.env:get_node(above)
134 local olddef_above = ItemStack({name=oldnode_above.name}):get_definition()
135 olddef_above = olddef_above or minetest.nodedef_default
137 if not olddef_above.buildable_to and not olddef_under.buildable_to then
138 minetest.log("info", placer:get_player_name() .. " tried to place"
139 .. " node in invalid position " .. minetest.pos_to_string(above)
140 .. ", replacing " .. oldnode_above.name)
144 -- Place above pointed node
145 local place_to = {x = above.x, y = above.y, z = above.z}
147 -- If node under is buildable_to, place into it instead (eg. snow)
148 if olddef_under.buildable_to then
149 minetest.log("info", "node under is buildable to")
150 place_to = {x = under.x, y = under.y, z = under.z}
153 minetest.log("action", placer:get_player_name() .. " places node "
154 .. def.name .. " at " .. minetest.pos_to_string(place_to))
156 local newnode = {name = def.name, param1 = 0, param2 = 0}
158 -- Calculate direction for wall mounted stuff like torches and signs
159 if def.paramtype2 == 'wallmounted' then
161 x = under.x - above.x,
162 y = under.y - above.y,
163 z = under.z - above.z
165 newnode.param2 = minetest.dir_to_wallmounted(dir)
166 -- Calculate the direction for furnaces and chests and stuff
167 elseif def.paramtype2 == 'facedir' then
168 local placer_pos = placer:getpos()
171 x = above.x - placer_pos.x,
172 y = above.y - placer_pos.y,
173 z = above.z - placer_pos.z
175 newnode.param2 = minetest.dir_to_facedir(dir)
176 minetest.log("action", "facedir: " .. newnode.param2)
180 -- Add node and update
181 minetest.env:add_node(place_to, newnode)
184 if def.after_place_node then
185 def.after_place_node(place_to, placer)
188 -- Run script hook (deprecated)
190 for _, callback in ipairs(minetest.registered_on_placenodes) do
191 callback(place_to, newnode, placer)
194 itemstack:take_item()
199 function minetest.item_place_object(itemstack, placer, pointed_thing)
200 local pos = minetest.get_pointed_thing_position(pointed_thing, true)
202 local item = itemstack:take_item()
203 minetest.env:add_item(pos, item)
208 function minetest.item_place(itemstack, placer, pointed_thing)
209 if itemstack:get_definition().type == "node" then
210 return minetest.item_place_node(itemstack, placer, pointed_thing)
212 return minetest.item_place_object(itemstack, placer, pointed_thing)
216 function minetest.item_drop(itemstack, dropper, pos)
217 if dropper.get_player_name then
218 local v = dropper:get_look_dir()
219 local p = {x=pos.x+v.x, y=pos.y+1.5+v.y, z=pos.z+v.z}
220 local obj = minetest.env:add_item(p, itemstack)
226 minetest.env:add_item(pos, itemstack)
231 function minetest.item_eat(hp_change, replace_with_item)
232 return function(itemstack, user, pointed_thing) -- closure
233 if itemstack:take_item() ~= nil then
234 user:set_hp(user:get_hp() + hp_change)
235 itemstack:add_item(replace_with_item) -- note: replace_with_item is optional
241 function minetest.node_punch(pos, node, puncher)
244 for _, callback in ipairs(minetest.registered_on_punchnodes) do
245 callback(pos, node, puncher)
250 function minetest.node_dig(pos, node, digger)
251 minetest.debug("node_dig")
253 local def = ItemStack({name=node.name}):get_definition()
254 if not def.diggable or (def.can_dig and not def.can_dig(pos,digger)) then
255 minetest.debug("not diggable")
256 minetest.log("info", digger:get_player_name() .. " tried to dig "
257 .. node.name .. " which is not diggable "
258 .. minetest.pos_to_string(pos))
262 minetest.log('action', digger:get_player_name() .. " digs "
263 .. node.name .. " at " .. minetest.pos_to_string(pos))
265 if not minetest.setting_getbool("creative_mode") then
266 local wielded = digger:get_wielded_item()
267 local drops = minetest.get_node_drops(node.name, wielded:get_name())
270 tp = wielded:get_tool_capabilities()
271 dp = minetest.get_dig_params(def.groups, tp)
272 wielded:add_wear(dp.wear)
273 digger:set_wielded_item(wielded)
275 -- Add dropped items to object's inventory
276 if digger:get_inventory() then
277 local _, dropped_item
278 for _, dropped_item in ipairs(drops) do
279 digger:get_inventory():add_item("main", dropped_item)
285 local oldmetadata = nil
286 if def.after_dig_node then
288 oldmetadata = minetest.env:get_meta(pos):to_table()
291 -- Remove node and update
292 minetest.env:remove_node(pos)
295 if def.after_dig_node then
296 def.after_dig_node(pos, oldnode, oldmetadata, digger)
299 -- Run script hook (deprecated)
301 for _, callback in ipairs(minetest.registered_on_dignodes) do
302 callback(pos, node, digger)
306 function minetest.node_metadata_inventory_move_allow_all(pos, from_list,
307 from_index, to_list, to_index, count, player)
308 minetest.log("verbose", "node_metadata_inventory_move_allow_all")
309 local meta = minetest.env:get_meta(pos)
310 local inv = meta:get_inventory()
312 local from_stack = inv:get_stack(from_list, from_index)
313 local taken_items = from_stack:take_item(count)
314 inv:set_stack(from_list, from_index, from_stack)
316 local to_stack = inv:get_stack(to_list, to_index)
317 to_stack:add_item(taken_items)
318 inv:set_stack(to_list, to_index, to_stack)
321 function minetest.node_metadata_inventory_offer_allow_all(pos, listname, index, stack, player)
322 minetest.log("verbose", "node_metadata_inventory_offer_allow_all")
323 local meta = minetest.env:get_meta(pos)
324 local inv = meta:get_inventory()
325 local the_stack = inv:get_stack(listname, index)
326 the_stack:add_item(stack)
327 inv:set_stack(listname, index, the_stack)
331 function minetest.node_metadata_inventory_take_allow_all(pos, listname, index, count, player)
332 minetest.log("verbose", "node_metadata_inventory_take_allow_all")
333 local meta = minetest.env:get_meta(pos)
334 local inv = meta:get_inventory()
335 local the_stack = inv:get_stack(listname, index)
336 local taken_items = the_stack:take_item(count)
337 inv:set_stack(listname, index, the_stack)
341 -- This is used to allow mods to redefine minetest.item_place and so on
342 -- NOTE: This is not the preferred way. Preferred way is to provide enough
343 -- callbacks to not require redefining global functions. -celeron55
344 local function redef_wrapper(table, name)
346 return table[name](...)
351 -- Item definition defaults
354 minetest.nodedef_default = {
357 -- name intentionally not defined here
360 inventory_image = "",
362 wield_scale = {x=1,y=1,z=1},
365 liquids_pointable = false,
366 tool_capabilities = nil,
367 node_placement_prediction = nil,
369 -- Interaction callbacks
370 on_place = redef_wrapper(minetest, 'item_place'), -- minetest.item_place
371 on_drop = redef_wrapper(minetest, 'item_drop'), -- minetest.item_drop
375 on_punch = redef_wrapper(minetest, 'node_punch'), -- minetest.node_punch
376 on_dig = redef_wrapper(minetest, 'node_dig'), -- minetest.node_dig
378 on_receive_fields = nil,
380 on_metadata_inventory_move = minetest.node_metadata_inventory_move_allow_all,
381 on_metadata_inventory_offer = minetest.node_metadata_inventory_offer_allow_all,
382 on_metadata_inventory_take = minetest.node_metadata_inventory_take_allow_all,
387 -- Don't define these because otherwise the old tile_images and
388 -- special_materials wouldn't be read
391 -- {name="", backface_culling=true},
392 -- {name="", backface_culling=true},
395 post_effect_color = {a=0, r=0, g=0, b=0},
398 is_ground_content = false,
399 sunlight_propagates = false,
404 buildable_to = false,
406 liquid_alternative_flowing = "",
407 liquid_alternative_source = "",
408 liquid_viscosity = 0,
410 damage_per_second = 0,
411 selection_box = {type="regular"},
412 legacy_facedir_simple = false,
413 legacy_wallmounted = false,
416 minetest.craftitemdef_default = {
418 -- name intentionally not defined here
421 inventory_image = "",
423 wield_scale = {x=1,y=1,z=1},
425 liquids_pointable = false,
426 tool_capabilities = nil,
428 -- Interaction callbacks
429 on_place = redef_wrapper(minetest, 'item_place'), -- minetest.item_place
430 on_drop = redef_wrapper(minetest, 'item_drop'), -- minetest.item_drop
434 minetest.tooldef_default = {
436 -- name intentionally not defined here
439 inventory_image = "",
441 wield_scale = {x=1,y=1,z=1},
443 liquids_pointable = false,
444 tool_capabilities = nil,
446 -- Interaction callbacks
447 on_place = redef_wrapper(minetest, 'item_place'), -- minetest.item_place
448 on_drop = redef_wrapper(minetest, 'item_drop'), -- minetest.item_drop
452 minetest.noneitemdef_default = { -- This is used for the hand and unknown items
454 -- name intentionally not defined here
457 inventory_image = "",
459 wield_scale = {x=1,y=1,z=1},
461 liquids_pointable = false,
462 tool_capabilities = nil,
464 -- Interaction callbacks