api.lua 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. image[0,5.2;1,1;gui_hb_bg.png]
  36. image[1,5.2;1,1;gui_hb_bg.png]
  37. image[2,5.2;1,1;gui_hb_bg.png]
  38. image[3,5.2;1,1;gui_hb_bg.png]
  39. image[4,5.2;1,1;gui_hb_bg.png]
  40. image[5,5.2;1,1;gui_hb_bg.png]
  41. image[6,5.2;1,1;gui_hb_bg.png]
  42. image[7,5.2;1,1;gui_hb_bg.png]
  43. list[current_player;main;0,5.2;8,1;]
  44. list[current_player;main;0,6.35;8,3;8]
  45. ]]
  46. function sfinv.make_formspec(player, context, content, show_inv, size)
  47. local tmp = {
  48. size or "size[8,9.1]",
  49. sfinv.get_nav_fs(player, context, context.nav_titles, context.nav_idx),
  50. show_inv and theme_inv or "",
  51. content
  52. }
  53. return table.concat(tmp, "")
  54. end
  55. function sfinv.get_homepage_name(player)
  56. return "sfinv:crafting"
  57. end
  58. function sfinv.get_formspec(player, context)
  59. -- Generate navigation tabs
  60. local nav = {}
  61. local nav_ids = {}
  62. local current_idx = 1
  63. for i, pdef in pairs(sfinv.pages_unordered) do
  64. if not pdef.is_in_nav or pdef:is_in_nav(player, context) then
  65. nav[#nav + 1] = pdef.title
  66. nav_ids[#nav_ids + 1] = pdef.name
  67. if pdef.name == context.page then
  68. current_idx = #nav_ids
  69. end
  70. end
  71. end
  72. context.nav = nav_ids
  73. context.nav_titles = nav
  74. context.nav_idx = current_idx
  75. -- Generate formspec
  76. local page = sfinv.pages[context.page] or sfinv.pages["404"]
  77. if page then
  78. return page:get(player, context)
  79. else
  80. local old_page = context.page
  81. local home_page = sfinv.get_homepage_name(player)
  82. if old_page == home_page then
  83. minetest.log("error", "[sfinv] Couldn't find " .. dump(old_page) ..
  84. ", which is also the old page")
  85. return ""
  86. end
  87. context.page = home_page
  88. assert(sfinv.pages[context.page], "[sfinv] Invalid homepage")
  89. minetest.log("warning", "[sfinv] Couldn't find " .. dump(old_page) ..
  90. " so switching to homepage")
  91. return sfinv.get_formspec(player, context)
  92. end
  93. end
  94. function sfinv.get_or_create_context(player)
  95. local name = player:get_player_name()
  96. local context = sfinv.contexts[name]
  97. if not context then
  98. context = {
  99. page = sfinv.get_homepage_name(player)
  100. }
  101. sfinv.contexts[name] = context
  102. end
  103. return context
  104. end
  105. function sfinv.set_context(player, context)
  106. sfinv.contexts[player:get_player_name()] = context
  107. end
  108. function sfinv.set_player_inventory_formspec(player, context)
  109. local fs = sfinv.get_formspec(player,
  110. context or sfinv.get_or_create_context(player))
  111. player:set_inventory_formspec(fs)
  112. end
  113. function sfinv.set_page(player, pagename)
  114. local context = sfinv.get_or_create_context(player)
  115. local oldpage = sfinv.pages[context.page]
  116. if oldpage and oldpage.on_leave then
  117. oldpage:on_leave(player, context)
  118. end
  119. context.page = pagename
  120. local page = sfinv.pages[pagename]
  121. if page.on_enter then
  122. page:on_enter(player, context)
  123. end
  124. sfinv.set_player_inventory_formspec(player, context)
  125. end
  126. function sfinv.get_page(player)
  127. local context = sfinv.contexts[player:get_player_name()]
  128. return context and context.page or sfinv.get_homepage_name(player)
  129. end
  130. minetest.register_on_joinplayer(function(player)
  131. if sfinv.enabled then
  132. sfinv.set_player_inventory_formspec(player)
  133. end
  134. end)
  135. minetest.register_on_leaveplayer(function(player)
  136. sfinv.contexts[player:get_player_name()] = nil
  137. end)
  138. minetest.register_on_player_receive_fields(function(player, formname, fields)
  139. if formname ~= "" or not sfinv.enabled then
  140. return false
  141. end
  142. -- Get Context
  143. local name = player:get_player_name()
  144. local context = sfinv.contexts[name]
  145. if not context then
  146. sfinv.set_player_inventory_formspec(player)
  147. return false
  148. end
  149. -- Was a tab selected?
  150. if fields.sfinv_nav_tabs and context.nav then
  151. local tid = tonumber(fields.sfinv_nav_tabs)
  152. if tid and tid > 0 then
  153. local id = context.nav[tid]
  154. local page = sfinv.pages[id]
  155. if id and page then
  156. sfinv.set_page(player, id)
  157. end
  158. end
  159. else
  160. -- Pass event to page
  161. local page = sfinv.pages[context.page]
  162. if page and page.on_player_receive_fields then
  163. return page:on_player_receive_fields(player, context, fields)
  164. end
  165. end
  166. end)