made initial_config work better in edit_mode

This commit is contained in:
Sokomine 2024-02-11 13:26:44 +01:00
parent e2722d7c60
commit b04d37c963
2 changed files with 158 additions and 79 deletions

View File

@ -1,4 +1,144 @@
-- in addition: set who can edit this npc;
-- add buttons for fashion (skin editing) and properties;
local old_input_fs_initial_config = yl_speak_up.input_fs_initial_config
yl_speak_up.input_fs_initial_config = function(player, formname, fields)
local pname = player:get_player_name()
local n_id = yl_speak_up.speak_to[pname].n_id
if(fields.back_from_error_msg) then
-- no point in showing the formspec or error message again if we did so already
if(not(yl_speak_up.may_edit_npc(player, n_id))) then
return
end
-- show this formspec again
yl_speak_up.show_fs(player, "initial_config",
{n_id = n_id, d_id = yl_speak_up.speak_to[pname].d_id, false})
return
end
if(fields.edit_skin and fields.edit_skin ~= "") then
yl_speak_up.show_fs(player, "fashion")
return
end
if(fields.edit_properties and fields.edit_properties ~= "") then
yl_speak_up.show_fs(player, "properties")
return
end
if((not(fields.save_initial_config)
and not(fields.show_nametag)
and not(fields.list_may_edit)
and not(fields.add_may_edit)
and not(fields.delete_may_edit)
) or (fields and fields.exit)) then
local dialog = yl_speak_up.speak_to[pname].dialog
-- unconfigured NPC
if(fields and fields.exit and not(dialog) or not(dialog.n_dialogs)) then
minetest.chat_send_player(pname, "Aborting initial configuration.")
return
end
-- is the player editing the npc? then leaving this config
-- dialog has to lead back to the talk dialog
if(yl_speak_up.edit_mode[pname] == n_id and n_id) then
yl_speak_up.show_fs(player, "talk",
{n_id = n_id, d_id = yl_speak_up.speak_to[pname].d_id})
end
-- else we can quit here
return
end
local error_msg = nil
local dialog = yl_speak_up.speak_to[pname].dialog
local done = false
if(not(yl_speak_up.may_edit_npc(player, n_id))) then
error_msg = "You are not allowed to edit this NPC."
-- want to change who may edit this npc?
-- delete a player from the list of those allowed to edit the NPC
elseif(fields.delete_may_edit and fields.delete_may_edit ~= ""
and fields.list_may_edit and fields.list_may_edit ~= "") then
if(pname ~= yl_speak_up.npc_owner[ n_id ]) then
error_msg = "Only the owner of the NPC\ncan change this."
elseif(not(dialog)) then
error_msg = "Please set a name for your NPC first!"
else
-- actually delete the player from the list
dialog.n_may_edit[ fields.list_may_edit ] = nil
-- show the next entry
yl_speak_up.speak_to[pname].tmp_index = math.max(1,
yl_speak_up.speak_to[pname].tmp_index-1)
end
done = true
-- add a player who may edit this NPC
elseif(fields.add_may_edit_button and fields.add_may_edit and fields.add_may_edit ~= "") then
if(pname ~= yl_speak_up.npc_owner[ n_id ]) then
error_msg = "Only the owner of the NPC\ncan change this."
-- Note: The owner can now add himself as well. This may be useful before transfering
-- ownership of the NPC to an inexperienced new user who might need help.
-- elseif(fields.add_may_edit == pname) then
-- error_msg = "You are already the owner of this NPC!\nNo need to add you extra here."
elseif(not(minetest.check_player_privs(fields.add_may_edit, {interact=true}))) then
error_msg = "Player \""..minetest.formspec_escape(fields.add_may_edit)..
"\" not found."
elseif(not(dialog)) then
error_msg = "Please set a name for the NPC first!"
else
if(not(dialog.n_may_edit)) then
dialog.n_may_edit = {}
end
dialog.n_may_edit[ fields.add_may_edit ] = true
-- jump to the index with this player so that the player sees that he has been added
local tmp_list = yl_speak_up.sort_keys(dialog.n_may_edit, true)
local index = table.indexof(tmp_list, fields.add_may_edit)
if(index and index > 0) then
-- "Add player:" is added before all other names, so +1
yl_speak_up.speak_to[pname].tmp_index = index + 1
end
end
done = true
-- selected a player name in the woy may edit this NPC dropdown?
elseif(fields.list_may_edit and fields.list_may_edit ~= "") then
local tmp_list = yl_speak_up.sort_keys(dialog.n_may_edit, true)
local index = table.indexof(tmp_list, fields.list_may_edit)
if(fields.list_may_edit == "Add player:") then
index = 0
end
if(index and index > -1) then
yl_speak_up.speak_to[pname].tmp_index = index + 1
end
done = true
end
if(error_msg) then
yl_speak_up.show_fs(player, "msg", { input_to = "yl_speak_up:initial_config",
formspec = "size[6,2]"..
"label[0.2,0.0;"..tostring(error_msg).."]"..
"button[2,1.5;1,0.9;back_from_error_msg;Back]"})
return
end
if( fields.add_may_edit and fields.add_may_edit ~= "") then
yl_speak_up.save_dialog(n_id, dialog)
yl_speak_up.log_change(pname, n_id,
"Added to \"may be edited by\": "..tostring(fields.add_may_edit))
elseif(fields.delete_may_edit and fields.delete_may_edit ~= ""
and fields.list_may_edit and fields.list_may_edit ~= "") then
yl_speak_up.save_dialog(n_id, dialog)
yl_speak_up.log_change(pname, n_id,
"Removed from \"may be edited by\": "..tostring(fields.list_may_edit))
elseif(not(done) or fields.save_initial_config) then
return old_input_fs_initial_config(player, formname, fields)
end
-- update display after editing may_edit_npc:
yl_speak_up.show_fs(player, "initial_config",
{n_id = n_id, d_id = yl_speak_up.speak_to[pname].d_id, false})
end
-- initialize the npc without having to use a staff;
-- add option to show, add and delete other players who may edit this npc; -- add option to show, add and delete other players who may edit this npc;
-- add buttons for skin change and editing properties -- add buttons for skin change and editing properties
@ -21,22 +161,37 @@ yl_speak_up.get_fs_initial_config = function(player, n_id, d_id, is_initial_conf
"button[1.0,5.5;4,0.9;edit_skin;Edit Skin]", "button[1.0,5.5;4,0.9;edit_skin;Edit Skin]",
"button[6.0,5.5;4,0.9;edit_properties;Edit Properties]", "button[6.0,5.5;4,0.9;edit_properties;Edit Properties]",
-- who can edit this NPC? -- who can edit this NPC?
"label[0.2,4.75;May be edited by:]", "label[0.2,4.45;May be\nedited by:]",
-- offer a dropdown list and a text input field for player names for adding -- offer a dropdown list and a text input field for player names for adding
yl_speak_up.create_dropdown_playerlist(player, pname, yl_speak_up.create_dropdown_playerlist(player, pname,
table_of_names, yl_speak_up.speak_to[pname].tmp_index, table_of_names, yl_speak_up.speak_to[pname].tmp_index,
3.0, 4.3, 0.0, 1.0, "list_may_edit", "player", 2.2, 4.3, 0.0, 1.0, "list_may_edit", "player",
"Remove selected\nplayer from list", "Remove selected\nplayer from list",
"add_may_edit", "add_may_edit",
"Enter the name of the player whom you\n".. "Enter the name of the player whom you\n"..
"want to grant the right to edit your NPC.\n".. "want to grant the right to edit your NPC.\n"..
"The player needs at least the npc_talk_owner priv\n".. "The player needs at least the npc_talk_owner priv\n"..
"in order to actually edit the NPC.\n".. "in order to actually edit the NPC.\n"..
"Click on \"Save\" to add the new player.", "Click on \"Add\" to add the new player.",
"delete_may_edit", "delete_may_edit",
"If you click here, the player will no\n".. "If you click here, the player will no\n"..
"longer be able to edit your NPC." "longer be able to edit your NPC."
)} )}
if(not(yl_speak_up.speak_to[pname].tmp_index) or yl_speak_up.speak_to[pname].tmp_index < 2) then
table.insert(may_be_edited_by, "button[9.8,4.3;1.0,1.0;add_may_edit_button;Add]")
table.insert(may_be_edited_by, "tooltip[add_may_edit_button;Click here to add the player "..
"listed to the left\nto those who can edit this NPC.]")
end
-- show the formspec to the player -- show the formspec to the player
return old_get_fs_initial_config(player, n_id, d_id, is_initial_config, may_be_edited_by) return old_get_fs_initial_config(player, n_id, d_id, is_initial_config, may_be_edited_by)
end end
yl_speak_up.register_fs("initial_config",
-- this function has been changed here:
yl_speak_up.input_fs_initial_config,
-- still handled by the wrapper:
yl_speak_up.get_fs_initial_config_wrapper,
-- no special formspec required:
nil
)

