inventory.lua 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. -- creative/inventory.lua
  2. -- support for MT game translation.
  3. local S = creative.get_translator
  4. local player_inventory = {}
  5. local inventory_cache = {}
  6. local function init_creative_cache(items)
  7. inventory_cache[items] = {}
  8. local i_cache = inventory_cache[items]
  9. for name, def in pairs(items) do
  10. if def.groups.not_in_creative_inventory ~= 1 and
  11. def.description and def.description ~= "" then
  12. i_cache[name] = def
  13. end
  14. end
  15. table.sort(i_cache)
  16. return i_cache
  17. end
  18. function creative.init_creative_inventory(player)
  19. local player_name = player:get_player_name()
  20. player_inventory[player_name] = {
  21. size = 0,
  22. filter = "",
  23. start_i = 0,
  24. old_filter = nil, -- use only for caching in update_creative_inventory
  25. old_content = nil
  26. }
  27. minetest.create_detached_inventory("creative_" .. player_name, {
  28. allow_move = function(inv, from_list, from_index, to_list, to_index, count, player2)
  29. local name = player2 and player2:get_player_name() or ""
  30. if not minetest.is_creative_enabled(name) or
  31. to_list == "main" then
  32. return 0
  33. end
  34. return count
  35. end,
  36. allow_put = function(inv, listname, index, stack, player2)
  37. return 0
  38. end,
  39. allow_take = function(inv, listname, index, stack, player2)
  40. local name = player2 and player2:get_player_name() or ""
  41. if not minetest.is_creative_enabled(name) then
  42. return 0
  43. end
  44. return -1
  45. end,
  46. on_move = function(inv, from_list, from_index, to_list, to_index, count, player2)
  47. end,
  48. on_take = function(inv, listname, index, stack, player2)
  49. if stack and stack:get_count() > 0 then
  50. minetest.log("action", player_name .. " takes " .. stack:get_name().. " from creative inventory")
  51. end
  52. end,
  53. }, player_name)
  54. return player_inventory[player_name]
  55. end
  56. local NO_MATCH = 999
  57. local function match(s, filter)
  58. if filter == "" then
  59. return 0
  60. end
  61. if s:lower():find(filter, 1, true) then
  62. return #s - #filter
  63. end
  64. return NO_MATCH
  65. end
  66. local function description(def, lang_code)
  67. local s = def.description
  68. if lang_code then
  69. s = minetest.get_translated_string(lang_code, s)
  70. end
  71. return s:gsub("\n.*", "") -- First line only
  72. end
  73. function creative.update_creative_inventory(player_name, tab_content)
  74. local inv = player_inventory[player_name] or
  75. creative.init_creative_inventory(minetest.get_player_by_name(player_name))
  76. local player_inv = minetest.get_inventory({type = "detached", name = "creative_" .. player_name})
  77. if inv.filter == inv.old_filter and tab_content == inv.old_content then
  78. return
  79. end
  80. inv.old_filter = inv.filter
  81. inv.old_content = tab_content
  82. local items = inventory_cache[tab_content] or init_creative_cache(tab_content)
  83. local lang
  84. local player_info = minetest.get_player_information(player_name)
  85. if player_info and player_info.lang_code ~= "" then
  86. lang = player_info.lang_code
  87. end
  88. local creative_list = {}
  89. local order = {}
  90. for name, def in pairs(items) do
  91. local m = match(description(def), inv.filter)
  92. if m > 0 then
  93. m = math.min(m, match(description(def, lang), inv.filter))
  94. end
  95. if m > 0 then
  96. m = math.min(m, match(name, inv.filter))
  97. end
  98. if m < NO_MATCH then
  99. creative_list[#creative_list+1] = name
  100. -- Sort by match value first so closer matches appear earlier
  101. order[name] = string.format("%02d", m) .. name
  102. end
  103. end
  104. table.sort(creative_list, function(a, b) return order[a] < order[b] end)
  105. player_inv:set_size("main", #creative_list)
  106. player_inv:set_list("main", creative_list)
  107. inv.size = #creative_list
  108. end
  109. -- Create the trash field
  110. local trash = minetest.create_detached_inventory("creative_trash", {
  111. -- Allow the stack to be placed and remove it in on_put()
  112. -- This allows the creative inventory to restore the stack
  113. allow_put = function(inv, listname, index, stack, player)
  114. return stack:get_count()
  115. end,
  116. on_put = function(inv, listname)
  117. inv:set_list(listname, {})
  118. end,
  119. })
  120. trash:set_size("main", 1)
  121. creative.formspec_add = ""
  122. function creative.register_tab(name, title, items)
  123. sfinv.register_page("creative:" .. name, {
  124. title = title,
  125. is_in_nav = function(self, player, context)
  126. return minetest.is_creative_enabled(player:get_player_name())
  127. end,
  128. get = function(self, player, context)
  129. local player_name = player:get_player_name()
  130. creative.update_creative_inventory(player_name, items)
  131. local inv = player_inventory[player_name]
  132. local pagenum = math.floor(inv.start_i / (4*8) + 1)
  133. local pagemax = math.ceil(inv.size / (4*8))
  134. local esc = minetest.formspec_escape
  135. return sfinv.make_formspec(player, context,
  136. "label[5.8,4.15;" .. minetest.colorize("#FFFF00", tostring(pagenum)) .. " / " .. tostring(pagemax) .. "]" ..
  137. [[
  138. image[4.08,4.2;0.8,0.8;creative_trash_icon.png]
  139. listcolors[#00000069;#5A5A5A;#141318;#30434C;#FFF]
  140. list[detached:creative_trash;main;4.02,4.1;1,1;]
  141. listring[]
  142. image_button[5,4.05;0.8,0.8;creative_prev_icon.png;creative_prev;]
  143. image_button[7.2,4.05;0.8,0.8;creative_next_icon.png;creative_next;]
  144. image_button[2.63,4.05;0.8,0.8;creative_search_icon.png;creative_search;]
  145. image_button[3.25,4.05;0.8,0.8;creative_clear_icon.png;creative_clear;]
  146. ]] ..
  147. "tooltip[creative_search;" .. esc(S("Search")) .. "]" ..
  148. "tooltip[creative_clear;" .. esc(S("Reset")) .. "]" ..
  149. "tooltip[creative_prev;" .. esc(S("Previous page")) .. "]" ..
  150. "tooltip[creative_next;" .. esc(S("Next page")) .. "]" ..
  151. "listring[current_player;main]" ..
  152. "field_close_on_enter[creative_filter;false]" ..
  153. "field[0.3,4.2;2.8,1.2;creative_filter;;" .. esc(inv.filter) .. "]" ..
  154. "listring[detached:creative_" .. player_name .. ";main]" ..
  155. "list[detached:creative_" .. player_name .. ";main;0,0;8,4;" .. tostring(inv.start_i) .. "]" ..
  156. creative.formspec_add, true)
  157. end,
  158. on_enter = function(self, player, context)
  159. local player_name = player:get_player_name()
  160. local inv = player_inventory[player_name]
  161. if inv then
  162. inv.start_i = 0
  163. end
  164. end,
  165. on_player_receive_fields = function(self, player, context, fields)
  166. local player_name = player:get_player_name()
  167. local inv = player_inventory[player_name]
  168. assert(inv)
  169. if fields.creative_clear then
  170. inv.start_i = 0
  171. inv.filter = ""
  172. sfinv.set_player_inventory_formspec(player, context)
  173. elseif fields.creative_search or
  174. fields.key_enter_field == "creative_filter" then
  175. inv.start_i = 0
  176. inv.filter = fields.creative_filter:lower()
  177. sfinv.set_player_inventory_formspec(player, context)
  178. elseif not fields.quit then
  179. local start_i = inv.start_i or 0
  180. if fields.creative_prev then
  181. start_i = start_i - 4*8
  182. if start_i < 0 then
  183. start_i = inv.size - (inv.size % (4*8))
  184. if inv.size == start_i then
  185. start_i = math.max(0, inv.size - (4*8))
  186. end
  187. end
  188. elseif fields.creative_next then
  189. start_i = start_i + 4*8
  190. if start_i >= inv.size then
  191. start_i = 0
  192. end
  193. end
  194. inv.start_i = start_i
  195. sfinv.set_player_inventory_formspec(player, context)
  196. end
  197. end
  198. })
  199. end
  200. -- Sort registered items
  201. local registered_nodes = {}
  202. local registered_tools = {}
  203. local registered_craftitems = {}
  204. minetest.register_on_mods_loaded(function()
  205. for name, def in pairs(minetest.registered_items) do
  206. local group = def.groups or {}
  207. local nogroup = not (group.node or group.tool or group.craftitem)
  208. if group.node or (nogroup and minetest.registered_nodes[name]) then
  209. registered_nodes[name] = def
  210. elseif group.tool or (nogroup and minetest.registered_tools[name]) then
  211. registered_tools[name] = def
  212. elseif group.craftitem or (nogroup and minetest.registered_craftitems[name]) then
  213. registered_craftitems[name] = def
  214. end
  215. end
  216. end)
  217. creative.register_tab("all", S("All"), minetest.registered_items)
  218. creative.register_tab("nodes", S("Nodes"), registered_nodes)
  219. creative.register_tab("tools", S("Tools"), registered_tools)
  220. creative.register_tab("craftitems", S("Items"), registered_craftitems)
  221. local old_homepage_name = sfinv.get_homepage_name
  222. function sfinv.get_homepage_name(player)
  223. if minetest.is_creative_enabled(player:get_player_name()) then
  224. return "creative:all"
  225. else
  226. return old_homepage_name(player)
  227. end
  228. end