forked from Sokomine/yl_speak_up
looks like much, but only extracted yl_speak_up.save_element_p_or_a_or_e from yl_speak_up.input_fs_edit_option_related and adjusted identation
This commit is contained in:
parent
b03ab2e6b5
commit
c44ee99fbc
@ -193,196 +193,13 @@ yl_speak_up.delete_element_p_or_a_or_e = function(
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-- These two functions
|
-- helper function for yl_speak_up.input_fs_edit_option_related
|
||||||
-- * yl_speak_up.input_fs_edit_option_related and
|
yl_speak_up.save_element_p_or_a_or_e = function(
|
||||||
-- * yl_speak_up.get_fs_edit_option_related
|
player, pname, n_id, d_id, o_id, x_id, id_prefix, tmp_data_cache,
|
||||||
-- are very similar for preconditions and effects. Therefore they're called here
|
element_list_name, element_desc, max_entries_allowed,
|
||||||
-- with a lot of parameters. fs_edit_preconditions.lua and fs_edit_effects.lua
|
|
||||||
-- contain only wrappers.
|
|
||||||
|
|
||||||
yl_speak_up.input_fs_edit_option_related = function(player, formname, fields,
|
|
||||||
id_prefix, element_list_name, max_entries_allowed,
|
|
||||||
element_desc, tmp_data_cache,
|
|
||||||
text_ask_for_punching,
|
|
||||||
values_what, values_operator, values_block, values_trade, values_inv,
|
values_what, values_operator, values_block, values_trade, values_inv,
|
||||||
check_what, check_operator, check_block, check_trade, check_inv,
|
formspec_input_to, data, fields)
|
||||||
get_sorted_player_var_list_function,
|
|
||||||
formspec_input_to
|
|
||||||
)
|
|
||||||
if(not(player)) then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
local pname = player:get_player_name()
|
|
||||||
-- what are we talking about?
|
|
||||||
local n_id = yl_speak_up.speak_to[pname].n_id
|
|
||||||
local d_id = yl_speak_up.speak_to[pname].d_id
|
|
||||||
local o_id = yl_speak_up.speak_to[pname].o_id
|
|
||||||
local x_id = yl_speak_up.speak_to[pname][ id_prefix.."id"]
|
|
||||||
|
|
||||||
-- this only works in edit mode
|
|
||||||
if(not(n_id) or yl_speak_up.edit_mode[pname] ~= n_id) then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
if(fields.back_from_cannot_be_edited) then
|
|
||||||
yl_speak_up.show_fs(player, formspec_input_to, x_id)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
-- delete precondition, action or effect
|
|
||||||
if(fields.delete_element) then
|
|
||||||
yl_speak_up.delete_element_p_or_a_or_e( player, pname, n_id, d_id, o_id, x_id, id_prefix,
|
|
||||||
element_list_name, element_desc, formspec_input_to)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
if(fields.select_block_pos) then
|
|
||||||
minetest.chat_send_player(pname, text_ask_for_punching)
|
|
||||||
-- this formspec expects the block punch:
|
|
||||||
yl_speak_up.speak_to[pname].expect_block_punch = formspec_input_to
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
-- field inputs: those do not trigger a sending of the formspec on their own
|
|
||||||
|
|
||||||
local was_changed = false
|
|
||||||
-- are we talking about an inventory?
|
|
||||||
-- (inventory only applies to preconditions; not effects)
|
|
||||||
local data = yl_speak_up.speak_to[pname][ tmp_data_cache ]
|
|
||||||
if(((fields.inv_stack_name and fields.inv_stack_name ~= "")
|
|
||||||
or (fields.store_item_name and fields.store_item_name ~= ""))
|
|
||||||
and data and data.what
|
|
||||||
and ((id_prefix == "p_" and data.what >= 5 and data.what <= 6)
|
|
||||||
-- "give item (created out of thin air) to player (requires npc_master priv)", -- 7
|
|
||||||
-- "take item from player and destroy it (requires npc_master priv)", -- 8
|
|
||||||
or (id_prefix == "r_" and data.what >= 7 and data.what <= 8))) then
|
|
||||||
local wanted = ""
|
|
||||||
local wanted_name = ""
|
|
||||||
if(not(fields.store_item_name)) then
|
|
||||||
local parts = fields.inv_stack_name:split(" ")
|
|
||||||
local size = 1
|
|
||||||
if(parts and #parts > 1) then
|
|
||||||
size = tonumber(parts[2])
|
|
||||||
if(not(size) or size < 1) then
|
|
||||||
size = 1
|
|
||||||
end
|
|
||||||
end
|
|
||||||
wanted = parts[1].." "..tostring(size)
|
|
||||||
wanted_name = parts[1]
|
|
||||||
else
|
|
||||||
local trade_inv = minetest.get_inventory({type="detached",
|
|
||||||
name="yl_speak_up_player_"..pname})
|
|
||||||
if(not(trade_inv) or trade_inv:is_empty("npc_wants", 1)) then
|
|
||||||
-- show error message
|
|
||||||
yl_speak_up.show_fs(player, "msg", {
|
|
||||||
input_to = "yl_speak_up:"..formspec_input_to,
|
|
||||||
formspec = "size[8,2]"..
|
|
||||||
"label[0.2,0.0;Please put an item(stack) into the slot "..
|
|
||||||
"next to the\n\"Store\" button first!]"..
|
|
||||||
"button[1.5,1.5;2,0.9;back_from_error_msg;Back]"})
|
|
||||||
return
|
|
||||||
end
|
|
||||||
local stack = trade_inv:get_stack("npc_wants", 1)
|
|
||||||
wanted = stack:get_name().." "..stack:get_count()
|
|
||||||
wanted_name = stack:get_name()
|
|
||||||
end
|
|
||||||
-- does the item exist?
|
|
||||||
if(minetest.registered_items[ wanted_name ]) then
|
|
||||||
data.inv_stack_name = wanted
|
|
||||||
fields.inv_stack_name = wanted
|
|
||||||
else
|
|
||||||
-- show error message
|
|
||||||
yl_speak_up.show_fs(player, "msg", {
|
|
||||||
input_to = "yl_speak_up:"..formspec_input_to,
|
|
||||||
formspec = "size[8,2]"..
|
|
||||||
"label[0.2,0.5;Error: \""..
|
|
||||||
minetest.formspec_escape(wanted)..
|
|
||||||
"\" is not a valid item(stack).]"..
|
|
||||||
"button[1.5,1.5;2,0.9;back_from_error_msg;Back]"})
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
-- comparison value for a variable (same for both preconditions and effects)
|
|
||||||
elseif(fields.var_cmp_value
|
|
||||||
and data and data.what and data.what == 2 and id_prefix ~= "a_") then
|
|
||||||
data.var_cmp_value = fields.var_cmp_value
|
|
||||||
was_changed = true
|
|
||||||
|
|
||||||
-- text for a chat message
|
|
||||||
elseif(fields.chat_msg_text
|
|
||||||
and data and data.what and data.what == 6 and id_prefix == "r_") then
|
|
||||||
data.chat_msg_text = fields.chat_msg_text
|
|
||||||
was_changed = true
|
|
||||||
|
|
||||||
elseif(fields.custom_param
|
|
||||||
and fields.custom_param ~= "- Insert a text that is passed on to your function here -"
|
|
||||||
and fields.custom_param ~= ""
|
|
||||||
and data and data.what
|
|
||||||
and ((id_prefix == "a_" and data.what == 7)
|
|
||||||
or (id_prefix == "p_" and data.what == 8)
|
|
||||||
or (id_prefix == "r_" and data.what == 11))) then
|
|
||||||
data.custom_param = fields.custom_param
|
|
||||||
was_changed = true
|
|
||||||
|
|
||||||
elseif(fields.action_item_quest_id
|
|
||||||
and fields.action_item_quest_id ~= ""
|
|
||||||
and fields.action_item_quest_id ~= "- none set -"
|
|
||||||
and data and data.what and data.what == 4 and id_prefix == "a_") then
|
|
||||||
data.item_quest_id = fields.action_item_quest_id
|
|
||||||
was_changed = true
|
|
||||||
end
|
|
||||||
-- action_item_quest_id and action_item_desc can be set at the same time
|
|
||||||
if(fields.action_item_desc
|
|
||||||
and fields.action_item_desc ~= ""
|
|
||||||
and fields.action_item_desc ~= "- no item set -"
|
|
||||||
and data and data.what and data.what == 4 and id_prefix == "a_") then
|
|
||||||
-- TODO: check if it diffrent from the default one of the stack
|
|
||||||
data.item_desc = fields.action_item_desc
|
|
||||||
was_changed = true
|
|
||||||
end
|
|
||||||
if(fields.quest_question
|
|
||||||
and fields.quest_question ~= ""
|
|
||||||
and data and data.what and data.what == 6 and id_prefix == "a_") then
|
|
||||||
data.quest_question = fields.quest_question
|
|
||||||
was_changed = true
|
|
||||||
end
|
|
||||||
-- quest question and answer can be given with the same press of the save button
|
|
||||||
if(fields.quest_answer
|
|
||||||
and fields.quest_answer ~= "- Insert the correct answer here -"
|
|
||||||
and fields.quest_answer ~= ""
|
|
||||||
and data and data.what and data.what == 6 and id_prefix == "a_") then
|
|
||||||
data.quest_answer = fields.quest_answer
|
|
||||||
was_changed = true
|
|
||||||
end
|
|
||||||
|
|
||||||
-- "move the player to a given position (requires npc_master priv)", -- 9
|
|
||||||
if(fields.move_to_x or fields.move_to_y or fields.move_to_z) then
|
|
||||||
local dimension = {"x","y","z"}
|
|
||||||
for i, dim in ipairs(dimension) do
|
|
||||||
local text = fields["move_to_"..dim]
|
|
||||||
if(text and text ~= "") then
|
|
||||||
local val = tonumber(text)
|
|
||||||
if(not(val) or val < -32000 or val > 32000) then
|
|
||||||
yl_speak_up.show_fs(player, "msg", {
|
|
||||||
input_to = "yl_speak_up:"..formspec_input_to,
|
|
||||||
formspec = "size[9,2]"..
|
|
||||||
"label[0.2,0.5;Error: The coordinate values have "..
|
|
||||||
"be in the range of -32000..32000.]"..
|
|
||||||
"button[1.5,1.5;2,0.9;back_from_error_msg;Back]"})
|
|
||||||
return
|
|
||||||
else
|
|
||||||
data[ "move_to_"..dim ] = val
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
-- lua code
|
|
||||||
if(fields.lua_code) then
|
|
||||||
data.lua_code = fields.lua_code
|
|
||||||
end
|
|
||||||
|
|
||||||
-- the save button was pressed
|
|
||||||
if(fields.save_element and data and data.what and values_what[ data.what ]) then
|
|
||||||
-- for creating the new prerequirement; normal elements: p_type, p_value, p_id
|
-- for creating the new prerequirement; normal elements: p_type, p_value, p_id
|
||||||
local v = {}
|
local v = {}
|
||||||
-- determine p_type
|
-- determine p_type
|
||||||
@ -832,8 +649,218 @@ yl_speak_up.input_fs_edit_option_related = function(player, formname, fields,
|
|||||||
-- show the new/changed precondition
|
-- show the new/changed precondition
|
||||||
yl_speak_up.show_fs(player, formspec_input_to, x_id)
|
yl_speak_up.show_fs(player, formspec_input_to, x_id)
|
||||||
return
|
return
|
||||||
|
else
|
||||||
|
-- make sure the player is informed that saving failed
|
||||||
|
yl_speak_up.show_fs(player, "msg", {
|
||||||
|
input_to = "yl_speak_up:"..formspec_input_to,
|
||||||
|
formspec = "size[8,2]"..
|
||||||
|
"label[0.2,0.5;Error: There is no \""..tostring(id_prefix)..
|
||||||
|
"value\" set.\n"..
|
||||||
|
"\nCould not save.]"..
|
||||||
|
"button[1.5,1.5;2,0.9;back_from_error_msg;Back]"})
|
||||||
|
return
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
-- These two functions
|
||||||
|
-- * yl_speak_up.input_fs_edit_option_related and
|
||||||
|
-- * yl_speak_up.get_fs_edit_option_related
|
||||||
|
-- are very similar for preconditions and effects. Therefore they're called here
|
||||||
|
-- with a lot of parameters. fs_edit_preconditions.lua and fs_edit_effects.lua
|
||||||
|
-- contain only wrappers.
|
||||||
|
|
||||||
|
yl_speak_up.input_fs_edit_option_related = function(player, formname, fields,
|
||||||
|
id_prefix, element_list_name, max_entries_allowed,
|
||||||
|
element_desc, tmp_data_cache,
|
||||||
|
text_ask_for_punching,
|
||||||
|
values_what, values_operator, values_block, values_trade, values_inv,
|
||||||
|
check_what, check_operator, check_block, check_trade, check_inv,
|
||||||
|
get_sorted_player_var_list_function,
|
||||||
|
formspec_input_to
|
||||||
|
)
|
||||||
|
if(not(player)) then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
local pname = player:get_player_name()
|
||||||
|
-- what are we talking about?
|
||||||
|
local n_id = yl_speak_up.speak_to[pname].n_id
|
||||||
|
local d_id = yl_speak_up.speak_to[pname].d_id
|
||||||
|
local o_id = yl_speak_up.speak_to[pname].o_id
|
||||||
|
local x_id = yl_speak_up.speak_to[pname][ id_prefix.."id"]
|
||||||
|
|
||||||
|
-- this only works in edit mode
|
||||||
|
if(not(n_id) or yl_speak_up.edit_mode[pname] ~= n_id) then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
if(fields.back_from_cannot_be_edited) then
|
||||||
|
yl_speak_up.show_fs(player, formspec_input_to, x_id)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
-- delete precondition, action or effect
|
||||||
|
if(fields.delete_element) then
|
||||||
|
yl_speak_up.delete_element_p_or_a_or_e( player, pname, n_id, d_id, o_id, x_id, id_prefix,
|
||||||
|
element_list_name, element_desc, formspec_input_to)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
if(fields.select_block_pos) then
|
||||||
|
minetest.chat_send_player(pname, text_ask_for_punching)
|
||||||
|
-- this formspec expects the block punch:
|
||||||
|
yl_speak_up.speak_to[pname].expect_block_punch = formspec_input_to
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
-- field inputs: those do not trigger a sending of the formspec on their own
|
||||||
|
|
||||||
|
local was_changed = false
|
||||||
|
-- are we talking about an inventory?
|
||||||
|
-- (inventory only applies to preconditions; not effects)
|
||||||
|
local data = yl_speak_up.speak_to[pname][ tmp_data_cache ]
|
||||||
|
if(((fields.inv_stack_name and fields.inv_stack_name ~= "")
|
||||||
|
or (fields.store_item_name and fields.store_item_name ~= ""))
|
||||||
|
and data and data.what
|
||||||
|
and ((id_prefix == "p_" and data.what >= 5 and data.what <= 6)
|
||||||
|
-- "give item (created out of thin air) to player (requires npc_master priv)", -- 7
|
||||||
|
-- "take item from player and destroy it (requires npc_master priv)", -- 8
|
||||||
|
or (id_prefix == "r_" and data.what >= 7 and data.what <= 8))) then
|
||||||
|
local wanted = ""
|
||||||
|
local wanted_name = ""
|
||||||
|
if(not(fields.store_item_name)) then
|
||||||
|
local parts = fields.inv_stack_name:split(" ")
|
||||||
|
local size = 1
|
||||||
|
if(parts and #parts > 1) then
|
||||||
|
size = tonumber(parts[2])
|
||||||
|
if(not(size) or size < 1) then
|
||||||
|
size = 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
wanted = parts[1].." "..tostring(size)
|
||||||
|
wanted_name = parts[1]
|
||||||
|
else
|
||||||
|
local trade_inv = minetest.get_inventory({type="detached",
|
||||||
|
name="yl_speak_up_player_"..pname})
|
||||||
|
if(not(trade_inv) or trade_inv:is_empty("npc_wants", 1)) then
|
||||||
|
-- show error message
|
||||||
|
yl_speak_up.show_fs(player, "msg", {
|
||||||
|
input_to = "yl_speak_up:"..formspec_input_to,
|
||||||
|
formspec = "size[8,2]"..
|
||||||
|
"label[0.2,0.0;Please put an item(stack) into the slot "..
|
||||||
|
"next to the\n\"Store\" button first!]"..
|
||||||
|
"button[1.5,1.5;2,0.9;back_from_error_msg;Back]"})
|
||||||
|
return
|
||||||
|
end
|
||||||
|
local stack = trade_inv:get_stack("npc_wants", 1)
|
||||||
|
wanted = stack:get_name().." "..stack:get_count()
|
||||||
|
wanted_name = stack:get_name()
|
||||||
|
end
|
||||||
|
-- does the item exist?
|
||||||
|
if(minetest.registered_items[ wanted_name ]) then
|
||||||
|
data.inv_stack_name = wanted
|
||||||
|
fields.inv_stack_name = wanted
|
||||||
|
else
|
||||||
|
-- show error message
|
||||||
|
yl_speak_up.show_fs(player, "msg", {
|
||||||
|
input_to = "yl_speak_up:"..formspec_input_to,
|
||||||
|
formspec = "size[8,2]"..
|
||||||
|
"label[0.2,0.5;Error: \""..
|
||||||
|
minetest.formspec_escape(wanted)..
|
||||||
|
"\" is not a valid item(stack).]"..
|
||||||
|
"button[1.5,1.5;2,0.9;back_from_error_msg;Back]"})
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
-- comparison value for a variable (same for both preconditions and effects)
|
||||||
|
elseif(fields.var_cmp_value
|
||||||
|
and data and data.what and data.what == 2 and id_prefix ~= "a_") then
|
||||||
|
data.var_cmp_value = fields.var_cmp_value
|
||||||
|
was_changed = true
|
||||||
|
|
||||||
|
-- text for a chat message
|
||||||
|
elseif(fields.chat_msg_text
|
||||||
|
and data and data.what and data.what == 6 and id_prefix == "r_") then
|
||||||
|
data.chat_msg_text = fields.chat_msg_text
|
||||||
|
was_changed = true
|
||||||
|
|
||||||
|
elseif(fields.custom_param
|
||||||
|
and fields.custom_param ~= "- Insert a text that is passed on to your function here -"
|
||||||
|
and fields.custom_param ~= ""
|
||||||
|
and data and data.what
|
||||||
|
and ((id_prefix == "a_" and data.what == 7)
|
||||||
|
or (id_prefix == "p_" and data.what == 8)
|
||||||
|
or (id_prefix == "r_" and data.what == 11))) then
|
||||||
|
data.custom_param = fields.custom_param
|
||||||
|
was_changed = true
|
||||||
|
|
||||||
|
elseif(fields.action_item_quest_id
|
||||||
|
and fields.action_item_quest_id ~= ""
|
||||||
|
and fields.action_item_quest_id ~= "- none set -"
|
||||||
|
and data and data.what and data.what == 4 and id_prefix == "a_") then
|
||||||
|
data.item_quest_id = fields.action_item_quest_id
|
||||||
|
was_changed = true
|
||||||
|
end
|
||||||
|
-- action_item_quest_id and action_item_desc can be set at the same time
|
||||||
|
if(fields.action_item_desc
|
||||||
|
and fields.action_item_desc ~= ""
|
||||||
|
and fields.action_item_desc ~= "- no item set -"
|
||||||
|
and data and data.what and data.what == 4 and id_prefix == "a_") then
|
||||||
|
-- TODO: check if it diffrent from the default one of the stack
|
||||||
|
data.item_desc = fields.action_item_desc
|
||||||
|
was_changed = true
|
||||||
|
end
|
||||||
|
if(fields.quest_question
|
||||||
|
and fields.quest_question ~= ""
|
||||||
|
and data and data.what and data.what == 6 and id_prefix == "a_") then
|
||||||
|
data.quest_question = fields.quest_question
|
||||||
|
was_changed = true
|
||||||
|
end
|
||||||
|
-- quest question and answer can be given with the same press of the save button
|
||||||
|
if(fields.quest_answer
|
||||||
|
and fields.quest_answer ~= "- Insert the correct answer here -"
|
||||||
|
and fields.quest_answer ~= ""
|
||||||
|
and data and data.what and data.what == 6 and id_prefix == "a_") then
|
||||||
|
data.quest_answer = fields.quest_answer
|
||||||
|
was_changed = true
|
||||||
|
end
|
||||||
|
|
||||||
|
-- "move the player to a given position (requires npc_master priv)", -- 9
|
||||||
|
if(fields.move_to_x or fields.move_to_y or fields.move_to_z) then
|
||||||
|
local dimension = {"x","y","z"}
|
||||||
|
for i, dim in ipairs(dimension) do
|
||||||
|
local text = fields["move_to_"..dim]
|
||||||
|
if(text and text ~= "") then
|
||||||
|
local val = tonumber(text)
|
||||||
|
if(not(val) or val < -32000 or val > 32000) then
|
||||||
|
yl_speak_up.show_fs(player, "msg", {
|
||||||
|
input_to = "yl_speak_up:"..formspec_input_to,
|
||||||
|
formspec = "size[9,2]"..
|
||||||
|
"label[0.2,0.5;Error: The coordinate values have "..
|
||||||
|
"be in the range of -32000..32000.]"..
|
||||||
|
"button[1.5,1.5;2,0.9;back_from_error_msg;Back]"})
|
||||||
|
return
|
||||||
|
else
|
||||||
|
data[ "move_to_"..dim ] = val
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
-- lua code
|
||||||
|
if(fields.lua_code) then
|
||||||
|
data.lua_code = fields.lua_code
|
||||||
|
end
|
||||||
|
|
||||||
|
-- the save button was pressed
|
||||||
|
if(fields.save_element and data and data.what and values_what[ data.what ]) then
|
||||||
|
local v = yl_speak_up.save_element_p_or_a_or_e(
|
||||||
|
player, pname, n_id, d_id, o_id, x_id, id_prefix, tmp_data_cache,
|
||||||
|
element_list_name, element_desc, max_entries_allowed,
|
||||||
|
values_what, values_operator, values_block, values_trade, values_inv,
|
||||||
|
formspec_input_to, data, fields)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
-- selections in a dropdown menu (they trigger sending the formspec)
|
-- selections in a dropdown menu (they trigger sending the formspec)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user