]> git.lizzy.rs Git - worldedit.git/blobdiff - worldedit_gui/init.lua
Make sfinv gui code compatible with sfinv as included in MTG 0.4.15
[worldedit.git] / worldedit_gui / init.lua
index b1ebc72c8bd7d8c4263cc7b7cb1a1d95400850d0..d44d8bb9c24ac6c39b69ffebefdce112daf4839c 100644 (file)
@@ -14,7 +14,7 @@ Use `nil` for the `options` parameter to unregister the function associated with
 
 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.
 
@@ -24,6 +24,9 @@ The `on_select` function must not call `worldedit.show_page`
 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
@@ -37,17 +40,16 @@ Example:
 ]]
 
 worldedit.register_gui_handler = function(identifier, handler)
+       local enabled = true
        minetest.register_on_player_receive_fields(function(player, formname, fields)
-               --ensure the form is not being exited since this is a duplicate message
-               if fields.quit then
-                       return false
-               end
-               
+               if not enabled then return false end
+               enabled = false
+               minetest.after(0.2, function() enabled = true end)
                local name = player:get_player_name()
-               
+
                --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
@@ -68,7 +70,7 @@ local get_formspec = function(name, identifier)
 end
 
 --implement worldedit.show_page(name, page) in different ways depending on the available APIs
-if unified_inventory then --unified inventory installed
+if rawget(_G, "unified_inventory") then --unified inventory installed
        local old_func = worldedit.register_gui_function
        worldedit.register_gui_function = function(identifier, options)
                old_func(identifier, options)
@@ -101,9 +103,12 @@ if unified_inventory then --unified inventory installed
                        player:set_inventory_formspec(get_formspec(name, page))
                end
        end
-elseif inventory_plus then --inventory++ installed
+elseif rawget(_G, "inventory_plus") then --inventory++ installed
        minetest.register_on_joinplayer(function(player)
-               inventory_plus.register_button(player, "worldedit_gui", "WorldEdit")
+               local can_worldedit = minetest.check_player_privs(player:get_player_name(), {worldedit=true})
+               if can_worldedit then
+                       inventory_plus.register_button(player, "worldedit_gui", "WorldEdit")
+               end
        end)
 
        --show the form when the button is pressed and hide it when done
@@ -116,7 +121,7 @@ elseif inventory_plus then --inventory++ installed
                        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
@@ -129,7 +134,45 @@ elseif inventory_plus then --inventory++ installed
                        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)
@@ -141,8 +184,21 @@ else --fallback button
                if not player then --this is in case the player signs off while the media is loading
                        return
                end
-               if (minetest.check_player_privs(name, {creative=true}) or minetest.setting_getbool("creative_mode")) and creative_inventory then --creative_inventory is active, add button to modified formspec
-                       formspec = player:get_inventory_formspec() .. "image_button[6,0;1,1;inventory_plus_worldedit_gui.png;worldedit_gui;]"
+               if (minetest.check_player_privs(name, {creative=true}) or
+                               minetest.setting_getbool("creative_mode")) and
+                               creative then --creative is active, add button to modified formspec
+                       local creative_formspec = player:get_inventory_formspec()
+                       local tab_id = tonumber(creative_formspec:match("tabheader%[.-;(%d+)%;"))
+
+                       if tab_id == 1 then
+                               formspec = creative_formspec ..
+                                       "image_button[0,1;1,1;inventory_plus_worldedit_gui.png;worldedit_gui;]"
+                       elseif not tab_id then
+                               formspec = creative_formspec ..
+                                       "image_button[6,0;1,1;inventory_plus_worldedit_gui.png;worldedit_gui;]"
+                       else
+                               return
+                       end
                else
                        formspec = formspec .. "image_button[0,0;1,1;inventory_plus_worldedit_gui.png;worldedit_gui;]"
                end
@@ -195,6 +251,7 @@ end
 
 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
@@ -227,7 +284,7 @@ worldedit.register_gui_handler("worldedit_gui", function(name, fields)
        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