Move chat command handling code from C++ to Lua (#5528)
authorred-001 <red-001@outlook.ie>
Sat, 8 Apr 2017 18:03:57 +0000 (19:03 +0100)
committerSmallJoker <SmallJoker@users.noreply.github.com>
Sat, 8 Apr 2017 18:03:57 +0000 (20:03 +0200)
builtin/game/chatcommands.lua
src/server.cpp

index 16f5f3be9869762d0882bd668ae902499e54672c..8df3903d21734b7d8f9207ed63978a4a57316cd5 100644 (file)
@@ -7,13 +7,22 @@
 core.chatcommands = core.registered_chatcommands -- BACKWARDS COMPATIBILITY
 
 core.register_on_chat_message(function(name, message)
+       if message:sub(1,1) ~= "/" then
+               return
+       end
+
        local cmd, param = string.match(message, "^/([^ ]+) *(.*)")
-       if not param then
-               param = ""
+       if not cmd then
+               core.chat_send_player(name, "-!- Empty command")
+               return true
        end
+
+       param = param or ""
+
        local cmd_def = core.registered_chatcommands[cmd]
        if not cmd_def then
-               return false
+               core.chat_send_player(name, "-!- Invalid command: " .. cmd)
+               return true
        end
        local has_privs, missing_privs = core.check_player_privs(name, cmd_def.privs)
        if has_privs then
index 224af47a7c294ffd0e04f16dc54a805ad1d71b23..02e365718a521fefca250d35eb07b757c22b076c 100644 (file)
@@ -2820,25 +2820,14 @@ 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
        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);
+       if (check_shout_priv && !checkPriv(name, "shout")) {
+               line += L"-!- You don't have permission to shout.";
                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")) {
-                       line += L"-!- You don't have permission to shout.";
-                       broadcast_line = false;
-               } else {
-                       line += L"<";
-                       line += wname;
-                       line += L"> ";
-                       line += wmessage;
-               }
+               line += L"<";
+               line += wname;
+               line += L"> ";
+               line += wmessage;
        }
 
        /*