cocoa.lua 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. drop = {},
  116. groups = {
  117. snappy = 3, flammable = 2, plant = 1, growing = 1,
  118. not_in_creative_inventory = 1, leafdecay = 1, leafdecay_drop = 1
  119. },
  120. sounds = default.node_sound_leaves_defaults(),
  121. growth_check = function(pos, node_name)
  122. if minetest.find_node_near(pos, 1, {"default:jungletree"}) then
  123. return false
  124. end
  125. return true
  126. end
  127. }
  128. -- stage 1
  129. minetest.register_node("farming:cocoa_1", table.copy(def))
  130. -- stage 2
  131. def.tiles = {"farming_cocoa_2.png"}
  132. minetest.register_node("farming:cocoa_2", table.copy(def))
  133. -- stage3
  134. def.tiles = {"farming_cocoa_3.png"}
  135. def.drop = {
  136. items = {
  137. {items = {"farming:cocoa_beans 1"}, rarity = 1}
  138. }
  139. }
  140. minetest.register_node("farming:cocoa_3", table.copy(def))
  141. -- stage 4 (final)
  142. def.tiles = {"farming_cocoa_4.png"}
  143. def.groups.growing = nil
  144. def.growth_check = nil
  145. def.drop = {
  146. items = {
  147. {items = {"farming:cocoa_beans 2"}, rarity = 1},
  148. {items = {"farming:cocoa_beans 1"}, rarity = 2},
  149. {items = {"farming:cocoa_beans 1"}, rarity = 4}
  150. }
  151. }
  152. minetest.register_node("farming:cocoa_4", table.copy(def))
  153. -- add to registered_plants
  154. farming.registered_plants["farming:cocoa_beans"] = {
  155. crop = "farming:cocoa",
  156. seed = "farming:cocoa_beans",
  157. minlight = farming.min_light,
  158. maxlight = farming.max_light,
  159. steps = 4
  160. }
  161. -- add random cocoa pods to jungle tree's
  162. minetest.register_on_generated(function(minp, maxp)
  163. if maxp.y < 0 then
  164. return
  165. end
  166. local pos, dir
  167. local cocoa = minetest.find_nodes_in_area(minp, maxp, "default:jungletree")
  168. for n = 1, #cocoa do
  169. pos = cocoa[n]
  170. if minetest.find_node_near(pos, 1,
  171. {"default:jungleleaves", "moretrees:jungletree_leaves_green"}) then
  172. dir = math.random(1, 80)
  173. if dir == 1 then pos.x = pos.x + 1
  174. elseif dir == 2 then pos.x = pos.x - 1
  175. elseif dir == 3 then pos.z = pos.z + 1
  176. elseif dir == 4 then pos.z = pos.z -1
  177. end
  178. if dir < 5
  179. and minetest.get_node(pos).name == "air"
  180. and minetest.get_node_light(pos) > 12 then
  181. --print ("Cocoa Pod added at " .. minetest.pos_to_string(pos))
  182. minetest.swap_node(pos, {
  183. name = "farming:cocoa_" .. tostring(math.random(4))
  184. })
  185. end
  186. end
  187. end
  188. end)