134 lines
5.3 KiB
Lua
134 lines
5.3 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, "chat_help")
|
|
elseif fields.replant_help then
|
|
chat_formspec.show_misc(name, "replant_help")
|
|
elseif minetest.get_player_by_name(sendername) then
|
|
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)
|
|
|
|
context.target = 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 = 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)
|
|
|
|
|
|
-- show formspec where you can still customise the text
|
|
function chat_formspec.show_custom(name)
|
|
local context = chat_formspec.get_context(name)
|
|
minetest.show_formspec(name, "chat_formspec:custom", chat_formspec.create_custom_fs(context))
|
|
end
|
|
|
|
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
|
if formname ~= "chat_formspec:custom" then return false end
|
|
local name = player:get_player_name()
|
|
local context = chat_formspec.get_context(name)
|
|
if fields.target then
|
|
context.target = fields.target
|
|
end
|
|
if fields.back then
|
|
context.fs = nil
|
|
context.replacement = nil
|
|
chat_formspec.show_select(name)
|
|
elseif fields.change_text then
|
|
context.replacement = minetest.formspec_escape(fields.change_text_field or "")
|
|
minetest.show_formspec(name, "chat_formspec:custom", chat_formspec.create_custom_fs(context))
|
|
elseif fields.send then
|
|
if not minetest.check_player_privs(name, chat_formspec.priv) then return true end
|
|
local fs = chat_formspec.create_target_fs(context)
|
|
chat_formspec.show_to_target(name, fs)
|
|
end
|
|
if fields.quit then
|
|
chat_formspec.delete_context(name)
|
|
end
|
|
return true
|
|
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
|
|
minetest.show_formspec(name, "chat_formspec:target_fs", context.fs)
|
|
end
|
|
end)
|
|
else
|
|
minetest.show_formspec(name, "chat_formspec:target_fs", context.fs)
|
|
end
|
|
return true
|
|
end) |