init.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. -- modified by dcc to work with hoppers
  2. --
  3. -- NOTE: input hopper goes on SIDE not top... to allow punching of barrel - placing hopper to point correctly
  4. -- may require screwdriver
  5. --
  6. -- output hopper goes below barrel
  7. --
  8. -- If testing - please note the abm's only fire every 30 seconds so it can take some time
  9. --
  10. compost = {}
  11. compost.items = {}
  12. compost.groups = {}
  13. function compost.register_item(name)
  14. compost.items[name] = true
  15. end
  16. function compost.register_group(name)
  17. compost.groups[name] = true
  18. end
  19. function compost.can_compost(name)
  20. if compost.items[name] then
  21. return true
  22. else
  23. for k, i in pairs(minetest.registered_items[name].groups) do
  24. if i > 0 then
  25. if compost.groups[tostring(k)] then
  26. return true
  27. end
  28. end
  29. end
  30. return false
  31. end
  32. end
  33. -- grass
  34. compost.register_item("default:grass_1")
  35. compost.register_item("default:junglegrass")
  36. -- leaves
  37. compost.register_group("leaves")
  38. -- dirt
  39. compost.register_item("default:dirt")
  40. compost.register_item("default:dirt_with_grass")
  41. -- stick
  42. compost.register_item("default:stick")
  43. -- flowers
  44. compost.register_item("flowers:geranium")
  45. compost.register_item("flowers:tulip")
  46. compost.register_item("flowers:rose")
  47. compost.register_item("flowers:dandelion_yellow")
  48. compost.register_item("flowers:dandelion_white")
  49. -- food
  50. compost.register_item("farming:bread")
  51. compost.register_item("farming:wheat")
  52. -- groups
  53. compost.register_group("plant")
  54. compost.register_group("flower")
  55. -- dcc
  56. local wood_barrel_src_on_construct = function(pos)
  57. local inv = minetest.get_meta(pos):get_inventory()
  58. inv:set_size("src", 1)
  59. end
  60. local wood_barrel_dst_on_construct = function(pos)
  61. local inv = minetest.get_meta(pos):get_inventory()
  62. inv:set_size("dst", 1)
  63. -- this is for wood barrel #3 which has 1 compost ready for taking
  64. --
  65. inv:set_stack('dst', 1, 'compost:compost')
  66. local meta = minetest.get_meta(pos)
  67. meta:set_string("formspec",
  68. "size[8,5.3]" ..
  69. default.gui_bg ..
  70. default.gui_bg_img ..
  71. default.gui_slots ..
  72. "list[current_name;dst;3.5,0;1,1;]" ..
  73. "list[current_player;main;0,1.15;8,1;]" ..
  74. "list[current_player;main;0,2.38;8,3;8]" ..
  75. "listring[current_name;main]" ..
  76. "listring[current_player;main]" ..
  77. default.get_hotbar_bg(0,1.15)
  78. )
  79. end
  80. -- Only allow compostable items to be placed in input
  81. -- allow_metadata_inventory_put is a pre-check prior to actual
  82. -- putting...
  83. --
  84. local wood_barrel_allow_metadata_inventory_put = function(pos, listname, index, stack, player)
  85. --print ("[compost] wood_barrel_allow_metadata_inventory_put ", listname)
  86. if listname == "src" then
  87. local stackname = stack:get_name()
  88. --print ("[compost] wood_barrel_allow_metadata_inventory_put ", stackname )
  89. if compost.can_compost(stackname) then
  90. --print ("[compost] succeed wood_barrel_allow_metadata_inventory_put ", stackname)
  91. return 1 -- take only 1 item
  92. end
  93. end
  94. return 0
  95. end
  96. -- actual put
  97. --
  98. local wood_barrel_on_metadata_inventory_put = function(pos, listname, index, stack, player)
  99. --print ("[compost] wood_barrel_on_metadata_inventory_put ")
  100. if listname == "src" then
  101. -- check again just in case
  102. --
  103. local stackname = stack:get_name()
  104. if compost.can_compost(stackname) then
  105. -- we received a compostable item - change to new node type
  106. --
  107. minetest.set_node(pos, {name = "compost:wood_barrel_1"})
  108. return
  109. end
  110. end
  111. end
  112. -- Versions of callbacks for the inaccessible versions of the barrel
  113. --
  114. -- nope
  115. local wood_barrelNope_allow_metadata_inventory_put = function(pos, listname, index, stack, player)
  116. --print ("[compost] wood_barrelNope_allow_metadata_inventory_put ")
  117. return 0
  118. end
  119. -- nope
  120. local wood_barrelNope_on_metadata_inventory_put = function(pos, listname, index, stack, player)
  121. --print ("[compost] wood_barrelNope_on_metadata_inventory_put ")
  122. return
  123. end
  124. -- nope
  125. local wood_barrelNope_allow_metadata_inventory_take = function(pos, listname, index, stack, player)
  126. -- nope you can't take out here
  127. return 0
  128. end
  129. -- nope
  130. local wood_barrelNope_on_metadata_inventory_take = function(pos, listname, index, stack, player)
  131. return
  132. end
  133. -- Versions of callbacks for when compost is ready....
  134. -- nope
  135. local wood_barrel_allow_metadata_inventory_take = function(pos, listname, index, stack, player)
  136. -- YES you can take out here - we know it is 1 because we put it there with the constructor
  137. return 1
  138. end
  139. -- nope
  140. local wood_barrel_on_metadata_inventory_take = function(pos, listname, index, stack, player)
  141. -- when emptied we revert to a plain wood barrel
  142. local node = minetest.get_node (pos)
  143. if node.name == "compost:wood_barrel_3" then
  144. minetest.set_node(pos, {name = "compost:wood_barrel"})
  145. return ItemStack("compost:compost")
  146. end
  147. return
  148. end
  149. minetest.register_node("compost:wood_barrel", {
  150. description = "Wood Barrel",
  151. tiles = {"default_wood.png"},
  152. drawtype = "nodebox",
  153. node_box = {
  154. type = "fixed",
  155. fixed = {{-1/2, -1/2, -1/2, 1/2, -3/8, 1/2},
  156. {-1/2, -1/2, -1/2, -3/8, 1/2, 1/2},
  157. {3/8, -1/2, -1/2, 1/2, 1/2, 1/2},
  158. {-1/2, -1/2, -1/2, 1/2, 1/2, -3/8},
  159. {-1/2, -1/2, 3/8, 1/2, 1/2, 1/2}},
  160. },
  161. paramtype = "light",
  162. is_ground_content = false,
  163. groups = {choppy = 3},
  164. sounds = default.node_sound_wood_defaults(),
  165. on_construct = wood_barrel_src_on_construct, -- dcc
  166. allow_metadata_inventory_put = wood_barrel_allow_metadata_inventory_put, -- dcc
  167. on_metadata_inventory_put = wood_barrel_on_metadata_inventory_put, -- dcc
  168. allow_metadata_inventory_take = wood_barrelNope_allow_metadata_inventory_take, -- dcc
  169. on_metadata_inventory_take = wood_barrelNope_on_metadata_inventory_take, -- dcc
  170. on_punch = function(pos, node, puncher, pointed_thing)
  171. local wielded_item = puncher:get_wielded_item():get_name()
  172. if compost.can_compost(wielded_item) then
  173. minetest.set_node(pos, {name = "compost:wood_barrel_1"})
  174. local w = puncher:get_wielded_item()
  175. if not(minetest.setting_getbool("creative_mode")) then
  176. w:take_item(1)
  177. puncher:set_wielded_item(w)
  178. end
  179. end
  180. end
  181. })
  182. minetest.register_node("compost:wood_barrel_1", {
  183. description = "Wood Barrel with compost",
  184. tiles = {"default_wood.png^compost_compost_1.png", "default_wood.png"},
  185. drawtype = "nodebox",
  186. node_box = {
  187. type = "fixed",
  188. fixed = {{-1/2, -1/2, -1/2, 1/2, -3/8, 1/2},
  189. {-1/2, -1/2, -1/2, -3/8, 1/2, 1/2},
  190. {3/8, -1/2, -1/2, 1/2, 1/2, 1/2},
  191. {-1/2, -1/2, -1/2, 1/2, 1/2, -3/8},
  192. {-1/2, -1/2, 3/8, 1/2, 1/2, 1/2},
  193. {-3/8, -1/2, -3/8, 3/8, 3/8, 3/8}},
  194. },
  195. paramtype = "light",
  196. is_ground_content = false,
  197. allow_metadata_inventory_put = wood_barrelNope_allow_metadata_inventory_put, -- dcc
  198. on_metadata_inventory_put = wood_barrelNope_on_metadata_inventory_put, -- dcc
  199. allow_metadata_inventory_take = wood_barrelNope_allow_metadata_inventory_take, -- dcc
  200. on_metadata_inventory_take = wood_barrelNope_on_metadata_inventory_take, -- dcc
  201. groups = {choppy = 3},
  202. sounds = default.node_sound_wood_defaults(),
  203. })
  204. minetest.register_node("compost:wood_barrel_2", {
  205. description = "Wood Barrel with compost",
  206. tiles = {"default_wood.png^compost_compost_2.png", "default_wood.png"},
  207. drawtype = "nodebox",
  208. node_box = {
  209. type = "fixed",
  210. fixed = {{-1/2, -1/2, -1/2, 1/2, -3/8, 1/2},
  211. {-1/2, -1/2, -1/2, -3/8, 1/2, 1/2},
  212. {3/8, -1/2, -1/2, 1/2, 1/2, 1/2},
  213. {-1/2, -1/2, -1/2, 1/2, 1/2, -3/8},
  214. {-1/2, -1/2, 3/8, 1/2, 1/2, 1/2},
  215. {-3/8, -1/2, -3/8, 3/8, 3/8, 3/8}},
  216. },
  217. paramtype = "light",
  218. is_ground_content = false,
  219. allow_metadata_inventory_put = wood_barrelNope_allow_metadata_inventory_put, -- dcc
  220. on_metadata_inventory_put = wood_barrelNope_on_metadata_inventory_put, -- dcc
  221. allow_metadata_inventory_take = wood_barrelNope_allow_metadata_inventory_take, -- dcc
  222. on_metadata_inventory_take = wood_barrelNope_on_metadata_inventory_take, -- dcc
  223. groups = {choppy = 3},
  224. sounds = default.node_sound_wood_defaults(),
  225. })
  226. minetest.register_node("compost:wood_barrel_3", {
  227. description = "Wood Barrel",
  228. tiles = {"default_wood.png^compost_compost_3.png", "default_wood.png"},
  229. drawtype = "nodebox",
  230. node_box = {
  231. type = "fixed",
  232. fixed = {{-1/2, -1/2, -1/2, 1/2, -3/8, 1/2},
  233. {-1/2, -1/2, -1/2, -3/8, 1/2, 1/2},
  234. {3/8, -1/2, -1/2, 1/2, 1/2, 1/2},
  235. {-1/2, -1/2, -1/2, 1/2, 1/2, -3/8},
  236. {-1/2, -1/2, 3/8, 1/2, 1/2, 1/2},
  237. {-3/8, -1/2, -3/8, 3/8, 3/8, 3/8}},
  238. },
  239. paramtype = "light",
  240. is_ground_content = false,
  241. groups = {choppy = 3},
  242. on_construct = wood_barrel_dst_on_construct, -- dcc
  243. allow_metadata_inventory_put = wood_barrelNope_allow_metadata_inventory_put, -- dcc
  244. on_metadata_inventory_put = wood_barrelNope_on_metadata_inventory_put, -- dcc
  245. allow_metadata_inventory_take = wood_barrel_allow_metadata_inventory_take, -- dcc
  246. on_metadata_inventory_take = wood_barrel_on_metadata_inventory_take, -- dcc
  247. sounds = default.node_sound_wood_defaults(),
  248. on_punch = function(pos, node, player, pointed_thing)
  249. local p = {x = pos.x + math.random(0, 5)/5 - 0.5, y = pos.y+1, z = pos.z + math.random(0, 5)/5 - 0.5}
  250. minetest.add_item(p, {name = "compost:compost"})
  251. minetest.set_node(pos, {name = "compost:wood_barrel"})
  252. end
  253. })
  254. minetest.register_abm({
  255. nodenames = {"compost:wood_barrel_1"},
  256. interval = 30,
  257. chance = 5,
  258. action = function(pos, node, active_object_count, active_object_count_wider)
  259. minetest.set_node(pos, {name = "compost:wood_barrel_2"})
  260. end,
  261. })
  262. minetest.register_abm({
  263. nodenames = {"compost:wood_barrel_2"},
  264. interval = 30,
  265. chance = 3,
  266. action = function(pos, node, active_object_count, active_object_count_wider)
  267. minetest.set_node(pos, {name = "compost:wood_barrel_3"})
  268. end,
  269. })
  270. minetest.register_craft({
  271. output = "compost:wood_barrel",
  272. recipe = {
  273. {"default:wood", "", "default:wood"},
  274. {"default:wood", "", "default:wood"},
  275. {"default:wood", "stairs:slab_wood", "default:wood"}
  276. }
  277. })
  278. minetest.register_node("compost:compost", {
  279. description = "Compost",
  280. tiles = {"compost_compost.png"},
  281. groups = {crumbly = 3},
  282. sounds = default.node_sound_dirt_defaults(),
  283. })
  284. minetest.register_node("compost:garden_soil", {
  285. description = "Garden Soil",
  286. tiles = {"compost_garden_soil.png"},
  287. groups = {crumbly = 3, soil=3, grassland = 1, wet = 1},
  288. sounds = default.node_sound_dirt_defaults(),
  289. })
  290. minetest.register_craft({
  291. output = "compost:garden_soil",
  292. recipe = {
  293. {"compost:compost", "compost:compost"},
  294. }
  295. })
  296. if minetest.get_modpath ("tubelib") and tubelib then
  297. --print ("[compost] found tubelib")
  298. -- Thanks to @joe7575
  299. tubelib.register_node ("compost:wood_barrel", {
  300. "compost:wood_barrel_1",
  301. "compost:wood_barrel_2",
  302. "compost:wood_barrel_3"
  303. }, {
  304. on_push_item = function (pos, side, item, player_name)
  305. local node = minetest.get_node (pos)
  306. if node.name == "compost:wood_barrel" and compost.can_compost(item:get_name()) then
  307. minetest.set_node(pos, {name = "compost:wood_barrel_1"})
  308. return true
  309. else
  310. return false
  311. end
  312. end,
  313. on_unpull_item = function (pos, side, item, player_name)
  314. minetest.set_node(pos, {name = "compost:wood_barrel_3"})
  315. return true
  316. end,
  317. on_pull_item = function (pos, side, player_name)
  318. local node = minetest.get_node (pos)
  319. if node.name == "compost:wood_barrel_3" then
  320. minetest.set_node(pos, {name = "compost:wood_barrel"})
  321. return ItemStack("compost:compost")
  322. end
  323. return nil
  324. end,
  325. })
  326. end
  327. -- dcc
  328. if minetest.get_modpath ("hopper") and hopper then
  329. --print ("[compost] found hopper")
  330. hopper:add_container({
  331. {"top", "compost:wood_barrel_3", "dst"}, -- take compost from above into hopper below
  332. {"side", "compost:wood_barrel", "src"}, -- insert compostable items below to be composted from hopper at side
  333. })
  334. end