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