allow to import dialogs (without checking) with privs priv

This commit is contained in:
Sokomine 2023-10-31 22:05:15 +01:00
parent cf868e0761
commit a735a9a86f

View File

@ -5,19 +5,93 @@ yl_speak_up.input_fs_export = function(player, formname, fields)
return yl_speak_up.show_fs(player, "talk")
elseif(fields and fields.show_readable) then
return yl_speak_up.show_fs(player, "export", "show_readable")
elseif(fields and fields.show_export) then
elseif(fields and (fields.back_to_export or fields.show_export)) then
return yl_speak_up.show_fs(player, "export", "show_export")
elseif(fields and fields.show_simple_dialogs) then
return yl_speak_up.show_fs(player, "export", "show_simple_dialogs")
elseif(fields and (fields.import or fields.back_from_error_msg)) then
return yl_speak_up.show_fs(player, "export", "import")
elseif(fields and fields.really_import and fields.new_dialog_input) then
-- importing requires the "privs" priv
if(not(minetest.check_player_privs(player, {privs=true}))) then
return yl_speak_up.show_fs(player, "msg", {
input_to = "yl_speak_up:export",
formspec = yl_speak_up.get_fs_quest_edit_error(
"You need the \"privs\" priv in order to import NPC data.",
"back_from_error_msg")})
end
local pname = player:get_player_name()
local n_id = yl_speak_up.speak_to[pname].n_id
-- actually import the dialog
local new_dialog = minetest.parse_json(fields.new_dialog_input or "")
if(not(new_dialog)) then
return yl_speak_up.show_fs(player, "msg", {
input_to = "yl_speak_up:export",
formspec = yl_speak_up.get_fs_quest_edit_error(
"Failed to parse the .json data.",
"back_from_error_msg")})
end
-- TODO: the dialog has to be checked if it is a valid one (very big TODO!)
-- the ID has to be adjusted to this NPC
new_dialog.n_id = n_id
-- update the entity with name, description and owner
if yl_speak_up.speak_to[pname].obj then
local obj = yl_speak_up.speak_to[pname].obj
local ent = obj:get_luaentity()
if ent ~= nil then
ent.yl_speak_up.npc_name = new_dialog.n_npc
ent.yl_speak_up.npc_description = new_dialog.n_description
ent.owner = new_dialog.npc_owner
local i_text = new_dialog.n_npc .. "\n" ..
new_dialog.n_description .. "\n" ..
yl_speak_up.infotext
obj:set_properties({infotext = i_text})
yl_speak_up.update_nametag(ent)
end
end
-- update the stored dialog
yl_speak_up.speak_to[pname].dialog = new_dialog
-- save it
yl_speak_up.save_dialog(n_id, new_dialog)
-- log the change
yl_speak_up.log_change(pname, n_id, "Imported new dialog.")
return yl_speak_up.show_fs(player, "msg", {
input_to = "yl_speak_up:export",
formspec = "size[10,3]"..
"label[0.5,1.0;Data successfully imported.]"..
"button[3.5,2.0;2,0.9;back_from_error_msg;Back]"
})
end
end
yl_speak_up.get_fs_export = function(player, param)
local pname = player:get_player_name()
local n_id = yl_speak_up.speak_to[pname].n_id
-- generic dialogs are not part of the NPC
local dialog = yl_speak_up.speak_to[pname].dialog
local n_id = yl_speak_up.speak_to[pname].n_id
local text = ""
if(not(minetest.check_player_privs(pname, {privs=true}))) then
text = "You lack the \"privs\" priv that is required in order to import NPC data."
end
if(param and param == "import") then
return table.concat({"size[20,20]label[4,0.5;IMPORT for NPC ",
minetest.formspec_escape(n_id or "- ? -"),
"dialog data in .json format]",
"button[17.8,0.2;2.0,0.9;back_to_export;Back]",
"button[3.6,17.2;6.0,0.9;really_import;Yes, I'm sure. Import it!]",
"button[10.2,17.2;6.0,0.9;back_to_export;No. Go back, please.]",
-- read-only
"textarea[0.2,2;19.6,15;new_dialog_input;NEW dialog for ",
minetest.formspec_escape(n_id or "- ? -"),
":;",
text,
"]",
"textarea[0.2,18.2;19.6,1.8;;;",
"WARNING: This is highly experimental and requires the \"privs\" priv. "..
"Use in singleplayer on a new NPC - but not on a live server!]",
})
end
local content = ""
local explanation = ""
local b1 = "button[0.2,17.2;6.0,0.9;show_readable;Human readable format]"..
@ -48,6 +122,7 @@ yl_speak_up.get_fs_export = function(player, param)
table.sort(d_liste)
for i, d_id in ipairs(d_liste) do
table.insert(tmp, "===")
-- TODO: use labels here when available
table.insert(tmp, tostring(d_id))
table.insert(tmp, "\n")
-- :, > and = are not allowed as line start in simple dialogs
@ -86,6 +161,9 @@ yl_speak_up.get_fs_export = function(player, param)
minetest.formspec_escape(n_id or "- ? -"),
"dialog data in .json format]",
"button[17.8,0.2;2.0,0.9;back;Back]",
"button[15.4,0.2;2.0,0.9;import;Import]",
"tooltip[import;WARNING: This is highly experimental and requires the \"privs\" priv.\n"..
"Use in singleplayer on a new NPC - but not on a live server!]",
b1, b2, b3,
-- background color for the textarea below
"box[0.2,2;19.6,15;#AAAAAA]",