cocoa.lua 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. local S = farming.translate
  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, pt)
  19. end
  20. -- check if pointing at jungletree
  21. if (under.name ~= "default:jungletree" and under.name ~= "mcl_core: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_raw",
  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_raw", {
  50. description = S("Raw Cocoa Beans"),
  51. inventory_image = "farming_cocoa_beans.png^[brighten",
  52. groups = {compostability = 48, seed = 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_craftitem("farming:cocoa_beans", {
  58. description = S("Cocoa Beans"),
  59. inventory_image = "farming_cocoa_beans.png",
  60. groups = {compostability = 65, food_cocoa = 1, flammable = 2}
  61. })
  62. minetest.register_craft({
  63. type = "cooking",
  64. cooktime = 5,
  65. output = "farming:cocoa_beans",
  66. recipe = "farming:cocoa_beans_raw"
  67. })
  68. minetest.register_craft( {
  69. output = farming.mcl and "mcl_dye:brown 2" or "dye:brown 2",
  70. recipe = {{"farming:cocoa_beans"}}
  71. })
  72. -- chocolate cookie
  73. minetest.register_craftitem("farming:cookie", {
  74. description = S("Cookie"),
  75. inventory_image = "farming_cookie.png",
  76. on_use = minetest.item_eat(2)
  77. })
  78. minetest.register_craft( {
  79. output = "farming:cookie 8",
  80. recipe = {
  81. {"group:food_wheat", "group:food_cocoa", "group:food_wheat" }
  82. }
  83. })
  84. -- bar of dark chocolate (thanks to Ice Pandora for her deviantart.com chocolate tutorial)
  85. minetest.register_craftitem("farming:chocolate_dark", {
  86. description = S("Bar of Dark Chocolate"),
  87. inventory_image = "farming_chocolate_dark.png",
  88. on_use = minetest.item_eat(3)
  89. })
  90. minetest.register_craft( {
  91. output = "farming:chocolate_dark",
  92. recipe = {
  93. {"group:food_cocoa", "group:food_cocoa", "group:food_cocoa"}
  94. }
  95. })
  96. -- chocolate block
  97. minetest.register_node("farming:chocolate_block", {
  98. description = S("Chocolate Block"),
  99. tiles = {"farming_chocolate_block.png"},
  100. is_ground_content = false,
  101. groups = {cracky = 2, oddly_breakable_by_hand = 2},
  102. sounds = farming.sounds.node_sound_stone_defaults()
  103. })
  104. minetest.register_craft({
  105. output = "farming:chocolate_block",
  106. recipe = {
  107. {"farming:chocolate_dark", "farming:chocolate_dark", "farming:chocolate_dark"},
  108. {"farming:chocolate_dark", "farming:chocolate_dark", "farming:chocolate_dark"},
  109. {"farming:chocolate_dark", "farming:chocolate_dark", "farming:chocolate_dark"}
  110. }
  111. })
  112. minetest.register_craft({
  113. output = "farming:chocolate_dark 9",
  114. recipe = {{"farming:chocolate_block"}}
  115. })
  116. -- cocoa definition
  117. local def = {
  118. drawtype = "plantlike",
  119. tiles = {"farming_cocoa_1.png"},
  120. paramtype = "light",
  121. walkable = false,
  122. selection_box = {
  123. type = "fixed",
  124. fixed = {-0.3, -0.5, -0.3, 0.3, 0.5, 0.3}
  125. },
  126. drop = {},
  127. groups = {
  128. handy = 1, snappy = 3, flammable = 2, plant = 1, growing = 1,
  129. not_in_creative_inventory = 1, leafdecay = 1, leafdecay_drop = 1
  130. },
  131. sounds = farming.sounds.node_sound_leaves_defaults(),
  132. growth_check = function(pos, node_name)
  133. if minetest.find_node_near(pos, 1,
  134. {"default:jungletree", "mcl_core:jungletree"}) then
  135. return false -- can grow
  136. end
  137. return true -- cannot grow
  138. end
  139. }
  140. -- stage 1
  141. minetest.register_node("farming:cocoa_1", table.copy(def))
  142. -- stage 2
  143. def.tiles = {"farming_cocoa_2.png"}
  144. minetest.register_node("farming:cocoa_2", table.copy(def))
  145. -- stage3
  146. def.tiles = {"farming_cocoa_3.png"}
  147. def.drop = {
  148. items = {
  149. {items = {"farming:cocoa_beans_raw 1"}, rarity = 1}
  150. }
  151. }
  152. minetest.register_node("farming:cocoa_3", table.copy(def))
  153. -- stage 4 (final)
  154. def.tiles = {"farming_cocoa_4.png"}
  155. def.groups.growing = nil
  156. def.growth_check = nil
  157. def.drop = {
  158. items = {
  159. {items = {"farming:cocoa_beans_raw 2"}, rarity = 1},
  160. {items = {"farming:cocoa_beans_raw 1"}, rarity = 2},
  161. {items = {"farming:cocoa_beans_raw 1"}, rarity = 4}
  162. }
  163. }
  164. minetest.register_node("farming:cocoa_4", table.copy(def))
  165. -- add to registered_plants
  166. farming.registered_plants["farming:cocoa_beans"] = {
  167. trellis = "default:jungletree",
  168. crop = "farming:cocoa",
  169. seed = "farming:cocoa_beans_raw",
  170. minlight = farming.min_light,
  171. maxlight = farming.max_light,
  172. steps = 4
  173. }
  174. -- localize math.random for speed
  175. local random = math.random
  176. -- add random cocoa pods to jungle tree's
  177. minetest.register_on_generated(function(minp, maxp)
  178. if maxp.y < 0 then
  179. return
  180. end
  181. local pos, dir
  182. local cocoa = minetest.find_nodes_in_area(minp, maxp,
  183. {"default:jungletree", "mcl_core:jungletree"})
  184. for n = 1, #cocoa do
  185. pos = cocoa[n]
  186. if minetest.find_node_near(pos, 1,
  187. {"default:jungleleaves", "moretrees:jungletree_leaves_green",
  188. "mcl_core:jungleleaves"}) then
  189. dir = random(80)
  190. if dir == 1 then pos.x = pos.x + 1
  191. elseif dir == 2 then pos.x = pos.x - 1
  192. elseif dir == 3 then pos.z = pos.z + 1
  193. elseif dir == 4 then pos.z = pos.z -1
  194. end
  195. if dir < 5
  196. and minetest.get_node(pos).name == "air"
  197. and minetest.get_node_light(pos) > 12 then
  198. --print ("Cocoa Pod added at " .. minetest.pos_to_string(pos))
  199. minetest.swap_node(pos, {
  200. name = "farming:cocoa_" .. tostring(random(4))
  201. })
  202. end
  203. end
  204. end
  205. end)