]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/common/chatcommands.lua
Add spider
[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 if INIT == "client" then
37         function core.register_list_command(command, desc, setting)
38                 local def = {}
39                 def.description = desc
40                 def.params = "del <item> | add <item> | list"
41                 function def.func(param)
42                         local list = (minetest.settings:get(setting) or ""):split(",")
43                         if param == "list" then
44                                 return true, table.concat(list, ", ")
45                         else
46                                 local sparam = param:split(" ")
47                                 local cmd = sparam[1]
48                                 local item = sparam[2]
49                                 if cmd == "del" then
50                                         if not item then
51                                                 return false, "Missing item."
52                                         end
53                                         local i = table.indexof(list, item)
54                                         if i == -1 then
55                                                 return false, item .. " is not on the list."
56                                         else
57                                                 table.remove(list, i)
58                                                 core.settings:set(setting, table.concat(list, ","))
59                                                 return true, "Removed " .. item .. " from the list."
60                                         end
61                                 elseif cmd == "add" then
62                                         if not item then
63                                                 return false, "Missing item."
64                                         end
65                                         local i = table.indexof(list, item)
66                                         if i ~= -1 then
67                                                 return false, item .. " is already on the list."
68                                         else
69                                                 table.insert(list, item)
70                                                 core.settings:set(setting, table.concat(list, ","))
71                                                 return true, "Added " .. item .. " to the list."
72                                         end
73                                 end
74                         end
75                         return false, "Invalid usage. (See .help " .. command .. ")"
76                 end
77                 core.register_chatcommand(command, def)
78         end
79 end
80
81 local function do_help_cmd(name, param)
82         local function format_help_line(cmd, def)
83                 local cmd_marker = "/"
84                 if INIT == "client" then
85                         cmd_marker = "."
86                 end
87                 local msg = core.colorize("#00ffff", cmd_marker .. cmd)
88                 if def.params and def.params ~= "" then
89                         msg = msg .. " " .. def.params
90                 end
91                 if def.description and def.description ~= "" then
92                         msg = msg .. ": " .. def.description
93                 end
94                 return msg
95         end
96         if param == "" then
97                 local cmds = {}
98                 for cmd, def in pairs(core.registered_chatcommands) do
99                         if INIT == "client" or core.check_player_privs(name, def.privs) then
100                                 cmds[#cmds + 1] = cmd
101                         end
102                 end
103                 table.sort(cmds)
104                 local msg
105                 if INIT == "game" then
106                         msg = S("Available commands: @1",
107                                 table.concat(cmds, " ")) .. "\n"
108                                 .. S("Use '/help <cmd>' to get more "
109                                 .. "information, or '/help all' to list "
110                                 .. "everything.")
111                 else
112                         msg = core.gettext("Available commands: ")
113                                 .. table.concat(cmds, " ") .. "\n"
114                                 .. core.gettext("Use '.help <cmd>' to get more "
115                                 .. "information, or '.help all' to list "
116                                 .. "everything.")
117                 end
118                 return true, msg
119         elseif param == "all" then
120                 local cmds = {}
121                 for cmd, def in pairs(core.registered_chatcommands) do
122                         if INIT == "client" or core.check_player_privs(name, def.privs) then
123                                 cmds[#cmds + 1] = format_help_line(cmd, def)
124                         end
125                 end
126                 table.sort(cmds)
127                 local msg
128                 if INIT == "game" then
129                         msg = S("Available commands:")
130                 else
131                         msg = core.gettext("Available commands:")
132                 end
133                 return true, msg.."\n"..table.concat(cmds, "\n")
134         elseif INIT == "game" and param == "privs" then
135                 local privs = {}
136                 for priv, def in pairs(core.registered_privileges) do
137                         privs[#privs + 1] = priv .. ": " .. def.description
138                 end
139                 table.sort(privs)
140                 return true, S("Available privileges:").."\n"..table.concat(privs, "\n")
141         else
142                 local cmd = param
143                 local def = core.registered_chatcommands[cmd]
144                 if not def then
145                         local msg
146                         if INIT == "game" then
147                                 msg = S("Command not available: @1", cmd)
148                         else
149                                 msg = core.gettext("Command not available: ") .. cmd
150                         end
151                         return false, msg
152                 else
153                         return true, format_help_line(cmd, def)
154                 end
155         end
156 end
157
158 if INIT == "client" then
159         core.register_chatcommand("help", {
160                 params = core.gettext("[all | <cmd>]"),
161                 description = core.gettext("Get help for commands"),
162                 func = function(param)
163                         return do_help_cmd(nil, param)
164                 end,
165         })
166 else
167         core.register_chatcommand("help", {
168                 params = S("[all | privs | <cmd>]"),
169                 description = S("Get help for commands or list privileges"),
170                 func = do_help_cmd,
171         })
172 end