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