handle formspec sending / receiving

This commit is contained in:
tour 2024-01-03 16:07:27 +01:00
parent ec41200bb3
commit 448a8acf6a
4 changed files with 143 additions and 5 deletions

View File

@ -4,5 +4,10 @@ local modpath = minetest.get_modpath(minetest.get_current_modname())
dofile(modpath .. "/predefined_formspecs.lua") dofile(modpath .. "/predefined_formspecs.lua")
dofile(modpath .. "/specs.lua") dofile(modpath .. "/specs.lua")
dofile(modpath .. "/show-receive.lua")
minetest.register_on_player_receive_fields(function(player, formname, fields)
print(dump(fields))
end)

113
show-receive.lua Normal file
View File

@ -0,0 +1,113 @@
-- 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)
print(dump(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
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 playername = player:get_player_name()
local context = get_context(playername)
local sendername = context.sender
if minetest.get_player_by_name(sendername) then
local answer = fields.answer or ""
minetest.chat_send_player(sendername, "chat formspec: " .. sendername .. " answered with \"" .. answer .. "\"")
end
if fields.quit then
clear_context(playername)
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 playername = player:get_player_name()
if not fields.c_fs then
minetest.chat_send_player(playername, "Please provide a formspec")
return
end
chat_formspec.show_to_target(playername, fields.target, fields.c_fs)
return true
end)
-- show predefined formspecs
function chat_formspec.show_predefined(name, id)
local context = get_context(name)
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 = 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
local fs = chat_formspec.create_targest_fs(context.id, context.replacement)
clear_context(name)
chat_formspec.show_to_target(name, fields.target, fs)
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)

View File

@ -2,7 +2,7 @@ local function create_selection_formspec()
local fs = [[ local fs = [[
formspec_version[6] formspec_version[6]
size[16,12] size[16,12]
dropdown[2.6,2.8;5.9;select;%s;0;true] dropdown[2.6,2.8;5.9;select;%s;1;false]
button[9.5,2.8;3,0.8;template;use this template] button[9.5,2.8;3,0.8;template;use this template]
]] ]]
@ -29,9 +29,9 @@ Please make sure the field the player should answer with has the name "answer"]
textarea[0.8,8.9;13.7,1.1;;;There is no validation check done for this formspec. textarea[0.8,8.9;13.7,1.1;;;There is no validation check done for this formspec.
If you are not sure wheter it looks correct\, consider sending it to yourself] If you are not sure wheter it looks correct\, consider sending it to yourself]
button[0.8,10.5;3,0.8;back;Back] button_exit[0.8,10.5;3,0.8;cancel;Cancel]
field[7.8,10.5;3,0.8;target;send to player;] field[7.8,10.5;3,0.8;target;send to player;]
button[11.7,10.5;3,0.8;send;Send] button_exit[11.7,10.5;3,0.8;send;Send]
]] ]]
chat_formspec.predefined_template = [[ chat_formspec.predefined_template = [[
@ -41,16 +41,30 @@ chat_formspec.predefined_template = [[
%s %s
button[1,10.8;3,0.8;back;Back] button[1,10.8;3,0.8;back;Back]
button[12.4,10.7;3,0.9;send;Send] button[12.4,10.7;3,0.9;send;Send]
textarea[0.8,8.6;14.6,1.8;change_text;change default text;default text] textarea[0.8,8.6;14.6,1.8;change_text_field;change default text;%s]
button[4.7,10.8;3,0.8;change_text;change text] button[4.7,10.8;3,0.8;change_text;change text]
field[8.8,10.9;3,0.8;target;send to player;] field[8.8,10.9;3,0.8;target;send to player;]
]] ]]
function chat_formspec.get_predefined_template(id, replacement) function chat_formspec.get_predefined_template(id, replacement)
if not chat_formspec.predefined[id] then return end -- in case someone hacks their client and sends an invalid id in their fs submission
local predefined = chat_formspec.predefined[id].fs local predefined = chat_formspec.predefined[id].fs
local replacement = replacement or chat_formspec.predefined[id].replacement local replacement = replacement or chat_formspec.predefined[id].replacement
predefined = string.format(predefined, replacement) predefined = string.format(predefined, replacement)
local fs = string.format(chat_formspec.predefined_template, predefined) local fs = string.format(chat_formspec.predefined_template, predefined, replacement)
return fs
end
function chat_formspec.create_targest_fs(id, replacement)
if not chat_formspec.predefined[id] then return end -- in case someone hacks their client and sends an invalid id in their fs submission
local fs = [[formspec_version[1]
size[14.6,7.7]
]]
local predefined = chat_formspec.predefined[id].fs
local replacement = replacement or chat_formspec.predefined[id].replacement
predefined = string.format(predefined, replacement)
fs = fs .. predefined
return fs return fs
end end

6
todo.txt Normal file
View File

@ -0,0 +1,6 @@
check to not send formspecs to death players :/ (done?)
priv checks (bailiff / staff) before sending out a formspec to the target
create formspecs how to replant / how to chat
add a licence
colorize answers / maybe even play a notification sound to provent the message getting lost in the chat flow
translation