drink_machines.lua 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. --Craft Recipes
  2. minetest.register_craft({
  3. output = 'drinks:juice_press',
  4. recipe = {
  5. {'default:stick', 'default:steel_ingot', 'default:stick'},
  6. {'default:stick', 'bucket:bucket_empty', 'default:stick'},
  7. {'stairs:slab_wood', 'stairs:slab_wood', 'vessels:drinking_glass'},
  8. }
  9. })
  10. minetest.register_craft({
  11. output = 'drinks:liquid_barrel',
  12. recipe = {
  13. {'group:wood', 'group:wood', 'group:wood'},
  14. {'group:wood', 'group:wood', 'group:wood'},
  15. {'stairs:slab_wood', '', 'stairs:slab_wood'},
  16. }
  17. })
  18. minetest.register_craft({
  19. output = 'drinks:liquid_silo',
  20. recipe = {
  21. {'drinks:liquid_barrel'},
  22. {'drinks:liquid_barrel'},
  23. {'drinks:liquid_barrel'}
  24. }
  25. })
  26. local press_idle_formspec =
  27. 'size[8,7]'..
  28. 'label[1.5,0;Organic juice is just a squish away.]' ..
  29. 'label[4.3,.75;Put fruit here ->]'..
  30. 'label[3.5,1.75;Put container here ->]'..
  31. 'label[0.2,1.8;4 fruits to a glass,]'..
  32. 'label[0.2,2.1;8 fruits to a bottle,]'..
  33. 'label[0.2,2.4;16 fruits to a bucket.]'..
  34. 'button[1,1;2,1;press;Start Juicing]'..
  35. 'list[current_name;src;6.5,.5;1,1;]'..
  36. 'list[current_name;dst;6.5,1.5;1,1;]'..
  37. 'list[current_player;main;0,3;8,4;]'
  38. local press_running_formspec =
  39. 'size[8,7]'..
  40. 'label[1.5,0;Organic juice coming right up.]' ..
  41. 'label[4.3,.75;Put fruit here ->]'..
  42. 'label[3.5,1.75;Put container here ->]'..
  43. 'label[0.2,1.8;4 fruits to a glass,]'..
  44. 'label[0.2,2.1;8 fruits to a bottle,]'..
  45. 'label[0.2,2.4;16 fruits to a bucket.]'..
  46. 'button[1,1;2,1;press;Start Juicing]'..
  47. 'list[current_name;src;6.5,.5;1,1;]'..
  48. 'list[current_name;dst;6.5,1.5;1,1;]'..
  49. 'list[current_player;main;0,3;8,4;]'
  50. local press_error_formspec =
  51. 'size[8,7]'..
  52. 'label[1.5,0;You need to add more fruit.]' ..
  53. 'label[4.3,.75;Put fruit here ->]'..
  54. 'label[3.5,1.75;Put container here ->]'..
  55. 'label[0.2,1.8;4 fruits to a glass,]'..
  56. 'label[0.2,2.1;8 fruits to a bottle,]'..
  57. 'label[0.2,2.4;16 fruits to a bucket.]'..
  58. 'button[1,1;2,1;press;Start Juicing]'..
  59. 'list[current_name;src;6.5,.5;1,1;]'..
  60. 'list[current_name;dst;6.5,1.5;1,1;]'..
  61. 'list[current_player;main;0,3;8,4;]'
  62. minetest.register_node('drinks:juice_press', {
  63. description = 'Juice Press',
  64. _doc_items_longdesc = "A machine for creating drinks out of various fruits and vegetables.",
  65. _doc_items_usagehelp = "Right-click the press to access inventory and begin juicing.",
  66. drawtype = 'mesh',
  67. mesh = 'drinks_press.obj',
  68. tiles = {name='drinks_press.png'},
  69. groups = {choppy=2, dig_immediate=2,},
  70. paramtype = 'light',
  71. paramtype2 = 'facedir',
  72. selection_box = {
  73. type = 'fixed',
  74. fixed = {-.5, -.5, -.5, .5, .5, .5},
  75. },
  76. collision_box = {
  77. type = 'fixed',
  78. fixed = {-.5, -.5, -.5, .5, .5, .5},
  79. },
  80. on_construct = function(pos)
  81. local meta = minetest.get_meta(pos)
  82. local inv = meta:get_inventory()
  83. inv:set_size('main', 8*4)
  84. inv:set_size('src', 1)
  85. inv:set_size('dst', 1)
  86. meta:set_string('infotext', 'Empty Juice Press')
  87. meta:set_string('formspec', press_idle_formspec)
  88. end,
  89. on_receive_fields = function(pos, formname, fields, sender)
  90. if fields ['press'] then
  91. local meta = minetest.get_meta(pos)
  92. local inv = meta:get_inventory()
  93. local timer = minetest.get_node_timer(pos)
  94. local instack = inv:get_stack("src", 1)
  95. local fruitstack = instack:get_name()
  96. local mod, fruit = fruitstack:match("([^:]+):([^:]+)")
  97. if drinks.juiceable[fruit] then
  98. if string.find(fruit, '_') then
  99. local fruit, junk = fruit:match('([^_]+)_([^_]+)')
  100. meta:set_string('fruit', fruit)
  101. else
  102. meta:set_string('fruit', fruit)
  103. end
  104. local outstack = inv:get_stack("dst", 1)
  105. local vessel = outstack:get_name()
  106. if vessel == 'vessels:drinking_glass' then
  107. if instack:get_count() >= 4 then
  108. meta:set_string('container', 'jcu_')
  109. meta:set_string('fruitnumber', 4)
  110. meta:set_string('infotext', 'Juicing...')
  111. meta:set_string('formspec', press_running_formspec)
  112. timer:start(4)
  113. else
  114. meta:set_string('infotext', 'You need more fruit.')
  115. meta:set_string('formspec', press_error_formspec)
  116. end
  117. elseif vessel == 'vessels:glass_bottle' then
  118. if instack:get_count() >= 8 then
  119. meta:set_string('container', 'jbo_')
  120. meta:set_string('fruitnumber', 8)
  121. meta:set_string('infotext', 'Juicing...')
  122. meta:set_string('formspec', press_running_formspec)
  123. timer:start(8)
  124. else
  125. meta:set_string('infotext', 'You need more fruit.')
  126. meta:set_string('formspec', press_error_formspec)
  127. end
  128. elseif vessel == 'vessels:steel_bottle' then
  129. if instack:get_count() >= 8 then
  130. meta:set_string('container', 'jsb_')
  131. meta:set_string('fruitnumber', 8)
  132. meta:set_string('infotext', 'Juicing...')
  133. meta:set_string('formspec', press_running_formspec)
  134. timer:start(8)
  135. else
  136. meta:set_string('infotext', 'You need more fruit.')
  137. meta:set_string('formspec', press_error_formspec)
  138. end
  139. elseif vessel == 'bucket:bucket_empty' then
  140. if instack:get_count() >= 16 then
  141. meta:set_string('container', 'jbu_')
  142. meta:set_string('fruitnumber', 16)
  143. meta:set_string('infotext', 'Juicing...')
  144. meta:set_string('formspec', press_running_formspec)
  145. timer:start(16)
  146. else
  147. meta:set_string('infotext', 'You need more fruit.')
  148. meta:set_string('formspec', press_error_formspec)
  149. end
  150. elseif vessel == 'default:papyrus' then
  151. if instack:get_count() >= 2 then
  152. local under_node = {x=pos.x, y=pos.y-1, z=pos.z}
  153. local under_node_name = minetest.get_node_or_nil(under_node)
  154. local under_node_2 = {x=pos.x, y=pos.y-2, z=pos.z}
  155. local under_node_name_2 = minetest.get_node_or_nil(under_node_2)
  156. if under_node_name.name == 'drinks:liquid_barrel' then
  157. local meta_u = minetest.get_meta(under_node)
  158. local stored_fruit = meta_u:get_string('fruit')
  159. if fruit == stored_fruit or stored_fruit == 'empty' then
  160. meta:set_string('container', 'tube')
  161. meta:set_string('fruitnumber', 2)
  162. meta:set_string('infotext', 'Juicing...')
  163. meta_u:set_string('fruit', fruit)
  164. timer:start(4)
  165. else
  166. meta:set_string('infotext', "You can't mix juices.")
  167. end
  168. elseif under_node_name_2.name == 'drinks:liquid_silo' then
  169. local meta_u = minetest.get_meta(under_node_2)
  170. local stored_fruit = meta_u:get_string('fruit')
  171. if fruit == stored_fruit or stored_fruit == 'empty' then
  172. meta:set_string('container', 'tube')
  173. meta:set_string('fruitnumber', 2)
  174. meta:set_string('infotext', 'Juicing...')
  175. meta_u:set_string('fruit', fruit)
  176. timer:start(4)
  177. else
  178. meta:set_string('infotext', "You can't mix juices.")
  179. end
  180. else
  181. meta:set_string('infotext', 'You need more fruit.')
  182. meta:set_string('formspec', press_error_formspec)
  183. end
  184. end
  185. end
  186. end
  187. end
  188. end,
  189. on_timer = function(pos)
  190. local meta = minetest.get_meta(pos)
  191. local inv = meta:get_inventory()
  192. local container = meta:get_string('container')
  193. local instack = inv:get_stack("src", 1)
  194. local outstack = inv:get_stack("dst", 1)
  195. local fruit = meta:get_string('fruit')
  196. local fruitnumber = tonumber(meta:get_string('fruitnumber'))
  197. if container == 'tube' then
  198. local timer = minetest.get_node_timer(pos)
  199. local under_node = {x=pos.x, y=pos.y-1, z=pos.z}
  200. local under_node_name = minetest.get_node_or_nil(under_node)
  201. local under_node_2 = {x=pos.x, y=pos.y-2, z=pos.z}
  202. local under_node_name_2 = minetest.get_node_or_nil(under_node_2)
  203. if under_node_name.name == 'drinks:liquid_barrel' then
  204. local meta_u = minetest.get_meta(under_node)
  205. local fullness = tonumber(meta_u:get_string('fullness'))
  206. instack:take_item(tonumber(fruitnumber))
  207. inv:set_stack('src', 1, instack)
  208. if fullness + 2 > 128 then
  209. timer:stop()
  210. meta:set_string('infotext', 'Barrel is full of juice.')
  211. return
  212. else
  213. local fullness = fullness + 2
  214. meta_u:set_string('fullness', fullness)
  215. meta_u:set_string('infotext', (math.floor((fullness/128)*100))..' % full of '..fruit..' juice.')
  216. meta_u:set_string('formspec', drinks.liquid_storage_formspec(fruit, fullness, 128))
  217. if instack:get_count() >= 2 then
  218. timer:start(4)
  219. else
  220. meta:set_string('infotext', 'You need more fruit.')
  221. end
  222. end
  223. elseif under_node_name_2.name == 'drinks:liquid_silo' then
  224. local meta_u = minetest.get_meta(under_node_2)
  225. local fullness = tonumber(meta_u:get_string('fullness'))
  226. instack:take_item(tonumber(fruitnumber))
  227. inv:set_stack('src', 1, instack)
  228. if fullness + 2 > 256 then
  229. timer:stop()
  230. meta:set_string('infotext', 'Silo is full of juice.')
  231. return
  232. else
  233. local fullness = fullness + 2
  234. meta_u:set_string('fullness', fullness)
  235. meta_u:set_string('infotext', (math.floor((fullness/256)*100))..' % full of '..fruit..' juice.')
  236. meta_u:set_string('formspec', drinks.liquid_storage_formspec(fruit, fullness, 256))
  237. if instack:get_count() >= 2 then
  238. timer:start(4)
  239. else
  240. meta:set_string('infotext', 'You need more fruit.')
  241. end
  242. end
  243. end
  244. else
  245. meta:set_string('infotext', 'Collect your juice.')
  246. meta:set_string('formspec', press_idle_formspec)
  247. instack:take_item(tonumber(fruitnumber))
  248. inv:set_stack('src', 1, instack)
  249. inv:set_stack('dst', 1 ,'drinks:'..container..fruit)
  250. end
  251. end,
  252. on_metadata_inventory_take = function(pos, listname, index, stack, player)
  253. local timer = minetest.get_node_timer(pos)
  254. local meta = minetest.get_meta(pos)
  255. local inv = meta:get_inventory()
  256. timer:stop()
  257. meta:set_string('infotext', 'Ready for more juicing.')
  258. meta:set_string('formspec', press_idle_formspec)
  259. end,
  260. on_metadata_inventory_put = function(pos, listname, index, stack, player)
  261. local meta = minetest.get_meta(pos)
  262. local inv = meta:get_inventory()
  263. meta:set_string('infotext', 'Ready for juicing.')
  264. end,
  265. can_dig = function(pos)
  266. local meta = minetest.get_meta(pos);
  267. local inv = meta:get_inventory()
  268. if inv:is_empty("src") and
  269. inv:is_empty("dst") then
  270. return true
  271. else
  272. return false
  273. end
  274. end,
  275. allow_metadata_inventory_put = function(pos, listname, index, stack, player)
  276. if listname == 'dst' then
  277. if stack:get_name() == ('bucket:bucket_empty') then
  278. return 1
  279. elseif stack:get_name() == ('vessels:drinking_glass') then
  280. return 1
  281. elseif stack:get_name() == ('vessels:glass_bottle') then
  282. return 1
  283. elseif stack:get_name() == ('vessels:steel_bottle') then
  284. return 1
  285. elseif stack:get_name() == ('default:papyrus') then
  286. return 1
  287. else
  288. return 0
  289. end
  290. else
  291. return 99
  292. end
  293. end,
  294. })
  295. function drinks.drinks_liquid_sub(liq_vol, ves_typ, ves_vol, pos, able_to_fill, leftover_count, outputstack)
  296. local meta = minetest.get_meta(pos)
  297. local fullness = tonumber(meta:get_string('fullness'))
  298. local fruit = meta:get_string('fruit')
  299. local fruit_name = meta:get_string('fruit_name')
  300. local inv = meta:get_inventory()
  301. local fullness = fullness - (liq_vol*able_to_fill)
  302. meta:set_string('fullness', fullness)
  303. meta:set_string('infotext', (math.floor((fullness/ves_vol)*100))..' % full of '..fruit_name..' juice.')
  304. if ves_vol == 128 then
  305. meta:set_string('formspec', drinks.liquid_storage_formspec(fruit_name, fullness, 128))
  306. elseif ves_vol == 256 then
  307. meta:set_string('formspec', drinks.liquid_storage_formspec(fruit_name, fullness, 256))
  308. end
  309. if ves_typ == 'jcu' or ves_typ == 'jbo' or ves_typ == 'jsb' or ves_typ == 'jbu' then
  310. inv:set_stack('dst', 1, 'drinks:'..ves_typ..'_'..fruit..' '..able_to_fill)
  311. inv:set_stack('src', 1, outputstack..' '..leftover_count)
  312. elseif ves_typ == 'thirsty:bronze_canteen' then
  313. inv:set_stack('dst', 1, {name="thirsty:bronze_canteen", count=1, wear=60, metadata=""})
  314. elseif ves_typ == 'thirsty:steel_canteen' then
  315. inv:set_stack('dst', 1, {name="thirsty:steel_canteen", count=1, wear=40, metadata=""})
  316. end
  317. end
  318. function drinks.drinks_liquid_avail_sub(liq_vol, ves_typ, ves_vol, outputstack, pos, count)
  319. local meta = minetest.get_meta(pos)
  320. local fullness = tonumber(meta:get_string('fullness'))
  321. if fullness - (liq_vol*count) < 0 then
  322. local able_to_fill = math.floor(fullness/liq_vol)
  323. local leftover_count = count - able_to_fill
  324. drinks.drinks_liquid_sub(liq_vol, ves_typ, ves_vol, pos, able_to_fill, leftover_count, outputstack)
  325. elseif fullness - (liq_vol*count) >= 0 then
  326. drinks.drinks_liquid_sub(liq_vol, ves_typ, ves_vol, pos, count, 0, outputstack)
  327. end
  328. end
  329. function drinks.drinks_liquid_add(liq_vol, ves_typ, ves_vol, pos, inputcount, leftover_count, inputstack)
  330. local meta = minetest.get_meta(pos)
  331. local fullness = tonumber(meta:get_string('fullness'))
  332. local fruit = meta:get_string('fruit')
  333. local fruit_name = meta:get_string('fruit_name')
  334. local inv = meta:get_inventory()
  335. local fullness = fullness + (liq_vol*inputcount)
  336. meta:set_string('fullness', fullness)
  337. inv:set_stack('src', 1, ves_typ..' '..inputcount)
  338. inv:set_stack('dst', 1, inputstack..' '..leftover_count)
  339. meta:set_string('infotext', (math.floor((fullness/ves_vol)*100))..' % full of '..fruit_name..' juice.')
  340. if ves_vol == 256 then
  341. meta:set_string('formspec', drinks.liquid_storage_formspec(fruit_name, fullness, 256))
  342. elseif ves_vol == 128 then
  343. meta:set_string('formspec', drinks.liquid_storage_formspec(fruit_name, fullness, 128))
  344. end
  345. end
  346. function drinks.drinks_liquid_avail_add(liq_vol, ves_typ, ves_vol, pos, inputstack, inputcount)
  347. local meta = minetest.get_meta(pos)
  348. local fullness = tonumber(meta:get_string('fullness'))
  349. if fullness + (liq_vol*inputcount) > ves_vol then
  350. local avail_ves_vol = ves_vol - fullness
  351. local can_empty = math.floor(avail_ves_vol/liq_vol)
  352. local leftover_count = inputcount - can_empty
  353. drinks.drinks_liquid_add(liq_vol, ves_typ, ves_vol, pos, can_empty, leftover_count, inputstack)
  354. elseif fullness + (liq_vol*inputcount) <= ves_vol then
  355. drinks.drinks_liquid_add(liq_vol, ves_typ, ves_vol, pos, inputcount, 0, inputstack)
  356. end
  357. end
  358. function drinks.drinks_barrel(pos, inputstack, inputcount)
  359. local meta = minetest.get_meta(pos)
  360. local vessel = string.sub(inputstack, 8, 10)
  361. drinks.drinks_liquid_avail_add(drinks.shortname[vessel].size, drinks.shortname[vessel].name, 128, pos, inputstack, inputcount)
  362. end
  363. function drinks.drinks_silo(pos, inputstack, inputcount)
  364. local meta = minetest.get_meta(pos)
  365. local vessel = string.sub(inputstack, 8, 10)
  366. drinks.drinks_liquid_avail_add(drinks.shortname[vessel].size, drinks.shortname[vessel].name, 256, pos, inputstack, inputcount)
  367. end
  368. minetest.register_node('drinks:liquid_barrel', {
  369. description = 'Barrel of Liquid',
  370. _doc_items_longdesc = "A node that provides a simple way to store juice.",
  371. _doc_items_usagehelp = "Add or remove liquids from the barrel using buckets, bottles, or cups.",
  372. drawtype = 'mesh',
  373. mesh = 'drinks_liquid_barrel.obj',
  374. tiles = {name='drinks_barrel.png'},
  375. groups = {choppy=2, dig_immediate=2,},
  376. paramtype = 'light',
  377. paramtype2 = 'facedir',
  378. selection_box = {
  379. type = 'fixed',
  380. fixed = {-.5, -.5, -.5, .5, .5, .5},
  381. },
  382. collision_box = {
  383. type = 'fixed',
  384. fixed = {-.5, -.5, -.5, .5, .5, .5},
  385. },
  386. on_construct = function(pos)
  387. local meta = minetest.get_meta(pos)
  388. local inv = meta:get_inventory()
  389. inv:set_size('main', 8*4)
  390. inv:set_size('src', 1)
  391. inv:set_size('dst', 1)
  392. meta:set_string('fullness', 0)
  393. meta:set_string('fruit', 'empty')
  394. meta:set_string('infotext', 'Empty Drink Barrel')
  395. meta:set_string('formspec', 'size[8,8]'..
  396. 'label[0,0;Fill with the drink of your choice,]'..
  397. 'label[0,.4;you can only add more of the same type of drink.]'..
  398. 'label[4.5,1.2;Add liquid ->]'..
  399. 'label[.75,1.75;The barrel is empty]'..
  400. 'label[4.5,2.25;Take liquid ->]'..
  401. 'label[2,3.2;(This empties the barrel completely)]'..
  402. 'button[0,3;2,1;purge;Purge]'..
  403. 'list[current_name;src;6.5,1;1,1;]'..
  404. 'list[current_name;dst;6.5,2;1,1;]'..
  405. 'list[current_player;main;0,4;8,5;]')
  406. end,
  407. on_metadata_inventory_put = function(pos, listname, index, stack, player)
  408. local meta = minetest.get_meta(pos)
  409. local inv = meta:get_inventory()
  410. local instack = inv:get_stack('src', 1)
  411. local outstack = inv:get_stack('dst', 1)
  412. local outputstack = outstack:get_name()
  413. local inputstack = instack:get_name()
  414. local outputcount = outstack:get_count()
  415. local inputcount = instack:get_count()
  416. local fruit = string.sub(inputstack, 12, -1)
  417. local fruit_in = meta:get_string('fruit')
  418. if fruit_in == 'empty' then
  419. meta:set_string('fruit', fruit)
  420. local fruit_name = minetest.registered_nodes[instack:get_name()]
  421. meta:set_string('fruit_name', string.lower(fruit_name.juice_type))
  422. local vessel = string.sub(inputstack, 8, 10)
  423. drinks.drinks_barrel(pos, inputstack, inputcount)
  424. end
  425. if fruit == fruit_in then
  426. local vessel = string.sub(inputstack, 8, 10)
  427. drinks.drinks_barrel(pos, inputstack, inputcount)
  428. end
  429. if drinks.longname[outputstack] then
  430. drinks.drinks_liquid_avail_sub(drinks.longname[outputstack].size, drinks.longname[outputstack].name, 128, outputstack, pos, outputcount)
  431. end
  432. end,
  433. on_receive_fields = function(pos, formname, fields, sender)
  434. if fields['purge'] then
  435. local player_name = sender:get_player_name()
  436. if minetest.is_protected(pos, player_name) and not minetest.check_player_privs(sender, 'protection_bypass') then
  437. return
  438. end
  439. local meta = minetest.get_meta(pos)
  440. local fullness = 0
  441. local fruit_name = 'no'
  442. meta:set_string('fullness', 0)
  443. meta:set_string('fruit', 'empty')
  444. meta:set_string('infotext', 'Empty Drink Barrel')
  445. meta:set_string('formspec', drinks.liquid_storage_formspec(fruit_name, fullness, 128))
  446. end
  447. end,
  448. can_dig = function(pos)
  449. local meta = minetest.get_meta(pos);
  450. local inv = meta:get_inventory()
  451. if inv:is_empty("src") and
  452. inv:is_empty("dst") and
  453. tonumber(meta:get_string('fullness')) == 0 then
  454. return true
  455. else
  456. return false
  457. end
  458. end,
  459. allow_metadata_inventory_put = function(pos, listname, index, stack, player)
  460. local meta = minetest.get_meta(pos)
  461. if minetest.is_protected(pos, player:get_player_name()) and not minetest.check_player_privs(player, 'protection_bypass') then
  462. return 0
  463. end
  464. if listname == 'src' then --adding liquid
  465. local inputstack = stack:get_name()
  466. local inputcount = stack:get_count()
  467. local valid = string.sub(inputstack, 1, 8)
  468. if valid == 'drinks:j' then
  469. return inputcount
  470. else
  471. return 0
  472. end
  473. elseif listname == 'dst' then --removing liquid
  474. --make sure there is a liquid to remove
  475. local juice = meta:get_string('fruit')
  476. if juice ~= 'empty' then
  477. local inputstack = stack:get_name()
  478. local inputcount = stack:get_count()
  479. local valid = string.sub(inputstack, 1, 7)
  480. if valid == 'vessels' or valid == 'bucket:' then
  481. return inputcount
  482. else
  483. return 0
  484. end
  485. else
  486. return 0
  487. end
  488. end
  489. end,
  490. })
  491. minetest.register_node('drinks:liquid_silo', {
  492. description = 'Silo of Liquid',
  493. _doc_items_longdesc = "A node that provides a simple way to store juice.",
  494. _doc_items_usagehelp = "Add or remove liquids from the silo using buckets, bottles, or cups.",
  495. drawtype = 'mesh',
  496. mesh = 'drinks_silo.obj',
  497. tiles = {name='drinks_silo.png'},
  498. groups = {choppy=2, dig_immediate=2,},
  499. paramtype = 'light',
  500. paramtype2 = 'facedir',
  501. selection_box = {
  502. type = 'fixed',
  503. fixed = {-.5, -.5, -.5, .5, 1.5, .5},
  504. },
  505. collision_box = {
  506. type = 'fixed',
  507. fixed = {-.5, -.5, -.5, .5, 1.5, .5},
  508. },
  509. after_place_node = function(pos, placer, itemstack)
  510. local above = {x = pos.x, y = pos.y + 1, z = pos.z}
  511. local top_node = minetest.get_node_or_nil(above)
  512. if top_node.name ~= 'air' then
  513. minetest.remove_node(pos)
  514. return itemstack
  515. end
  516. end,
  517. on_construct = function(pos)
  518. local meta = minetest.get_meta(pos)
  519. local inv = meta:get_inventory()
  520. inv:set_size('main', 8*4)
  521. inv:set_size('src', 1)
  522. inv:set_size('dst', 1)
  523. meta:set_string('fullness', 0)
  524. meta:set_string('fruit', 'empty')
  525. meta:set_string('infotext', 'Empty Drink Silo')
  526. meta:set_string('formspec', 'size[8,8]'..
  527. 'label[0,0;Fill with the drink of your choice,]'..
  528. 'label[0,.4;you can only add more of the same type of drink.]'..
  529. 'label[4.5,1.2;Add liquid ->]'..
  530. 'label[.75,1.75;The Silo is empty]'..
  531. 'label[4.5,2.25;Take liquid ->]'..
  532. 'label[2,3.2;(This empties the silo completely)]'..
  533. 'button[0,3;2,1;purge;Purge]'..
  534. 'list[current_name;src;6.5,1;1,1;]'..
  535. 'list[current_name;dst;6.5,2;1,1;]'..
  536. 'list[current_player;main;0,4;8,5;]')
  537. end,
  538. on_metadata_inventory_put = function(pos, listname, index, stack, player)
  539. local meta = minetest.get_meta(pos)
  540. local inv = meta:get_inventory()
  541. local instack = inv:get_stack("src", 1)
  542. local outstack = inv:get_stack('dst', 1)
  543. local outputstack = outstack:get_name()
  544. local inputstack = instack:get_name()
  545. local outputcount = outstack:get_count()
  546. local inputcount = instack:get_count()
  547. local fruit = string.sub(inputstack, 12, -1)
  548. local fruit_in = meta:get_string('fruit')
  549. if fruit_in == 'empty' then
  550. meta:set_string('fruit', fruit)
  551. local fruit_name = minetest.registered_nodes[instack:get_name()]
  552. meta:set_string('fruit_name', string.lower(fruit_name.juice_type))
  553. local vessel = string.sub(inputstack, 8, 10)
  554. drinks.drinks_silo(pos, inputstack, inputcount)
  555. end
  556. if fruit == fruit_in then
  557. local vessel = string.sub(inputstack, 8, 10)
  558. drinks.drinks_silo(pos, inputstack, inputcount)
  559. end
  560. if drinks.longname[outputstack] then
  561. drinks.drinks_liquid_avail_sub(drinks.longname[outputstack].size, drinks.longname[outputstack].name, 256, outputstack, pos, outputcount)
  562. end
  563. end,
  564. on_receive_fields = function(pos, formname, fields, sender)
  565. local player_name = sender:get_player_name()
  566. if fields['purge'] then
  567. if minetest.is_protected(pos, player_name) and not minetest.check_player_privs(sender, 'protection_bypass') then
  568. return
  569. end
  570. local meta = minetest.get_meta(pos)
  571. local fullness = 0
  572. local fruit_name = 'no'
  573. meta:set_string('fullness', 0)
  574. meta:set_string('fruit', 'empty')
  575. meta:set_string('infotext', 'Empty Drink Silo')
  576. meta:set_string('formspec', drinks.liquid_storage_formspec(fruit_name, fullness, 256))
  577. end
  578. end,
  579. can_dig = function(pos)
  580. local meta = minetest.get_meta(pos);
  581. local inv = meta:get_inventory()
  582. if inv:is_empty("src") and
  583. inv:is_empty("dst") and
  584. tonumber(meta:get_string('fullness')) == 0 then
  585. return true
  586. else
  587. return false
  588. end
  589. end,
  590. allow_metadata_inventory_put = function(pos, listname, index, stack, player)
  591. local meta = minetest.get_meta(pos)
  592. if minetest.is_protected(pos, player:get_player_name()) and not minetest.check_player_privs(player, 'protection_bypass') then
  593. return 0
  594. end
  595. if listname == 'src' then --adding liquid
  596. local inputstack = stack:get_name()
  597. local inputcount = stack:get_count()
  598. local valid = string.sub(inputstack, 1, 8)
  599. if valid == 'drinks:j' then
  600. return inputcount
  601. else
  602. return 0
  603. end
  604. elseif listname == 'dst' then --removing liquid
  605. --make sure there is liquid to take_item
  606. local juice = meta:get_string('fruit')
  607. if juice ~= 'empty' then
  608. local inputstack = stack:get_name()
  609. local inputcount = stack:get_count()
  610. local valid = string.sub(inputstack, 1, 7)
  611. if valid == 'vessels' or valid == 'bucket:' then
  612. return inputcount
  613. else
  614. return 0
  615. end
  616. else
  617. return 0
  618. end
  619. end
  620. end,
  621. })