Move chat command handling code from C++ to Lua (#5528)

master
red-001 2017-04-08 19:03:57 +01:00 committed by SmallJoker
parent fb4c730708
commit d4e9dd4643
2 changed files with 19 additions and 21 deletions

View File

@ -7,13 +7,22 @@
core.chatcommands = core.registered_chatcommands -- BACKWARDS COMPATIBILITY core.chatcommands = core.registered_chatcommands -- BACKWARDS COMPATIBILITY
core.register_on_chat_message(function(name, message) core.register_on_chat_message(function(name, message)
local cmd, param = string.match(message, "^/([^ ]+) *(.*)") if message:sub(1,1) ~= "/" then
if not param then return
param = ""
end end
local cmd, param = string.match(message, "^/([^ ]+) *(.*)")
if not cmd then
core.chat_send_player(name, "-!- Empty command")
return true
end
param = param or ""
local cmd_def = core.registered_chatcommands[cmd] local cmd_def = core.registered_chatcommands[cmd]
if not cmd_def then if not cmd_def then
return false core.chat_send_player(name, "-!- Invalid command: " .. cmd)
return true
end end
local has_privs, missing_privs = core.check_player_privs(name, cmd_def.privs) local has_privs, missing_privs = core.check_player_privs(name, cmd_def.privs)
if has_privs then if has_privs then

View File

@ -2820,16 +2820,6 @@ std::wstring Server::handleChat(const std::string &name, const std::wstring &wna
// Whether to send line to the player that sent the message, or to all players // Whether to send line to the player that sent the message, or to all players
bool broadcast_line = true; bool broadcast_line = true;
// Commands are implemented in Lua, so only catch invalid
// commands that were not "eaten" and send an error back
if (wmessage[0] == L'/') {
std::wstring wcmd = wmessage.substr(1);
broadcast_line = false;
if (wcmd.length() == 0)
line += L"-!- Empty command";
else
line += L"-!- Invalid command: " + str_split(wcmd, L' ')[0];
} else {
if (check_shout_priv && !checkPriv(name, "shout")) { if (check_shout_priv && !checkPriv(name, "shout")) {
line += L"-!- You don't have permission to shout."; line += L"-!- You don't have permission to shout.";
broadcast_line = false; broadcast_line = false;
@ -2839,7 +2829,6 @@ std::wstring Server::handleChat(const std::string &name, const std::wstring &wna
line += L"> "; line += L"> ";
line += wmessage; line += wmessage;
} }
}
/* /*
Tell calling method to send the message to sender Tell calling method to send the message to sender