remove ad from mod.conf
[waspsaliva.git] / builtin / common / chatcommands.lua
blob564a973db58a12f596bcf3cfe99cb1dfc1cb1e81
1 -- Minetest: builtin/common/chatcommands.lua
3 core.registered_chatcommands = {}
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
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
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
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
77 local cmd_marker = "/"
79 local function gettext(...)
80 return ...
81 end
83 local function gettext_replace(text, replace)
84 return text:gsub("$1", replace)
85 end
88 if INIT == "client" then
89 cmd_marker = "."
90 gettext = core.gettext
91 gettext_replace = fgettext_ne
92 end
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
103 return msg
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
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)
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
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)
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,
152 function core.register_list_command(command, desc, setting)
153 local def = {}
154 def.description = desc
155 def.params = "del <item> | add <item> | list"
156 function def.func(param)
157 local list = (minetest.settings:get(setting) or ""):split(",")
158 if param == "list" then
159 return true, table.concat(list, ", ")
160 else
161 local sparam = param:split(" ")
162 local cmd = sparam[1]
163 local item = sparam[2]
164 if cmd == "del" then
165 if not item then
166 return false, "Missing item."
168 local i = table.indexof(list, item)
169 if i == -1 then
170 return false, item .. " is not on the list."
171 else
172 table.remove(list, i)
173 core.settings:set(setting, table.concat(list, ","))
174 return true, "Removed " .. item .. " from the list."
176 elseif cmd == "add" then
177 if not item then
178 return false, "Missing item."
180 local i = table.indexof(list, item)
181 if i ~= -1 then
182 return false, item .. " is already on the list."
183 else
184 table.insert(list, item)
185 core.settings:set(setting, table.concat(list, ","))
186 return true, "Added " .. item .. " to the list."
190 return false, "Invalid usage. (See /help " .. command .. ")"
192 core.register_chatcommand(command, def)
194 else
195 core.register_chatcommand("help", {
196 params = "[all | privs | <cmd>]",
197 description = "Get help for commands or list privileges",
198 func = do_help_cmd,