item.lua 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  1. -- Minetest: builtin/item.lua
  2. local builtin_shared = ...
  3. local function copy_pointed_thing(pointed_thing)
  4. return {
  5. type = pointed_thing.type,
  6. above = vector.new(pointed_thing.above),
  7. under = vector.new(pointed_thing.under),
  8. ref = pointed_thing.ref,
  9. }
  10. end
  11. --
  12. -- Item definition helpers
  13. --
  14. function core.inventorycube(img1, img2, img3)
  15. img2 = img2 or img1
  16. img3 = img3 or img1
  17. return "[inventorycube"
  18. .. "{" .. img1:gsub("%^", "&")
  19. .. "{" .. img2:gsub("%^", "&")
  20. .. "{" .. img3:gsub("%^", "&")
  21. end
  22. function core.get_pointed_thing_position(pointed_thing, above)
  23. if pointed_thing.type == "node" then
  24. if above then
  25. -- The position where a node would be placed
  26. return pointed_thing.above
  27. end
  28. -- The position where a node would be dug
  29. return pointed_thing.under
  30. elseif pointed_thing.type == "object" then
  31. return pointed_thing.ref and pointed_thing.ref:get_pos()
  32. end
  33. end
  34. function core.dir_to_facedir(dir, is6d)
  35. --account for y if requested
  36. if is6d and math.abs(dir.y) > math.abs(dir.x) and math.abs(dir.y) > math.abs(dir.z) then
  37. --from above
  38. if dir.y < 0 then
  39. if math.abs(dir.x) > math.abs(dir.z) then
  40. if dir.x < 0 then
  41. return 19
  42. else
  43. return 13
  44. end
  45. else
  46. if dir.z < 0 then
  47. return 10
  48. else
  49. return 4
  50. end
  51. end
  52. --from below
  53. else
  54. if math.abs(dir.x) > math.abs(dir.z) then
  55. if dir.x < 0 then
  56. return 15
  57. else
  58. return 17
  59. end
  60. else
  61. if dir.z < 0 then
  62. return 6
  63. else
  64. return 8
  65. end
  66. end
  67. end
  68. --otherwise, place horizontally
  69. elseif math.abs(dir.x) > math.abs(dir.z) then
  70. if dir.x < 0 then
  71. return 3
  72. else
  73. return 1
  74. end
  75. else
  76. if dir.z < 0 then
  77. return 2
  78. else
  79. return 0
  80. end
  81. end
  82. end
  83. -- Table of possible dirs
  84. local facedir_to_dir = {
  85. vector.new( 0, 0, 1),
  86. vector.new( 1, 0, 0),
  87. vector.new( 0, 0, -1),
  88. vector.new(-1, 0, 0),
  89. vector.new( 0, -1, 0),
  90. vector.new( 0, 1, 0),
  91. }
  92. -- Mapping from facedir value to index in facedir_to_dir.
  93. local facedir_to_dir_map = {
  94. [0]=1, 2, 3, 4,
  95. 5, 2, 6, 4,
  96. 6, 2, 5, 4,
  97. 1, 5, 3, 6,
  98. 1, 6, 3, 5,
  99. 1, 4, 3, 2,
  100. }
  101. function core.facedir_to_dir(facedir)
  102. return facedir_to_dir[facedir_to_dir_map[facedir % 32]]
  103. end
  104. function core.dir_to_wallmounted(dir)
  105. if math.abs(dir.y) > math.max(math.abs(dir.x), math.abs(dir.z)) then
  106. if dir.y < 0 then
  107. return 1
  108. else
  109. return 0
  110. end
  111. elseif math.abs(dir.x) > math.abs(dir.z) then
  112. if dir.x < 0 then
  113. return 3
  114. else
  115. return 2
  116. end
  117. else
  118. if dir.z < 0 then
  119. return 5
  120. else
  121. return 4
  122. end
  123. end
  124. end
  125. -- table of dirs in wallmounted order
  126. local wallmounted_to_dir = {
  127. [0] = vector.new( 0, 1, 0),
  128. vector.new( 0, -1, 0),
  129. vector.new( 1, 0, 0),
  130. vector.new(-1, 0, 0),
  131. vector.new( 0, 0, 1),
  132. vector.new( 0, 0, -1),
  133. }
  134. function core.wallmounted_to_dir(wallmounted)
  135. return wallmounted_to_dir[wallmounted % 8]
  136. end
  137. function core.dir_to_yaw(dir)
  138. return -math.atan2(dir.x, dir.z)
  139. end
  140. function core.yaw_to_dir(yaw)
  141. return vector.new(-math.sin(yaw), 0, math.cos(yaw))
  142. end
  143. function core.is_colored_paramtype(ptype)
  144. return (ptype == "color") or (ptype == "colorfacedir") or
  145. (ptype == "colorwallmounted") or (ptype == "colordegrotate")
  146. end
  147. function core.strip_param2_color(param2, paramtype2)
  148. if not core.is_colored_paramtype(paramtype2) then
  149. return nil
  150. end
  151. if paramtype2 == "colorfacedir" then
  152. param2 = math.floor(param2 / 32) * 32
  153. elseif paramtype2 == "colorwallmounted" then
  154. param2 = math.floor(param2 / 8) * 8
  155. elseif paramtype2 == "colordegrotate" then
  156. param2 = math.floor(param2 / 32) * 32
  157. end
  158. -- paramtype2 == "color" requires no modification.
  159. return param2
  160. end
  161. local function has_all_groups(tbl, required_groups)
  162. if type(required_groups) == "string" then
  163. return (tbl[required_groups] or 0) ~= 0
  164. end
  165. for _, group in ipairs(required_groups) do
  166. if (tbl[group] or 0) == 0 then
  167. return false
  168. end
  169. end
  170. return true
  171. end
  172. function core.get_node_drops(node, toolname)
  173. -- Compatibility, if node is string
  174. local nodename = node
  175. local param2 = 0
  176. -- New format, if node is table
  177. if (type(node) == "table") then
  178. nodename = node.name
  179. param2 = node.param2
  180. end
  181. local def = core.registered_nodes[nodename]
  182. local drop = def and def.drop
  183. local ptype = def and def.paramtype2
  184. -- get color, if there is color (otherwise nil)
  185. local palette_index = core.strip_param2_color(param2, ptype)
  186. if drop == nil then
  187. -- default drop
  188. if palette_index then
  189. local stack = ItemStack(nodename)
  190. stack:get_meta():set_int("palette_index", palette_index)
  191. return {stack:to_string()}
  192. end
  193. return {nodename}
  194. elseif type(drop) == "string" then
  195. -- itemstring drop
  196. return drop ~= "" and {drop} or {}
  197. elseif drop.items == nil then
  198. -- drop = {} to disable default drop
  199. return {}
  200. end
  201. -- Extended drop table
  202. local got_items = {}
  203. local got_count = 0
  204. for _, item in ipairs(drop.items) do
  205. local good_rarity = true
  206. local good_tool = true
  207. if item.rarity ~= nil then
  208. good_rarity = item.rarity < 1 or math.random(item.rarity) == 1
  209. end
  210. if item.tools ~= nil or item.tool_groups ~= nil then
  211. good_tool = false
  212. end
  213. if item.tools ~= nil and toolname then
  214. for _, tool in ipairs(item.tools) do
  215. if tool:sub(1, 1) == '~' then
  216. good_tool = toolname:find(tool:sub(2)) ~= nil
  217. else
  218. good_tool = toolname == tool
  219. end
  220. if good_tool then
  221. break
  222. end
  223. end
  224. end
  225. if item.tool_groups ~= nil and toolname then
  226. local tooldef = core.registered_items[toolname]
  227. if tooldef ~= nil and type(tooldef.groups) == "table" then
  228. if type(item.tool_groups) == "string" then
  229. -- tool_groups can be a string which specifies the required group
  230. good_tool = core.get_item_group(toolname, item.tool_groups) ~= 0
  231. else
  232. -- tool_groups can be a list of sufficient requirements.
  233. -- i.e. if any item in the list can be satisfied then the tool is good
  234. assert(type(item.tool_groups) == "table")
  235. for _, required_groups in ipairs(item.tool_groups) do
  236. -- required_groups can be either a string (a single group),
  237. -- or an array of strings where all must be in tooldef.groups
  238. good_tool = has_all_groups(tooldef.groups, required_groups)
  239. if good_tool then
  240. break
  241. end
  242. end
  243. end
  244. end
  245. end
  246. if good_rarity and good_tool then
  247. got_count = got_count + 1
  248. for _, add_item in ipairs(item.items) do
  249. -- add color, if necessary
  250. if item.inherit_color and palette_index then
  251. local stack = ItemStack(add_item)
  252. stack:get_meta():set_int("palette_index", palette_index)
  253. add_item = stack:to_string()
  254. end
  255. got_items[#got_items+1] = add_item
  256. end
  257. if drop.max_items ~= nil and got_count == drop.max_items then
  258. break
  259. end
  260. end
  261. end
  262. return got_items
  263. end
  264. local function user_name(user)
  265. return user and user:get_player_name() or ""
  266. end
  267. -- Returns a logging function. For empty names, does not log.
  268. local function make_log(name)
  269. return name ~= "" and core.log or function() end
  270. end
  271. function core.item_place_node(itemstack, placer, pointed_thing, param2,
  272. prevent_after_place)
  273. local def = itemstack:get_definition()
  274. if def.type ~= "node" or pointed_thing.type ~= "node" then
  275. return itemstack, nil
  276. end
  277. local under = pointed_thing.under
  278. local oldnode_under = core.get_node_or_nil(under)
  279. local above = pointed_thing.above
  280. local oldnode_above = core.get_node_or_nil(above)
  281. local playername = user_name(placer)
  282. local log = make_log(playername)
  283. if not oldnode_under or not oldnode_above then
  284. log("info", playername .. " tried to place"
  285. .. " node in unloaded position " .. core.pos_to_string(above))
  286. return itemstack, nil
  287. end
  288. local olddef_under = core.registered_nodes[oldnode_under.name]
  289. olddef_under = olddef_under or core.nodedef_default
  290. local olddef_above = core.registered_nodes[oldnode_above.name]
  291. olddef_above = olddef_above or core.nodedef_default
  292. if not olddef_above.buildable_to and not olddef_under.buildable_to then
  293. log("info", playername .. " tried to place"
  294. .. " node in invalid position " .. core.pos_to_string(above)
  295. .. ", replacing " .. oldnode_above.name)
  296. return itemstack, nil
  297. end
  298. -- Place above pointed node
  299. local place_to = vector.new(above)
  300. -- If node under is buildable_to, place into it instead (eg. snow)
  301. if olddef_under.buildable_to then
  302. log("info", "node under is buildable to")
  303. place_to = vector.new(under)
  304. end
  305. if core.is_protected(place_to, playername) then
  306. log("action", playername
  307. .. " tried to place " .. def.name
  308. .. " at protected position "
  309. .. core.pos_to_string(place_to))
  310. core.record_protection_violation(place_to, playername)
  311. return itemstack, nil
  312. end
  313. local oldnode = core.get_node(place_to)
  314. local newnode = {name = def.name, param1 = 0, param2 = param2 or 0}
  315. -- Calculate direction for wall mounted stuff like torches and signs
  316. if def.place_param2 ~= nil then
  317. newnode.param2 = def.place_param2
  318. elseif (def.paramtype2 == "wallmounted" or
  319. def.paramtype2 == "colorwallmounted") and not param2 then
  320. local dir = vector.subtract(under, above)
  321. newnode.param2 = core.dir_to_wallmounted(dir)
  322. -- Calculate the direction for furnaces and chests and stuff
  323. elseif (def.paramtype2 == "facedir" or
  324. def.paramtype2 == "colorfacedir") and not param2 then
  325. local placer_pos = placer and placer:get_pos()
  326. if placer_pos then
  327. local dir = vector.subtract(above, placer_pos)
  328. newnode.param2 = core.dir_to_facedir(dir)
  329. log("info", "facedir: " .. newnode.param2)
  330. end
  331. end
  332. local metatable = itemstack:get_meta():to_table().fields
  333. -- Transfer color information
  334. if metatable.palette_index and not def.place_param2 then
  335. local color_divisor = nil
  336. if def.paramtype2 == "color" then
  337. color_divisor = 1
  338. elseif def.paramtype2 == "colorwallmounted" then
  339. color_divisor = 8
  340. elseif def.paramtype2 == "colorfacedir" then
  341. color_divisor = 32
  342. elseif def.paramtype2 == "colordegrotate" then
  343. color_divisor = 32
  344. end
  345. if color_divisor then
  346. local color = math.floor(metatable.palette_index / color_divisor)
  347. local other = newnode.param2 % color_divisor
  348. newnode.param2 = color * color_divisor + other
  349. end
  350. end
  351. -- Check if the node is attached and if it can be placed there
  352. if core.get_item_group(def.name, "attached_node") ~= 0 and
  353. not builtin_shared.check_attached_node(place_to, newnode) then
  354. log("action", "attached node " .. def.name ..
  355. " can not be placed at " .. core.pos_to_string(place_to))
  356. return itemstack, nil
  357. end
  358. log("action", playername .. " places node "
  359. .. def.name .. " at " .. core.pos_to_string(place_to))
  360. -- Add node and update
  361. core.add_node(place_to, newnode)
  362. -- Play sound if it was done by a player
  363. if playername ~= "" and def.sounds and def.sounds.place then
  364. core.sound_play(def.sounds.place, {
  365. pos = place_to,
  366. exclude_player = playername,
  367. }, true)
  368. end
  369. local take_item = true
  370. -- Run callback
  371. if def.after_place_node and not prevent_after_place then
  372. -- Deepcopy place_to and pointed_thing because callback can modify it
  373. local place_to_copy = vector.new(place_to)
  374. local pointed_thing_copy = copy_pointed_thing(pointed_thing)
  375. if def.after_place_node(place_to_copy, placer, itemstack,
  376. pointed_thing_copy) then
  377. take_item = false
  378. end
  379. end
  380. -- Run script hook
  381. for _, callback in ipairs(core.registered_on_placenodes) do
  382. -- Deepcopy pos, node and pointed_thing because callback can modify them
  383. local place_to_copy = vector.new(place_to)
  384. local newnode_copy = {name=newnode.name, param1=newnode.param1, param2=newnode.param2}
  385. local oldnode_copy = {name=oldnode.name, param1=oldnode.param1, param2=oldnode.param2}
  386. local pointed_thing_copy = copy_pointed_thing(pointed_thing)
  387. if callback(place_to_copy, newnode_copy, placer, oldnode_copy, itemstack, pointed_thing_copy) then
  388. take_item = false
  389. end
  390. end
  391. if take_item then
  392. itemstack:take_item()
  393. end
  394. return itemstack, place_to
  395. end
  396. -- deprecated, item_place does not call this
  397. function core.item_place_object(itemstack, placer, pointed_thing)
  398. local pos = core.get_pointed_thing_position(pointed_thing, true)
  399. if pos ~= nil then
  400. local item = itemstack:take_item()
  401. core.add_item(pos, item)
  402. end
  403. return itemstack
  404. end
  405. function core.item_place(itemstack, placer, pointed_thing, param2)
  406. -- Call on_rightclick if the pointed node defines it
  407. if pointed_thing.type == "node" and placer and
  408. not placer:get_player_control().sneak then
  409. local n = core.get_node(pointed_thing.under)
  410. local nn = n.name
  411. if core.registered_nodes[nn] and core.registered_nodes[nn].on_rightclick then
  412. return core.registered_nodes[nn].on_rightclick(pointed_thing.under, n,
  413. placer, itemstack, pointed_thing) or itemstack, nil
  414. end
  415. end
  416. -- Place if node, otherwise do nothing
  417. if itemstack:get_definition().type == "node" then
  418. return core.item_place_node(itemstack, placer, pointed_thing, param2)
  419. end
  420. return itemstack, nil
  421. end
  422. function core.item_secondary_use(itemstack, placer)
  423. return itemstack
  424. end
  425. function core.item_drop(itemstack, dropper, pos)
  426. local dropper_is_player = dropper and dropper:is_player()
  427. local p = table.copy(pos)
  428. local cnt = itemstack:get_count()
  429. if dropper_is_player then
  430. p.y = p.y + 1.2
  431. end
  432. local item = itemstack:take_item(cnt)
  433. local obj = core.add_item(p, item)
  434. if obj then
  435. if dropper_is_player then
  436. local dir = dropper:get_look_dir()
  437. dir.x = dir.x * 2.9
  438. dir.y = dir.y * 2.9 + 2
  439. dir.z = dir.z * 2.9
  440. obj:set_velocity(dir)
  441. obj:get_luaentity().dropped_by = dropper:get_player_name()
  442. end
  443. return itemstack
  444. end
  445. -- If we reach this, adding the object to the
  446. -- environment failed
  447. end
  448. function core.do_item_eat(hp_change, replace_with_item, itemstack, user, pointed_thing)
  449. for _, callback in pairs(core.registered_on_item_eats) do
  450. local result = callback(hp_change, replace_with_item, itemstack, user, pointed_thing)
  451. if result then
  452. return result
  453. end
  454. end
  455. -- read definition before potentially emptying the stack
  456. local def = itemstack:get_definition()
  457. if itemstack:take_item():is_empty() then
  458. return itemstack
  459. end
  460. if def and def.sound and def.sound.eat then
  461. core.sound_play(def.sound.eat, {
  462. pos = user:get_pos(),
  463. max_hear_distance = 16
  464. }, true)
  465. end
  466. -- Changing hp might kill the player causing mods to do who-knows-what to the
  467. -- inventory, so do this before set_hp().
  468. if replace_with_item then
  469. if itemstack:is_empty() then
  470. itemstack:add_item(replace_with_item)
  471. else
  472. local inv = user:get_inventory()
  473. -- Check if inv is null, since non-players don't have one
  474. if inv and inv:room_for_item("main", {name=replace_with_item}) then
  475. inv:add_item("main", replace_with_item)
  476. else
  477. local pos = user:get_pos()
  478. pos.y = math.floor(pos.y + 0.5)
  479. core.add_item(pos, replace_with_item)
  480. end
  481. end
  482. end
  483. user:set_wielded_item(itemstack)
  484. user:set_hp(user:get_hp() + hp_change)
  485. return nil -- don't overwrite wield item a second time
  486. end
  487. function core.item_eat(hp_change, replace_with_item)
  488. return function(itemstack, user, pointed_thing) -- closure
  489. if user then
  490. return core.do_item_eat(hp_change, replace_with_item, itemstack, user, pointed_thing)
  491. end
  492. end
  493. end
  494. function core.node_punch(pos, node, puncher, pointed_thing)
  495. -- Run script hook
  496. for _, callback in ipairs(core.registered_on_punchnodes) do
  497. -- Copy pos and node because callback can modify them
  498. local pos_copy = vector.new(pos)
  499. local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
  500. local pointed_thing_copy = pointed_thing and copy_pointed_thing(pointed_thing) or nil
  501. callback(pos_copy, node_copy, puncher, pointed_thing_copy)
  502. end
  503. end
  504. function core.handle_node_drops(pos, drops, digger)
  505. -- Add dropped items to object's inventory
  506. local inv = digger and digger:get_inventory()
  507. local give_item
  508. if inv then
  509. give_item = function(item)
  510. return inv:add_item("main", item)
  511. end
  512. else
  513. give_item = function(item)
  514. -- itemstring to ItemStack for left:is_empty()
  515. return ItemStack(item)
  516. end
  517. end
  518. for _, dropped_item in pairs(drops) do
  519. local left = give_item(dropped_item)
  520. if not left:is_empty() then
  521. local p = vector.offset(pos,
  522. math.random()/2-0.25,
  523. math.random()/2-0.25,
  524. math.random()/2-0.25
  525. )
  526. core.add_item(p, left)
  527. end
  528. end
  529. end
  530. function core.node_dig(pos, node, digger)
  531. local diggername = user_name(digger)
  532. local log = make_log(diggername)
  533. local def = core.registered_nodes[node.name]
  534. -- Copy pos because the callback could modify it
  535. if def and (not def.diggable or
  536. (def.can_dig and not def.can_dig(vector.new(pos), digger))) then
  537. log("info", diggername .. " tried to dig "
  538. .. node.name .. " which is not diggable "
  539. .. core.pos_to_string(pos))
  540. return false
  541. end
  542. if core.is_protected(pos, diggername) then
  543. log("action", diggername
  544. .. " tried to dig " .. node.name
  545. .. " at protected position "
  546. .. core.pos_to_string(pos))
  547. core.record_protection_violation(pos, diggername)
  548. return false
  549. end
  550. log('action', diggername .. " digs "
  551. .. node.name .. " at " .. core.pos_to_string(pos))
  552. local wielded = digger and digger:get_wielded_item()
  553. local drops = core.get_node_drops(node, wielded and wielded:get_name())
  554. if wielded then
  555. local wdef = wielded:get_definition()
  556. local tp = wielded:get_tool_capabilities()
  557. local dp = core.get_dig_params(def and def.groups, tp, wielded:get_wear())
  558. if wdef and wdef.after_use then
  559. wielded = wdef.after_use(wielded, digger, node, dp) or wielded
  560. else
  561. -- Wear out tool
  562. if not core.is_creative_enabled(diggername) then
  563. wielded:add_wear(dp.wear)
  564. if wielded:get_count() == 0 and wdef.sound and wdef.sound.breaks then
  565. core.sound_play(wdef.sound.breaks, {
  566. pos = pos,
  567. gain = 0.5
  568. }, true)
  569. end
  570. end
  571. end
  572. digger:set_wielded_item(wielded)
  573. end
  574. -- Check to see if metadata should be preserved.
  575. if def and def.preserve_metadata then
  576. local oldmeta = core.get_meta(pos):to_table().fields
  577. -- Copy pos and node because the callback can modify them.
  578. local pos_copy = vector.new(pos)
  579. local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
  580. local drop_stacks = {}
  581. for k, v in pairs(drops) do
  582. drop_stacks[k] = ItemStack(v)
  583. end
  584. drops = drop_stacks
  585. def.preserve_metadata(pos_copy, node_copy, oldmeta, drops)
  586. end
  587. -- Handle drops
  588. core.handle_node_drops(pos, drops, digger)
  589. local oldmetadata = nil
  590. if def and def.after_dig_node then
  591. oldmetadata = core.get_meta(pos):to_table()
  592. end
  593. -- Remove node and update
  594. core.remove_node(pos)
  595. -- Play sound if it was done by a player
  596. if diggername ~= "" and def and def.sounds and def.sounds.dug then
  597. core.sound_play(def.sounds.dug, {
  598. pos = pos,
  599. exclude_player = diggername,
  600. }, true)
  601. end
  602. -- Run callback
  603. if def and def.after_dig_node then
  604. -- Copy pos and node because callback can modify them
  605. local pos_copy = vector.new(pos)
  606. local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
  607. def.after_dig_node(pos_copy, node_copy, oldmetadata, digger)
  608. end
  609. -- Run script hook
  610. for _, callback in ipairs(core.registered_on_dignodes) do
  611. local origin = core.callback_origins[callback]
  612. if origin then
  613. core.set_last_run_mod(origin.mod)
  614. end
  615. -- Copy pos and node because callback can modify them
  616. local pos_copy = vector.new(pos)
  617. local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
  618. callback(pos_copy, node_copy, digger)
  619. end
  620. return true
  621. end
  622. function core.itemstring_with_palette(item, palette_index)
  623. local stack = ItemStack(item) -- convert to ItemStack
  624. stack:get_meta():set_int("palette_index", palette_index)
  625. return stack:to_string()
  626. end
  627. function core.itemstring_with_color(item, colorstring)
  628. local stack = ItemStack(item) -- convert to ItemStack
  629. stack:get_meta():set_string("color", colorstring)
  630. return stack:to_string()
  631. end
  632. -- This is used to allow mods to redefine core.item_place and so on
  633. -- NOTE: This is not the preferred way. Preferred way is to provide enough
  634. -- callbacks to not require redefining global functions. -celeron55
  635. local function redef_wrapper(table, name)
  636. return function(...)
  637. return table[name](...)
  638. end
  639. end
  640. --
  641. -- Item definition defaults
  642. --
  643. local default_stack_max = tonumber(core.settings:get("default_stack_max")) or 99
  644. core.nodedef_default = {
  645. -- Item properties
  646. type="node",
  647. -- name intentionally not defined here
  648. description = "",
  649. groups = {},
  650. inventory_image = "",
  651. wield_image = "",
  652. wield_scale = vector.new(1, 1, 1),
  653. stack_max = default_stack_max,
  654. usable = false,
  655. liquids_pointable = false,
  656. tool_capabilities = nil,
  657. node_placement_prediction = nil,
  658. -- Interaction callbacks
  659. on_place = redef_wrapper(core, 'item_place'), -- core.item_place
  660. on_drop = redef_wrapper(core, 'item_drop'), -- core.item_drop
  661. on_use = nil,
  662. can_dig = nil,
  663. on_punch = redef_wrapper(core, 'node_punch'), -- core.node_punch
  664. on_rightclick = nil,
  665. on_dig = redef_wrapper(core, 'node_dig'), -- core.node_dig
  666. on_receive_fields = nil,
  667. -- Node properties
  668. drawtype = "normal",
  669. visual_scale = 1.0,
  670. -- Don't define these because otherwise the old tile_images and
  671. -- special_materials wouldn't be read
  672. --tiles ={""},
  673. --special_tiles = {
  674. -- {name="", backface_culling=true},
  675. -- {name="", backface_culling=true},
  676. --},
  677. post_effect_color = {a=0, r=0, g=0, b=0},
  678. paramtype = "none",
  679. paramtype2 = "none",
  680. is_ground_content = true,
  681. sunlight_propagates = false,
  682. walkable = true,
  683. pointable = true,
  684. diggable = true,
  685. climbable = false,
  686. buildable_to = false,
  687. floodable = false,
  688. liquidtype = "none",
  689. liquid_alternative_flowing = "",
  690. liquid_alternative_source = "",
  691. liquid_viscosity = 0,
  692. drowning = 0,
  693. light_source = 0,
  694. damage_per_second = 0,
  695. selection_box = {type="regular"},
  696. legacy_facedir_simple = false,
  697. legacy_wallmounted = false,
  698. }
  699. core.craftitemdef_default = {
  700. type="craft",
  701. -- name intentionally not defined here
  702. description = "",
  703. groups = {},
  704. inventory_image = "",
  705. wield_image = "",
  706. wield_scale = vector.new(1, 1, 1),
  707. stack_max = default_stack_max,
  708. liquids_pointable = false,
  709. tool_capabilities = nil,
  710. -- Interaction callbacks
  711. on_place = redef_wrapper(core, 'item_place'), -- core.item_place
  712. on_drop = redef_wrapper(core, 'item_drop'), -- core.item_drop
  713. on_secondary_use = redef_wrapper(core, 'item_secondary_use'),
  714. on_use = nil,
  715. }
  716. core.tooldef_default = {
  717. type="tool",
  718. -- name intentionally not defined here
  719. description = "",
  720. groups = {},
  721. inventory_image = "",
  722. wield_image = "",
  723. wield_scale = vector.new(1, 1, 1),
  724. stack_max = 1,
  725. liquids_pointable = false,
  726. tool_capabilities = nil,
  727. -- Interaction callbacks
  728. on_place = redef_wrapper(core, 'item_place'), -- core.item_place
  729. on_secondary_use = redef_wrapper(core, 'item_secondary_use'),
  730. on_drop = redef_wrapper(core, 'item_drop'), -- core.item_drop
  731. on_use = nil,
  732. }
  733. core.noneitemdef_default = { -- This is used for the hand and unknown items
  734. type="none",
  735. -- name intentionally not defined here
  736. description = "",
  737. groups = {},
  738. inventory_image = "",
  739. wield_image = "",
  740. wield_scale = vector.new(1, 1, 1),
  741. stack_max = default_stack_max,
  742. liquids_pointable = false,
  743. tool_capabilities = nil,
  744. -- Interaction callbacks
  745. on_place = redef_wrapper(core, 'item_place'),
  746. on_secondary_use = redef_wrapper(core, 'item_secondary_use'),
  747. on_drop = nil,
  748. on_use = nil,
  749. }