Add snippets.close_form.

This commit is contained in:
luk3yx 2019-08-21 20:04:02 +12:00
parent 57e1540e62
commit 34f0866bd9
1 changed files with 16 additions and 9 deletions

View File

@ -55,8 +55,9 @@ function Form:show()
local data = get(self) local data = get(self)
if not get_player_by_name(data.victim) then return false end if not get_player_by_name(data.victim) then return false end
open_formspecs[data.victim] = self open_formspecs[data.victim] = self
show_formspec(data.victim, data.formname, local formspec = data.prepend .. data.formspec .. data.append
data.prepend .. data.formspec .. data.append) if formspec == '' then formspec = ' ' end
show_formspec(data.victim, data.formname, formspec)
return true return true
end end
Form.open = Form.show Form.open = Form.show
@ -118,21 +119,27 @@ function Form:add_callback(...)
table.insert(data.callbacks[event], snippets.wrap_callback(func)) table.insert(data.callbacks[event], snippets.wrap_callback(func))
end end
-- Create a Formspec object -- Create a Form object
function snippets.Form(player) function snippets.Form(name)
if minetest.is_player(player) then player = player:get_player_name() end if minetest.is_player(name) then
if type(player) ~= 'string' or not get_player_by_name(player) then name = name:get_player_name()
elseif type(name) ~= 'string' or not get_player_by_name(name) then
error('Attempted to create a Form for a non-existent player!', 2) error('Attempted to create a Form for a non-existent player!', 2)
end end
local form = {context = {}, pname = player} local form = {context = {}, pname = name}
setmetatable(form, {__index = Form}) setmetatable(form, {__index = Form})
forms[form] = { forms[form] = {
victim = player, prepend = '', formspec = '', append = '', victim = name, prepend = '', formspec = '', append = '',
callbacks = {}, formname = get_next_formname(form), pname = player, callbacks = {}, formname = get_next_formname(form),
} }
return form return form
end end
function snippets.close_form(name, callbacks)
if minetest.is_player(name) then name = name:get_player_name() end
if open_formspecs[name] then open_formspecs[name]:close() end
end
-- Callbacks -- Callbacks
local function run_callbacks(callbacks, ...) local function run_callbacks(callbacks, ...)
if not callbacks then return end if not callbacks then return end