2019-07-20 18:22:32 +02:00
|
|
|
local COLOR_BLUE = "#7AF"
|
|
|
|
local COLOR_GREEN = "#7F7"
|
|
|
|
local COLOR_GRAY = "#BBB"
|
|
|
|
|
|
|
|
local LIST_FORMSPEC = [[
|
|
|
|
size[13,6.5]
|
|
|
|
label[0,-0.1;%s]
|
|
|
|
tablecolumns[color;tree;text;text]
|
|
|
|
table[0,0.5;12.8,5.5;list;%s;0]
|
|
|
|
button_exit[5,6;3,1;quit;%s]
|
|
|
|
]]
|
|
|
|
|
|
|
|
local LIST_FORMSPEC_DESCRIPTION = [[
|
|
|
|
size[13,7.5]
|
|
|
|
label[0,-0.1;%s]
|
|
|
|
tablecolumns[color;tree;text;text]
|
|
|
|
table[0,0.5;12.8,4.8;list;%s;%i]
|
|
|
|
box[0,5.5;12.8,1.5;#000]
|
|
|
|
textarea[0.3,5.5;13.05,1.9;;;%s]
|
|
|
|
button_exit[5,7;3,1;quit;%s]
|
|
|
|
]]
|
|
|
|
|
2021-03-05 16:27:33 +01:00
|
|
|
local F = core.formspec_escape
|
|
|
|
local S = core.get_translator("__builtin")
|
2022-11-30 00:25:47 +01:00
|
|
|
local check_player_privs = core.check_player_privs
|
2019-07-20 18:22:32 +02:00
|
|
|
|
|
|
|
|
|
|
|
-- CHAT COMMANDS FORMSPEC
|
|
|
|
|
|
|
|
local mod_cmds = {}
|
|
|
|
|
|
|
|
local function load_mod_command_tree()
|
|
|
|
mod_cmds = {}
|
|
|
|
|
|
|
|
for name, def in pairs(core.registered_chatcommands) do
|
|
|
|
mod_cmds[def.mod_origin] = mod_cmds[def.mod_origin] or {}
|
|
|
|
local cmds = mod_cmds[def.mod_origin]
|
|
|
|
|
|
|
|
-- Could be simplified, but avoid the priv checks whenever possible
|
|
|
|
cmds[#cmds + 1] = { name, def }
|
|
|
|
end
|
|
|
|
local sorted_mod_cmds = {}
|
|
|
|
for modname, cmds in pairs(mod_cmds) do
|
|
|
|
table.sort(cmds, function(a, b) return a[1] < b[1] end)
|
|
|
|
sorted_mod_cmds[#sorted_mod_cmds + 1] = { modname, cmds }
|
|
|
|
end
|
|
|
|
table.sort(sorted_mod_cmds, function(a, b) return a[1] < b[1] end)
|
|
|
|
mod_cmds = sorted_mod_cmds
|
|
|
|
end
|
|
|
|
|
|
|
|
core.after(0, load_mod_command_tree)
|
|
|
|
|
|
|
|
local function build_chatcommands_formspec(name, sel, copy)
|
|
|
|
local rows = {}
|
2021-03-05 16:27:33 +01:00
|
|
|
rows[1] = "#FFF,0,"..F(S("Command"))..","..F(S("Parameters"))
|
2019-07-20 18:22:32 +02:00
|
|
|
|
2021-03-05 16:27:33 +01:00
|
|
|
local description = S("For more information, click on "
|
|
|
|
.. "any entry in the list.").. "\n" ..
|
|
|
|
S("Double-click to copy the entry to the chat history.")
|
2019-07-20 18:22:32 +02:00
|
|
|
|
|
|
|
for i, data in ipairs(mod_cmds) do
|
2021-03-05 16:27:33 +01:00
|
|
|
rows[#rows + 1] = COLOR_BLUE .. ",0," .. F(data[1]) .. ","
|
2019-07-20 18:22:32 +02:00
|
|
|
for j, cmds in ipairs(data[2]) do
|
2024-02-02 22:12:59 +01:00
|
|
|
local has_priv = INIT == "client" or check_player_privs(name, cmds[2].privs)
|
2019-07-20 18:22:32 +02:00
|
|
|
rows[#rows + 1] = ("%s,1,%s,%s"):format(
|
|
|
|
has_priv and COLOR_GREEN or COLOR_GRAY,
|
2021-03-05 16:27:33 +01:00
|
|
|
cmds[1], F(cmds[2].params))
|
2019-07-20 18:22:32 +02:00
|
|
|
if sel == #rows then
|
|
|
|
description = cmds[2].description
|
|
|
|
if copy then
|
2024-02-02 22:12:59 +01:00
|
|
|
local msg = S("Command: @1 @2",
|
2024-08-22 23:15:55 +02:00
|
|
|
core.colorize("#0FF", (INIT == "client" and "." or "/") .. cmds[1]), cmds[2].params)
|
2024-02-02 22:12:59 +01:00
|
|
|
if INIT == "client" then
|
|
|
|
core.display_chat_message(msg)
|
|
|
|
else
|
|
|
|
core.chat_send_player(name, msg)
|
|
|
|
end
|
2019-07-20 18:22:32 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return LIST_FORMSPEC_DESCRIPTION:format(
|
2021-03-05 16:27:33 +01:00
|
|
|
F(S("Available commands: (see also: /help <cmd>)")),
|
2019-07-20 18:22:32 +02:00
|
|
|
table.concat(rows, ","), sel or 0,
|
2021-03-05 16:27:33 +01:00
|
|
|
F(description), F(S("Close"))
|
2019-07-20 18:22:32 +02:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2019-09-19 00:37:56 +02:00
|
|
|
-- PRIVILEGES FORMSPEC
|
2019-07-20 18:22:32 +02:00
|
|
|
|
|
|
|
local function build_privs_formspec(name)
|
|
|
|
local privs = {}
|
2019-08-06 20:30:18 +02:00
|
|
|
for priv_name, def in pairs(core.registered_privileges) do
|
|
|
|
privs[#privs + 1] = { priv_name, def }
|
2019-07-20 18:22:32 +02:00
|
|
|
end
|
|
|
|
table.sort(privs, function(a, b) return a[1] < b[1] end)
|
|
|
|
|
|
|
|
local rows = {}
|
2021-03-05 16:27:33 +01:00
|
|
|
rows[1] = "#FFF,0,"..F(S("Privilege"))..","..F(S("Description"))
|
2019-07-20 18:22:32 +02:00
|
|
|
|
|
|
|
local player_privs = core.get_player_privs(name)
|
|
|
|
for i, data in ipairs(privs) do
|
|
|
|
rows[#rows + 1] = ("%s,0,%s,%s"):format(
|
|
|
|
player_privs[data[1]] and COLOR_GREEN or COLOR_GRAY,
|
2021-03-05 16:27:33 +01:00
|
|
|
data[1], F(data[2].description))
|
2019-07-20 18:22:32 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
return LIST_FORMSPEC:format(
|
2021-03-05 16:27:33 +01:00
|
|
|
F(S("Available privileges:")),
|
2019-07-20 18:22:32 +02:00
|
|
|
table.concat(rows, ","),
|
2021-03-05 16:27:33 +01:00
|
|
|
F(S("Close"))
|
2019-07-20 18:22:32 +02:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- DETAILED CHAT COMMAND INFORMATION
|
2024-02-02 22:12:59 +01:00
|
|
|
if INIT == "client" then
|
|
|
|
core.register_on_formspec_input(function(formname, fields)
|
|
|
|
if formname ~= "__builtin:help_cmds" or fields.quit then
|
|
|
|
return
|
|
|
|
end
|
2019-07-20 18:22:32 +02:00
|
|
|
|
2024-02-02 22:12:59 +01:00
|
|
|
local event = core.explode_table_event(fields.list)
|
|
|
|
if event.type ~= "INV" then
|
|
|
|
core.show_formspec("__builtin:help_cmds",
|
|
|
|
build_chatcommands_formspec(nil, event.row, event.type == "DCL"))
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
else
|
|
|
|
core.register_on_player_receive_fields(function(player, formname, fields)
|
|
|
|
if formname ~= "__builtin:help_cmds" or fields.quit then
|
|
|
|
return
|
|
|
|
end
|
2019-07-20 18:22:32 +02:00
|
|
|
|
2024-02-02 22:12:59 +01:00
|
|
|
local event = core.explode_table_event(fields.list)
|
|
|
|
if event.type ~= "INV" then
|
|
|
|
local name = player:get_player_name()
|
|
|
|
core.show_formspec(name, "__builtin:help_cmds",
|
|
|
|
build_chatcommands_formspec(name, event.row, event.type == "DCL"))
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
2019-07-20 18:22:32 +02:00
|
|
|
|
2021-04-03 22:51:09 +02:00
|
|
|
function core.show_general_help_formspec(name)
|
2024-02-02 22:12:59 +01:00
|
|
|
if INIT == "client" then
|
|
|
|
core.show_formspec("__builtin:help_cmds",
|
|
|
|
build_chatcommands_formspec(name))
|
|
|
|
else
|
|
|
|
core.show_formspec(name, "__builtin:help_cmds",
|
|
|
|
build_chatcommands_formspec(name))
|
|
|
|
end
|
2019-07-20 18:22:32 +02:00
|
|
|
end
|
|
|
|
|
2024-02-02 22:12:59 +01:00
|
|
|
if INIT ~= "client" then
|
|
|
|
function core.show_privs_help_formspec(name)
|
|
|
|
core.show_formspec(name, "__builtin:help_privs",
|
|
|
|
build_privs_formspec(name))
|
|
|
|
end
|
2021-04-03 22:51:09 +02:00
|
|
|
end
|