]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/common/information_formspecs.lua
Add luacheck to check builtin (#7895)
[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 formspec_escape = core.formspec_escape
24 local check_player_privs = core.check_player_privs
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,Command,Parameters"
55
56         local description = "For more information, click on any entry in the list.\n" ..
57                 "Double-click to copy the entry to the chat history."
58
59         for i, data in ipairs(mod_cmds) do
60                 rows[#rows + 1] = COLOR_BLUE .. ",0," .. formspec_escape(data[1]) .. ","
61                 for j, cmds in ipairs(data[2]) do
62                         local has_priv = check_player_privs(name, cmds[2].privs)
63                         rows[#rows + 1] = ("%s,1,%s,%s"):format(
64                                 has_priv and COLOR_GREEN or COLOR_GRAY,
65                                 cmds[1], formspec_escape(cmds[2].params))
66                         if sel == #rows then
67                                 description = cmds[2].description
68                                 if copy then
69                                         core.chat_send_player(name, ("Command: %s %s"):format(
70                                                 core.colorize("#0FF", "/" .. cmds[1]), cmds[2].params))
71                                 end
72                         end
73                 end
74         end
75
76         return LIST_FORMSPEC_DESCRIPTION:format(
77                         "Available commands: (see also: /help <cmd>)",
78                         table.concat(rows, ","), sel or 0,
79                         description, "Close"
80                 )
81 end
82
83
84 --      PRIVILEGES FORMSPEC
85
86 local function build_privs_formspec(name)
87         local privs = {}
88         for priv_name, def in pairs(core.registered_privileges) do
89                 privs[#privs + 1] = { priv_name, def }
90         end
91         table.sort(privs, function(a, b) return a[1] < b[1] end)
92
93         local rows = {}
94         rows[1] = "#FFF,0,Privilege,Description"
95
96         local player_privs = core.get_player_privs(name)
97         for i, data in ipairs(privs) do
98                 rows[#rows + 1] = ("%s,0,%s,%s"):format(
99                         player_privs[data[1]] and COLOR_GREEN or COLOR_GRAY,
100                                 data[1], formspec_escape(data[2].description))
101         end
102
103         return LIST_FORMSPEC:format(
104                         "Available privileges:",
105                         table.concat(rows, ","),
106                         "Close"
107                 )
108 end
109
110
111 -- DETAILED CHAT COMMAND INFORMATION
112
113 core.register_on_player_receive_fields(function(player, formname, fields)
114         if formname ~= "__builtin:help_cmds" or fields.quit then
115                 return
116         end
117
118         local event = minetest.explode_table_event(fields.list)
119         if event.type ~= "INV" then
120                 local name = player:get_player_name()
121                 core.show_formspec(name, "__builtin:help_cmds",
122                         build_chatcommands_formspec(name, event.row, event.type == "DCL"))
123         end
124 end)
125
126
127 local help_command = core.registered_chatcommands["help"]
128 local old_help_func = help_command.func
129
130 help_command.func = function(name, param)
131         if param == "privs" then
132                 core.show_formspec(name, "__builtin:help_privs",
133                         build_privs_formspec(name))
134                 return
135         end
136         if param == "" or param == "all" then
137                 core.show_formspec(name, "__builtin:help_cmds",
138                         build_chatcommands_formspec(name))
139                 return
140         end
141
142         return old_help_func(name, param)
143 end
144