api.lua 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. -- the table containing the tree definitions
  2. treecapitator.trees = {}
  3. -- For the usage of this function, see trees.lua.
  4. local after_dig_wrap
  5. local after_dig_nodes = {}
  6. function treecapitator.register_tree(tr)
  7. for name,value in pairs(treecapitator.default_tree) do
  8. if tr[name] == nil then
  9. tr[name] = value --replaces not defined stuff
  10. end
  11. end
  12. treecapitator.trees[#treecapitator.trees+1] = tr
  13. if treecapitator.after_register[tr.type] then
  14. treecapitator.after_register[tr.type](tr)
  15. end
  16. for i = 1,#tr.trees do
  17. local nodename = tr.trees[i]
  18. local data = minetest.registered_nodes[nodename]
  19. if not data then
  20. error(nodename .. " has to be registered before calling " ..
  21. "treecapitator.register_tree.")
  22. end
  23. local func = after_dig_wrap
  24. local prev_after_dig = data.after_dig_node
  25. if prev_after_dig then
  26. func = function(pos, oldnode, oldmetadata, digger)
  27. prev_after_dig(pos, oldnode, oldmetadata, digger)
  28. treecapitator.capitate_tree(pos, digger)
  29. end
  30. end
  31. minetest.override_item(nodename, {after_dig_node = func})
  32. after_dig_nodes[#after_dig_nodes+1] = nodename
  33. end
  34. end
  35. -- Mods can set treecapitator.capitation_usually_disallowed to true and
  36. -- override this function, with params pos and digger, to make capitation
  37. -- transpire only under certain contitions.
  38. function treecapitator.capitation_allowed()
  39. return not treecapitator.capitation_usually_disallowed
  40. end
  41. -- Example of overriding this function
  42. if treecapitator.no_hand_capitation then
  43. -- disallow capitating trees if no proper tool is used
  44. treecapitator.capitation_usually_disallowed = true
  45. local allowed = treecapitator.capitation_allowed
  46. function treecapitator.capitation_allowed(pos, digger)
  47. local def = minetest.registered_nodes[
  48. minetest.get_node{x=pos.x, y=pos.y+1, z=pos.z}.name
  49. ]
  50. return def and def.groups and minetest.get_dig_params(def.groups,
  51. digger:get_wielded_item():get_tool_capabilities()).wear > 0
  52. or allowed(pos, digger)
  53. end
  54. end
  55. -- test if trunk nodes were redefined
  56. minetest.after(2, function()
  57. for i = 1,#after_dig_nodes do
  58. local nodename = after_dig_nodes[i]
  59. if not minetest.registered_nodes[nodename].after_dig_node then
  60. error(nodename .. " didn't keep after_dig_node.")
  61. end
  62. end
  63. after_dig_nodes = nil
  64. end)
  65. -- wrapping is necessary, someone may overwrite treecapitator.capitate_tree
  66. function after_dig_wrap(pos, _,_, digger)
  67. treecapitator.capitate_tree(pos, digger)
  68. end