This repository has been archived on 2024-02-14. You can view files and clone it, but cannot push or open issues or pull requests.
chat_formspec/show-receive.lua

142 lines
5.2 KiB
Lua

-- https://rubenwardy.com/minetest_modding_book/en/players/formspecs.html#contexts
local _contexts = {}
function chat_formspec.get_context(name)
local context = _contexts[name] or {}
_contexts[name] = context
return context
end
local function clear_context(name)
_contexts[name] = nil
end
minetest.register_on_leaveplayer(function(player)
clear_context(player:get_player_name())
end)
local function chat_escape(text) -- remove all control characters to prevent malformed chat messages
return string.gsub(text, "%c", "")
end
-- show a 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
clear_context(name)
end
return true
end)
-- show predefined formspecs
function chat_formspec.show_predefined(name)
local context = chat_formspec.get_context(name)
minetest.show_formspec(name, "chat_formspec:predefined", chat_formspec.get_predefined_template(context))
end
minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname ~= "chat_formspec:predefined" 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.replacement = nil
chat_formspec.show_select_predefined(name)
elseif fields.change_text then
context.replacement = minetest.formspec_escape(fields.change_text_field or "")
minetest.show_formspec(name, "chat_formspec:predefined", chat_formspec.get_predefined_template(context))
elseif fields.send then
if not minetest.check_player_privs(name, chat_formspec.predefined_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
clear_context(name)
end
return true
end)
-- show the formspec to select a predefined formspec
function chat_formspec.show_select_predefined(name)
minetest.show_formspec(name, "chat_formspec:select_predefined", chat_formspec.selection_fs)
end
minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname ~= "chat_formspec:select_predefined" then return end
local name = player:get_player_name()
local context = chat_formspec.get_context(name)
if fields.select then
context.id = fields.select
end
if fields.template then
chat_formspec.show_predefined(name)
end
if fields.quit then
clear_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)