View File

@ -16,20 +16,8 @@ yl_speak_up.input_fs_initial_config = function(player, formname, fields)
return return
end end
if(fields.edit_skin and fields.edit_skin ~= "") then
yl_speak_up.show_fs(player, "fashion")
return
end
if(fields.edit_properties and fields.edit_properties ~= "") then
yl_speak_up.show_fs(player, "properties")
return
end
if((not(fields.save_initial_config) if((not(fields.save_initial_config)
and not(fields.show_nametag) and not(fields.show_nametag)
and not(fields.list_may_edit)
and not(fields.add_may_edit)
) or (fields and fields.exit)) then ) or (fields and fields.exit)) then
local dialog = yl_speak_up.speak_to[pname].dialog local dialog = yl_speak_up.speak_to[pname].dialog
-- unconfigured NPC -- unconfigured NPC
@ -37,12 +25,6 @@ yl_speak_up.input_fs_initial_config = function(player, formname, fields)
minetest.chat_send_player(pname, "Aborting initial configuration.") minetest.chat_send_player(pname, "Aborting initial configuration.")
return return
end end
-- is the player editing the npc? then leaving this config
-- dialog has to lead back to the talk dialog
if(yl_speak_up.edit_mode[pname] == n_id and n_id) then
yl_speak_up.show_fs(player, "talk",
{n_id = n_id, d_id = yl_speak_up.speak_to[pname].d_id})
end
-- else we can quit here -- else we can quit here
return return
end end
@ -80,55 +62,6 @@ yl_speak_up.input_fs_initial_config = function(player, formname, fields)
"have the \"npc_talk_owner\" priv. Else the\n".. "have the \"npc_talk_owner\" priv. Else the\n"..
"new owner could not edit his own NPC." "new owner could not edit his own NPC."
end end
-- want to change who may edit this npc?
-- delete a player from the list of those allowed to edit the NPC
elseif(fields.delete_may_edit and fields.delete_may_edit ~= ""
and fields.list_may_edit and fields.list_may_edit ~= "") then
if(pname ~= yl_speak_up.npc_owner[ n_id ]) then
error_msg = "Only the owner of the NPC\ncan change this."
elseif(not(dialog)) then
error_msg = "Please set a name for your NPC first!"
else
-- actually delete the player from the list
dialog.n_may_edit[ fields.list_may_edit ] = nil
-- show the next entry
yl_speak_up.speak_to[pname].tmp_index = math.max(1,
yl_speak_up.speak_to[pname].tmp_index-1)
end
-- add a player who may edit this NPC
elseif(fields.add_may_edit and fields.add_may_edit ~= "") then
if(pname ~= yl_speak_up.npc_owner[ n_id ]) then
error_msg = "Only the owner of the NPC\ncan change this."
elseif(fields.add_may_edit == pname) then
error_msg = "You are already the owner of this NPC!\nNo need to add you extra here."
elseif(not(minetest.check_player_privs(fields.add_may_edit, {interact=true}))) then
error_msg = "Player \""..minetest.formspec_escape(fields.add_may_edit)..
"\" not found."
elseif(not(dialog)) then
error_msg = "Please set a name for the NPC first!"
else
if(not(dialog.n_may_edit)) then
dialog.n_may_edit = {}
end
dialog.n_may_edit[ fields.add_may_edit ] = true
-- jump to the index with this player so that the player sees that he has been added
local tmp_list = yl_speak_up.sort_keys(dialog.n_may_edit, true)
local index = table.indexof(tmp_list, fields.add_may_edit)
if(index and index > 0) then
-- "Add player:" is added before all other names, so +1
yl_speak_up.speak_to[pname].tmp_index = index + 1
end
end
-- selected a player name in the woy may edit this NPC dropdown?
elseif(fields.list_may_edit and fields.list_may_edit ~= "") then
local tmp_list = yl_speak_up.sort_keys(dialog.n_may_edit, true)
local index = table.indexof(tmp_list, fields.list_may_edit)
if(fields.list_may_edit == "Add player:") then
index = 0
end
if(index and index > -1) then
yl_speak_up.speak_to[pname].tmp_index = index + 1
end
end end
if(error_msg) then if(error_msg) then
yl_speak_up.show_fs(player, "msg", { input_to = "yl_speak_up:initial_config", yl_speak_up.show_fs(player, "msg", { input_to = "yl_speak_up:initial_config",
@ -169,15 +102,6 @@ yl_speak_up.input_fs_initial_config = function(player, formname, fields)
-- initialize the NPC with first dialog, name, description and owner: -- initialize the NPC with first dialog, name, description and owner:
yl_speak_up.initialize_npc_dialog_once(pname, nil, n_id, fields.n_npc, fields.n_description) yl_speak_up.initialize_npc_dialog_once(pname, nil, n_id, fields.n_npc, fields.n_description)
elseif(fields.add_may_edit and fields.add_may_edit ~= "") then
yl_speak_up.save_dialog(n_id, dialog)
yl_speak_up.log_change(pname, n_id,
"Added to \"may be edited by\": "..tostring(fields.add_may_edit))
elseif(fields.delete_may_edit and fields.delete_may_edit ~= ""
and fields.list_may_edit and fields.list_may_edit ~= "") then
yl_speak_up.save_dialog(n_id, dialog)
yl_speak_up.log_change(pname, n_id,
"Removed from \"may be edited by\": "..tostring(fields.list_may_edit))
-- just change name and description -- just change name and description
elseif((fields.n_npc and fields.n_npc ~= "") elseif((fields.n_npc and fields.n_npc ~= "")
or (fields.n_description and fields.n_description ~= "")) then or (fields.n_description and fields.n_description ~= "")) then