Change command prefix to "." and add "help" command.

This commit is contained in:
red-001 2017-03-24 23:43:36 +00:00 committed by Nils Dagsson Moskopp
parent 35edba888b
commit 5943ad8975
Signed by: erle
GPG Key ID: A3BC671C35191080
4 changed files with 91 additions and 63 deletions

View File

@ -2,27 +2,35 @@
core.register_on_sending_chat_messages(function(message)
if not (message:sub(1,1) == "/") then
local first_char = message:sub(1,1)
if first_char == "/" or first_char == "." then
core.display_chat_message("issued command: " .. message)
end
if first_char ~= "." then
return false
end
core.display_chat_message("issued command: " .. message)
local cmd, param = string.match(message, "^/([^ ]+) *(.*)")
local cmd, param = string.match(message, "^%.([^ ]+) *(.*)")
if not param then
param = ""
end
local cmd_def = core.registered_chatcommands[cmd]
if not cmd then
core.display_chat_message("-!- Empty command")
return true
end
local cmd_def = core.registered_chatcommands[cmd]
if cmd_def then
core.set_last_run_mod(cmd_def.mod_origin)
local _, message = cmd_def.func(param)
if message then
core.display_chat_message(message)
end
return true
else
core.display_chat_message("-!- Invalid command: " .. cmd)
end
return false
return true
end)

View File

@ -27,4 +27,75 @@ function core.override_chatcommand(name, redefinition)
rawset(chatcommand, k, v)
end
core.registered_chatcommands[name] = chatcommand
end
end
local cmd_marker = "/"
if INIT == "client" then
cmd_marker = "."
end
local function do_help_cmd(name, param)
local function format_help_line(cmd, def)
local msg = core.colorize("#00ffff", cmd_marker .. cmd)
if def.params and def.params ~= "" then
msg = msg .. " " .. def.params
end
if def.description and def.description ~= "" then
msg = msg .. ": " .. def.description
end
return msg
end
if param == "" then
local cmds = {}
for cmd, def in pairs(core.registered_chatcommands) do
if INIT == "client" or core.check_player_privs(name, def.privs) then
cmds[#cmds + 1] = cmd
end
end
table.sort(cmds)
return true, "Available commands: " .. table.concat(cmds, " ") .. "\n"
.. "Use '"..cmd_marker.."help <cmd>' to get more information,"
.. " or '"..cmd_marker.."help all' to list everything."
elseif param == "all" then
local cmds = {}
for cmd, def in pairs(core.registered_chatcommands) do
if INIT == "client" or core.check_player_privs(name, def.privs) then
cmds[#cmds + 1] = format_help_line(cmd, def)
end
end
table.sort(cmds)
return true, "Available commands:\n"..table.concat(cmds, "\n")
elseif INIT == "game" and param == "privs" then
local privs = {}
for priv, def in pairs(core.registered_privileges) do
privs[#privs + 1] = priv .. ": " .. def.description
end
table.sort(privs)
return true, "Available privileges:\n"..table.concat(privs, "\n")
else
local cmd = param
local def = core.registered_chatcommands[cmd]
if not def then
return false, "Command not available: "..cmd
else
return true, format_help_line(cmd, def)
end
end
end
if INIT == "client" then
core.register_chatcommand("help", {
params = "[all/<cmd>]",
description = "Get help for commands",
func = function(param)
return do_help_cmd(nil, param)
end,
})
else
core.register_chatcommand("help", {
params = "[all/privs/<cmd>]",
description = "Get help for commands or list privileges",
func = do_help_cmd,
})
end

View File

@ -82,61 +82,6 @@ core.register_chatcommand("admin", {
end,
})
core.register_chatcommand("help", {
privs = {},
params = "[all/privs/<cmd>]",
description = "Get help for commands or list privileges",
func = function(name, param)
local function format_help_line(cmd, def)
local msg = core.colorize("#00ffff", "/"..cmd)
if def.params and def.params ~= "" then
msg = msg .. " " .. def.params
end
if def.description and def.description ~= "" then
msg = msg .. ": " .. def.description
end
return msg
end
if param == "" then
local msg = ""
local cmds = {}
for cmd, def in pairs(core.registered_chatcommands) do
if core.check_player_privs(name, def.privs) then
cmds[#cmds + 1] = cmd
end
end
table.sort(cmds)
return true, "Available commands: " .. table.concat(cmds, " ") .. "\n"
.. "Use '/help <cmd>' to get more information,"
.. " or '/help all' to list everything."
elseif param == "all" then
local cmds = {}
for cmd, def in pairs(core.registered_chatcommands) do
if core.check_player_privs(name, def.privs) then
cmds[#cmds + 1] = format_help_line(cmd, def)
end
end
table.sort(cmds)
return true, "Available commands:\n"..table.concat(cmds, "\n")
elseif param == "privs" then
local privs = {}
for priv, def in pairs(core.registered_privileges) do
privs[#privs + 1] = priv .. ": " .. def.description
end
table.sort(privs)
return true, "Available privileges:\n"..table.concat(privs, "\n")
else
local cmd = param
local def = core.registered_chatcommands[cmd]
if not def then
return false, "Command not available: "..cmd
else
return true, format_help_line(cmd, def)
end
end
end,
})
core.register_chatcommand("privs", {
params = "<name>",
description = "Print privileges of player",

View File

@ -156,6 +156,10 @@ keymap_chat (Chat key) key KEY_KEY_T
# See http://irrlicht.sourceforge.net/docu/namespaceirr.html#a54da2a0e231901735e3da1b0edf72eb3
keymap_cmd (Command key) key /
# Key for opening the chat window to type local commands.
# See http://irrlicht.sourceforge.net/docu/namespaceirr.html#a54da2a0e231901735e3da1b0edf72eb3
keymap_cmd_local (Command key) key .
# Key for opening the chat console.
# See http://irrlicht.sourceforge.net/docu/namespaceirr.html#a54da2a0e231901735e3da1b0edf72eb3
keyman_console (Console key) key KEY_F10