init.lua 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. condition = function(player)
  68. return minetest.check_player_privs(player:get_player_name(), {worldedit=true})
  69. end,
  70. })
  71. minetest.register_on_player_receive_fields(function(player, formname, fields)
  72. local name = player:get_player_name()
  73. if fields.worldedit_gui then --main page
  74. worldedit.show_page(name, "worldedit_gui")
  75. return true
  76. elseif fields.worldedit_gui_exit then --return to original page
  77. local player = minetest.get_player_by_name(name)
  78. if player then
  79. unified_inventory.set_inventory_formspec(player, "craft")
  80. end
  81. return true
  82. end
  83. return false
  84. end)
  85. worldedit.show_page = function(name, page)
  86. local player = minetest.get_player_by_name(name)
  87. if player then
  88. player:set_inventory_formspec(get_formspec(name, page))
  89. end
  90. end
  91. elseif minetest.global_exists("inventory_plus") then -- inventory++ installed
  92. minetest.register_on_joinplayer(function(player)
  93. local can_worldedit = minetest.check_player_privs(player:get_player_name(), {worldedit=true})
  94. if can_worldedit then
  95. inventory_plus.register_button(player, "worldedit_gui", "WorldEdit")
  96. end
  97. end)
  98. --show the form when the button is pressed and hide it when done
  99. local gui_player_formspecs = {}
  100. minetest.register_on_player_receive_fields(function(player, formname, fields)
  101. local name = player:get_player_name()
  102. if fields.worldedit_gui then --main page
  103. gui_player_formspecs[name] = player:get_inventory_formspec()
  104. worldedit.show_page(name, "worldedit_gui")
  105. return true
  106. elseif fields.worldedit_gui_exit then --return to original page
  107. if gui_player_formspecs[name] then
  108. inventory_plus.set_inventory_formspec(player, inventory_plus.get_formspec(player, "main"))
  109. end
  110. return true
  111. end
  112. return false
  113. end)
  114. worldedit.show_page = function(name, page)
  115. local player = minetest.get_player_by_name(name)
  116. if player then
  117. inventory_plus.set_inventory_formspec(player, get_formspec(name, page))
  118. end
  119. end
  120. elseif minetest.global_exists("smart_inventory") then -- smart_inventory installed
  121. -- redefinition: Update the code element on inventory page to show the we-page
  122. function worldedit.show_page(name, page)
  123. local state = smart_inventory.get_page_state("worldedit_gui", name)
  124. if state then
  125. state:get("code"):set_we_formspec(page)
  126. state.location.rootState:show() -- update inventory page
  127. end
  128. end
  129. -- smart_inventory page callback. Contains just a "custom code" element
  130. local function smart_worldedit_gui_callback(state)
  131. local codebox = state:element("code", { name = "code", code = "" })
  132. function codebox:set_we_formspec(we_page)
  133. local new_formspec = get_formspec(state.location.rootState.location.player, we_page)
  134. new_formspec = new_formspec:gsub('button_exit','button') --no inventory closing
  135. self.data.code = "container[1,1]".. new_formspec .. "container_end[]"
  136. end
  137. codebox:set_we_formspec("worldedit_gui")
  138. -- process input (the back button)
  139. state:onInput(function(state, fields, player)
  140. if fields.worldedit_gui then --main page
  141. state:get("code"):set_we_formspec("worldedit_gui")
  142. elseif fields.worldedit_gui_exit then --return to original page
  143. state:get("code"):set_we_formspec("worldedit_gui")
  144. state.location.parentState:get("crafting_button"):submit() -- switch to the crafting tab
  145. end
  146. end)
  147. end
  148. -- all handler should return false to force inventory UI update
  149. local orig_register_gui_handler = worldedit.register_gui_handler
  150. worldedit.register_gui_handler = function(identifier, handler)
  151. local wrapper = function(...)
  152. handler(...)
  153. return false
  154. end
  155. orig_register_gui_handler(identifier, wrapper)
  156. end
  157. -- register the inventory button
  158. smart_inventory.register_page({
  159. name = "worldedit_gui",
  160. tooltip = "Edit your World!",
  161. icon = "inventory_plus_worldedit_gui.png",
  162. smartfs_callback = smart_worldedit_gui_callback,
  163. sequence = 99
  164. })
  165. elseif minetest.global_exists("sfinv") then -- sfinv installed
  166. assert(sfinv.enabled)
  167. local orig_get = sfinv.pages["sfinv:crafting"].get
  168. sfinv.override_page("sfinv:crafting", {
  169. get = function(self, player, context)
  170. local can_worldedit = minetest.check_player_privs(player, {worldedit=true})
  171. local fs = orig_get(self, player, context)
  172. return fs .. (can_worldedit and "image_button[0,0;1,1;inventory_plus_worldedit_gui.png;worldedit_gui;]" or "")
  173. end
  174. })
  175. --show the form when the button is pressed and hide it when done
  176. minetest.register_on_player_receive_fields(function(player, formname, fields)
  177. if fields.worldedit_gui then --main page
  178. worldedit.show_page(player:get_player_name(), "worldedit_gui")
  179. return true
  180. elseif fields.worldedit_gui_exit then --return to original page
  181. sfinv.set_page(player, "sfinv:crafting")
  182. return true
  183. end
  184. return false
  185. end)
  186. worldedit.show_page = function(name, page)
  187. local player = minetest.get_player_by_name(name)
  188. if player then
  189. player:set_inventory_formspec(get_formspec(name, page))
  190. end
  191. end
  192. else
  193. error(
  194. "worldedit_gui requires a supported gui management mod to be installed.\n"..
  195. "To use the it you need to either:\n"..
  196. "* use minetest_game or another sfinv-compatible subgame\n"..
  197. "* install Unified Inventory, Inventory++ or Smart Inventory\n"..
  198. "If you don't want to use worldedit_gui, disable it by editing world.mt or from the main menu."
  199. )
  200. end
  201. worldedit.register_gui_function("worldedit_gui", {
  202. name = "WorldEdit GUI",
  203. privs = {interact=true},
  204. get_formspec = function(name)
  205. --create a form with all the buttons arranged in a grid
  206. local buttons, x, y, index = {}, 0, 1, 0
  207. local width, height = 3, 0.8
  208. local columns = 5
  209. for i, identifier in pairs(identifiers) do
  210. if identifier ~= "worldedit_gui" then
  211. local entry = worldedit.pages[identifier]
  212. table.insert(buttons, string.format((entry.get_formspec and "button" or "button_exit") ..
  213. "[%g,%g;%g,%g;%s;%s]", x, y, width, height, identifier, minetest.formspec_escape(entry.name)))
  214. index, x = index + 1, x + width
  215. if index == columns then --row is full
  216. x, y = 0, y + height
  217. index = 0
  218. end
  219. end
  220. end
  221. if index == 0 then --empty row
  222. y = y - height
  223. end
  224. return string.format("size[%g,%g]", math.max(columns * width, 5), math.max(y + 0.5, 3)) ..
  225. "button[0,0;2,0.5;worldedit_gui_exit;Back]" ..
  226. "label[2,0;WorldEdit GUI]" ..
  227. table.concat(buttons)
  228. end,
  229. })
  230. worldedit.register_gui_handler("worldedit_gui", function(name, fields)
  231. for identifier, entry in pairs(worldedit.pages) do --check for WorldEdit GUI main formspec button selection
  232. if fields[identifier] and identifier ~= "worldedit_gui" then
  233. --ensure player has permission to perform action
  234. local has_privs, missing_privs = minetest.check_player_privs(name, entry.privs)
  235. if not has_privs then
  236. worldedit.player_notify(name, "you are not allowed to use this function (missing privileges: " .. table.concat(missing_privs, ", ") .. ")")
  237. return false
  238. end
  239. if entry.on_select then
  240. entry.on_select(name)
  241. end
  242. if entry.get_formspec then
  243. worldedit.show_page(name, identifier)
  244. end
  245. return true
  246. end
  247. end
  248. return false
  249. end)
  250. dofile(minetest.get_modpath(minetest.get_current_modname()) .. "/functionality.lua")