station_churn.lua 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. unified_inventory.register_craft_type('churn', {
  2. description = 'Butter Churn',
  3. icon = 'stations_churn_icon.png',
  4. width = 1,
  5. height = 1
  6. })
  7. unified_inventory.register_craft({
  8. type = 'churn',
  9. items = {'mobs:bucket_milk'},
  10. output = 'food:butter 4'
  11. })
  12. local function milk_formspec(pos)
  13. local meta = minetest.get_meta(pos)
  14. local milk_level = tonumber(meta:get_string('milk'))
  15. local formspec =
  16. 'size[8,8.5]'..
  17. 'list[current_name;input;.5,0;1,1;]'..
  18. 'label[1.5,0.25;Input Milk]'..
  19. 'list[current_name;output;.5,2.5;1,1;]'..
  20. 'label[1.5,2.75;Take Butter]'..
  21. 'list[current_player;main;0,4.5;8,4;]'..
  22. 'image[5,.5;1,3;stations_stain_bg.png^[lowpart:'..(milk_level*12.5)..':stations_churn_fg.png]'..
  23. 'listring[context;output]'..
  24. 'listring[current_player;main]'..
  25. 'listring[context;input]'
  26. return formspec
  27. end
  28. minetest.register_node('stations:churn', {
  29. description = 'Butter Churn',
  30. drawtype = 'mesh',
  31. mesh = 'stations_churn.obj',
  32. tiles = {'stations_churn.png'},
  33. use_texture_alpha = 'opaque',
  34. sounds = default.node_sound_wood_defaults(),
  35. paramtype2 = 'facedir',
  36. paramtype = 'light',
  37. selection_box = {
  38. type = 'fixed',
  39. fixed = {{-.3, -.5, -.3, .3, .3, .3},
  40. {-.1, .3, -.1, .1, 1, .1}}},
  41. collision_box = {
  42. type = 'fixed',
  43. fixed = {{-.3, -.5, -.3, .3, .3, .3},
  44. {-.1, .3, -.1, .1, 1, .1}}},
  45. groups = {oddly_breakable_by_hand = 1, choppy=3},
  46. on_construct = function(pos)
  47. local meta = minetest.get_meta(pos)
  48. local inv = meta:get_inventory()
  49. inv:set_size('input', 1)
  50. inv:set_size('output', 1)
  51. meta:set_string('infotext', 'Butter Churn')
  52. meta:set_string('milk', 0)
  53. meta:set_string('formspec', milk_formspec(pos))
  54. end,
  55. after_place_node = function(pos, placer, itemstack)
  56. if not epic.space_on_top(pos) then
  57. minetest.remove_node(pos)
  58. return itemstack
  59. end
  60. end,
  61. after_dig_node = function(pos, oldnode, oldmetadata, digger)
  62. epic.remove_top_node(pos, oldnode)
  63. end,
  64. can_dig = function(pos,player)
  65. local meta = minetest.get_meta(pos);
  66. local inv = meta:get_inventory()
  67. if inv:is_empty('input') and inv:is_empty('output') then
  68. return true
  69. else
  70. return false
  71. end
  72. end,
  73. allow_metadata_inventory_put = function(pos, listname, index, stack, player)
  74. local input = stack:get_name()
  75. if listname == 'input' then
  76. if input == 'mobs:bucket_milk' then
  77. return 99
  78. else
  79. return 0
  80. end
  81. elseif listname == 'output' then
  82. return 0
  83. end
  84. end,
  85. on_metadata_inventory_put = function(pos, listname, index, stack, player)
  86. local input = stack:get_name()
  87. if listname == 'input' then
  88. if input == 'mobs:bucket_milk' then
  89. local meta = minetest.get_meta(pos)
  90. local inv = meta:get_inventory()
  91. local milk_level = tonumber(meta:get_string('milk'))
  92. if milk_level + 4 <= 8 then
  93. local timer = minetest.get_node_timer(pos)
  94. inv:set_stack('input', 1, 'bucket:bucket_empty')
  95. meta:set_string('milk', (milk_level + 4))
  96. timer:start(10)
  97. meta:set_string('formspec', milk_formspec(pos))
  98. meta:set_string('infotext', 'Making Butter')
  99. end
  100. end
  101. end
  102. end,
  103. on_timer = function(pos)
  104. local meta = minetest.get_meta(pos)
  105. local inv = meta:get_inventory()
  106. local milk_level = tonumber(meta:get_string('milk'))
  107. if milk_level >= 1 then
  108. local timer = minetest.get_node_timer(pos)
  109. inv:add_item('output', 'food:butter')
  110. meta:set_string('milk', (milk_level - 1))
  111. timer:start(10)
  112. meta:set_string('formspec', milk_formspec(pos))
  113. meta:set_string('infotext', 'Making Butter')
  114. else
  115. meta:set_string('infotext', 'Butter Churn')
  116. end
  117. end,
  118. })
  119. minetest.register_node('stations:churn_locked', {
  120. description = 'Butter Churn (locked)',
  121. drawtype = 'mesh',
  122. mesh = 'stations_churn.obj',
  123. tiles = {'stations_churn.png'},
  124. use_texture_alpha = 'opaque',
  125. sounds = default.node_sound_wood_defaults(),
  126. paramtype2 = 'facedir',
  127. paramtype = 'light',
  128. selection_box = {
  129. type = 'fixed',
  130. fixed = {{-.3, -.5, -.3, .3, .3, .3},
  131. {-.1, .3, -.1, .1, 1, .1}}},
  132. collision_box = {
  133. type = 'fixed',
  134. fixed = {{-.3, -.5, -.3, .3, .3, .3},
  135. {-.1, .3, -.1, .1, 1, .1}}},
  136. groups = {oddly_breakable_by_hand = 1, choppy=3},
  137. on_construct = function(pos)
  138. local meta = minetest.get_meta(pos)
  139. local inv = meta:get_inventory()
  140. inv:set_size('input', 1)
  141. inv:set_size('output', 1)
  142. meta:set_string('infotext', 'Butter Churn (locked)')
  143. meta:set_string('milk', 0)
  144. meta:set_string('formspec', milk_formspec(pos))
  145. end,
  146. after_place_node = function(pos, placer, itemstack)
  147. if not epic.space_on_top(pos) then
  148. minetest.remove_node(pos)
  149. return itemstack
  150. end
  151. end,
  152. after_dig_node = function(pos, oldnode, oldmetadata, digger)
  153. epic.remove_top_node(pos, oldnode)
  154. end,
  155. can_dig = function(pos,player)
  156. local meta = minetest.get_meta(pos);
  157. local inv = meta:get_inventory()
  158. if inv:is_empty('input') and inv:is_empty('output') then
  159. return true
  160. else
  161. return false
  162. end
  163. end,
  164. allow_metadata_inventory_put = function(pos, listname, index, stack, player)
  165. local player_name = player:get_player_name()
  166. if minetest.is_protected(pos, player_name) and not minetest.check_player_privs(player, 'protection_bypass') then
  167. return 0
  168. else
  169. local input = stack:get_name()
  170. if listname == 'input' then
  171. if input == 'mobs:bucket_milk' then
  172. return 99
  173. else
  174. return 0
  175. end
  176. elseif listname == 'output' then
  177. return 0
  178. end
  179. end
  180. end,
  181. on_metadata_inventory_put = function(pos, listname, index, stack, player)
  182. local input = stack:get_name()
  183. if listname == 'input' then
  184. if input == 'mobs:bucket_milk' then
  185. local meta = minetest.get_meta(pos)
  186. local inv = meta:get_inventory()
  187. local milk_level = tonumber(meta:get_string('milk'))
  188. if milk_level + 4 <= 8 then
  189. local timer = minetest.get_node_timer(pos)
  190. inv:set_stack('input', 1, 'bucket:bucket_empty')
  191. meta:set_string('milk', (milk_level + 4))
  192. timer:start(10)
  193. meta:set_string('formspec', milk_formspec(pos))
  194. meta:set_string('infotext', 'Making Butter')
  195. end
  196. end
  197. end
  198. end,
  199. on_timer = function(pos)
  200. local meta = minetest.get_meta(pos)
  201. local inv = meta:get_inventory()
  202. local milk_level = tonumber(meta:get_string('milk'))
  203. if milk_level >= 1 then
  204. local timer = minetest.get_node_timer(pos)
  205. inv:add_item('output', 'food:butter')
  206. meta:set_string('milk', (milk_level - 1))
  207. timer:start(10)
  208. meta:set_string('formspec', milk_formspec(pos))
  209. meta:set_string('infotext', 'Making Butter')
  210. else
  211. meta:set_string('infotext', 'Butter Churn')
  212. end
  213. end,
  214. allow_metadata_inventory_take = function(pos, listname, index, stack, player)
  215. local player_name = player:get_player_name()
  216. if minetest.is_protected(pos, player_name) and not minetest.check_player_privs(player, 'protection_bypass') then
  217. return 0
  218. else
  219. return 99
  220. end
  221. end,
  222. })