callbacks.lua 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. local function default_refill(stack)
  2. stack:set_count(stack:get_stack_max())
  3. local itemdef = minetest.registered_items[stack:get_name()]
  4. if itemdef
  5. and (itemdef.wear_represents or "mechanical_wear") == "mechanical_wear"
  6. and stack:get_wear() ~= 0 then
  7. stack:set_wear(0)
  8. end
  9. return stack
  10. end
  11. minetest.register_on_joinplayer(function(player)
  12. local player_name = player:get_player_name()
  13. unified_inventory.players[player_name] = {}
  14. unified_inventory.current_index[player_name] = 1
  15. unified_inventory.filtered_items_list[player_name] =
  16. unified_inventory.items_list
  17. unified_inventory.activefilter[player_name] = ""
  18. unified_inventory.active_search_direction[player_name] = "nochange"
  19. unified_inventory.apply_filter(player, "", "nochange")
  20. unified_inventory.current_searchbox[player_name] = ""
  21. unified_inventory.alternate[player_name] = 1
  22. unified_inventory.current_item[player_name] = nil
  23. unified_inventory.current_craft_direction[player_name] = "recipe"
  24. unified_inventory.set_inventory_formspec(player,
  25. unified_inventory.default)
  26. -- Refill slot
  27. local refill = minetest.create_detached_inventory(player_name.."refill", {
  28. allow_put = function(inv, listname, index, stack, player)
  29. local player_name = player:get_player_name()
  30. if unified_inventory.is_creative(player_name) then
  31. return stack:get_count()
  32. else
  33. return 0
  34. end
  35. end,
  36. on_put = function(inv, listname, index, stack, player)
  37. local player_name = player:get_player_name()
  38. local handle_refill = (minetest.registered_items[stack:get_name()] or {}).on_refill or default_refill
  39. stack = handle_refill(stack)
  40. inv:set_stack(listname, index, stack)
  41. minetest.sound_play("electricity",
  42. {to_player=player_name, gain = 1.0})
  43. end,
  44. }, player_name)
  45. refill:set_size("main", 1)
  46. end)
  47. minetest.register_on_player_receive_fields(function(player, formname, fields)
  48. local player_name = player:get_player_name()
  49. local ui_peruser,draw_lite_mode = unified_inventory.get_per_player_formspec(player_name)
  50. if formname ~= "" then
  51. return
  52. end
  53. -- always take new search text, even if not searching on it yet
  54. if fields.searchbox
  55. and fields.searchbox ~= unified_inventory.current_searchbox[player_name] then
  56. unified_inventory.current_searchbox[player_name] = fields.searchbox
  57. unified_inventory.set_inventory_formspec(player, unified_inventory.current_page[player_name])
  58. end
  59. for i, def in pairs(unified_inventory.buttons) do
  60. if fields[def.name] then
  61. def.action(player)
  62. minetest.sound_play("click",
  63. {to_player=player_name, gain = 0.1})
  64. return
  65. end
  66. end
  67. -- Inventory page controls
  68. local start = math.floor(
  69. unified_inventory.current_index[player_name] / ui_peruser.items_per_page + 1)
  70. local start_i = start
  71. local pagemax = math.floor(
  72. (#unified_inventory.filtered_items_list[player_name] - 1)
  73. / (ui_peruser.items_per_page) + 1)
  74. if fields.start_list then
  75. start_i = 1
  76. end
  77. if fields.rewind1 then
  78. start_i = start_i - 1
  79. end
  80. if fields.forward1 then
  81. start_i = start_i + 1
  82. end
  83. if fields.rewind3 then
  84. start_i = start_i - 3
  85. end
  86. if fields.forward3 then
  87. start_i = start_i + 3
  88. end
  89. if fields.end_list then
  90. start_i = pagemax
  91. end
  92. if start_i < 1 then
  93. start_i = 1
  94. end
  95. if start_i > pagemax then
  96. start_i = pagemax
  97. end
  98. if start_i ~= start then
  99. minetest.sound_play("paperflip1",
  100. {to_player=player_name, gain = 1.0})
  101. unified_inventory.current_index[player_name] = (start_i - 1) * ui_peruser.items_per_page + 1
  102. unified_inventory.set_inventory_formspec(player,
  103. unified_inventory.current_page[player_name])
  104. end
  105. local clicked_item
  106. for name, value in pairs(fields) do
  107. if string.sub(name, 1, 12) == "item_button_" then
  108. local new_dir, mangled_item = string.match(name, "^item_button_([a-z]+)_(.*)$")
  109. clicked_item = unified_inventory.demangle_for_formspec(mangled_item)
  110. if string.sub(clicked_item, 1, 6) == "group:" then
  111. minetest.sound_play("click", {to_player=player_name, gain = 0.1})
  112. unified_inventory.apply_filter(player, clicked_item, new_dir)
  113. unified_inventory.current_searchbox[player_name] = clicked_item
  114. unified_inventory.set_inventory_formspec(player,
  115. unified_inventory.current_page[player_name])
  116. return
  117. end
  118. if new_dir == "recipe"
  119. or new_dir == "usage" then
  120. unified_inventory.current_craft_direction[player_name] = new_dir
  121. end
  122. break
  123. end
  124. end
  125. if clicked_item then
  126. minetest.sound_play("click",
  127. {to_player=player_name, gain = 0.1})
  128. local page = unified_inventory.current_page[player_name]
  129. local player_creative = unified_inventory.is_creative(player_name)
  130. if not player_creative then
  131. page = "craftguide"
  132. end
  133. if page == "craftguide" then
  134. unified_inventory.current_item[player_name] = clicked_item
  135. unified_inventory.alternate[player_name] = 1
  136. unified_inventory.set_inventory_formspec(player, "craftguide")
  137. elseif player_creative then
  138. local inv = player:get_inventory()
  139. local stack = ItemStack(clicked_item)
  140. stack:set_count(stack:get_stack_max())
  141. if inv:room_for_item("main", stack) then
  142. inv:add_item("main", stack)
  143. end
  144. end
  145. end
  146. if fields.searchbutton
  147. or fields.key_enter_field == "searchbox" then
  148. unified_inventory.apply_filter(player, unified_inventory.current_searchbox[player_name], "nochange")
  149. unified_inventory.set_inventory_formspec(player,
  150. unified_inventory.current_page[player_name])
  151. minetest.sound_play("paperflip2",
  152. {to_player=player_name, gain = 1.0})
  153. elseif fields.searchresetbutton then
  154. unified_inventory.apply_filter(player, "", "nochange")
  155. unified_inventory.current_searchbox[player_name] = ""
  156. unified_inventory.set_inventory_formspec(player,
  157. unified_inventory.current_page[player_name])
  158. minetest.sound_play("click",
  159. {to_player=player_name, gain = 0.1})
  160. end
  161. -- alternate buttons
  162. if not (fields.alternate or fields.alternate_prev) then
  163. return
  164. end
  165. minetest.sound_play("click",
  166. {to_player=player_name, gain = 0.1})
  167. local item_name = unified_inventory.current_item[player_name]
  168. if not item_name then
  169. return
  170. end
  171. local crafts = unified_inventory.crafts_for[unified_inventory.current_craft_direction[player_name]][item_name]
  172. if not crafts then
  173. return
  174. end
  175. local alternates = #crafts
  176. if alternates <= 1 then
  177. return
  178. end
  179. local alternate
  180. if fields.alternate then
  181. alternate = unified_inventory.alternate[player_name] + 1
  182. if alternate > alternates then
  183. alternate = 1
  184. end
  185. elseif fields.alternate_prev then
  186. alternate = unified_inventory.alternate[player_name] - 1
  187. if alternate < 1 then
  188. alternate = alternates
  189. end
  190. end
  191. unified_inventory.alternate[player_name] = alternate
  192. unified_inventory.set_inventory_formspec(player,
  193. unified_inventory.current_page[player_name])
  194. end)
  195. if minetest.delete_detached_inventory then
  196. minetest.register_on_leaveplayer(function(player)
  197. local player_name = player:get_player_name()
  198. minetest.delete_detached_inventory(player_name.."_bags")
  199. minetest.delete_detached_inventory(player_name.."craftrecipe")
  200. minetest.delete_detached_inventory(player_name.."refill")
  201. end)
  202. end