Use `nil` for the `get_formspec` field to denote that the function does not have its own screen.
-Use `nil` for the `privs` field to denote that no special privileges are required to use the function.
+The `privs` field may not be `nil`.
If the identifier is already registered to another function, it will be replaced by the new one.
worldedit.pages = {} --mapping of identifiers to options
local identifiers = {} --ordered list of identifiers
worldedit.register_gui_function = function(identifier, options)
+ if options.privs == nil or next(options.privs) == nil then
+ error("privs unset")
+ end
worldedit.pages[identifier] = options
table.insert(identifiers, identifier)
end
--ensure the player has permission to perform the action
local entry = worldedit.pages[identifier]
- if entry and minetest.check_player_privs(name, entry.privs or {}) then
+ if entry and minetest.check_player_privs(name, entry.privs) then
return handler(name, fields)
end
return false
return true
elseif fields.worldedit_gui_exit then --return to original page
if gui_player_formspecs[name] then
- inventory_plus.set_inventory_formspec(player, gui_player_formspecs[name])
+ inventory_plus.set_inventory_formspec(player, inventory_plus.get_formspec(player, "main"))
end
return true
end
inventory_plus.set_inventory_formspec(player, get_formspec(name, page))
end
end
+elseif rawget(_G, "sfinv") then --sfinv installed (part of minetest_game since 0.4.15)
+ assert(sfinv.enabled)
+ local orig_get = sfinv.pages["sfinv:crafting"].get
+ sfinv.override_page("sfinv:crafting", {
+ get = function(self, player, context)
+ local can_worldedit = minetest.check_player_privs(player, {worldedit=true})
+ local fs = orig_get(self, player, context)
+ return fs .. (can_worldedit and "image_button[0,0;1,1;inventory_plus_worldedit_gui.png;worldedit_gui;]" or "")
+ end
+ })
+
+ --compatibility with pre-0.4.16 sfinv
+ local set_page = sfinv.set_page or function(player, name)
+ --assumptions: src pg has no leave callback, dst pg has no enter callback
+ local ctx = {page=name}
+ sfinv.contexts[player:get_player_name()] = ctx
+ sfinv.set_player_inventory_formspec(player, ctx)
+ end
+
+ --show the form when the button is pressed and hide it when done
+ minetest.register_on_player_receive_fields(function(player, formname, fields)
+ if fields.worldedit_gui then --main page
+ worldedit.show_page(player:get_player_name(), "worldedit_gui")
+ return true
+ elseif fields.worldedit_gui_exit then --return to original page
+ set_page(player, "sfinv:crafting")
+ return true
+ end
+ return false
+ end)
+
+ worldedit.show_page = function(name, page)
+ local player = minetest.get_player_by_name(name)
+ if player then
+ player:set_inventory_formspec(get_formspec(name, page))
+ end
+ end
else --fallback button
+ -- FIXME: this is a huge clusterfuck and the back button is broken
local player_formspecs = {}
local update_main_formspec = function(name)
worldedit.register_gui_function("worldedit_gui", {
name = "WorldEdit GUI",
+ privs = {interact=true},
get_formspec = function(name)
--create a form with all the buttons arranged in a grid
local buttons, x, y, index = {}, 0, 1, 0
for identifier, entry in pairs(worldedit.pages) do --check for WorldEdit GUI main formspec button selection
if fields[identifier] and identifier ~= "worldedit_gui" then
--ensure player has permission to perform action
- local has_privs, missing_privs = minetest.check_player_privs(name, entry.privs or {})
+ local has_privs, missing_privs = minetest.check_player_privs(name, entry.privs)
if not has_privs then
worldedit.player_notify(name, "you are not allowed to use this function (missing privileges: " .. table.concat(missing_privs, ", ") .. ")")
return false