-- allow show_fs to be extended more easily; -- key: formname without yl_speak_up: prefix yl_speak_up.registered_forms_get_fs = {} yl_speak_up.registered_forms_input_handler = {} -- force_fs_ver can be nil if no special formspec version is required yl_speak_up.registered_forms_force_fs_ver = {} yl_speak_up.register_fs = function(formname, fun_input_handler, fun_get_fs, force_fs_ver) yl_speak_up.registered_forms_input_handler[formname] = fun_input_handler yl_speak_up.registered_forms_get_fs[formname] = fun_get_fs yl_speak_up.registered_forms_force_fs_ver[formname] = force_fs_ver end -- route player input to the right functions; -- return true when the right function has been found -- called in minetest.register_on_player_receive_fields yl_speak_up.input_handler = function(player, formname, fields) if(not(formname)) then return false end -- cut off the leading "yl_speak_up:" prefix local fs_name = string.sub(formname, 13) if(fs_name and fs_name ~= "") then local fun = yl_speak_up.registered_forms_input_handler[fs_name] if(fun) then fun(player, formname, fields) return true end end -- handled in fs_initial_config.lua if formname == "yl_speak_up:initial_config" then yl_speak_up.input_fs_initial_config(player, formname, fields) return true end end -- show formspec with highest possible version information for the player -- force_version: optional parameter yl_speak_up.show_fs_ver = function(pname, formname, formspec, force_version) -- if the formspec already calls for a specific formspec version: use that one if(string.sub(formspec, 1, 17) == "formspec_version[") then minetest.show_formspec(pname, formname, formspec) return end local fs_ver = (yl_speak_up.fs_version[pname] or "2") if(force_version) then fs_ver = force_version end minetest.show_formspec(pname, formname, "formspec_version["..tostring(fs_ver).."]".. formspec) end -- call show_formspec with the right input_* function for the right formspec -- (handles all show_formspec-calls) yl_speak_up.show_fs = function(player, fs_name, param) if(not(player)) then return end local pname = player:get_player_name() if(not(yl_speak_up.speak_to[pname])) then return end local last_fs = yl_speak_up.speak_to[pname].last_fs if(false) then -- the player either saved or discarded; we may proceed now elseif(fs_name and fs_name == "proceed_after_save") then fs_name = yl_speak_up.speak_to[pname].next_fs param = yl_speak_up.speak_to[pname].next_fs_param yl_speak_up.speak_to[pname].next_fs = nil yl_speak_up.speak_to[pname].next_fs_param = nil yl_speak_up.speak_to[pname].last_fs = fs_name yl_speak_up.speak_to[pname].last_fs_param = param if(not(fs_name) or fs_name == "quit") then yl_speak_up.reset_vars_for_player(pname, false) return end -- the player clicked on "back" in the above dialog elseif(fs_name and fs_name == "show_last_fs") then -- call the last formspec again - and with the same parameters fs_name = yl_speak_up.speak_to[pname].last_fs param = yl_speak_up.speak_to[pname].last_fs_param -- do we need to check if there is something that needs saving? elseif(fs_name -- msg is just a loop for displaying (mostly error) messages and fs_name ~= "msg" and fs_name ~= "player_offers_item" -- is the player editing the NPC? that is: might there be any changes? and yl_speak_up.edit_mode and (yl_speak_up.edit_mode[pname] == yl_speak_up.speak_to[pname].n_id)) then local last_fs = yl_speak_up.speak_to[pname].last_fs local d_id = yl_speak_up.speak_to[pname].d_id local o_id = yl_speak_up.speak_to[pname].o_id -- only these two formspecs need to ask specificly if the data ought to be saved if(last_fs == "talk" or last_fs == "edit_option_dialog" or fs_name == "quit") then local last_param = yl_speak_up.speak_to[pname].last_fs_param local show_save_fs = false if(not(param)) then param = {} end -- set the target dialog yl_speak_up.speak_to[pname].target_dialog = param.d_id -- if we are switching from one dialog to another: is it the same? if(last_fs == "talk" and fs_name == last_fs and param and param.d_id and param.d_id ~= d_id) then -- diffrent parameters: save (if needed) show_save_fs = true elseif(fs_name == "talk" and param and param.do_save) then -- player clicked on save button show_save_fs = true -- leaving a dialog: save! elseif(last_fs == "talk" and fs_name ~= last_fs) then show_save_fs = true -- clicking on "save" in an edit option dialog: save! elseif(last_fs == "edit_option_dialog" and fs_name == last_fs and param and param.caller and param.caller == "save_option") then show_save_fs = true -- leaving editing an option: save! elseif(last_fs == "edit_option_dialog" and fs_name ~= last_fs) then show_save_fs = true -- quitting: save! elseif(fs_name == "quit") then yl_speak_up.speak_to[pname].target_dialog = nil show_save_fs = true end -- show the save or discard dialog if(show_save_fs) then yl_speak_up.speak_to[pname].next_fs = fs_name yl_speak_up.speak_to[pname].next_fs_param = param -- check first if it's necessary to ask for save or discard yl_speak_up.input_save_or_discard_changes(player, "", {}) return end end -- store the new formspec yl_speak_up.speak_to[pname].last_fs = fs_name -- and its parameter yl_speak_up.speak_to[pname].last_fs_param = param end -- abort talk if we hit d_end if(fs_name == "talk" and param and param.d_id and param.d_id == "d_end") then yl_speak_up.stop_talking(pname) return end local fun = yl_speak_up.registered_forms_get_fs[fs_name] if(fun) then yl_speak_up.show_fs_ver(pname, "yl_speak_up:"..fs_name, fun(player, param), yl_speak_up.registered_forms_force_fs_ver[fs_name]) return true end -- this is here mostly to fascilitate debugging - so that really all calls to -- minetest.show_formspec are routed through here if(fs_name == "msg") then if(not(param)) then param = {} end yl_speak_up.show_fs_ver(pname, param.input_to, param.formspec, 1) elseif(fs_name == "quit") then return elseif(fs_name == "initial_config") then if(not(param)) then param = {} end yl_speak_up.show_fs_ver(pname, "yl_speak_up:initial_config", yl_speak_up.get_fs_initial_config(player, param.n_id, param.d_id, param.is_initial_config)) -- fallback in case of wrong call else minetest.chat_send_player(pname, "Error: Trying to show wrong ".. "formspec: \""..tostring(fs_name).."\". Please notify ".. "an admin.") end end