api.lua 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. sfinv = {
  2. pages = {},
  3. pages_unordered = {},
  4. contexts = {},
  5. enabled = true
  6. }
  7. function sfinv.register_page(name, def)
  8. assert(name, "Invalid sfinv page. Requires a name")
  9. assert(def, "Invalid sfinv page. Requires a def[inition] table")
  10. assert(def.get, "Invalid sfinv page. Def requires a get function.")
  11. assert(not sfinv.pages[name], "Attempt to register already registered sfinv page " .. dump(name))
  12. sfinv.pages[name] = def
  13. def.name = name
  14. table.insert(sfinv.pages_unordered, def)
  15. end
  16. function sfinv.override_page(name, def)
  17. assert(name, "Invalid sfinv page override. Requires a name")
  18. assert(def, "Invalid sfinv page override. Requires a def[inition] table")
  19. local page = sfinv.pages[name]
  20. assert(page, "Attempt to override sfinv page " .. dump(name) .. " which does not exist.")
  21. for key, value in pairs(def) do
  22. page[key] = value
  23. end
  24. end
  25. function sfinv.get_nav_fs(player, context, nav, current_idx)
  26. -- Only show tabs if there is more than one page
  27. if #nav > 1 then
  28. return "tabheader[0,0;sfinv_nav_tabs;" .. table.concat(nav, ",") ..
  29. ";" .. current_idx .. ";true;false]"
  30. else
  31. return ""
  32. end
  33. end
  34. local theme_inv = [[
  35. listcolors[#00000069;#5A5A5A;#141318;#30434C;#FFF]
  36. list[current_player;main;0,3.25;8,2;]
  37. ]]
  38. function sfinv.make_formspec(player, context, content, show_inv, size)
  39. local tmp = {
  40. size or "size[8,5]",
  41. sfinv.get_nav_fs(player, context, context.nav_titles, context.nav_idx),
  42. show_inv and theme_inv or "",
  43. content
  44. }
  45. return table.concat(tmp, "")
  46. end
  47. function sfinv.get_homepage_name(player)
  48. return "sfinv:help"
  49. end
  50. function sfinv.get_formspec(player, context)
  51. -- Generate navigation tabs
  52. local nav = {}
  53. local nav_ids = {}
  54. local current_idx = 1
  55. for i, pdef in pairs(sfinv.pages_unordered) do
  56. if not pdef.is_in_nav or pdef:is_in_nav(player, context) then
  57. nav[#nav + 1] = pdef.title
  58. nav_ids[#nav_ids + 1] = pdef.name
  59. if pdef.name == context.page then
  60. current_idx = #nav_ids
  61. end
  62. end
  63. end
  64. context.nav = nav_ids
  65. context.nav_titles = nav
  66. context.nav_idx = current_idx
  67. -- Generate formspec
  68. local page = sfinv.pages[context.page] or sfinv.pages["404"]
  69. if page then
  70. return page:get(player, context)
  71. else
  72. local old_page = context.page
  73. local home_page = sfinv.get_homepage_name(player)
  74. if old_page == home_page then
  75. minetest.log("error", "[sfinv] Couldn't find " .. dump(old_page) ..
  76. ", which is also the old page")
  77. return ""
  78. end
  79. context.page = home_page
  80. assert(sfinv.pages[context.page], "[sfinv] Invalid homepage")
  81. minetest.log("warning", "[sfinv] Couldn't find " .. dump(old_page) ..
  82. " so switching to homepage")
  83. return sfinv.get_formspec(player, context)
  84. end
  85. end
  86. function sfinv.get_or_create_context(player)
  87. local name = player:get_player_name()
  88. local context = sfinv.contexts[name]
  89. if not context then
  90. context = {
  91. page = sfinv.get_homepage_name(player)
  92. }
  93. sfinv.contexts[name] = context
  94. end
  95. return context
  96. end
  97. function sfinv.set_context(player, context)
  98. sfinv.contexts[player:get_player_name()] = context
  99. end
  100. function sfinv.set_player_inventory_formspec(player, context)
  101. local fs = sfinv.get_formspec(player,
  102. context or sfinv.get_or_create_context(player))
  103. player:set_inventory_formspec(fs)
  104. end
  105. function sfinv.set_page(player, pagename)
  106. local context = sfinv.get_or_create_context(player)
  107. local oldpage = sfinv.pages[context.page]
  108. if oldpage and oldpage.on_leave then
  109. oldpage:on_leave(player, context)
  110. end
  111. context.page = pagename
  112. local page = sfinv.pages[pagename]
  113. if page.on_enter then
  114. page:on_enter(player, context)
  115. end
  116. sfinv.set_player_inventory_formspec(player, context)
  117. end
  118. function sfinv.get_page(player)
  119. local context = sfinv.contexts[player:get_player_name()]
  120. return context and context.page or sfinv.get_homepage_name(player)
  121. end
  122. minetest.register_on_joinplayer(function(player)
  123. if sfinv.enabled then
  124. sfinv.set_player_inventory_formspec(player)
  125. end
  126. end)
  127. minetest.register_on_leaveplayer(function(player)
  128. sfinv.contexts[player:get_player_name()] = nil
  129. end)
  130. minetest.register_on_player_receive_fields(function(player, formname, fields)
  131. if formname ~= "" or not sfinv.enabled then
  132. return false
  133. end
  134. -- Get Context
  135. local name = player:get_player_name()
  136. local context = sfinv.contexts[name]
  137. if not context then
  138. sfinv.set_player_inventory_formspec(player)
  139. return false
  140. end
  141. -- Was a tab selected?
  142. if fields.sfinv_nav_tabs and context.nav then
  143. local tid = tonumber(fields.sfinv_nav_tabs)
  144. if tid and tid > 0 then
  145. local id = context.nav[tid]
  146. local page = sfinv.pages[id]
  147. if id and page then
  148. sfinv.set_page(player, id)
  149. end
  150. end
  151. else
  152. -- Pass event to page
  153. local page = sfinv.pages[context.page]
  154. if page and page.on_player_receive_fields then
  155. return page:on_player_receive_fields(player, context, fields)
  156. end
  157. end
  158. end)