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" or pointed_thing.type ~= "node" then
131 local under = pointed_thing.under
132 local oldnode_under = minetest.env:get_node(under)
133 local olddef_under = ItemStack({name=oldnode_under.name}):get_definition()
134 olddef_under = olddef_under or minetest.nodedef_default
135 local above = pointed_thing.above
136 local oldnode_above = minetest.env:get_node(above)
137 local olddef_above = ItemStack({name=oldnode_above.name}):get_definition()
138 olddef_above = olddef_above or minetest.nodedef_default
140 if not olddef_above.buildable_to and not olddef_under.buildable_to then
141 minetest.log("info", placer:get_player_name() .. " tried to place"
142 .. " node in invalid position " .. minetest.pos_to_string(above)
143 .. ", replacing " .. oldnode_above.name)
147 -- Place above pointed node
148 local place_to = {x = above.x, y = above.y, z = above.z}
150 -- If node under is buildable_to, place into it instead (eg. snow)
151 if olddef_under.buildable_to then
152 minetest.log("info", "node under is buildable to")
153 place_to = {x = under.x, y = under.y, z = under.z}
156 minetest.log("action", placer:get_player_name() .. " places node "
157 .. def.name .. " at " .. minetest.pos_to_string(place_to))
159 local oldnode = minetest.env:get_node(place_to)
160 local newnode = {name = def.name, param1 = 0, param2 = 0}
162 -- Calculate direction for wall mounted stuff like torches and signs
163 if def.paramtype2 == 'wallmounted' then
165 x = under.x - above.x,
166 y = under.y - above.y,
167 z = under.z - above.z
169 newnode.param2 = minetest.dir_to_wallmounted(dir)
170 -- Calculate the direction for furnaces and chests and stuff
171 elseif def.paramtype2 == 'facedir' then
172 local placer_pos = placer:getpos()
175 x = above.x - placer_pos.x,
176 y = above.y - placer_pos.y,
177 z = above.z - placer_pos.z
179 newnode.param2 = minetest.dir_to_facedir(dir)
180 minetest.log("action", "facedir: " .. newnode.param2)
184 -- Add node and update
185 minetest.env:add_node(place_to, newnode)
188 if def.after_place_node then
189 -- Copy place_to because callback can modify it
190 local place_to_copy = {x=place_to.x, y=place_to.y, z=place_to.z}
191 def.after_place_node(place_to_copy, placer)
196 for _, callback in ipairs(minetest.registered_on_placenodes) do
197 -- Copy pos and node because callback can modify them
198 local place_to_copy = {x=place_to.x, y=place_to.y, z=place_to.z}
199 local newnode_copy = {name=newnode.name, param1=newnode.param1, param2=newnode.param2}
200 local oldnode_copy = {name=oldnode.name, param1=oldnode.param1, param2=oldnode.param2}
201 callback(place_to_copy, newnode_copy, placer, oldnode_copy)
204 itemstack:take_item()
208 function minetest.item_place_object(itemstack, placer, pointed_thing)
209 local pos = minetest.get_pointed_thing_position(pointed_thing, true)
211 local item = itemstack:take_item()
212 minetest.env:add_item(pos, item)
217 function minetest.item_place(itemstack, placer, pointed_thing)
218 if itemstack:get_definition().type == "node" then
219 return minetest.item_place_node(itemstack, placer, pointed_thing)
221 return minetest.item_place_object(itemstack, placer, pointed_thing)
225 function minetest.item_drop(itemstack, dropper, pos)
226 if dropper.get_player_name then
227 local v = dropper:get_look_dir()
228 local p = {x=pos.x+v.x, y=pos.y+1.5+v.y, z=pos.z+v.z}
229 local obj = minetest.env:add_item(p, itemstack)
237 minetest.env:add_item(pos, itemstack)
242 function minetest.item_eat(hp_change, replace_with_item)
243 return function(itemstack, user, pointed_thing) -- closure
244 if itemstack:take_item() ~= nil then
245 user:set_hp(user:get_hp() + hp_change)
246 itemstack:add_item(replace_with_item) -- note: replace_with_item is optional
252 function minetest.node_punch(pos, node, puncher)
255 for _, callback in ipairs(minetest.registered_on_punchnodes) do
256 -- Copy pos and node because callback can modify them
257 local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
258 local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
259 callback(pos_copy, node_copy, puncher)
263 function minetest.handle_node_drops(pos, drops, digger)
264 -- Add dropped items to object's inventory
265 if digger:get_inventory() then
266 local _, dropped_item
267 for _, dropped_item in ipairs(drops) do
268 digger:get_inventory():add_item("main", dropped_item)
273 function minetest.node_dig(pos, node, digger)
274 minetest.debug("node_dig")
276 local def = ItemStack({name=node.name}):get_definition()
277 -- Check if def ~= 0 because we always want to be able to remove unknown nodes
278 if #def ~= 0 and not def.diggable or (def.can_dig and not def.can_dig(pos,digger)) then
279 minetest.debug("not diggable")
280 minetest.log("info", digger:get_player_name() .. " tried to dig "
281 .. node.name .. " which is not diggable "
282 .. minetest.pos_to_string(pos))
286 minetest.log('action', digger:get_player_name() .. " digs "
287 .. node.name .. " at " .. minetest.pos_to_string(pos))
289 local wielded = digger:get_wielded_item()
290 local drops = minetest.get_node_drops(node.name, wielded:get_name())
293 local tp = wielded:get_tool_capabilities()
294 local dp = minetest.get_dig_params(def.groups, tp)
295 wielded:add_wear(dp.wear)
296 digger:set_wielded_item(wielded)
299 minetest.handle_node_drops(pos, drops, digger)
301 local oldmetadata = nil
302 if def.after_dig_node then
303 oldmetadata = minetest.env:get_meta(pos):to_table()
306 -- Remove node and update
307 minetest.env:remove_node(pos)
310 if def.after_dig_node then
311 -- Copy pos and node because callback can modify them
312 local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
313 local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
314 def.after_dig_node(pos_copy, node_copy, oldmetadata, digger)
319 for _, callback in ipairs(minetest.registered_on_dignodes) do
320 -- Copy pos and node because callback can modify them
321 local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
322 local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
323 callback(pos_copy, node_copy, digger)
327 -- This is used to allow mods to redefine minetest.item_place and so on
328 -- NOTE: This is not the preferred way. Preferred way is to provide enough
329 -- callbacks to not require redefining global functions. -celeron55
330 local function redef_wrapper(table, name)
332 return table[name](...)
337 -- Item definition defaults
340 minetest.nodedef_default = {
343 -- name intentionally not defined here
346 inventory_image = "",
348 wield_scale = {x=1,y=1,z=1},
351 liquids_pointable = false,
352 tool_capabilities = nil,
353 node_placement_prediction = nil,
355 -- Interaction callbacks
356 on_place = redef_wrapper(minetest, 'item_place'), -- minetest.item_place
357 on_drop = redef_wrapper(minetest, 'item_drop'), -- minetest.item_drop
361 on_punch = redef_wrapper(minetest, 'node_punch'), -- minetest.node_punch
362 on_dig = redef_wrapper(minetest, 'node_dig'), -- minetest.node_dig
364 on_receive_fields = nil,
366 on_metadata_inventory_move = minetest.node_metadata_inventory_move_allow_all,
367 on_metadata_inventory_offer = minetest.node_metadata_inventory_offer_allow_all,
368 on_metadata_inventory_take = minetest.node_metadata_inventory_take_allow_all,
373 -- Don't define these because otherwise the old tile_images and
374 -- special_materials wouldn't be read
377 -- {name="", backface_culling=true},
378 -- {name="", backface_culling=true},
381 post_effect_color = {a=0, r=0, g=0, b=0},
384 is_ground_content = false,
385 sunlight_propagates = false,
390 buildable_to = false,
392 liquid_alternative_flowing = "",
393 liquid_alternative_source = "",
394 liquid_viscosity = 0,
396 damage_per_second = 0,
397 selection_box = {type="regular"},
398 legacy_facedir_simple = false,
399 legacy_wallmounted = false,
402 minetest.craftitemdef_default = {
404 -- name intentionally not defined here
407 inventory_image = "",
409 wield_scale = {x=1,y=1,z=1},
411 liquids_pointable = false,
412 tool_capabilities = nil,
414 -- Interaction callbacks
415 on_place = redef_wrapper(minetest, 'item_place'), -- minetest.item_place
416 on_drop = redef_wrapper(minetest, 'item_drop'), -- minetest.item_drop
420 minetest.tooldef_default = {
422 -- name intentionally not defined here
425 inventory_image = "",
427 wield_scale = {x=1,y=1,z=1},
429 liquids_pointable = false,
430 tool_capabilities = nil,
432 -- Interaction callbacks
433 on_place = redef_wrapper(minetest, 'item_place'), -- minetest.item_place
434 on_drop = redef_wrapper(minetest, 'item_drop'), -- minetest.item_drop
438 minetest.noneitemdef_default = { -- This is used for the hand and unknown items
440 -- name intentionally not defined here
443 inventory_image = "",
445 wield_scale = {x=1,y=1,z=1},
447 liquids_pointable = false,
448 tool_capabilities = nil,
450 -- Interaction callbacks