inventory.lua 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. local player_inventory = {}
  2. local inventory_cache = {}
  3. local S = minetest.get_translator("creative")
  4. local function init_creative_cache(items)
  5. inventory_cache[items] = {}
  6. local i_cache = inventory_cache[items]
  7. for name, def in pairs(items) do
  8. if def.groups.not_in_creative_inventory ~= 1 and
  9. def.description and def.description ~= "" then
  10. i_cache[name] = def
  11. end
  12. end
  13. table.sort(i_cache)
  14. return i_cache
  15. end
  16. function creative.init_creative_inventory(player)
  17. local player_name = player:get_player_name()
  18. player_inventory[player_name] = {
  19. size = 0,
  20. filter = "",
  21. start_i = 0
  22. }
  23. minetest.create_detached_inventory("creative_" .. player_name, {
  24. allow_move = function(inv, from_list, from_index, to_list, to_index, count, player2)
  25. local name = player2 and player2:get_player_name() or ""
  26. if not creative.is_enabled_for(name) or
  27. to_list == "main" then
  28. return 0
  29. end
  30. return count
  31. end,
  32. allow_put = function(inv, listname, index, stack, player2)
  33. return 0
  34. end,
  35. allow_take = function(inv, listname, index, stack, player2)
  36. local name = player2 and player2:get_player_name() or ""
  37. if not creative.is_enabled_for(name) then
  38. return 0
  39. end
  40. return -1
  41. end,
  42. on_move = function(inv, from_list, from_index, to_list, to_index, count, player2)
  43. end,
  44. on_take = function(inv, listname, index, stack, player2)
  45. if stack and stack:get_count() > 0 then
  46. minetest.log("action", player_name .. " takes " .. stack:get_name().. " from creative inventory")
  47. end
  48. end,
  49. }, player_name)
  50. return player_inventory[player_name]
  51. end
  52. function creative.update_creative_inventory(player_name, tab_content)
  53. local creative_list = {}
  54. local inv = player_inventory[player_name] or
  55. creative.init_creative_inventory(minetest.get_player_by_name(player_name))
  56. local player_inv = minetest.get_inventory({type = "detached", name = "creative_" .. player_name})
  57. local items = inventory_cache[tab_content] or init_creative_cache(tab_content)
  58. for name, def in pairs(items) do
  59. if def.name:find(inv.filter, 1, true) or
  60. def.description:lower():find(inv.filter, 1, true) then
  61. creative_list[#creative_list+1] = name
  62. end
  63. end
  64. table.sort(creative_list)
  65. player_inv:set_size("main", #creative_list)
  66. player_inv:set_list("main", creative_list)
  67. inv.size = #creative_list
  68. end
  69. -- Create the trash field
  70. local trash = minetest.create_detached_inventory("creative_trash", {
  71. -- Allow the stack to be placed and remove it in on_put()
  72. -- This allows the creative inventory to restore the stack
  73. allow_put = function(inv, listname, index, stack, player)
  74. return stack:get_count()
  75. end,
  76. on_put = function(inv, listname)
  77. inv:set_list(listname, {})
  78. end,
  79. })
  80. trash:set_size("main", 1)
  81. creative.formspec_add = ""
  82. function creative.register_tab(name, title, items)
  83. sfinv.register_page("creative:" .. name, {
  84. title = title,
  85. is_in_nav = function(self, player, context)
  86. return creative.is_enabled_for(player:get_player_name())
  87. end,
  88. get = function(self, player, context)
  89. local player_name = player:get_player_name()
  90. creative.update_creative_inventory(player_name, items)
  91. local inv = player_inventory[player_name]
  92. local start_i = inv.start_i or 0
  93. local pagenum = math.floor(start_i / (3*8) + 1)
  94. local pagemax = math.ceil(inv.size / (3*8))
  95. return sfinv.make_formspec(player, context,
  96. "label[6.2,3.35;" .. minetest.colorize("#FFFF00", tostring(pagenum)) .. " / " .. tostring(pagemax) .. "]" ..
  97. [[
  98. image[4.06,3.4;0.8,0.8;creative_trash_icon.png]
  99. list[current_player;main;0,4.7;8,1;]
  100. list[current_player;main;0,5.85;8,3;8]
  101. list[detached:creative_trash;main;4,3.3;1,1;]
  102. listring[]
  103. image_button[5.4,3.25;0.8,0.8;creative_prev_icon.png;creative_prev;]
  104. image_button[7.2,3.25;0.8,0.8;creative_next_icon.png;creative_next;]
  105. image_button[2.1,3.25;0.8,0.8;creative_search_icon.png;creative_search;]
  106. image_button[2.75,3.25;0.8,0.8;creative_clear_icon.png;creative_clear;]
  107. tooltip[creative_search;Search]
  108. tooltip[creative_clear;Reset]
  109. tooltip[creative_prev;Previous page]
  110. tooltip[creative_next;Next page]
  111. listring[current_player;main]
  112. field_close_on_enter[creative_filter;false]
  113. ]] ..
  114. "field[0.3,3.5;2.2,1;creative_filter;;" .. minetest.formspec_escape(inv.filter) .. "]" ..
  115. "listring[detached:creative_" .. player_name .. ";main]" ..
  116. "list[detached:creative_" .. player_name .. ";main;0,0;8,3;" .. tostring(start_i) .. "]" ..
  117. creative.formspec_add, false)
  118. end,
  119. on_enter = function(self, player, context)
  120. local player_name = player:get_player_name()
  121. local inv = player_inventory[player_name]
  122. if inv then
  123. inv.start_i = 0
  124. end
  125. end,
  126. on_player_receive_fields = function(self, player, context, fields)
  127. local player_name = player:get_player_name()
  128. local inv = player_inventory[player_name]
  129. assert(inv)
  130. if fields.creative_clear then
  131. inv.start_i = 0
  132. inv.filter = ""
  133. creative.update_creative_inventory(player_name, items)
  134. sfinv.set_player_inventory_formspec(player, context)
  135. elseif fields.creative_search or
  136. fields.key_enter_field == "creative_filter" then
  137. inv.start_i = 0
  138. inv.filter = fields.creative_filter:lower()
  139. creative.update_creative_inventory(player_name, items)
  140. sfinv.set_player_inventory_formspec(player, context)
  141. elseif not fields.quit then
  142. local start_i = inv.start_i or 0
  143. if fields.creative_prev then
  144. start_i = start_i - 3*8
  145. if start_i < 0 then
  146. start_i = inv.size - (inv.size % (3*8))
  147. if inv.size == start_i then
  148. start_i = math.max(0, inv.size - (3*8))
  149. end
  150. end
  151. elseif fields.creative_next then
  152. start_i = start_i + 3*8
  153. if start_i >= inv.size then
  154. start_i = 0
  155. end
  156. end
  157. inv.start_i = start_i
  158. sfinv.set_player_inventory_formspec(player, context)
  159. end
  160. end
  161. })
  162. end
  163. creative.register_tab("all", S("All"), minetest.registered_items)
  164. creative.register_tab("nodes", S("Nodes"), minetest.registered_nodes)
  165. creative.register_tab("tools", S("Tools"), minetest.registered_tools)
  166. creative.register_tab("craftitems", S("Items"), minetest.registered_craftitems)
  167. local old_homepage_name = sfinv.get_homepage_name
  168. function sfinv.get_homepage_name(player)
  169. if creative.is_enabled_for(player:get_player_name()) then
  170. return "creative:all"
  171. else
  172. return old_homepage_name(player)
  173. end
  174. end