station_weaving_loom.lua 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. local function loom_formspec(progress)
  2. formspec =
  3. 'size[8,8.5]'..
  4. 'list[current_name;input;.5,1;1,1;]'..
  5. 'label[1.5,1.5;Input string]'..
  6. 'list[current_name;output;.5,3;1,1;]'..
  7. 'label[1.5,3.5;Take fabric]'..
  8. 'image[5,1;2,2.375;furniture_fabric_bg.png^[lowpart:'..(progress*20)..':furniture_fabric_fg.png]'..
  9. 'list[current_player;main;0,4.5;8,4;]'..
  10. 'listring[current_player;main]'..
  11. 'listring[context;input]'..
  12. 'listring[context;output]'..
  13. 'listring[current_player;main]'
  14. return formspec
  15. end
  16. minetest.register_node('stations:weaving_loom', {
  17. description = 'Weaving Loom',
  18. drawtype = 'mesh',
  19. mesh = 'stations_weaving_loom.obj',
  20. tiles = {'stations_weaving_loom.png'},
  21. sounds = default.node_sound_wood_defaults(),
  22. paramtype2 = 'facedir',
  23. paramtype = 'light',
  24. selection_box = {
  25. type = 'fixed',
  26. fixed = {
  27. {-.5, -.5, -.15, 1.5, 1.5, .15},
  28. }
  29. },
  30. collision_box = {
  31. type = 'fixed',
  32. fixed = {
  33. {-.5, -.5, -.15, 1.5, 1.5, .15},
  34. }
  35. },
  36. groups = {oddly_breakable_by_hand = 1, choppy=3},
  37. after_place_node = function(pos, placer, itemstack)
  38. if not epic.space_to_side(pos) then
  39. minetest.remove_node(pos)
  40. return itemstack
  41. end
  42. end,
  43. after_dig_node = function(pos, oldnode, oldmetadata, digger)
  44. epic.remove_side_node(pos, oldnode)
  45. end,
  46. on_construct = function(pos)
  47. local meta = minetest.get_meta(pos)
  48. local inv = meta:get_inventory()
  49. inv:set_size('main', 8*4)
  50. inv:set_size('input', 1)
  51. inv:set_size('output', 1)
  52. meta:set_string('progress', 0)
  53. meta:set_string('infotext', 'Weaving Loom')
  54. meta:set_string('formspec', loom_formspec(0))
  55. end,
  56. can_dig = function(pos,player)
  57. local meta = minetest.get_meta(pos);
  58. local inv = meta:get_inventory()
  59. if inv:is_empty('input') and inv:is_empty('output') then
  60. return true
  61. else
  62. return false
  63. end
  64. end,
  65. allow_metadata_inventory_put = function(pos, listname, index, stack, player)
  66. local input = stack:get_name()
  67. if listname == 'input' then
  68. if input == 'farming:string' then
  69. return 99
  70. else
  71. return 0
  72. end
  73. elseif listname == 'output' then
  74. return 0
  75. end
  76. end,
  77. on_metadata_inventory_put = function(pos, listname, index, stack, player)
  78. local timer = minetest.get_node_timer(pos)
  79. timer:start(5)
  80. end,
  81. on_metadata_inventory_take = function(pos, listname, index, stack, player)
  82. if listname == 'output' then
  83. local meta = minetest.get_meta(pos)
  84. local progress = tonumber(meta:get_string('progress'))
  85. if progress == 0 then
  86. meta:set_string('infotext', 'Weaving Loom')
  87. meta:set_string('formspec', loom_formspec(0))
  88. end
  89. end
  90. end,
  91. on_timer = function(pos)
  92. local timer = minetest.get_node_timer(pos)
  93. local meta = minetest.get_meta(pos)
  94. local progress = tonumber(meta:get_string('progress'))
  95. local inv = meta:get_inventory()
  96. local input = inv:get_stack('input', 1)
  97. if input:get_name() == 'farming:string' then
  98. input:take_item()
  99. inv:set_stack('input',1,input)
  100. meta:set_string('progress', (progress+1))
  101. meta:set_string('infotext', 'Weaving Loom ('..((progress+1)*20)..'%)')
  102. meta:set_string('formspec', loom_formspec(progress+1))
  103. timer:start(5)
  104. if progress >= 4 then
  105. inv:add_item('output', 'furniture:fabric_white')
  106. meta:set_string('progress', 0)
  107. meta:set_string('formspec', loom_formspec(0))
  108. end
  109. end
  110. end,
  111. on_rotate = function(pos, node)
  112. return false
  113. end,
  114. })
  115. minetest.register_node('stations:weaving_loom_locked', {
  116. description = 'Weaving Loom (locked)',
  117. drawtype = 'mesh',
  118. mesh = 'stations_weaving_loom.obj',
  119. tiles = {'stations_weaving_loom.png'},
  120. sounds = default.node_sound_wood_defaults(),
  121. paramtype2 = 'facedir',
  122. paramtype = 'light',
  123. selection_box = {
  124. type = 'fixed',
  125. fixed = {
  126. {-.5, -.5, -.15, 1.5, 1.5, .15},
  127. }
  128. },
  129. collision_box = {
  130. type = 'fixed',
  131. fixed = {
  132. {-.5, -.5, -.15, 1.5, 1.5, .15},
  133. }
  134. },
  135. groups = {oddly_breakable_by_hand = 1, choppy=3},
  136. after_place_node = function(pos, placer, itemstack)
  137. if not epic.space_to_side(pos) then
  138. minetest.remove_node(pos)
  139. return itemstack
  140. end
  141. end,
  142. after_dig_node = function(pos, oldnode, oldmetadata, digger)
  143. epic.remove_side_node(pos, oldnode)
  144. end,
  145. on_construct = function(pos)
  146. local meta = minetest.get_meta(pos)
  147. local inv = meta:get_inventory()
  148. inv:set_size('main', 8*4)
  149. inv:set_size('input', 1)
  150. inv:set_size('output', 1)
  151. meta:set_string('progress', 0)
  152. meta:set_string('infotext', 'Weaving Loom (locked)')
  153. meta:set_string('formspec', loom_formspec(0))
  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 == 'farming:string' 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 timer = minetest.get_node_timer(pos)
  183. timer:start(5)
  184. end,
  185. allow_metadata_inventory_take = function(pos, listname, index, stack, player)
  186. local player_name = player:get_player_name()
  187. if minetest.is_protected(pos, player_name) and not minetest.check_player_privs(player, 'protection_bypass') then
  188. return 0
  189. else
  190. return 99
  191. end
  192. end,
  193. on_metadata_inventory_take = function(pos, listname, index, stack, player)
  194. if listname == 'output' then
  195. local meta = minetest.get_meta(pos)
  196. local progress = tonumber(meta:get_string('progress'))
  197. if progress == 0 then
  198. meta:set_string('infotext', 'Weaving Loom')
  199. meta:set_string('formspec', loom_formspec(0))
  200. end
  201. end
  202. end,
  203. on_timer = function(pos)
  204. local timer = minetest.get_node_timer(pos)
  205. local meta = minetest.get_meta(pos)
  206. local progress = tonumber(meta:get_string('progress'))
  207. local inv = meta:get_inventory()
  208. local input = inv:get_stack('input', 1)
  209. if input:get_name() == 'farming:string' then
  210. input:take_item()
  211. inv:set_stack('input',1,input)
  212. meta:set_string('progress', (progress+1))
  213. meta:set_string('infotext', 'Weaving Loom ('..((progress+1)*20)..'%)')
  214. meta:set_string('formspec', loom_formspec(progress+1))
  215. timer:start(5)
  216. if progress >= 4 then
  217. inv:add_item('output', 'furniture:fabric_white')
  218. meta:set_string('progress', 0)
  219. meta:set_string('formspec', loom_formspec(0))
  220. end
  221. end
  222. end,
  223. on_rotate = function(pos, node)
  224. return false
  225. end,
  226. })