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 pos = pointed_thing.above
129 local oldnode = minetest.env:get_node(pos)
130 local olddef = ItemStack({name=oldnode.name}):get_definition()
132 if not olddef.buildable_to then
133 minetest.log("info", placer:get_player_name() .. " tried to place"
134 .. " node in invalid position " .. minetest.pos_to_string(pos)
135 .. ", replacing " .. oldnode.name)
139 minetest.log("action", placer:get_player_name() .. " places node "
140 .. def.name .. " at " .. minetest.pos_to_string(pos))
142 local newnode = {name = def.name, param1 = 0, param2 = 0}
144 -- Calculate direction for wall mounted stuff like torches and signs
145 if def.paramtype2 == 'wallmounted' then
146 local under = pointed_thing.under
147 local above = pointed_thing.above
148 local dir = {x = under.x - above.x, y = under.y - above.y, z = under.z - above.z}
149 newnode.param2 = minetest.dir_to_wallmounted(dir)
150 -- Calculate the direction for furnaces and chests and stuff
151 elseif def.paramtype2 == 'facedir' then
152 local placer_pos = placer:getpos()
154 local dir = {x = pos.x - placer_pos.x, y = pos.y - placer_pos.y, z = pos.z - placer_pos.z}
155 newnode.param2 = minetest.dir_to_facedir(dir)
156 minetest.log("action", "facedir: " .. newnode.param2)
160 -- Add node and update
161 minetest.env:add_node(pos, newnode)
164 if def.after_place_node then
165 def.after_place_node(pos, placer)
168 -- Run script hook (deprecated)
170 for _, callback in ipairs(minetest.registered_on_placenodes) do
171 callback(pos, newnode, placer)
174 itemstack:take_item()
179 function minetest.item_place_object(itemstack, placer, pointed_thing)
180 local pos = minetest.get_pointed_thing_position(pointed_thing, true)
182 local item = itemstack:take_item()
183 minetest.env:add_item(pos, item)
188 function minetest.item_place(itemstack, placer, pointed_thing)
189 if itemstack:get_definition().type == "node" then
190 return minetest.item_place_node(itemstack, placer, pointed_thing)
192 return minetest.item_place_object(itemstack, placer, pointed_thing)
196 function minetest.item_drop(itemstack, dropper, pos)
197 if dropper.get_player_name then
198 local v = dropper:get_look_dir()
199 local p = {x=pos.x+v.x, y=pos.y+1.5+v.y, z=pos.z+v.z}
200 local obj = minetest.env:add_item(p, itemstack)
206 minetest.env:add_item(pos, itemstack)
211 function minetest.item_eat(hp_change, replace_with_item)
212 return function(itemstack, user, pointed_thing) -- closure
213 if itemstack:take_item() ~= nil then
214 user:set_hp(user:get_hp() + hp_change)
215 itemstack:add_item(replace_with_item) -- note: replace_with_item is optional
221 function minetest.node_punch(pos, node, puncher)
224 for _, callback in ipairs(minetest.registered_on_punchnodes) do
225 callback(pos, node, puncher)
230 function minetest.node_dig(pos, node, digger)
231 minetest.debug("node_dig")
233 local def = ItemStack({name=node.name}):get_definition()
234 if not def.diggable or (def.can_dig and not def.can_dig(pos,digger)) then
235 minetest.debug("not diggable")
236 minetest.log("info", digger:get_player_name() .. " tried to dig "
237 .. node.name .. " which is not diggable "
238 .. minetest.pos_to_string(pos))
242 minetest.log('action', digger:get_player_name() .. " digs "
243 .. node.name .. " at " .. minetest.pos_to_string(pos))
245 if not minetest.setting_getbool("creative_mode") then
246 local wielded = digger:get_wielded_item()
247 local drops = minetest.get_node_drops(node.name, wielded:get_name())
250 tp = wielded:get_tool_capabilities()
251 dp = minetest.get_dig_params(def.groups, tp)
252 wielded:add_wear(dp.wear)
253 digger:set_wielded_item(wielded)
255 -- Add dropped items to object's inventory
256 if digger:get_inventory() then
257 local _, dropped_item
258 for _, dropped_item in ipairs(drops) do
259 digger:get_inventory():add_item("main", dropped_item)
265 local oldmetadata = nil
266 if def.after_dig_node then
268 oldmetadata = minetest.env:get_meta(pos):to_table()
271 -- Remove node and update
272 minetest.env:remove_node(pos)
275 if def.after_dig_node then
276 def.after_dig_node(pos, oldnode, oldmetadata, digger)
279 -- Run script hook (deprecated)
281 for _, callback in ipairs(minetest.registered_on_dignodes) do
282 callback(pos, node, digger)
286 function minetest.node_metadata_inventory_move_allow_all(pos, from_list,
287 from_index, to_list, to_index, count, player)
288 minetest.log("verbose", "node_metadata_inventory_move_allow_all")
289 local meta = minetest.env:get_meta(pos)
290 local inv = meta:get_inventory()
292 local from_stack = inv:get_stack(from_list, from_index)
293 local taken_items = from_stack:take_item(count)
294 inv:set_stack(from_list, from_index, from_stack)
296 local to_stack = inv:get_stack(to_list, to_index)
297 to_stack:add_item(taken_items)
298 inv:set_stack(to_list, to_index, to_stack)
301 function minetest.node_metadata_inventory_offer_allow_all(pos, listname, index, stack, player)
302 minetest.log("verbose", "node_metadata_inventory_offer_allow_all")
303 local meta = minetest.env:get_meta(pos)
304 local inv = meta:get_inventory()
305 local the_stack = inv:get_stack(listname, index)
306 the_stack:add_item(stack)
307 inv:set_stack(listname, index, the_stack)
311 function minetest.node_metadata_inventory_take_allow_all(pos, listname, index, count, player)
312 minetest.log("verbose", "node_metadata_inventory_take_allow_all")
313 local meta = minetest.env:get_meta(pos)
314 local inv = meta:get_inventory()
315 local the_stack = inv:get_stack(listname, index)
316 local taken_items = the_stack:take_item(count)
317 inv:set_stack(listname, index, the_stack)
321 -- This is used to allow mods to redefine minetest.item_place and so on
322 -- NOTE: This is not the preferred way. Preferred way is to provide enough
323 -- callbacks to not require redefining global functions. -celeron55
324 local function redef_wrapper(table, name)
326 return table[name](...)
331 -- Item definition defaults
334 minetest.nodedef_default = {
337 -- name intentionally not defined here
340 inventory_image = "",
342 wield_scale = {x=1,y=1,z=1},
345 liquids_pointable = false,
346 tool_capabilities = nil,
347 node_placement_prediction = nil,
349 -- Interaction callbacks
350 on_place = redef_wrapper(minetest, 'item_place'), -- minetest.item_place
351 on_drop = redef_wrapper(minetest, 'item_drop'), -- minetest.item_drop
355 on_punch = redef_wrapper(minetest, 'node_punch'), -- minetest.node_punch
356 on_dig = redef_wrapper(minetest, 'node_dig'), -- minetest.node_dig
358 on_receive_fields = nil,
360 on_metadata_inventory_move = minetest.node_metadata_inventory_move_allow_all,
361 on_metadata_inventory_offer = minetest.node_metadata_inventory_offer_allow_all,
362 on_metadata_inventory_take = minetest.node_metadata_inventory_take_allow_all,
367 -- Don't define these because otherwise the old tile_images and
368 -- special_materials wouldn't be read
371 -- {name="", backface_culling=true},
372 -- {name="", backface_culling=true},
375 post_effect_color = {a=0, r=0, g=0, b=0},
378 is_ground_content = false,
379 sunlight_propagates = false,
384 buildable_to = false,
386 liquid_alternative_flowing = "",
387 liquid_alternative_source = "",
388 liquid_viscosity = 0,
390 damage_per_second = 0,
391 selection_box = {type="regular"},
392 legacy_facedir_simple = false,
393 legacy_wallmounted = false,
396 minetest.craftitemdef_default = {
398 -- name intentionally not defined here
401 inventory_image = "",
403 wield_scale = {x=1,y=1,z=1},
405 liquids_pointable = false,
406 tool_capabilities = nil,
408 -- Interaction callbacks
409 on_place = redef_wrapper(minetest, 'item_place'), -- minetest.item_place
410 on_drop = redef_wrapper(minetest, 'item_drop'), -- minetest.item_drop
414 minetest.tooldef_default = {
416 -- name intentionally not defined here
419 inventory_image = "",
421 wield_scale = {x=1,y=1,z=1},
423 liquids_pointable = false,
424 tool_capabilities = nil,
426 -- Interaction callbacks
427 on_place = redef_wrapper(minetest, 'item_place'), -- minetest.item_place
428 on_drop = redef_wrapper(minetest, 'item_drop'), -- minetest.item_drop
432 minetest.noneitemdef_default = { -- This is used for the hand and unknown items
434 -- name intentionally not defined here
437 inventory_image = "",
439 wield_scale = {x=1,y=1,z=1},
441 liquids_pointable = false,
442 tool_capabilities = nil,
444 -- Interaction callbacks