callbacks.lua 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. })
  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. return
  114. end
  115. if new_dir == "recipe"
  116. or new_dir == "usage" then
  117. unified_inventory.current_craft_direction[player_name] = new_dir
  118. end
  119. break
  120. end
  121. end
  122. if clicked_item then
  123. minetest.sound_play("click",
  124. {to_player=player_name, gain = 0.1})
  125. local page = unified_inventory.current_page[player_name]
  126. local player_creative = unified_inventory.is_creative(player_name)
  127. if not player_creative then
  128. page = "craftguide"
  129. end
  130. if page == "craftguide" then
  131. unified_inventory.current_item[player_name] = clicked_item
  132. unified_inventory.alternate[player_name] = 1
  133. unified_inventory.set_inventory_formspec(player, "craftguide")
  134. elseif player_creative then
  135. local inv = player:get_inventory()
  136. local stack = ItemStack(clicked_item)
  137. stack:set_count(stack:get_stack_max())
  138. if inv:room_for_item("main", stack) then
  139. inv:add_item("main", stack)
  140. end
  141. end
  142. end
  143. if fields.searchbutton then
  144. unified_inventory.apply_filter(player, unified_inventory.current_searchbox[player_name], "nochange")
  145. unified_inventory.current_searchbox[player_name] = ""
  146. unified_inventory.set_inventory_formspec(player,
  147. unified_inventory.current_page[player_name])
  148. minetest.sound_play("paperflip2",
  149. {to_player=player_name, gain = 1.0})
  150. end
  151. -- alternate button
  152. if not fields.alternate then
  153. return
  154. end
  155. minetest.sound_play("click",
  156. {to_player=player_name, gain = 0.1})
  157. local item_name = unified_inventory.current_item[player_name]
  158. if not item_name then
  159. return
  160. end
  161. local crafts = unified_inventory.crafts_for[unified_inventory.current_craft_direction[player_name]][item_name]
  162. if not crafts then
  163. return
  164. end
  165. local alternates = #crafts
  166. if alternates <= 1 then
  167. return
  168. end
  169. local alternate = unified_inventory.alternate[player_name] + 1
  170. if alternate > alternates then
  171. alternate = 1
  172. end
  173. unified_inventory.alternate[player_name] = alternate
  174. unified_inventory.set_inventory_formspec(player,
  175. unified_inventory.current_page[player_name])
  176. end)