cocoa.lua 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. local S = farming.intllib
  2. -- place cocoa
  3. local function place_cocoa(itemstack, placer, pointed_thing, plantname)
  4. local pt = pointed_thing
  5. -- check if pointing at a node
  6. if not pt or pt.type ~= "node" then
  7. return
  8. end
  9. local under = minetest.get_node(pt.under)
  10. -- return if any of the nodes are not registered
  11. if not minetest.registered_nodes[under.name] then
  12. return
  13. end
  14. -- am I right-clicking on something that has a custom on_place set?
  15. -- thanks to Krock for helping with this issue :)
  16. local def = minetest.registered_nodes[under.name]
  17. if placer and itemstack and def and def.on_rightclick then
  18. return def.on_rightclick(pt.under, under, placer, itemstack)
  19. end
  20. -- check if pointing at jungletree
  21. if under.name ~= "default:jungletree"
  22. or minetest.get_node(pt.above).name ~= "air" then
  23. return
  24. end
  25. -- is player planting crop?
  26. local name = placer and placer:get_player_name() or ""
  27. -- check for protection
  28. if minetest.is_protected(pt.above, name) then
  29. return
  30. end
  31. -- add the node and remove 1 item from the itemstack
  32. minetest.set_node(pt.above, {name = plantname})
  33. minetest.sound_play("default_place_node", {pos = pt.above, gain = 1.0})
  34. if placer and not farming.is_creative(placer:get_player_name()) then
  35. itemstack:take_item()
  36. -- check for refill
  37. if itemstack:get_count() == 0 then
  38. minetest.after(0.20,
  39. farming.refill_plant,
  40. placer,
  41. "farming:cocoa_beans",
  42. placer:get_wield_index()
  43. )
  44. end
  45. end
  46. return itemstack
  47. end
  48. -- cocoa beans
  49. minetest.register_craftitem("farming:cocoa_beans", {
  50. description = S("Cocoa Beans"),
  51. inventory_image = "farming_cocoa_beans.png",
  52. groups = {seed = 2, food_cocoa = 1, flammable = 2},
  53. on_place = function(itemstack, placer, pointed_thing)
  54. return place_cocoa(itemstack, placer, pointed_thing, "farming:cocoa_1")
  55. end
  56. })
  57. minetest.register_craft( {
  58. output = "dye:brown 2",
  59. recipe = {{"farming:cocoa_beans"}}
  60. })
  61. -- chocolate cookie
  62. minetest.register_craftitem("farming:cookie", {
  63. description = S("Cookie"),
  64. inventory_image = "farming_cookie.png",
  65. on_use = minetest.item_eat(2)
  66. })
  67. minetest.register_craft( {
  68. output = "farming:cookie 8",
  69. recipe = {
  70. {"group:food_wheat", "group:food_cocoa", "group:food_wheat" }
  71. }
  72. })
  73. -- bar of dark chocolate (thanks to Ice Pandora for her deviantart.com chocolate tutorial)
  74. minetest.register_craftitem("farming:chocolate_dark", {
  75. description = S("Bar of Dark Chocolate"),
  76. inventory_image = "farming_chocolate_dark.png",
  77. on_use = minetest.item_eat(3)
  78. })
  79. minetest.register_craft( {
  80. output = "farming:chocolate_dark",
  81. recipe = {
  82. {"group:food_cocoa", "group:food_cocoa", "group:food_cocoa"}
  83. }
  84. })
  85. -- chocolate block
  86. minetest.register_node("farming:chocolate_block", {
  87. description = S("Chocolate Block"),
  88. tiles = {"farming_chocolate_block.png"},
  89. is_ground_content = false,
  90. groups = {cracky = 2, oddly_breakable_by_hand = 2},
  91. sounds = default.node_sound_stone_defaults()
  92. })
  93. minetest.register_craft({
  94. output = "farming:chocolate_block",
  95. recipe = {
  96. {"farming:chocolate_dark", "farming:chocolate_dark", "farming:chocolate_dark"},
  97. {"farming:chocolate_dark", "farming:chocolate_dark", "farming:chocolate_dark"},
  98. {"farming:chocolate_dark", "farming:chocolate_dark", "farming:chocolate_dark"}
  99. }
  100. })
  101. minetest.register_craft({
  102. output = "farming:chocolate_dark 9",
  103. recipe = {{"farming:chocolate_block"}}
  104. })
  105. -- cocoa definition
  106. local def = {
  107. drawtype = "plantlike",
  108. tiles = {"farming_cocoa_1.png"},
  109. paramtype = "light",
  110. walkable = false,
  111. selection_box = {
  112. type = "fixed",
  113. fixed = {-0.3, -0.5, -0.3, 0.3, 0.5, 0.3}
  114. },
  115. groups = {
  116. snappy = 3, flammable = 2, plant = 1, growing = 1,
  117. not_in_creative_inventory = 1, leafdecay = 1, leafdecay_drop = 1
  118. },
  119. sounds = default.node_sound_leaves_defaults(),
  120. growth_check = function(pos, node_name)
  121. if minetest.find_node_near(pos, 1, {"default:jungletree"}) then
  122. return false
  123. end
  124. return true
  125. end
  126. }
  127. -- stage 1
  128. minetest.register_node("farming:cocoa_1", table.copy(def))
  129. -- stage 2
  130. def.tiles = {"farming_cocoa_2.png"}
  131. minetest.register_node("farming:cocoa_2", table.copy(def))
  132. -- stage3
  133. def.tiles = {"farming_cocoa_3.png"}
  134. def.drop = {
  135. items = {
  136. {items = {"farming:cocoa_beans 1"}, rarity = 1}
  137. }
  138. }
  139. minetest.register_node("farming:cocoa_3", table.copy(def))
  140. -- stage 4 (final)
  141. def.tiles = {"farming_cocoa_4.png"}
  142. def.groups.growing = nil
  143. def.growth_check = nil
  144. def.drop = {
  145. items = {
  146. {items = {"farming:cocoa_beans 2"}, rarity = 1},
  147. {items = {"farming:cocoa_beans 1"}, rarity = 2},
  148. {items = {"farming:cocoa_beans 1"}, rarity = 4}
  149. }
  150. }
  151. minetest.register_node("farming:cocoa_4", table.copy(def))
  152. -- add to registered_plants
  153. farming.registered_plants["farming:cocoa_beans"] = {
  154. crop = "farming:cocoa",
  155. seed = "farming:cocoa_beans",
  156. minlight = farming.min_light,
  157. maxlight = farming.max_light,
  158. steps = 4
  159. }
  160. -- add random cocoa pods to jungle tree's
  161. minetest.register_on_generated(function(minp, maxp)
  162. if maxp.y < 0 then
  163. return
  164. end
  165. local pos, dir
  166. local cocoa = minetest.find_nodes_in_area(minp, maxp, "default:jungletree")
  167. for n = 1, #cocoa do
  168. pos = cocoa[n]
  169. if minetest.find_node_near(pos, 1,
  170. {"default:jungleleaves", "moretrees:jungletree_leaves_green"}) then
  171. dir = math.random(1, 80)
  172. if dir == 1 then pos.x = pos.x + 1
  173. elseif dir == 2 then pos.x = pos.x - 1
  174. elseif dir == 3 then pos.z = pos.z + 1
  175. elseif dir == 4 then pos.z = pos.z -1
  176. end
  177. if dir < 5
  178. and minetest.get_node(pos).name == "air"
  179. and minetest.get_node_light(pos) > 12 then
  180. --print ("Cocoa Pod added at " .. minetest.pos_to_string(pos))
  181. minetest.swap_node(pos, {
  182. name = "farming:cocoa_" .. tostring(math.random(4))
  183. })
  184. end
  185. end
  186. end
  187. end)