incrementaltp: respect physics overrides
[waspsaliva.git] / builtin / common / information_formspecs.lua
blob8afa5bc87cbbb253e9836b2c0724fd6ee03ecea4
1 local COLOR_BLUE = "#7AF"
2 local COLOR_GREEN = "#7F7"
3 local COLOR_GRAY = "#BBB"
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]
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]
23 local formspec_escape = core.formspec_escape
24 local check_player_privs = core.check_player_privs
27 -- CHAT COMMANDS FORMSPEC
29 local mod_cmds = {}
31 local function load_mod_command_tree()
32 mod_cmds = {}
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]
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
50 core.after(0, load_mod_command_tree)
52 local function build_chatcommands_formspec(name, sel, copy)
53 local rows = {}
54 rows[1] = "#FFF,0,Command,Parameters"
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."
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
76 return LIST_FORMSPEC_DESCRIPTION:format(
77 "Available commands: (see also: /help <cmd>)",
78 table.concat(rows, ","), sel or 0,
79 description, "Close"
81 end
84 -- PRIVILEGES FORMSPEC
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)
93 local rows = {}
94 rows[1] = "#FFF,0,Privilege,Description"
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))
103 return LIST_FORMSPEC:format(
104 "Available privileges:",
105 table.concat(rows, ","),
106 "Close"
111 -- DETAILED CHAT COMMAND INFORMATION
113 core.register_on_player_receive_fields(function(player, formname, fields)
114 if formname ~= "__builtin:help_cmds" or fields.quit then
115 return
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"))
124 end)
127 local help_command = core.registered_chatcommands["help"]
128 local old_help_func = help_command.func
130 help_command.func = function(name, param)
131 local admin = core.settings:get("name")
133 -- If the admin ran help, put the output in the chat buffer as well to
134 -- work with the server terminal
135 if param == "privs" then
136 core.show_formspec(name, "__builtin:help_privs",
137 build_privs_formspec(name))
138 if name ~= admin then
139 return true
142 if param == "" or param == "all" then
143 core.show_formspec(name, "__builtin:help_cmds",
144 build_chatcommands_formspec(name))
145 if name ~= admin then
146 return true
150 return old_help_func(name, param)