Expose and document chatcommands as minetest.registered_chatcommands

This commit is contained in:
rubenwardy 2016-12-28 13:01:32 +00:00
parent ca3629637c
commit e8b7179ccd
2 changed files with 11 additions and 9 deletions

View File

@ -4,14 +4,15 @@
-- Chat command handler -- Chat command handler
-- --
core.chatcommands = {} core.registered_chatcommands = {}
core.chatcommands = core.registered_chatcommands -- BACKWARDS COMPATIBILITY
function core.register_chatcommand(cmd, def) function core.register_chatcommand(cmd, def)
def = def or {} def = def or {}
def.params = def.params or "" def.params = def.params or ""
def.description = def.description or "" def.description = def.description or ""
def.privs = def.privs or {} def.privs = def.privs or {}
def.mod_origin = core.get_current_modname() or "??" def.mod_origin = core.get_current_modname() or "??"
core.chatcommands[cmd] = def core.registered_chatcommands[cmd] = def
end end
core.register_on_chat_message(function(name, message) core.register_on_chat_message(function(name, message)
@ -19,7 +20,7 @@ core.register_on_chat_message(function(name, message)
if not param then if not param then
param = "" param = ""
end end
local cmd_def = core.chatcommands[cmd] local cmd_def = core.registered_chatcommands[cmd]
if not cmd_def then if not cmd_def then
return false return false
end end
@ -107,7 +108,7 @@ core.register_chatcommand("help", {
if param == "" then if param == "" then
local msg = "" local msg = ""
local cmds = {} local cmds = {}
for cmd, def in pairs(core.chatcommands) do for cmd, def in pairs(core.registered_chatcommands) do
if core.check_player_privs(name, def.privs) then if core.check_player_privs(name, def.privs) then
cmds[#cmds + 1] = cmd cmds[#cmds + 1] = cmd
end end
@ -118,7 +119,7 @@ core.register_chatcommand("help", {
.. " or '/help all' to list everything." .. " or '/help all' to list everything."
elseif param == "all" then elseif param == "all" then
local cmds = {} local cmds = {}
for cmd, def in pairs(core.chatcommands) do for cmd, def in pairs(core.registered_chatcommands) do
if core.check_player_privs(name, def.privs) then if core.check_player_privs(name, def.privs) then
cmds[#cmds + 1] = format_help_line(cmd, def) cmds[#cmds + 1] = format_help_line(cmd, def)
end end
@ -134,7 +135,7 @@ core.register_chatcommand("help", {
return true, "Available privileges:\n"..table.concat(privs, "\n") return true, "Available privileges:\n"..table.concat(privs, "\n")
else else
local cmd = param local cmd = param
local def = core.chatcommands[cmd] local def = core.registered_chatcommands[cmd]
if not def then if not def then
return false, "Command not available: "..cmd return false, "Command not available: "..cmd
else else
@ -161,7 +162,7 @@ local function handle_grant_command(caller, grantname, grantprivstr)
if not (caller_privs.privs or caller_privs.basic_privs) then if not (caller_privs.privs or caller_privs.basic_privs) then
return false, "Your privileges are insufficient." return false, "Your privileges are insufficient."
end end
if not core.get_auth_handler().get_auth(grantname) then if not core.get_auth_handler().get_auth(grantname) then
return false, "Player " .. grantname .. " does not exist." return false, "Player " .. grantname .. " does not exist."
end end
@ -204,7 +205,7 @@ core.register_chatcommand("grant", {
local grantname, grantprivstr = string.match(param, "([^ ]+) (.+)") local grantname, grantprivstr = string.match(param, "([^ ]+) (.+)")
if not grantname or not grantprivstr then if not grantname or not grantprivstr then
return false, "Invalid parameters (see /help grant)" return false, "Invalid parameters (see /help grant)"
end end
return handle_grant_command(name, grantname, grantprivstr) return handle_grant_command(name, grantname, grantprivstr)
end, end,
}) })
@ -215,7 +216,7 @@ core.register_chatcommand("grantme", {
func = function(name, param) func = function(name, param)
if param == "" then if param == "" then
return false, "Invalid parameters (see /help grantme)" return false, "Invalid parameters (see /help grantme)"
end end
return handle_grant_command(name, name, param) return handle_grant_command(name, name, param)
end, end,
}) })

View File

@ -2076,6 +2076,7 @@ Call these functions only at load time!
### Other registration functions ### Other registration functions
* `minetest.register_chatcommand(cmd, chatcommand definition)` * `minetest.register_chatcommand(cmd, chatcommand definition)`
* Adds definition to minetest.registered_chatcommands
* `minetest.register_privilege(name, definition)` * `minetest.register_privilege(name, definition)`
* `definition`: `"description text"` * `definition`: `"description text"`
* `definition`: `{ description = "description text", give_to_singleplayer = boolean}` * `definition`: `{ description = "description text", give_to_singleplayer = boolean}`