]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/common/chatcommands.lua
[CSM] Add function to get player privileges (#5933)
[dragonfireclient.git] / builtin / common / chatcommands.lua
1 -- Minetest: builtin/common/chatcommands.lua
2
3 core.registered_chatcommands = {}
4
5 function core.register_chatcommand(cmd, def)
6         def = def or {}
7         def.params = def.params or ""
8         def.description = def.description or ""
9         def.privs = def.privs or {}
10         def.mod_origin = core.get_current_modname() or "??"
11         core.registered_chatcommands[cmd] = def
12 end
13
14 function core.unregister_chatcommand(name)
15         if core.registered_chatcommands[name] then
16                 core.registered_chatcommands[name] = nil
17         else
18                 core.log("warning", "Not unregistering chatcommand " ..name..
19                         " because it doesn't exist.")
20         end
21 end
22
23 function core.override_chatcommand(name, redefinition)
24         local chatcommand = core.registered_chatcommands[name]
25         assert(chatcommand, "Attempt to override non-existent chatcommand "..name)
26         for k, v in pairs(redefinition) do
27                 rawset(chatcommand, k, v)
28         end
29         core.registered_chatcommands[name] = chatcommand
30 end
31
32 local cmd_marker = "/"
33
34 local function gettext(...)
35         return ...
36 end
37
38 local function gettext_replace(text, replace)
39         return text:gsub("$1", replace)
40 end
41
42
43 if INIT == "client" then
44         cmd_marker = "."
45         gettext = core.gettext
46         gettext_replace = fgettext_ne
47 end
48
49 local function do_help_cmd(name, param)
50         local function format_help_line(cmd, def)
51                 local msg = core.colorize("#00ffff", cmd_marker .. cmd)
52                 if def.params and def.params ~= "" then
53                         msg = msg .. " " .. def.params
54                 end
55                 if def.description and def.description ~= "" then
56                         msg = msg .. ": " .. def.description
57                 end
58                 return msg
59         end
60         if param == "" then
61                 local cmds = {}
62                 for cmd, def in pairs(core.registered_chatcommands) do
63                         if INIT == "client" or core.check_player_privs(name, def.privs) then
64                                 cmds[#cmds + 1] = cmd
65                         end
66                 end
67                 table.sort(cmds)
68                 return true, gettext("Available commands: ") .. table.concat(cmds, " ") .. "\n"
69                                 .. gettext_replace("Use '$1help <cmd>' to get more information,"
70                                 .. " or '$1help all' to list everything.", cmd_marker)
71         elseif param == "all" then
72                 local cmds = {}
73                 for cmd, def in pairs(core.registered_chatcommands) do
74                         if INIT == "client" or core.check_player_privs(name, def.privs) then
75                                 cmds[#cmds + 1] = format_help_line(cmd, def)
76                         end
77                 end
78                 table.sort(cmds)
79                 return true, gettext("Available commands:").."\n"..table.concat(cmds, "\n")
80         elseif INIT == "game" and param == "privs" then
81                 local privs = {}
82                 for priv, def in pairs(core.registered_privileges) do
83                         privs[#privs + 1] = priv .. ": " .. def.description
84                 end
85                 table.sort(privs)
86                 return true, "Available privileges:\n"..table.concat(privs, "\n")
87         else
88                 local cmd = param
89                 local def = core.registered_chatcommands[cmd]
90                 if not def then
91                         return false, gettext("Command not available: ")..cmd
92                 else
93                         return true, format_help_line(cmd, def)
94                 end
95         end
96 end
97
98 if INIT == "client" then
99         core.register_chatcommand("help", {
100                 params = gettext("[all/<cmd>]"),
101                 description = gettext("Get help for commands"),
102                 func = function(param)
103                         return do_help_cmd(nil, param)
104                 end,
105         })
106 else
107         core.register_chatcommand("help", {
108                 params = "[all/privs/<cmd>]",
109                 description = "Get help for commands or list privileges",
110                 func = do_help_cmd,
111         })
112 end