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