api.lua 4.8 KB

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