callbacks.lua 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. minetest.register_on_joinplayer(function(player)
  2. local player_name = player:get_player_name()
  3. unified_inventory.players[player_name] = {}
  4. unified_inventory.current_index[player_name] = 1
  5. unified_inventory.filtered_items_list[player_name] =
  6. unified_inventory.items_list
  7. unified_inventory.activefilter[player_name] = ""
  8. unified_inventory.apply_filter(player, "")
  9. unified_inventory.alternate[player_name] = 1
  10. unified_inventory.current_item[player_name] = nil
  11. unified_inventory.set_inventory_formspec(player,
  12. unified_inventory.default)
  13. -- Crafting guide inventories
  14. local inv = minetest.create_detached_inventory(player_name.."craftrecipe", {
  15. allow_put = function(inv, listname, index, stack, player)
  16. return 0
  17. end,
  18. allow_take = function(inv, listname, index, stack, player)
  19. return 0
  20. end,
  21. allow_move = function(inv, from_list, from_index, to_list,
  22. to_index, count, player)
  23. return 0
  24. end,
  25. })
  26. inv:set_size("output", 1)
  27. -- Refill slot
  28. local refill = minetest.create_detached_inventory(player_name.."refill", {
  29. allow_put = function(inv, listname, index, stack, player)
  30. local player_name = player:get_player_name()
  31. if unified_inventory.is_creative(player_name) then
  32. return stack:get_count()
  33. else
  34. return 0
  35. end
  36. end,
  37. on_put = function(inv, listname, index, stack, player)
  38. local player_name = player:get_player_name()
  39. stack:set_count(stack:get_stack_max())
  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. if formname ~= "" then
  49. return
  50. end
  51. local player_name = player:get_player_name()
  52. for i, def in pairs(unified_inventory.buttons) do
  53. if fields[def.name] then
  54. def.action(player)
  55. minetest.sound_play("click",
  56. {to_player=player_name, gain = 0.1})
  57. return
  58. end
  59. end
  60. -- Inventory page controls
  61. local start = math.floor(
  62. unified_inventory.current_index[player_name] / 80 + 1)
  63. local start_i = start
  64. local pagemax = math.floor(
  65. (#unified_inventory.filtered_items_list[player_name] - 1)
  66. / (80) + 1)
  67. if fields.start_list then
  68. minetest.sound_play("paperflip1",
  69. {to_player=player_name, gain = 1.0})
  70. start_i = 1
  71. end
  72. if fields.rewind1 then
  73. minetest.sound_play("paperflip1",
  74. {to_player=player_name, gain = 1.0})
  75. start_i = start_i - 1
  76. end
  77. if fields.forward1 then
  78. minetest.sound_play("paperflip1",
  79. {to_player=player_name, gain = 1.0})
  80. start_i = start_i + 1
  81. end
  82. if fields.rewind3 then
  83. minetest.sound_play("paperflip1",
  84. {to_player=player_name, gain = 1.0})
  85. start_i = start_i - 3
  86. end
  87. if fields.forward3 then
  88. minetest.sound_play("paperflip1",
  89. {to_player=player_name, gain = 1.0})
  90. start_i = start_i + 3
  91. end
  92. if fields.end_list then
  93. minetest.sound_play("paperflip1",
  94. {to_player=player_name, gain = 1.0})
  95. start_i = pagemax
  96. end
  97. if start_i < 1 then
  98. start_i = 1
  99. end
  100. if start_i > pagemax then
  101. start_i = pagemax
  102. end
  103. if not (start_i == start) then
  104. unified_inventory.current_index[player_name] = (start_i - 1) * 80 + 1
  105. unified_inventory.set_inventory_formspec(player,
  106. unified_inventory.current_page[player_name])
  107. end
  108. local clicked_item = nil
  109. for name, value in pairs(fields) do
  110. if string.sub(name, 1, 12) == "item_button_" then
  111. clicked_item = string.sub(name, 13)
  112. break
  113. elseif string.sub(name, 1, 11) == "item_group_" then
  114. minetest.sound_play("click",
  115. {to_player=player_name, gain = 0.1})
  116. unified_inventory.apply_filter(player, "group:"..string.sub(name, 12))
  117. return
  118. end
  119. end
  120. if clicked_item then
  121. minetest.sound_play("click",
  122. {to_player=player_name, gain = 0.1})
  123. local page = unified_inventory.current_page[player_name]
  124. if not unified_inventory.is_creative(player_name) then
  125. page = "craftguide"
  126. end
  127. if page == "craftguide" then
  128. unified_inventory.current_item[player_name] = clicked_item
  129. unified_inventory.alternate[player_name] = 1
  130. unified_inventory.set_inventory_formspec(player,
  131. "craftguide")
  132. else
  133. if unified_inventory.is_creative(player_name) then
  134. local inv = player:get_inventory()
  135. local stack = ItemStack(clicked_item)
  136. stack:set_count(stack:get_stack_max())
  137. if inv:room_for_item("main", stack) then
  138. inv:add_item("main", stack)
  139. end
  140. end
  141. end
  142. end
  143. if fields.searchbutton then
  144. unified_inventory.apply_filter(player, fields.searchbox)
  145. unified_inventory.set_inventory_formspec(player,
  146. unified_inventory.current_page[player_name])
  147. minetest.sound_play("paperflip2",
  148. {to_player=player_name, gain = 1.0})
  149. end
  150. -- alternate button
  151. if fields.alternate then
  152. minetest.sound_play("click",
  153. {to_player=player_name, gain = 0.1})
  154. local item_name = unified_inventory.current_item[player_name]
  155. if item_name then
  156. local alternates = 0
  157. local alternate = unified_inventory.alternate[player_name]
  158. local crafts = unified_inventory.crafts_table[item_name]
  159. if crafts ~= nil then
  160. alternates = #crafts
  161. end
  162. if alternates > 1 then
  163. alternate = alternate + 1
  164. if alternate > alternates then
  165. alternate = 1
  166. end
  167. unified_inventory.alternate[player_name] = alternate
  168. unified_inventory.set_inventory_formspec(player,
  169. unified_inventory.current_page[player_name])
  170. end
  171. end
  172. end
  173. end)