added yl_speak_up.show_fs to combine almost all formspecs

This commit is contained in:
Sokomine 2021-05-31 21:15:42 +02:00
parent caf72721b3
commit 4095be4e43
3 changed files with 57 additions and 59 deletions

View File

@ -1792,6 +1792,44 @@ minetest.register_on_player_receive_fields( function(player, formname, fields)
end)
-- call show_formspec with the right input_* function for the right formspec
yl_speak_up.show_fs = function(player, fs_name, param)
if(not(player)) then
return
end
local pname = player:get_player_name()
if(fs_name == "trade_list") then
minetest.show_formspec(pname,
"yl_speak_up:trade_list",
yl_speak_up.get_fs_trade_list(player))
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()
@ -1802,7 +1840,7 @@ yl_speak_up.input_inventory = function(player, formname, fields)
yl_speak_up.save_npc_inventory(n_id)
-- show the trade list?
if(fields.inventory_show_tradelist) then
yl_speak_up.show_fs_trade_list(player)
yl_speak_up.show_fs(player, "trade_list")
return
end
-- ..and go back to the normal talk formspec
@ -2254,7 +2292,7 @@ yl_speak_up.save_changes_and_switch_to_other_dialog = function(player, fields, t
-- the trade list is not really a dialog...
if(target_dialog == "trade_list") then
yl_speak_up.show_fs_trade_list(player)
yl_speak_up.show_fs(player, "trade_list")
return
end
-- move on to the target dialog
@ -2703,7 +2741,7 @@ yl_speak_up.input_edit_option_dialog = function(player, formname, fields)
-- do not switch target dialog (we are in edit mode)
yl_speak_up.speak_to[pname].target_d_id = nil
-- create a new trade for this dialog and option - with ID "<d_id> <o_id>"
yl_speak_up.show_fs_add_trade_simple(player, tostring(d_id).." "..tostring(o_id))
yl_speak_up.show_fs(player, "add_trade_simple", tostring(d_id).." "..tostring(o_id))
return
-- the player wants to see the previous option/answer
@ -2743,7 +2781,7 @@ yl_speak_up.input_edit_option_dialog = function(player, formname, fields)
-- do not switch target dialog (we are in edit mode)
yl_speak_up.speak_to[pname].target_d_id = nil
-- show the trade with ID "<d_id> <o_id>"
yl_speak_up.show_fs_trade_simple(player, tostring(d_id).." "..tostring(o_id))
yl_speak_up.show_fs(player, "trade_simple", tostring(d_id).." "..tostring(o_id))
return
end
@ -2979,7 +3017,7 @@ yl_speak_up.input_talk = function(player, formname, fields)
yl_speak_up.save_changes_and_switch_to_other_dialog(player, fields, "trade_list")
return
end
yl_speak_up.show_fs_trade_list(player)
yl_speak_up.show_fs(player, "trade_list")
return
end
@ -3212,7 +3250,7 @@ yl_speak_up.input_talk = function(player, formname, fields)
-- which dialog shall be shown in case of a successful trade?
yl_speak_up.speak_to[pname].target_d_id = target_dialog
-- show the trade dialog
yl_speak_up.show_fs_trade_simple(player, trade_id)
yl_speak_up.show_fs(player, "trade_simple", trade_id)
return
end

View File

@ -1,17 +1,5 @@
-- show a list of all trades
-- provides an easier call for the trade list formspec
yl_speak_up.show_fs_trade_list = function(player)
if(not(player)) then
return
end
minetest.show_formspec(player:get_player_name(),
"yl_speak_up:trade_list",
yl_speak_up.get_fs_trade_list(player))
end
-- the player is accessing the trade list
yl_speak_up.input_trade_list = function(player, formname, fields)
local pname = player:get_player_name()
@ -27,7 +15,7 @@ yl_speak_up.input_trade_list = function(player, formname, fields)
-- the player wants to add a new trade
if(fields.trade_list_add_trade) then
-- show the trade config dialog for a new trade
yl_speak_up.show_fs_add_trade_simple(player, "new")
yl_speak_up.show_fs(player, "add_trade_simple", "new")
return
end
@ -41,7 +29,7 @@ yl_speak_up.input_trade_list = function(player, formname, fields)
-- normal mode: the player wants to see a particular trade
for k,v in pairs(dialog.trades) do
if(fields[ k ]) then
yl_speak_up.show_fs_trade_simple(player, k)
yl_speak_up.show_fs(player, "trade_simple", k)
return
end
end

View File

@ -6,34 +6,6 @@ yl_speak_up.trade_fail_fs = "size[6,2]"..
"button_exit[2,1.5;1,0.9;exit;Exit]"
-- provides an easier call for the trade simple formspec
yl_speak_up.show_fs_trade_simple = function(player, trade_id)
if(not(player)) then
return
end
if(not(trade_id) and yl_speak_up.speak_to[pname]) then
trade_id = yl_speak_up.speak_to[pname].trade_id
end
minetest.show_formspec(player:get_player_name(),
"yl_speak_up:do_trade_simple",
yl_speak_up.get_fs_trade_simple(player, trade_id))
end
-- also provides an easier way to call this function
yl_speak_up.show_fs_add_trade_simple = function(player, trade_id)
if(not(player)) then
return
end
if(not(trade_id) and yl_speak_up.speak_to[pname]) then
trade_id = yl_speak_up.speak_to[pname].trade_id
end
minetest.show_formspec(player:get_player_name(),
"yl_speak_up:add_trade_simple",
yl_speak_up.get_fs_add_trade_simple(player, trade_id))
end
-- possible inputs:
-- fields.edit_trade_simple go on to showing the add_trade_simple formspec
-- fields.abort_trade_simple, ESC, depends on context
@ -53,13 +25,13 @@ yl_speak_up.input_do_trade_simple = function(player, formname, fields)
-- show the trade list
if(fields.back_to_trade_list) then
yl_speak_up.show_fs_trade_list(player)
yl_speak_up.show_fs(player, "trade_list")
return
end
-- a new trade has been stored - show it
if(fields.trade_simple_stored) then
yl_speak_up.show_fs_trade_simple(player, yl_speak_up.speak_to[pname].trade_id)
yl_speak_up.show_fs(player, "trade_simple", yl_speak_up.speak_to[pname].trade_id)
return
end
@ -102,7 +74,7 @@ yl_speak_up.input_do_trade_simple = function(player, formname, fields)
yl_speak_up.trade[pname] = nil
end
-- go back showing the trade list (since we deleted this trade)
yl_speak_up.show_fs_trade_list(player)
yl_speak_up.show_fs(player, "trade_list")
return
end
@ -127,7 +99,7 @@ yl_speak_up.input_do_trade_simple = function(player, formname, fields)
-- show the edit trade formspec
if(fields.edit_trade_simple) then
yl_speak_up.show_fs_add_trade_simple(player, trade.trade_id)
yl_speak_up.show_fs(player, "add_trade_simple", trade.trade_id)
return
end
@ -161,7 +133,7 @@ yl_speak_up.input_do_trade_simple = function(player, formname, fields)
end
-- show this formspec again
yl_speak_up.show_fs_trade_simple(player)
yl_speak_up.show_fs(player, "trade_simple")
end
@ -250,7 +222,7 @@ yl_speak_up.input_add_trade_simple = function(player, formname, fields)
-- we return from showing an error message (the player may not have noticed
-- a chat message while viewing a formspec; thus, we showed a formspec message)
if(fields.back_from_error_msg) then
yl_speak_up.show_fs_add_trade_simple(player)
yl_speak_up.show_fs(player, "add_trade_simple")
return
end
@ -273,7 +245,7 @@ yl_speak_up.input_add_trade_simple = function(player, formname, fields)
-- return the items (setting up the trade was aborted)
yl_speak_up.add_trade_simple_return_items(player, trade_inv, pay, buy)
-- ..else go back to the edit options formspec
yl_speak_up.show_fs_trade_list(player)
yl_speak_up.show_fs(player, "trade_list")
return
end
-- adding a new trade via the trade list?
@ -405,7 +377,7 @@ yl_speak_up.input_add_trade_simple = function(player, formname, fields)
-- we are no longer trading
yl_speak_up.speak_to[pname].trade_id = nil
-- ..else go back to the edit options formspec
yl_speak_up.show_fs_trade_list(player)
yl_speak_up.show_fs(player, "trade_list")
else
-- we are no longer trading
yl_speak_up.speak_to[pname].trade_id = nil
@ -744,7 +716,7 @@ minetest.register_on_joinplayer(function(player, last_login)
if(listname == "pay") then
local pname = player:get_player_name()
-- show formspec with updated information (perhaps sale is now possible)
yl_speak_up.show_fs_trade_simple(player)
yl_speak_up.show_fs(player, "trade_simple")
end
end,
on_take = function(inv, listname, index, stack, player)
@ -753,12 +725,12 @@ minetest.register_on_joinplayer(function(player, last_login)
if(listname == "pay") then
local pname = player:get_player_name()
-- show formspec with updated information (perhaps sale is now possible)
yl_speak_up.show_fs_trade_simple(player)
yl_speak_up.show_fs(player, "trade_simple")
elseif(listname == "buy") then
yl_speak_up.do_trade_simple(player, stack:get_count())
-- information may require an update (NPC might now be out of stock), or
-- the player can do the trade a second time
yl_speak_up.show_fs_trade_simple(player)
yl_speak_up.show_fs(player, "trade_simple")
end
end,
})