cocoa.lua 5.7 KB

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