fruitregrow.lua 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. hb4 = hb4 or {}
  2. hb4.fruitregrow = hb4.fruitregrow or {}
  3. -- Localize for performance.
  4. local math_random = math.random
  5. function hb4.fruitregrow.on_timer(pos, elapsed)
  6. local meta = minetest.get_meta(pos)
  7. local node = minetest.get_node({x=pos.x, y=pos.y+1, z=pos.z})
  8. if node.name == meta:get_string("leafname") then
  9. local fruit = meta:get_string("fruitname")
  10. if fruit ~= "" and minetest.reg_ns_nodes[fruit] then
  11. -- param2 is 0, so fruit can regrow if picked again.
  12. minetest.add_node(pos, {name=fruit})
  13. return
  14. end
  15. end
  16. -- Else just remove the dummy node.
  17. minetest.remove_node(pos)
  18. end
  19. function hb4.fruitregrow.after_place_node_impl(pos, placer, itemstack, pointed_thing)
  20. local node = minetest.get_node(pos)
  21. -- Fruit placed by players does not regrow if picked.
  22. node.param2 = 1
  23. minetest.swap_node(pos, node)
  24. end
  25. function hb4.fruitregrow.after_place_node()
  26. return function(...)
  27. return hb4.fruitregrow.after_place_node_impl(...)
  28. end
  29. end
  30. -- We must set param2 on fallen fruit nodes, otherwise they
  31. -- will reproduce infinitely!
  32. function hb4.fruitregrow.on_finish_collapse_impl(pos, node)
  33. node.param2 = 1
  34. minetest.swap_node(pos, node)
  35. end
  36. function hb4.fruitregrow.on_finish_collapse()
  37. return function(...)
  38. return hb4.fruitregrow.on_finish_collapse_impl(...)
  39. end
  40. end
  41. function hb4.fruitregrow.after_dig_node_impl(pos, oldnode, oldmetadata, digger)
  42. -- Only for fruit placed by the mapgen/voxelmanip/schems.
  43. if oldnode.param2 == 0 then
  44. local node = minetest.get_node({x=pos.x, y=pos.y+1, z=pos.z})
  45. minetest.add_node(pos, {name="hb4:fruitregrow"})
  46. local meta = minetest.get_meta(pos)
  47. meta:set_string("fruitname", oldnode.name)
  48. meta:set_string("leafname", node.name)
  49. local timer = minetest.get_node_timer(pos)
  50. timer:start(math_random(60*20, 60*120))
  51. end
  52. end
  53. function hb4.fruitregrow.after_dig_node()
  54. return function(...)
  55. return hb4.fruitregrow.after_dig_node_impl(...)
  56. end
  57. end
  58. if not hb4.fruitregrow.registered then
  59. minetest.register_node("hb4:fruitregrow", {
  60. drawtype = "airlike",
  61. description = "Fruit Spawner (Please Report to Admin)",
  62. paramtype = "light",
  63. sunlight_propagates = true,
  64. walkable = false,
  65. pointable = false,
  66. groups = {immovable = 1},
  67. climbable = false,
  68. buildable_to = true,
  69. floodable = true,
  70. drop = "",
  71. on_timer = function(...)
  72. return hb4.fruitregrow.on_timer(...)
  73. end,
  74. on_finish_collapse = function(pos, node)
  75. minetest.remove_node(pos)
  76. end,
  77. on_collapse_to_entity = function(pos, node)
  78. -- Do nothing.
  79. end,
  80. })
  81. minetest.register_node(":basictrees:tree_apple", {
  82. description = "Apple",
  83. drawtype = "plantlike",
  84. tiles = {"default_apple.png"},
  85. inventory_image = "default_apple.png",
  86. paramtype = "light",
  87. sunlight_propagates = true,
  88. walkable = false,
  89. selection_box = {
  90. type = "fixed",
  91. fixed = {-0.2, -0.5, -0.2, 0.2, 0, 0.2}
  92. },
  93. -- Apples do not rot.
  94. groups = {fleshy=3, dig_immediate=3, flammable=2, leafdecay=3, leafdecay_drop=1},
  95. on_use = minetest.item_eat(2),
  96. sounds = default.node_sound_leaves_defaults(),
  97. on_construct = enhanced_leafdecay.make_leaf_constructor({}),
  98. on_timer = enhanced_leafdecay.make_leaf_nodetimer({}),
  99. after_dig_node = hb4.fruitregrow.after_dig_node(),
  100. after_place_node = hb4.fruitregrow.after_place_node(),
  101. on_finish_collapse = hb4.fruitregrow.on_finish_collapse(),
  102. })
  103. hb4.fruitregrow.registered = true
  104. end