init.lua 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. if minetest.global_exists("sfinv") then -- sfinv installed
  58. assert(sfinv.enabled)
  59. local orig_get = sfinv.pages["sfinv:main"].get
  60. sfinv.override_page("sfinv:main", {
  61. get = function(self, player, context)
  62. local can_worldedit = minetest.check_player_privs(player, {worldedit=true})
  63. local fs = orig_get(self, player, context)
  64. return fs .. (can_worldedit and "image_button[0,0;1,1;inventory_plus_worldedit_gui.png;worldedit_gui;]" or "")
  65. end
  66. })
  67. --show the form when the button is pressed and hide it when done
  68. minetest.register_on_player_receive_fields(function(player, formname, fields)
  69. if fields.worldedit_gui then --main page
  70. worldedit.show_page(player:get_player_name(), "worldedit_gui")
  71. return true
  72. elseif fields.worldedit_gui_exit then --return to original page
  73. sfinv.set_page(player, "sfinv:main")
  74. return true
  75. end
  76. return false
  77. end)
  78. worldedit.show_page = function(name, page)
  79. local player = minetest.get_player_by_name(name)
  80. if player then
  81. player:set_inventory_formspec(get_formspec(name, page))
  82. end
  83. end
  84. else
  85. error(
  86. "worldedit_gui requires a supported gui management mod to be installed.\n"..
  87. "To use the it you need to either:\n"..
  88. "* use minetest_game or another sfinv-compatible subgame\n"..
  89. "* install Unified Inventory, Inventory++ or Smart Inventory\n"..
  90. "If you don't want to use worldedit_gui, disable it by editing world.mt or from the main menu."
  91. )
  92. end
  93. worldedit.register_gui_function("worldedit_gui", {
  94. name = "WorldEdit GUI",
  95. privs = {interact=true},
  96. get_formspec = function(name)
  97. --create a form with all the buttons arranged in a grid
  98. local buttons, x, y, index = {}, 0, 1, 0
  99. local width, height = 3, 0.8
  100. local columns = 5
  101. for i, identifier in pairs(identifiers) do
  102. if identifier ~= "worldedit_gui" then
  103. local entry = worldedit.pages[identifier]
  104. table.insert(buttons, string.format((entry.get_formspec and "button" or "button_exit") ..
  105. "[%g,%g;%g,%g;%s;%s]", x, y, width, height, identifier, minetest.formspec_escape(entry.name)))
  106. index, x = index + 1, x + width
  107. if index == columns then --row is full
  108. x, y = 0, y + height
  109. index = 0
  110. end
  111. end
  112. end
  113. if index == 0 then --empty row
  114. y = y - height
  115. end
  116. return string.format("size[%g,%g]", math.max(columns * width, 5), math.max(y + 0.5, 3)) ..
  117. "button[0,0;2,0.5;worldedit_gui_exit;Back]" ..
  118. "label[2,0;WorldEdit GUI]" ..
  119. table.concat(buttons)
  120. end,
  121. })
  122. worldedit.register_gui_handler("worldedit_gui", function(name, fields)
  123. for identifier, entry in pairs(worldedit.pages) do --check for WorldEdit GUI main formspec button selection
  124. if fields[identifier] and identifier ~= "worldedit_gui" then
  125. --ensure player has permission to perform action
  126. local has_privs, missing_privs = minetest.check_player_privs(name, entry.privs)
  127. if not has_privs then
  128. worldedit.player_notify(name, "you are not allowed to use this function (missing privileges: " .. table.concat(missing_privs, ", ") .. ")")
  129. return false
  130. end
  131. if entry.on_select then
  132. entry.on_select(name)
  133. end
  134. if entry.get_formspec then
  135. worldedit.show_page(name, identifier)
  136. end
  137. return true
  138. end
  139. end
  140. return false
  141. end)
  142. dofile(minetest.get_modpath(minetest.get_current_modname()) .. "/functionality.lua")