From 9456e8292374e281b5be3831fe82ac15491506af Mon Sep 17 00:00:00 2001 From: ORelio Date: Sat, 14 Jun 2014 13:20:15 +0200 Subject: [PATCH] Handle all internal MCC commands in one place - MCC internal commands for command prompt, remote control and scripts are handled in one place, thus it's no more needed to add them in 3 different places. - "exit" command in scripts is not equivalent to "/quit" - removed "disconnect" command in scripts /!\ - bots can now easily perform internal MCC commands. --- MinecraftClient/ChatBot.cs | 27 +++- MinecraftClient/ChatBots/RemoteControl.cs | 27 +--- MinecraftClient/ChatBots/Script.cs | 22 +--- MinecraftClient/McTcpClient.cs | 154 +++++++++++++++------- 4 files changed, 144 insertions(+), 86 deletions(-) diff --git a/MinecraftClient/ChatBot.cs b/MinecraftClient/ChatBot.cs index 6d8c3bb2..bdd7c4cc 100644 --- a/MinecraftClient/ChatBot.cs +++ b/MinecraftClient/ChatBot.cs @@ -75,11 +75,34 @@ namespace MinecraftClient /// Send text to the server. Can be anything such as chat messages or commands /// /// Text to send to the server + /// True if the text was sent with no error - protected void SendText(string text) + protected bool SendText(string text) { ConsoleIO.WriteLineFormatted("ยง8BOT:" + text, false); - handler.SendChatMessage(text); + return handler.SendChatMessage(text); + } + + /// + /// Check if the given command is a valid internal MCC command + /// + /// The command or command name + /// TRUE if this is an internal command + + protected bool isInternalCommand(string command) + { + return handler.isInternalCommand(command); + } + + /// + /// Perform an internal MCC command (not a server command, use SendText() instead for that!) + /// + /// The command + /// TRUE if the command was successfully recognized and performed + + protected bool performInternalCommand(string command) + { + return handler.performInternalCommand(command); } /// diff --git a/MinecraftClient/ChatBots/RemoteControl.cs b/MinecraftClient/ChatBots/RemoteControl.cs index 93a07dbc..44abdb12 100644 --- a/MinecraftClient/ChatBots/RemoteControl.cs +++ b/MinecraftClient/ChatBots/RemoteControl.cs @@ -20,27 +20,6 @@ namespace MinecraftClient.ChatBots string cmd_name = command.Split(' ')[0]; switch (cmd_name.ToLower()) { - case "exit": - DisconnectAndExit(); - break; - case "reco": - ReconnectToTheServer(); - break; - case "script": - if (command.Length >= 8) - RunScript(command.Substring(7), sender); - break; - case "send": - if (command.Length >= 6) - SendText(command.Substring(5)); - break; - case "connect": - if (command.Length >= 9) - { - Settings.setServerIP(command.Substring(8)); - ReconnectToTheServer(); - } - break; case "help": if (command.Length >= 6) { @@ -59,7 +38,11 @@ namespace MinecraftClient.ChatBots else SendPrivateMessage(sender, "help . Available commands: exit, reco, script, send, connect."); break; default: - SendPrivateMessage(sender, "Unknown command '" + cmd_name + "'. Use 'help' for help."); + if (isInternalCommand(command)) + { + performInternalCommand(command); + } + else SendPrivateMessage(sender, "Unknown command '" + cmd_name + "'. Use 'help' for help."); break; } } diff --git a/MinecraftClient/ChatBots/Script.cs b/MinecraftClient/ChatBots/Script.cs index a4e9167b..18d83cf4 100644 --- a/MinecraftClient/ChatBots/Script.cs +++ b/MinecraftClient/ChatBots/Script.cs @@ -90,9 +90,6 @@ namespace MinecraftClient.ChatBots string instruction_name = instruction_line.Split(' ')[0]; switch (instruction_name.ToLower()) { - case "send": - SendText(instruction_line.Substring(5, instruction_line.Length - 5)); - break; case "wait": int ticks = 10; try @@ -102,21 +99,12 @@ namespace MinecraftClient.ChatBots catch { } sleepticks = ticks; break; - case "disconnect": - DisconnectAndExit(); - break; - case "exit": //Exit bot & stay connected to the server - UnloadBot(); - break; - case "connect": - if (instruction_line.Length >= 9) - { - Settings.setServerIP(instruction_line.Substring(8)); - ReconnectToTheServer(); - } - break; default: - sleepticks = 0; Update(); //Unknown command : process next line immediately + if (isInternalCommand(instruction_line)) + { + performInternalCommand(instruction_line); + } + else sleepticks = 0; Update(); //Unknown command : process next line immediately break; } } diff --git a/MinecraftClient/McTcpClient.cs b/MinecraftClient/McTcpClient.cs index 89054b8e..488c8439 100644 --- a/MinecraftClient/McTcpClient.cs +++ b/MinecraftClient/McTcpClient.cs @@ -171,60 +171,103 @@ namespace MinecraftClient else { text = text.Trim(); - if (text.ToLower() == "/quit" || text.ToLower() == "/reco") + if (text.Length > 0) { - break; - } - else if (text.ToLower() == "/respawn") - { - handler.SendRespawnPacket(); - ConsoleIO.WriteLine("You have respawned."); - } - else if (text.ToLower().StartsWith("/script ")) - { - BotLoad(new ChatBots.Script(text.Substring(8))); - } - else if (text.ToLower().StartsWith("/connect ")) - { - Settings.setServerIP(text.Substring(9)); - Program.Restart(); - } - else if (text != "") - { - //Message is too long - if (text.Length > 100) + if (text[0] == '/') { - if (text[0] == '/') + string command = text.Substring(1); + if (isInternalCommand(command)) { - //Send the first 100 chars of the command - text = text.Substring(0, 100); - handler.SendChatMessage(text); - } - else - { - //Send the message splitted into several messages - while (text.Length > 100) - { - handler.SendChatMessage(text.Substring(0, 100)); - text = text.Substring(100, text.Length - 100); - } - handler.SendChatMessage(text); + performInternalCommand(command, true); } + else SendChatMessage(text); } - else handler.SendChatMessage(text); } } } - - switch (text.ToLower()) - { - case "/quit": Program.Exit(); break; - case "/reco": Program.Restart(); break; - } } catch (IOException) { } } + /// + /// Check if the given command is a valid internal MCC command + /// + /// The command or command name + /// TRUE if this is an internal command + + public bool isInternalCommand(string command) + { + string command_name = command.Split(' ')[0].ToLower(); + switch (command_name) + { + case "exit": + case "quit": + case "reco": + case "respawn": + case "send": + case "script": + case "connect": + return true; + default: + return false; + } + } + + /// + /// Perform an internal MCC command (not a server command, use SendChatMessage() instead for that!) + /// + /// The command + /// Set to true if command was sent by the user using the command prompt + /// TRUE if the command was successfully recognized and performed + + public bool performInternalCommand(string command, bool interactive_mode = false) + { + string[] command_args = command.Split(' '); + string command_name = command_args[0].ToLower(); + switch (command_name) + { + case "exit": + case "quit": + Program.Exit(); + break; + + case "reco": + Program.Restart(); + break; + + case "respawn": + handler.SendRespawnPacket(); + if (interactive_mode) + ConsoleIO.WriteLine("You have respawned."); + break; + + case "send": + if (command.Length > 5) + { + string text = command.Substring(5); + SendChatMessage(text); + } + break; + + case "script": + if (command.Length > 8) + BotLoad(new ChatBots.Script(command.Substring(8))); + break; + + case "connect": + if (command_args.Length > 1) + { + Settings.setServerIP(command_args[1]); + Program.Restart(); + } + break; + + default: + return false; + } + return true; + } + /// /// Disconnect the client from the server /// @@ -306,12 +349,33 @@ namespace MinecraftClient } /// - /// Send a chat message to the server + /// Send a chat message or command to the server /// + /// Text to send to the server + /// True if the text was sent with no error - public void SendChatMessage(string message) + public bool SendChatMessage(string text) { - handler.SendChatMessage(message); + if (text.Length > 100) //Message is too long? + { + if (text[0] == '/') + { + //Send the first 100 chars of the command + text = text.Substring(0, 100); + return handler.SendChatMessage(text); + } + else + { + //Send the message splitted into several messages + while (text.Length > 100) + { + handler.SendChatMessage(text.Substring(0, 100)); + text = text.Substring(100, text.Length - 100); + } + return handler.SendChatMessage(text); + } + } + else return handler.SendChatMessage(text); } } }