preparations for chatcommand params

This commit is contained in:
tour 2024-01-04 14:03:22 +01:00
parent cf33877a6e
commit 5048fc40e8
2 changed files with 44 additions and 25 deletions

View File

@ -1,6 +1,6 @@
-- https://rubenwardy.com/minetest_modding_book/en/players/formspecs.html#contexts
local _contexts = {}
local function get_context(name)
function chat_formspec.get_context(name)
local context = _contexts[name] or {}
_contexts[name] = context
return context
@ -21,7 +21,9 @@ end
-- show a formspec to a player
function chat_formspec.show_to_target(sendername, targetname, fs)
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
@ -35,16 +37,16 @@ function chat_formspec.show_to_target(sendername, targetname, fs)
return
end
minetest.show_formspec(targetname, "chat_formspec:target_fs", fs)
local context = get_context(targetname)
context.sender = sendername
context.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 = get_context(name)
local context = chat_formspec.get_context(name)
local sendername = context.sender
if fields.chat_help then
chat_formspec.show_misc(name, "chat_help")
@ -70,35 +72,49 @@ 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
local context = chat_formspec.get_context(name)
if fields.quit then
clear_context(name)
return true
end
if fields.target then
context.target = fields.target
end
if fields.send then
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.c_fs)
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))
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 = get_context(name)
local context = chat_formspec.get_context(name)
if fields.target then
context.target = fields.target
end
if fields.back then
clear_context(name)
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.id, context.replacement))
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_targest_fs(context.id, context.replacement)
chat_formspec.show_to_target(name, fields.target, fs)
chat_formspec.show_to_target(name, fs)
end
if fields.quit then
clear_context(name)
@ -115,12 +131,15 @@ 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)
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, context.id)
chat_formspec.show_predefined(name)
end
if fields.quit then
clear_context(name)
end
return true
end)
@ -134,7 +153,7 @@ 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)
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

View File

@ -31,8 +31,8 @@ Please make sure the field the player should answer with has the name "answer"]
If you are not sure wheter it looks correct\, consider sending it to yourself]
button_exit[0.8,10.5;3,0.8;cancel;Cancel]
field[7.8,10.5;3,0.8;target;send to player;]
field_close_on_enter[target;false]
field[7.8,10.5;3,0.8;target;send to player;]
button[11.7,10.5;3,0.8;send;Send]
]]
@ -52,10 +52,10 @@ chat_formspec.predefined_template = [[
field_close_on_enter[target;false]
]]
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 replacement = replacement or chat_formspec.predefined[id].replacement
function chat_formspec.get_predefined_template(context)
if not chat_formspec.predefined[context.id] then return end -- in case someone hacks their client and sends an invalid id in their fs submission
local predefined = chat_formspec.predefined[context.id].fs
local replacement = context.replacement or chat_formspec.predefined[context.id].replacement
predefined = string.format(predefined, replacement)
local fs = string.format(chat_formspec.predefined_template, predefined, replacement)