api.lua 4.7 KB

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