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
2024-01-04 10:25:27 +01:00

143 lines
5.2 KiB
Lua

-- https://rubenwardy.com/minetest_modding_book/en/players/formspecs.html#contexts
local _contexts = {}
local function 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)
-- show a formspec to a player
function chat_formspec.show_to_target(sendername, targetname, fs)
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, targetname .. " is not online")
return
end
if target:get_hp() == 0 then
minetest.chat_send_player(sendername, 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 context = get_context(targetname)
context.sender = sendername
context.fs = fs
minetest.chat_send_player(sendername, "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 = 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, "chat formspec: " .. name .. " answered with \"" .. answer .. "\"")
end
if fields.quit then
clear_context(name)
end
return true
end)
-- creating new formspecs
function chat_formspec.show_create_new(name)
minetest.show_formspec(name, "chat_formspec:create_new", chat_formspec.create_new)
end
minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname ~= "chat_formspec:create_new" then return false end
local name = player:get_player_name()
if not fields.c_fs then
minetest.chat_send_player(name, "Please provide a formspec")
return
end
if not minetest.check_player_privs(name, chat_formspec.create_new_priv) then return true end
chat_formspec.show_to_target(name, fields.target, fields.c_fs)
return true
end)
-- show predefined formspecs
function chat_formspec.show_predefined(name, id)
minetest.show_formspec(name, "chat_formspec:predefined", chat_formspec.get_predefined_template(id, nil))
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 = get_context(name)
if fields.back then
clear_context(name)
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.id, context.replacement))
elseif fields.send then
if not minetest.check_player_privs(name, chat_formspec.predefined_priv) then return true end
local fs = chat_formspec.create_targest_fs(context.id, context.replacement)
chat_formspec.show_to_target(name, fields.target, 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 = get_context(name)
if fields.select then
context.id = fields.select
end
if fields.template then
chat_formspec.show_predefined(name, context.id)
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 = 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)