table.lua 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. local MP = minetest.get_modpath(minetest.get_current_modname())
  2. local S, NS = dofile(MP.."/intllib.lua")
  3. local modpath_default = minetest.get_modpath("default")
  4. -- table_def can have the following:
  5. --{
  6. -- show_guides = true or false,
  7. -- alphabetize_items = true or false,
  8. -- description = string,
  9. -- hopper_node_name = string,
  10. -- enable_pipeworks = true or false,
  11. -- append_to_formspec = string,
  12. -- protect_inventory = true or false,
  13. --}
  14. simplecrafting_lib.generate_table_functions = function(craft_type, table_def)
  15. if table_def == nil then
  16. table_def = {}
  17. end
  18. local output_width = 8
  19. local output_height = 6
  20. local output_count = output_width * output_height
  21. -- Hopper compatibility
  22. if table_def.hopper_node_name and minetest.get_modpath("hopper") and hopper ~= nil and hopper.add_container ~= nil then
  23. hopper:add_container({
  24. {"top", table_def.hopper_node_name, "input"},
  25. {"bottom", table_def.hopper_node_name, "input"},
  26. {"side", table_def.hopper_node_name, "input"},
  27. })
  28. end
  29. local function make_formspec(pos, meta, inv)
  30. local row = meta:get_int("row")
  31. local item_count = inv:get_size("output")
  32. local max_mode = meta:get_string("max_mode") == "True"
  33. local pos_string = pos.x .. "," .. pos.y .. "," ..pos.z
  34. local inventory = {
  35. "size[10.2,10.2]",
  36. "list[nodemeta:"..pos_string..";input;0,0.5;2,5;]",
  37. "list[nodemeta:"..pos_string..";output;2.2,0;"..output_width..","..output_height..";" , tostring(row*output_width), "]",
  38. "list[current_player;main;1.1,6.25;8,1;]",
  39. "list[current_player;main;1.1,7.5;8,3;8]",
  40. "listring[nodemeta:"..pos_string..";output]",
  41. "listring[current_player;main]",
  42. "listring[nodemeta:"..pos_string..";input]",
  43. "listring[current_player;main]",
  44. }
  45. if table_def.description then
  46. inventory[#inventory+1] = "label[0,0;"..table_def.description.."]"
  47. end
  48. if modpath_default then
  49. inventory[#inventory+1] = default.gui_bg
  50. inventory[#inventory+1] = default.gui_bg_img
  51. inventory[#inventory+1] = default.gui_slots
  52. end
  53. local pages = false
  54. local page_button_y = "7.3"
  55. if item_count > ((row/output_height)+1) * output_count then
  56. inventory[#inventory+1] = "button[9.3,"..page_button_y..";1,0.75;next;»]"
  57. inventory[#inventory+1] = "tooltip[next;"..S("Next page of crafting products").."]"
  58. page_button_y = "8.0"
  59. pages = true
  60. end
  61. if row >= output_height then
  62. inventory[#inventory+1] = "button[9.3,"..page_button_y..";1,0.75;prev;«]"
  63. inventory[#inventory+1] = "tooltip[prev;"..S("Previous page of crafting products").."]"
  64. pages = true
  65. end
  66. if pages then
  67. inventory[#inventory+1] = "label[9.3,6.5;" .. S("Page @1", tostring(row/output_height+1)) .. "]"
  68. end
  69. if max_mode then
  70. inventory[#inventory+1] = "button[9.3,8.7;1,0.75;max_mode;"..S("Max\nOutput").."]"
  71. else
  72. inventory[#inventory+1] = "button[9.3,8.7;1,0.75;max_mode;"..S("Min\nOutput").."]"
  73. end
  74. if table_def.show_guides then
  75. inventory[#inventory+1] = "button[9.3,9.7;1,0.75;show_guide;"..S("Show\nGuide").."]"
  76. end
  77. if table_def.append_to_formspec then
  78. inventory[#inventory+1] = table_def.append_to_formspec
  79. end
  80. return table.concat(inventory), row
  81. end
  82. local function refresh_output(inv, max_mode)
  83. local craftable = simplecrafting_lib.get_craftable_items(craft_type, inv:get_list("input"), max_mode, table_def.alphabetize_items)
  84. inv:set_size("output", #craftable + (output_count - (#craftable%output_count)))
  85. inv:set_list("output", craftable)
  86. end
  87. local function refresh_inv(pos, meta)
  88. local inv = meta:get_inventory()
  89. local max_mode = meta:get_string("max_mode")
  90. refresh_output(inv, max_mode == "True")
  91. local row = meta:get_int("row")
  92. local item_count = inv:get_size("output")
  93. if item_count < output_count then
  94. meta:set_int("row", 0)
  95. elseif (row*output_width)+output_count > item_count then
  96. meta:set_int("row", (item_count - output_count) / output_width)
  97. end
  98. end
  99. local on_construct = function(pos)
  100. local meta = minetest.get_meta(pos)
  101. local inv = meta:get_inventory()
  102. inv:set_size("input", 2*5)
  103. inv:set_size("output", output_count)
  104. meta:set_int("row", 0)
  105. end
  106. local allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, number, player)
  107. if to_list == "output" then
  108. return 0
  109. end
  110. if table_def.protect_inventory and
  111. minetest.is_protected(pos, player:get_player_name())
  112. and not minetest.check_player_privs(player:get_player_name(), "protection_bypass") then
  113. return 0
  114. end
  115. if to_list == "input" then
  116. if from_list == "input" then
  117. return number
  118. end
  119. local meta = minetest.get_meta(pos)
  120. local inv = meta:get_inventory()
  121. local stack = inv:get_stack(from_list, from_index)
  122. if simplecrafting_lib.is_possible_input(craft_type, stack:get_name()) then
  123. return number
  124. end
  125. end
  126. return 0
  127. end
  128. local _pipeworks_override_player = {} -- Horrible hack. Pipeworks gets to insert stuff regardless of protection.
  129. local allow_metadata_inventory_put = function(pos, list_name, index, stack, player)
  130. if table_def.protect_inventory and
  131. player ~= _pipeworks_override_player and
  132. minetest.is_protected(pos, player:get_player_name())
  133. and not minetest.check_player_privs(player:get_player_name(), "protection_bypass") then
  134. return 0
  135. end
  136. if list_name == "output" then
  137. return 0
  138. end
  139. if list_name == "input" and simplecrafting_lib.is_possible_input(craft_type, stack:get_name()) then
  140. return stack:get_count()
  141. end
  142. return 0
  143. end
  144. -- Pipeworks compatibility
  145. local tube = nil
  146. if table_def.enable_pipeworks and minetest.get_modpath("pipeworks") then
  147. tube = {
  148. insert_object = function(pos, node, stack, direction)
  149. local meta = minetest.get_meta(pos)
  150. local inv = meta:get_inventory()
  151. return inv:add_item("input", stack)
  152. end,
  153. can_insert = function(pos, node, stack, direction)
  154. local meta = minetest.get_meta(pos)
  155. local inv = meta:get_inventory()
  156. return allow_metadata_inventory_put(pos, "input", 1, stack, _pipeworks_override_player) > 0 and inv:room_for_item("input", stack)
  157. end,
  158. input_inventory = "input",
  159. connect_sides = {left = 1, right = 1, back = 1, bottom = 1, top = 1}
  160. }
  161. end
  162. local function allow_metadata_inventory_take(pos, listname, index, stack, player)
  163. local meta = minetest.get_meta(pos)
  164. local inv = meta:get_inventory()
  165. local input_stack = inv:get_stack(listname, index)
  166. local player_inv = player:get_inventory()
  167. if table_def.protect_inventory and
  168. minetest.is_protected(pos, player:get_player_name())
  169. and not minetest.check_player_privs(player:get_player_name(), "protection_bypass") then
  170. return 0
  171. end
  172. if not player_inv:room_for_item('main', input_stack) then
  173. return 0
  174. end
  175. return stack:get_count()
  176. end
  177. local on_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, number, player)
  178. local meta = minetest.get_meta(pos)
  179. if from_list == "output" and to_list == "input" then
  180. local inv = meta:get_inventory()
  181. local stack = inv:get_stack(to_list, to_index)
  182. stack:set_count(number)
  183. simplecrafting_lib.craft_stack(craft_type, stack, inv, "input", inv, to_list, player)
  184. if simplecrafting_lib.award_crafting then
  185. simplecrafting_lib.award_crafting(player, stack)
  186. end
  187. end
  188. refresh_inv(pos, meta)
  189. end
  190. local on_metadata_inventory_take = function(pos, list_name, index, stack, player)
  191. local meta = minetest.get_meta(pos)
  192. if list_name == "output" then
  193. local inv = meta:get_inventory()
  194. local input_stack = inv:get_stack(list_name, index)
  195. if not input_stack:is_empty() and input_stack:get_name()~=stack:get_name() then
  196. local player_inv = player:get_inventory()
  197. if player_inv:room_for_item("main", input_stack) then
  198. player_inv:add_item("main", input_stack)
  199. end
  200. end
  201. simplecrafting_lib.craft_stack(craft_type, stack, inv, "input", player:get_inventory(), "main", player)
  202. if modpath_awards then
  203. awards.increment_item_counter(awards.players[player:get_player_name()], "craft", ItemStack(stack):get_name(), ItemStack(stack):get_count())
  204. end
  205. end
  206. refresh_inv(pos, meta)
  207. end
  208. local on_metadata_inventory_put = function(pos, list_name, index, stack, player)
  209. local meta = minetest.get_meta(pos)
  210. refresh_inv(pos, meta)
  211. end
  212. local on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
  213. local meta = minetest.get_meta(pos)
  214. local inv = meta:get_inventory()
  215. minetest.show_formspec(clicker:get_player_name(),
  216. "simplecrafting_lib:table_"..craft_type..minetest.pos_to_string(pos),
  217. make_formspec(pos, meta, inv))
  218. end
  219. local can_dig = function(pos, player)
  220. local meta = minetest.get_meta(pos)
  221. local inv = meta:get_inventory()
  222. return inv:is_empty("input")
  223. end
  224. local prefix_length = string.len("simplecrafting_lib:table_"..craft_type)
  225. minetest.register_on_player_receive_fields(function(sender, formname, fields)
  226. if string.sub(formname, 1, prefix_length) ~= "simplecrafting_lib:table_"..craft_type then
  227. return false -- not a formspec we handle
  228. end
  229. local pos = minetest.string_to_pos(string.sub(formname, prefix_length+1))
  230. if pos == nil then return false end
  231. local meta = minetest.get_meta(pos)
  232. local inv = meta:get_inventory()
  233. local size = inv:get_size("output")
  234. local row = meta:get_int("row")
  235. if fields.next then
  236. minetest.sound_play("paperflip1", {to_player=sender:get_player_name(), gain = 1.0})
  237. row = row + output_height
  238. meta:set_int("row", row)
  239. elseif fields.prev then
  240. minetest.sound_play("paperflip2", {to_player=sender:get_player_name(), gain = 1.0})
  241. row = row - output_height
  242. meta:set_int("row", row)
  243. elseif fields.max_mode then
  244. local max_mode = meta:get_string("max_mode")
  245. if max_mode == "" then
  246. max_mode = "True"
  247. else
  248. max_mode = ""
  249. end
  250. meta:set_string("max_mode", max_mode)
  251. refresh_output(inv, max_mode == "True")
  252. elseif fields.show_guide and table_def.show_guides then
  253. simplecrafting_lib.show_crafting_guide(craft_type, sender, function()
  254. minetest.after(0.1, function()
  255. minetest.show_formspec(sender:get_player_name(), formname, make_formspec(pos, meta, inv))
  256. end)
  257. end)
  258. return true
  259. elseif fields.quit then
  260. return true
  261. end
  262. minetest.show_formspec(sender:get_player_name(), formname, make_formspec(pos, meta, inv))
  263. return true
  264. end)
  265. return {
  266. allow_metadata_inventory_move = allow_metadata_inventory_move,
  267. allow_metadata_inventory_put = allow_metadata_inventory_put,
  268. allow_metadata_inventory_take = allow_metadata_inventory_take,
  269. can_dig = can_dig,
  270. on_construct = on_construct,
  271. on_metadata_inventory_move = on_metadata_inventory_move,
  272. on_metadata_inventory_put = on_metadata_inventory_put,
  273. on_metadata_inventory_take = on_metadata_inventory_take,
  274. on_rightclick = on_rightclick,
  275. tube = tube,
  276. }
  277. end