117 lines
5.0 KiB
Lua
117 lines
5.0 KiB
Lua
local function chat_escape(text) -- remove all control characters to prevent malformed chat messages
|
|
return string.gsub(text, "%c", "")
|
|
end
|
|
|
|
|
|
-- show a chat_formspec to a player
|
|
function chat_formspec.show_to_target(sendername, fs)
|
|
local sendercontext = chat_formspec.get_context(sendername)
|
|
local targetname = sendercontext.target
|
|
if not targetname then
|
|
minetest.chat_send_player(sendername, "please provide a playername to whom the formspec shall be shown")
|
|
end
|
|
local target = minetest.get_player_by_name(targetname)
|
|
if not target then
|
|
minetest.chat_send_player(sendername, chat_escape(targetname .. " is not online"))
|
|
return
|
|
end
|
|
if target:get_hp() == 0 then
|
|
minetest.chat_send_player(sendername, chat_escape(targetname ..
|
|
" is dead. We cannot send them a formspec without removing" ..
|
|
"their respawn-formspec. Please try again later"))
|
|
return
|
|
end
|
|
minetest.show_formspec(targetname, "chat_formspec:target_fs", fs)
|
|
local targetcontext = chat_formspec.get_context(targetname)
|
|
targetcontext.sender = sendername
|
|
targetcontext.fs = fs
|
|
minetest.chat_send_player(sendername, chat_escape("formspec send to " .. targetname))
|
|
end
|
|
|
|
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
|
if formname ~= "chat_formspec:target_fs" then return false end
|
|
local name = player:get_player_name()
|
|
local context = chat_formspec.get_context(name)
|
|
local sendername = context.sender
|
|
if fields.chat_help then
|
|
chat_formspec.show_misc(name, "platform_select")
|
|
elseif sendername and minetest.get_player_by_name(sendername) then
|
|
if not sendername then return true end -- empty context, we can't restore it :/
|
|
local answer = fields.answer or ""
|
|
minetest.chat_send_player(sendername, minetest.colorize("orange",
|
|
chat_escape("chat formspec: " .. name .. " answered:")))
|
|
local msg_func = minetest.registered_chatcommands["msg"].func
|
|
msg_func(name, sendername .. " " .. answer)
|
|
end
|
|
if fields.quit then
|
|
chat_formspec.delete_context(name)
|
|
end
|
|
return true
|
|
end)
|
|
|
|
|
|
-- show the player a formspec to create a chat_formspec
|
|
function chat_formspec.show_sender_fs(name)
|
|
local context = chat_formspec.get_context(name)
|
|
local fs = chat_formspec.create_sender_fs(context)
|
|
minetest.show_formspec(name, "chat_formspec:sender_fs", fs)
|
|
end
|
|
|
|
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
|
if formname ~= "chat_formspec:sender_fs" then return false end
|
|
local name = player:get_player_name()
|
|
local context = chat_formspec.get_context(name)
|
|
if not context.sender then -- empty context, the player already closed this formspec before submitting again
|
|
-- reason might be client-server desync
|
|
fields.send = nil
|
|
fields.update = true -- will restore most of the context
|
|
-- restore what can't be restored with fields.update:
|
|
context.sender = name
|
|
-- inform the player
|
|
minetest.chat_send_player(name, "chat_formspec: something went wrong, please try again!")
|
|
end
|
|
|
|
context.target = minetest.formspec_escape(fields.target or "")
|
|
if fields.send then
|
|
local target_fs = chat_formspec.create_target_fs(context)
|
|
chat_formspec.show_to_target(name, target_fs)
|
|
elseif fields.update then
|
|
context.easy_syntax = minetest.formspec_escape(fields.easy_syntax or "")
|
|
chat_formspec.show_sender_fs(name)
|
|
elseif fields.select and not(fields.key_enter and fields.key_enter_field) then
|
|
context.easy_syntax = chat_formspec.preset[fields.select] or ""
|
|
chat_formspec.show_sender_fs(name)
|
|
end
|
|
|
|
if fields.quit then
|
|
chat_formspec.delete_context(name)
|
|
end
|
|
end)
|
|
|
|
|
|
-- help formspecs (how to chat / replant)
|
|
function chat_formspec.show_misc(name, id)
|
|
minetest.show_formspec(name, "chat_formspec:misc", chat_formspec.misc_fs[id])
|
|
end
|
|
|
|
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
|
if formname ~= "chat_formspec:misc" then return end
|
|
local name = player:get_player_name()
|
|
local context = chat_formspec.get_context(name)
|
|
if fields.quit then -- for whatever reason it doesn't work to send another formspec (maybe side effect of quit?)
|
|
minetest.after(1, function() -- for whatever reason it doesn't work to use 0 (or 0.1)
|
|
if minetest.get_player_by_name(name) then
|
|
if not context.fs then return true end -- empty context, we can't restore it
|
|
minetest.show_formspec(name, "chat_formspec:target_fs", context.fs)
|
|
end
|
|
end)
|
|
elseif fields.PC then
|
|
chat_formspec.show_misc(name, "chat_PC")
|
|
elseif fields.touch then
|
|
chat_formspec.show_misc(name, "chat_touch")
|
|
else
|
|
if not context.fs then return true end -- empty context, we can't restore it
|
|
minetest.show_formspec(name, "chat_formspec:target_fs", context.fs)
|
|
end
|
|
return true
|
|
end) |