init.lua 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. worldedit = worldedit or {}
  2. --[[
  3. Example:
  4. worldedit.register_gui_function("worldedit_gui_hollow_cylinder", {
  5. name = "Make Hollow Cylinder",
  6. privs = {worldedit=true},
  7. get_formspec = function(name) return "some formspec here" end,
  8. on_select = function(name) print(name .. " clicked the button!") end,
  9. })
  10. Use `nil` for the `options` parameter to unregister the function associated with the given identifier.
  11. Use `nil` for the `get_formspec` field to denote that the function does not have its own screen.
  12. The `privs` field may not be `nil`.
  13. If the identifier is already registered to another function, it will be replaced by the new one.
  14. The `on_select` function must not call `worldedit.show_page`
  15. ]]
  16. worldedit.pages = {} --mapping of identifiers to options
  17. local identifiers = {} --ordered list of identifiers
  18. worldedit.register_gui_function = function(identifier, options)
  19. if options.privs == nil or next(options.privs) == nil then
  20. error("privs unset")
  21. end
  22. worldedit.pages[identifier] = options
  23. table.insert(identifiers, identifier)
  24. end
  25. --[[
  26. Example:
  27. worldedit.register_gui_handler("worldedit_gui_hollow_cylinder", function(name, fields)
  28. print(minetest.serialize(fields))
  29. end)
  30. ]]
  31. worldedit.register_gui_handler = function(identifier, handler)
  32. local enabled = true
  33. minetest.register_on_player_receive_fields(function(player, formname, fields)
  34. if not enabled then return false end
  35. enabled = false
  36. minetest.after(0.2, function() enabled = true end)
  37. local name = player:get_player_name()
  38. --ensure the player has permission to perform the action
  39. local entry = worldedit.pages[identifier]
  40. if entry and minetest.check_player_privs(name, entry.privs) then
  41. return handler(name, fields)
  42. end
  43. return false
  44. end)
  45. end
  46. worldedit.get_formspec_header = function(identifier)
  47. local entry = worldedit.pages[identifier] or {}
  48. return "button[0,0;2,0.5;worldedit_gui;Back]" ..
  49. string.format("label[2,0;WorldEdit GUI > %s]", entry.name or "")
  50. end
  51. local get_formspec = function(name, identifier)
  52. if worldedit.pages[identifier] then
  53. return worldedit.pages[identifier].get_formspec(name)
  54. end
  55. return worldedit.pages["worldedit_gui"].get_formspec(name) --default to showing main page if an unknown page is given
  56. end
  57. --implement worldedit.show_page(name, page) in different ways depending on the available APIs
  58. if minetest.global_exists("unified_inventory") then -- unified inventory installed
  59. local old_func = worldedit.register_gui_function
  60. worldedit.register_gui_function = function(identifier, options)
  61. old_func(identifier, options)
  62. unified_inventory.register_page(identifier, {get_formspec=function(player) return {formspec=options.get_formspec(player:get_player_name())} end})
  63. end
  64. unified_inventory.register_button("worldedit_gui", {
  65. type = "image",
  66. image = "inventory_plus_worldedit_gui.png",
  67. })
  68. minetest.register_on_player_receive_fields(function(player, formname, fields)
  69. local name = player:get_player_name()
  70. if fields.worldedit_gui then --main page
  71. worldedit.show_page(name, "worldedit_gui")
  72. return true
  73. elseif fields.worldedit_gui_exit then --return to original page
  74. local player = minetest.get_player_by_name(name)
  75. if player then
  76. unified_inventory.set_inventory_formspec(player, "craft")
  77. end
  78. return true
  79. end
  80. return false
  81. end)
  82. worldedit.show_page = function(name, page)
  83. local player = minetest.get_player_by_name(name)
  84. if player then
  85. player:set_inventory_formspec(get_formspec(name, page))
  86. end
  87. end
  88. elseif minetest.global_exists("inventory_plus") then -- inventory++ installed
  89. minetest.register_on_joinplayer(function(player)
  90. local can_worldedit = minetest.check_player_privs(player:get_player_name(), {worldedit=true})
  91. if can_worldedit then
  92. inventory_plus.register_button(player, "worldedit_gui", "WorldEdit")
  93. end
  94. end)
  95. --show the form when the button is pressed and hide it when done
  96. local gui_player_formspecs = {}
  97. minetest.register_on_player_receive_fields(function(player, formname, fields)
  98. local name = player:get_player_name()
  99. if fields.worldedit_gui then --main page
  100. gui_player_formspecs[name] = player:get_inventory_formspec()
  101. worldedit.show_page(name, "worldedit_gui")
  102. return true
  103. elseif fields.worldedit_gui_exit then --return to original page
  104. if gui_player_formspecs[name] then
  105. inventory_plus.set_inventory_formspec(player, inventory_plus.get_formspec(player, "main"))
  106. end
  107. return true
  108. end
  109. return false
  110. end)
  111. worldedit.show_page = function(name, page)
  112. local player = minetest.get_player_by_name(name)
  113. if player then
  114. inventory_plus.set_inventory_formspec(player, get_formspec(name, page))
  115. end
  116. end
  117. elseif minetest.global_exists("smart_inventory") then -- smart_inventory installed
  118. -- redefinition: Update the code element on inventory page to show the we-page
  119. function worldedit.show_page(name, page)
  120. local state = smart_inventory.get_page_state("worldedit_gui", name)
  121. if state then
  122. state:get("code"):set_we_formspec(page)
  123. state.location.rootState:show() -- update inventory page
  124. end
  125. end
  126. -- smart_inventory page callback. Contains just a "custom code" element
  127. local function smart_worldedit_gui_callback(state)
  128. local codebox = state:element("code", { name = "code", code = "" })
  129. function codebox:set_we_formspec(we_page)
  130. local new_formspec = get_formspec(state.location.rootState.location.player, we_page)
  131. new_formspec = new_formspec:gsub('button_exit','button') --no inventory closing
  132. self.data.code = "container[1,1]".. new_formspec .. "container_end[]"
  133. end
  134. codebox:set_we_formspec("worldedit_gui")
  135. -- process input (the back button)
  136. state:onInput(function(state, fields, player)
  137. if fields.worldedit_gui then --main page
  138. state:get("code"):set_we_formspec("worldedit_gui")
  139. elseif fields.worldedit_gui_exit then --return to original page
  140. state:get("code"):set_we_formspec("worldedit_gui")
  141. state.location.parentState:get("crafting_button"):submit() -- switch to the crafting tab
  142. end
  143. end)
  144. end
  145. -- all handler should return false to force inventory UI update
  146. local orig_register_gui_handler = worldedit.register_gui_handler
  147. worldedit.register_gui_handler = function(identifier, handler)
  148. local wrapper = function(...)
  149. handler(...)
  150. return false
  151. end
  152. orig_register_gui_handler(identifier, wrapper)
  153. end
  154. -- register the inventory button
  155. smart_inventory.register_page({
  156. name = "worldedit_gui",
  157. tooltip = "Edit your World!",
  158. icon = "inventory_plus_worldedit_gui.png",
  159. smartfs_callback = smart_worldedit_gui_callback,
  160. sequence = 99
  161. })
  162. elseif minetest.global_exists("sfinv") then -- sfinv installed
  163. assert(sfinv.enabled)
  164. local orig_get = sfinv.pages["sfinv:crafting"].get
  165. sfinv.override_page("sfinv:crafting", {
  166. get = function(self, player, context)
  167. local can_worldedit = minetest.check_player_privs(player, {worldedit=true})
  168. local fs = orig_get(self, player, context)
  169. return fs .. (can_worldedit and "image_button[0,0;1,1;inventory_plus_worldedit_gui.png;worldedit_gui;]" or "")
  170. end
  171. })
  172. --show the form when the button is pressed and hide it when done
  173. minetest.register_on_player_receive_fields(function(player, formname, fields)
  174. if fields.worldedit_gui then --main page
  175. worldedit.show_page(player:get_player_name(), "worldedit_gui")
  176. return true
  177. elseif fields.worldedit_gui_exit then --return to original page
  178. sfinv.set_page(player, "sfinv:crafting")
  179. return true
  180. end
  181. return false
  182. end)
  183. worldedit.show_page = function(name, page)
  184. local player = minetest.get_player_by_name(name)
  185. if player then
  186. player:set_inventory_formspec(get_formspec(name, page))
  187. end
  188. end
  189. else
  190. error(
  191. "worldedit_gui requires a supported gui management mod to be installed.\n"..
  192. "To use the it you need to either:\n"..
  193. "* use minetest_game or another sfinv-compatible subgame\n"..
  194. "* install Unified Inventory, Inventory++ or Smart Inventory\n"..
  195. "If you don't want to use worldedit_gui, disable it by editing world.mt or from the main menu."
  196. )
  197. end
  198. worldedit.register_gui_function("worldedit_gui", {
  199. name = "WorldEdit GUI",
  200. privs = {interact=true},
  201. get_formspec = function(name)
  202. --create a form with all the buttons arranged in a grid
  203. local buttons, x, y, index = {}, 0, 1, 0
  204. local width, height = 3, 0.8
  205. local columns = 5
  206. for i, identifier in pairs(identifiers) do
  207. if identifier ~= "worldedit_gui" then
  208. local entry = worldedit.pages[identifier]
  209. table.insert(buttons, string.format((entry.get_formspec and "button" or "button_exit") ..
  210. "[%g,%g;%g,%g;%s;%s]", x, y, width, height, identifier, minetest.formspec_escape(entry.name)))
  211. index, x = index + 1, x + width
  212. if index == columns then --row is full
  213. x, y = 0, y + height
  214. index = 0
  215. end
  216. end
  217. end
  218. if index == 0 then --empty row
  219. y = y - height
  220. end
  221. return string.format("size[%g,%g]", math.max(columns * width, 5), math.max(y + 0.5, 3)) ..
  222. "button[0,0;2,0.5;worldedit_gui_exit;Back]" ..
  223. "label[2,0;WorldEdit GUI]" ..
  224. table.concat(buttons)
  225. end,
  226. })
  227. worldedit.register_gui_handler("worldedit_gui", function(name, fields)
  228. for identifier, entry in pairs(worldedit.pages) do --check for WorldEdit GUI main formspec button selection
  229. if fields[identifier] and identifier ~= "worldedit_gui" then
  230. --ensure player has permission to perform action
  231. local has_privs, missing_privs = minetest.check_player_privs(name, entry.privs)
  232. if not has_privs then
  233. worldedit.player_notify(name, "you are not allowed to use this function (missing privileges: " .. table.concat(missing_privs, ", ") .. ")")
  234. return false
  235. end
  236. if entry.on_select then
  237. entry.on_select(name)
  238. end
  239. if entry.get_formspec then
  240. worldedit.show_page(name, identifier)
  241. end
  242. return true
  243. end
  244. end
  245. return false
  246. end)
  247. dofile(minetest.get_modpath(minetest.get_current_modname()) .. "/functionality.lua")