]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/common/information_formspecs.lua
Builtin: Optimize misc helpers (#12377)
[dragonfireclient.git] / builtin / common / information_formspecs.lua
1 local COLOR_BLUE = "#7AF"
2 local COLOR_GREEN = "#7F7"
3 local COLOR_GRAY = "#BBB"
4
5 local LIST_FORMSPEC = [[
6                 size[13,6.5]
7                 label[0,-0.1;%s]
8                 tablecolumns[color;tree;text;text]
9                 table[0,0.5;12.8,5.5;list;%s;0]
10                 button_exit[5,6;3,1;quit;%s]
11         ]]
12
13 local LIST_FORMSPEC_DESCRIPTION = [[
14                 size[13,7.5]
15                 label[0,-0.1;%s]
16                 tablecolumns[color;tree;text;text]
17                 table[0,0.5;12.8,4.8;list;%s;%i]
18                 box[0,5.5;12.8,1.5;#000]
19                 textarea[0.3,5.5;13.05,1.9;;;%s]
20                 button_exit[5,7;3,1;quit;%s]
21         ]]
22
23 local F = core.formspec_escape
24 local S = core.get_translator("__builtin")
25
26
27 -- CHAT COMMANDS FORMSPEC
28
29 local mod_cmds = {}
30
31 local function load_mod_command_tree()
32         mod_cmds = {}
33
34         for name, def in pairs(core.registered_chatcommands) do
35                 mod_cmds[def.mod_origin] = mod_cmds[def.mod_origin] or {}
36                 local cmds = mod_cmds[def.mod_origin]
37
38                 -- Could be simplified, but avoid the priv checks whenever possible
39                 cmds[#cmds + 1] = { name, def }
40         end
41         local sorted_mod_cmds = {}
42         for modname, cmds in pairs(mod_cmds) do
43                 table.sort(cmds, function(a, b) return a[1] < b[1] end)
44                 sorted_mod_cmds[#sorted_mod_cmds + 1] = { modname, cmds }
45         end
46         table.sort(sorted_mod_cmds, function(a, b) return a[1] < b[1] end)
47         mod_cmds = sorted_mod_cmds
48 end
49
50 core.after(0, load_mod_command_tree)
51
52 local function build_chatcommands_formspec(name, sel, copy)
53         local rows = {}
54         rows[1] = "#FFF,0,"..F(S("Command"))..","..F(S("Parameters"))
55
56         local description = S("For more information, click on "
57                 .. "any entry in the list.").. "\n" ..
58                 S("Double-click to copy the entry to the chat history.")
59
60         local privs = core.get_player_privs(name)
61         for i, data in ipairs(mod_cmds) do
62                 rows[#rows + 1] = COLOR_BLUE .. ",0," .. F(data[1]) .. ","
63                 for j, cmds in ipairs(data[2]) do
64                         local has_priv = privs[cmds[2].privs]
65                         rows[#rows + 1] = ("%s,1,%s,%s"):format(
66                                 has_priv and COLOR_GREEN or COLOR_GRAY,
67                                 cmds[1], F(cmds[2].params))
68                         if sel == #rows then
69                                 description = cmds[2].description
70                                 if copy then
71                                         core.chat_send_player(name, S("Command: @1 @2",
72                                                 core.colorize("#0FF", "/" .. cmds[1]), cmds[2].params))
73                                 end
74                         end
75                 end
76         end
77
78         return LIST_FORMSPEC_DESCRIPTION:format(
79                         F(S("Available commands: (see also: /help <cmd>)")),
80                         table.concat(rows, ","), sel or 0,
81                         F(description), F(S("Close"))
82                 )
83 end
84
85
86 -- PRIVILEGES FORMSPEC
87
88 local function build_privs_formspec(name)
89         local privs = {}
90         for priv_name, def in pairs(core.registered_privileges) do
91                 privs[#privs + 1] = { priv_name, def }
92         end
93         table.sort(privs, function(a, b) return a[1] < b[1] end)
94
95         local rows = {}
96         rows[1] = "#FFF,0,"..F(S("Privilege"))..","..F(S("Description"))
97
98         local player_privs = core.get_player_privs(name)
99         for i, data in ipairs(privs) do
100                 rows[#rows + 1] = ("%s,0,%s,%s"):format(
101                         player_privs[data[1]] and COLOR_GREEN or COLOR_GRAY,
102                                 data[1], F(data[2].description))
103         end
104
105         return LIST_FORMSPEC:format(
106                         F(S("Available privileges:")),
107                         table.concat(rows, ","),
108                         F(S("Close"))
109                 )
110 end
111
112
113 -- DETAILED CHAT COMMAND INFORMATION
114
115 core.register_on_player_receive_fields(function(player, formname, fields)
116         if formname ~= "__builtin:help_cmds" or fields.quit then
117                 return
118         end
119
120         local event = core.explode_table_event(fields.list)
121         if event.type ~= "INV" then
122                 local name = player:get_player_name()
123                 core.show_formspec(name, "__builtin:help_cmds",
124                         build_chatcommands_formspec(name, event.row, event.type == "DCL"))
125         end
126 end)
127
128 function core.show_general_help_formspec(name)
129         core.show_formspec(name, "__builtin:help_cmds",
130                 build_chatcommands_formspec(name))
131 end
132
133 function core.show_privs_help_formspec(name)
134         core.show_formspec(name, "__builtin:help_privs",
135                 build_privs_formspec(name))
136 end