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