Skip to content

Commit

Permalink
Change command prefix to "." and add "help" command.
Browse files Browse the repository at this point in the history
  • Loading branch information
red-001 authored and paramat committed Mar 26, 2017
1 parent 4d5177f commit e70e151
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 63 deletions.
22 changes: 15 additions & 7 deletions builtin/client/chatcommands.lua
Expand Up @@ -2,27 +2,35 @@


core.register_on_sending_chat_messages(function(message)
if not (message:sub(1,1) == "/") then
return false
local first_char = message:sub(1,1)
if first_char == "/" or first_char == "." then
core.display_chat_message("issued command: " .. message)
end

core.display_chat_message("issued command: " .. message)
if first_char ~= "." then
return false
end

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)
73 changes: 72 additions & 1 deletion builtin/common/chatcommands.lua
Expand Up @@ -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
55 changes: 0 additions & 55 deletions builtin/game/chatcommands.lua
Expand Up @@ -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",
Expand Down
4 changes: 4 additions & 0 deletions builtin/settingtypes.txt
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions minetest.conf.example
Expand Up @@ -148,6 +148,11 @@
# type: key
# keymap_cmd = /

# Key for opening the chat window to type local commands.
# See http://irrlicht.sourceforge.net/docu/namespaceirr.html#a54da2a0e231901735e3da1b0edf72eb3
# type: key
# keymap_cmd_local = .

# Key for opening the chat console.
# See http://irrlicht.sourceforge.net/docu/namespaceirr.html#a54da2a0e231901735e3da1b0edf72eb3
# type: key
Expand Down
1 change: 1 addition & 0 deletions src/client/keys.h
Expand Up @@ -42,6 +42,7 @@ class KeyType {
INVENTORY,
CHAT,
CMD,
CMD_LOCAL,
CONSOLE,
MINIMAP,
FREEMOVE,
Expand Down
1 change: 1 addition & 0 deletions src/defaultsettings.cpp
Expand Up @@ -72,6 +72,7 @@ void set_default_settings(Settings *settings)
settings->setDefault("keymap_special1", "KEY_KEY_E");
settings->setDefault("keymap_chat", "KEY_KEY_T");
settings->setDefault("keymap_cmd", "/");
settings->setDefault("keymap_cmd_local", ".");
settings->setDefault("keymap_minimap", "KEY_F9");
settings->setDefault("keymap_console", "KEY_F10");
settings->setDefault("keymap_rangeselect", "KEY_KEY_R");
Expand Down
3 changes: 3 additions & 0 deletions src/game.cpp
Expand Up @@ -1034,6 +1034,7 @@ void KeyCache::populate()
key[KeyType::INVENTORY] = getKeySetting("keymap_inventory");
key[KeyType::CHAT] = getKeySetting("keymap_chat");
key[KeyType::CMD] = getKeySetting("keymap_cmd");
key[KeyType::CMD_LOCAL] = getKeySetting("keymap_cmd_local");
key[KeyType::CONSOLE] = getKeySetting("keymap_console");
key[KeyType::MINIMAP] = getKeySetting("keymap_minimap");
key[KeyType::FREEMOVE] = getKeySetting("keymap_freemove");
Expand Down Expand Up @@ -2449,6 +2450,8 @@ void Game::processKeyInput()
openConsole(0.2, L"");
} else if (wasKeyDown(KeyType::CMD)) {
openConsole(0.2, L"/");
} else if (wasKeyDown(KeyType::CMD_LOCAL)) {
openConsole(0.2, L".");
} else if (wasKeyDown(KeyType::CONSOLE)) {
openConsole(core::clamp(g_settings->getFloat("console_height"), 0.1f, 1.0f));
} else if (wasKeyDown(KeyType::FREEMOVE)) {
Expand Down
2 changes: 2 additions & 0 deletions src/guiKeyChangeMenu.cpp
Expand Up @@ -53,6 +53,7 @@ enum
GUI_ID_KEY_CINEMATIC_BUTTON,
GUI_ID_KEY_CHAT_BUTTON,
GUI_ID_KEY_CMD_BUTTON,
GUI_ID_KEY_CMD_LOCAL_BUTTON,
GUI_ID_KEY_CONSOLE_BUTTON,
GUI_ID_KEY_SNEAK_BUTTON,
GUI_ID_KEY_DROP_BUTTON,
Expand Down Expand Up @@ -408,6 +409,7 @@ void GUIKeyChangeMenu::init_keys()
this->add_key(GUI_ID_KEY_INVENTORY_BUTTON, wgettext("Inventory"), "keymap_inventory");
this->add_key(GUI_ID_KEY_CHAT_BUTTON, wgettext("Chat"), "keymap_chat");
this->add_key(GUI_ID_KEY_CMD_BUTTON, wgettext("Command"), "keymap_cmd");
this->add_key(GUI_ID_KEY_CMD_LOCAL_BUTTON, wgettext("Local command"), "keymap_cmd_local");
this->add_key(GUI_ID_KEY_CONSOLE_BUTTON, wgettext("Console"), "keymap_console");
this->add_key(GUI_ID_KEY_FLY_BUTTON, wgettext("Toggle fly"), "keymap_freemove");
this->add_key(GUI_ID_KEY_FAST_BUTTON, wgettext("Toggle fast"), "keymap_fastmove");
Expand Down

0 comments on commit e70e151

Please sign in to comment.