moved general formspec functions to show_fs.lua

This commit is contained in:
Sokomine 2021-06-02 19:10:58 +02:00
parent 7b12a37efe
commit b491ec46de
4 changed files with 140 additions and 138 deletions

View File

@ -1770,144 +1770,6 @@ end
-- options
-- route player input to the right functions;
-- return true when the right function has been found
minetest.register_on_player_receive_fields( function(player, formname, fields)
if formname == "yl_speak_up:optiondialog" then
yl_speak_up.input_optiondialog(player, formname, fields)
return true
elseif formname == "yl_speak_up:setdialog" then
yl_speak_up.input_setdialog(player, formname, fields)
return true
elseif formname == "yl_speak_up:confirm_save" then
yl_speak_up.input_confirm_save(player, formname, fields)
return true
elseif formname == "yl_speak_up:edit_option_dialog" then
yl_speak_up.input_edit_option_dialog(player, formname, fields)
return true
elseif formname == "yl_speak_up:talk" then
yl_speak_up.input_talk(player, formname, fields)
return true
elseif formname == "yl_speak_up:fashion" then
yl_speak_up.input_fashion(player, formname, fields)
return true
elseif formname == "yl_speak_up:inventory" then
yl_speak_up.input_inventory(player, formname, fields)
return true
elseif formname == "yl_speak_up:trade_list" then
yl_speak_up.input_trade_list(player, formname, fields)
return true
elseif formname == "yl_speak_up:do_trade_simple" then
yl_speak_up.input_do_trade_simple(player, formname, fields)
return true
elseif formname == "yl_speak_up:add_trade_simple" then
yl_speak_up.input_add_trade_simple(player, formname, fields)
return true
end
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()
-- 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
minetest.show_formspec(pname, param.input_to, param.formspec)
elseif(fs_name == "optiondialog") then
if(not(param)) then
param = {}
end
minetest.show_formspec(pname, "yl_speak_up:optiondialog",
get_fs_optiondialog(player, param.n_id, param.d_id, param.o_id, param.p_id, param.r_id))
elseif(fs_name == "setdialog") then
if(not(param)) then
param = {}
end
minetest.show_formspec(pname, "yl_speak_up:setdialog",
get_fs_setdialog(player, param.n_id, param.d_id))
-- "confirm_save" does not have its own option here; doesn't have a get_fs_-function either
elseif(fs_name == "edit_option_dialog") then
-- the optional "caller" parameter can be used for debugging
if(not(param)) then
param = {}
end
minetest.show_formspec(pname, "yl_speak_up:edit_option_dialog",
yl_speak_up.get_fs_edit_option_dialog(player, param.n_id, param.d_id, param.o_id))
elseif(fs_name == "talk") then
if(not(param)) then
param = {}
end
minetest.show_formspec(pname, "yl_speak_up:talk",
yl_speak_up.get_fs_talkdialog(player, param.n_id, param.d_id))
elseif(fs_name == "fashion") then
minetest.show_formspec(pname, "yl_speak_up:fashion",
yl_speak_up.get_fs_fashion(pname))
elseif(fs_name == "inventory") then
minetest.show_formspec(pname, "yl_speak_up:inventory",
yl_speak_up.get_fs_inventory(player))
elseif(fs_name == "trade_list") then
minetest.show_formspec(pname, "yl_speak_up:trade_list",
yl_speak_up.get_fs_trade_list(player, param))
elseif(fs_name == "trade_simple") then
-- the optional parameter param is the trade_id
if(not(param) and yl_speak_up.speak_to[pname]) then
param = yl_speak_up.speak_to[pname].trade_id
end
minetest.show_formspec(pname, "yl_speak_up:do_trade_simple",
yl_speak_up.get_fs_trade_simple(player, param))
elseif(fs_name == "add_trade_simple") then
-- the optional parameter param is the trade_id
if(not(param) and yl_speak_up.speak_to[pname]) then
param = yl_speak_up.speak_to[pname].trade_id
end
minetest.show_formspec(pname, "yl_speak_up:add_trade_simple",
yl_speak_up.get_fs_add_trade_simple(player, param))
-- 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
-- the player has closed the inventory formspec of the NPC - save it
yl_speak_up.input_inventory = function(player, formname, fields)
local pname = player:get_player_name()
local d_id = yl_speak_up.speak_to[pname].d_id
local n_id = yl_speak_up.speak_to[pname].n_id
-- after closing the inventory formspec:
-- ..save the (very probably) modified inventory
yl_speak_up.save_npc_inventory(n_id)
-- show the trade list?
if(fields.inventory_show_tradelist) then
yl_speak_up.show_fs(player, "trade_list")
return
end
-- ..and go back to the normal talk formspec
yl_speak_up.show_fs(player, "talk", {n_id = n_id, d_id = d_id})
end
yl_speak_up.input_optiondialog = function(player, formname, fields)
if formname ~= "yl_speak_up:optiondialog" then

View File

@ -17,6 +17,8 @@ yl_speak_up.speak_to = {}
dofile(modpath .. "config.lua")
dofile(modpath .. "privs.lua")
-- handle on_player_receive_fields and showing of formspecs
dofile(modpath .. "show_fs.lua")
-- inventory management, trading and handling of quest items:
dofile(modpath .. "inventory.lua")
-- trade one item(stack) against one other item(stack)

View File

@ -28,6 +28,24 @@ yl_speak_up.check_stack_has_meta = function(player, stack)
end
-- the player has closed the inventory formspec of the NPC - save it
yl_speak_up.input_inventory = function(player, formname, fields)
local pname = player:get_player_name()
local d_id = yl_speak_up.speak_to[pname].d_id
local n_id = yl_speak_up.speak_to[pname].n_id
-- after closing the inventory formspec:
-- ..save the (very probably) modified inventory
yl_speak_up.save_npc_inventory(n_id)
-- show the trade list?
if(fields.inventory_show_tradelist) then
yl_speak_up.show_fs(player, "trade_list")
return
end
-- ..and go back to the normal talk formspec
yl_speak_up.show_fs(player, "talk", {n_id = n_id, d_id = d_id})
end
-- access the inventory of the NPC (only possible for players with the right priv)
yl_speak_up.get_fs_inventory = function(player)
if(not(player)) then

120
show_fs.lua Normal file
View File

@ -0,0 +1,120 @@
-- route player input to the right functions;
-- return true when the right function has been found
minetest.register_on_player_receive_fields( function(player, formname, fields)
if formname == "yl_speak_up:optiondialog" then
yl_speak_up.input_optiondialog(player, formname, fields)
return true
elseif formname == "yl_speak_up:setdialog" then
yl_speak_up.input_setdialog(player, formname, fields)
return true
elseif formname == "yl_speak_up:confirm_save" then
yl_speak_up.input_confirm_save(player, formname, fields)
return true
elseif formname == "yl_speak_up:edit_option_dialog" then
yl_speak_up.input_edit_option_dialog(player, formname, fields)
return true
elseif formname == "yl_speak_up:talk" then
yl_speak_up.input_talk(player, formname, fields)
return true
elseif formname == "yl_speak_up:fashion" then
yl_speak_up.input_fashion(player, formname, fields)
return true
elseif formname == "yl_speak_up:inventory" then
yl_speak_up.input_inventory(player, formname, fields)
return true
elseif formname == "yl_speak_up:trade_list" then
yl_speak_up.input_trade_list(player, formname, fields)
return true
elseif formname == "yl_speak_up:do_trade_simple" then
yl_speak_up.input_do_trade_simple(player, formname, fields)
return true
elseif formname == "yl_speak_up:add_trade_simple" then
yl_speak_up.input_add_trade_simple(player, formname, fields)
return true
end
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()
-- 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
minetest.show_formspec(pname, param.input_to, param.formspec)
elseif(fs_name == "optiondialog") then
if(not(param)) then
param = {}
end
minetest.show_formspec(pname, "yl_speak_up:optiondialog",
get_fs_optiondialog(player, param.n_id, param.d_id, param.o_id, param.p_id, param.r_id))
elseif(fs_name == "setdialog") then
if(not(param)) then
param = {}
end
minetest.show_formspec(pname, "yl_speak_up:setdialog",
get_fs_setdialog(player, param.n_id, param.d_id))
-- "confirm_save" does not have its own option here; doesn't have a get_fs_-function either
elseif(fs_name == "edit_option_dialog") then
-- the optional "caller" parameter can be used for debugging
if(not(param)) then
param = {}
end
minetest.show_formspec(pname, "yl_speak_up:edit_option_dialog",
yl_speak_up.get_fs_edit_option_dialog(player, param.n_id, param.d_id, param.o_id))
elseif(fs_name == "talk") then
if(not(param)) then
param = {}
end
minetest.show_formspec(pname, "yl_speak_up:talk",
yl_speak_up.get_fs_talkdialog(player, param.n_id, param.d_id))
elseif(fs_name == "fashion") then
minetest.show_formspec(pname, "yl_speak_up:fashion",
yl_speak_up.get_fs_fashion(pname))
elseif(fs_name == "inventory") then
minetest.show_formspec(pname, "yl_speak_up:inventory",
yl_speak_up.get_fs_inventory(player))
elseif(fs_name == "trade_list") then
minetest.show_formspec(pname, "yl_speak_up:trade_list",
yl_speak_up.get_fs_trade_list(player, param))
elseif(fs_name == "trade_simple") then
-- the optional parameter param is the trade_id
if(not(param) and yl_speak_up.speak_to[pname]) then
param = yl_speak_up.speak_to[pname].trade_id
end
minetest.show_formspec(pname, "yl_speak_up:do_trade_simple",
yl_speak_up.get_fs_trade_simple(player, param))
elseif(fs_name == "add_trade_simple") then
-- the optional parameter param is the trade_id
if(not(param) and yl_speak_up.speak_to[pname]) then
param = yl_speak_up.speak_to[pname].trade_id
end
minetest.show_formspec(pname, "yl_speak_up:add_trade_simple",
yl_speak_up.get_fs_add_trade_simple(player, param))
-- 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