2
0

inventory.lua 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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("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.max(math.ceil(inv.size / (4*8)), 1)
  134. local esc = minetest.formspec_escape
  135. return sfinv.make_formspec(player, context,
  136. (inv.size == 0 and ("label[3,2;"..esc(S("No items to show.")).."]") or "") ..
  137. "label[5.8,4.15;" .. minetest.colorize("#FFFF00", tostring(pagenum)) .. " / " .. tostring(pagemax) .. "]" ..
  138. [[
  139. image[4.08,4.2;0.8,0.8;creative_trash_icon.png]
  140. listcolors[#00000069;#5A5A5A;#141318;#30434C;#FFF]
  141. list[detached:trash;main;4.02,4.1;1,1;]
  142. listring[]
  143. image_button[5,4.05;0.8,0.8;creative_prev_icon.png;creative_prev;]
  144. image_button[7.25,4.05;0.8,0.8;creative_next_icon.png;creative_next;]
  145. image_button[2.63,4.05;0.8,0.8;creative_search_icon.png;creative_search;]
  146. image_button[3.25,4.05;0.8,0.8;creative_clear_icon.png;creative_clear;]
  147. ]] ..
  148. "tooltip[creative_search;" .. esc(S("Search")) .. "]" ..
  149. "tooltip[creative_clear;" .. esc(S("Reset")) .. "]" ..
  150. "tooltip[creative_prev;" .. esc(S("Previous page")) .. "]" ..
  151. "tooltip[creative_next;" .. esc(S("Next page")) .. "]" ..
  152. "listring[current_player;main]" ..
  153. "field_close_on_enter[creative_filter;false]" ..
  154. "field[0.3,4.2;2.8,1.2;creative_filter;;" .. esc(inv.filter) .. "]" ..
  155. "listring[detached:creative_" .. player_name .. ";main]" ..
  156. "list[detached:creative_" .. player_name .. ";main;0,0;8,4;" .. tostring(inv.start_i) .. "]" ..
  157. creative.formspec_add, true)
  158. end,
  159. on_enter = function(self, player, context)
  160. local player_name = player:get_player_name()
  161. local inv = player_inventory[player_name]
  162. if inv then
  163. inv.start_i = 0
  164. end
  165. end,
  166. on_player_receive_fields = function(self, player, context, fields)
  167. local player_name = player:get_player_name()
  168. local inv = player_inventory[player_name]
  169. assert(inv)
  170. if fields.creative_clear then
  171. inv.start_i = 0
  172. inv.filter = ""
  173. sfinv.set_player_inventory_formspec(player, context)
  174. elseif (fields.creative_search or
  175. fields.key_enter_field == "creative_filter")
  176. and fields.creative_filter then
  177. inv.start_i = 0
  178. inv.filter = fields.creative_filter:sub(1, 128) -- truncate to a sane length
  179. :gsub("[%z\1-\8\11-\31\127]", "") -- strip naughty control characters (keeps \t and \n)
  180. :lower() -- search is case insensitive
  181. sfinv.set_player_inventory_formspec(player, context)
  182. elseif not fields.quit then
  183. local start_i = inv.start_i or 0
  184. if fields.creative_prev then
  185. start_i = start_i - 4*8
  186. if start_i < 0 then
  187. start_i = inv.size - (inv.size % (4*8))
  188. if inv.size == start_i then
  189. start_i = math.max(0, inv.size - (4*8))
  190. end
  191. end
  192. elseif fields.creative_next then
  193. start_i = start_i + 4*8
  194. if start_i >= inv.size then
  195. start_i = 0
  196. end
  197. end
  198. inv.start_i = start_i
  199. sfinv.set_player_inventory_formspec(player, context)
  200. end
  201. end
  202. })
  203. end
  204. -- Sort registered items
  205. local registered_nodes = {}
  206. local registered_tools = {}
  207. local registered_craftitems = {}
  208. minetest.register_on_mods_loaded(function()
  209. for name, def in pairs(minetest.registered_items) do
  210. local group = def.groups or {}
  211. local nogroup = not (group.node or group.tool or group.craftitem)
  212. if group.node or (nogroup and minetest.registered_nodes[name]) then
  213. registered_nodes[name] = def
  214. elseif group.tool or (nogroup and minetest.registered_tools[name]) then
  215. registered_tools[name] = def
  216. elseif group.craftitem or (nogroup and minetest.registered_craftitems[name]) then
  217. registered_craftitems[name] = def
  218. end
  219. end
  220. end)
  221. creative.register_tab("all", S("All"), minetest.registered_items)
  222. creative.register_tab("nodes", S("Nodes"), registered_nodes)
  223. creative.register_tab("tools", S("Tools"), registered_tools)
  224. creative.register_tab("craftitems", S("Items"), registered_craftitems)
  225. local old_homepage_name = sfinv.get_homepage_name
  226. function sfinv.get_homepage_name(player)
  227. if minetest.is_creative_enabled(player:get_player_name()) then
  228. return "creative:all"
  229. else
  230. return old_homepage_name(player)
  231. end
  232. end