fruitregrow.lua 3.3 KB

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