diff --git a/MinecraftClient/ChatBot.cs b/MinecraftClient/ChatBot.cs index 9ed626ae..8955eca6 100644 --- a/MinecraftClient/ChatBot.cs +++ b/MinecraftClient/ChatBot.cs @@ -81,7 +81,7 @@ namespace MinecraftClient protected bool SendText(string text) { ConsoleIO.WriteLineFormatted("§8BOT:" + text, false); - return handler.SendChatMessage(text); + return handler.SendText(text); } /// diff --git a/MinecraftClient/Commands/Connect.cs b/MinecraftClient/Commands/Connect.cs index d36890fd..3449177c 100644 --- a/MinecraftClient/Commands/Connect.cs +++ b/MinecraftClient/Commands/Connect.cs @@ -8,15 +8,27 @@ namespace MinecraftClient.Commands public class Connect : Command { public override string CMDName { get { return "connect"; } } - public override string CMDDesc { get { return "connect : connect to the specified server."; } } + public override string CMDDesc { get { return "connect [account]: connect to the specified server."; } } public override string Run(McTcpClient handler, string command) { if (hasArg(command)) { - Settings.setServerIP(getArgs(command)[0]); - Program.Restart(); - return ""; + string[] args = getArgs(command); + if (args.Length > 1) + { + if (!Settings.setAccount(args[1])) + { + return "Unknown account '" + args[1] + "'."; + } + } + + if (Settings.setServerIP(args[0])) + { + Program.Restart(); + return ""; + } + else return "Invalid server IP '" + args[0] + "'."; } else return CMDDesc; } diff --git a/MinecraftClient/Commands/Reco.cs b/MinecraftClient/Commands/Reco.cs index a691ae66..520f4fea 100644 --- a/MinecraftClient/Commands/Reco.cs +++ b/MinecraftClient/Commands/Reco.cs @@ -8,10 +8,18 @@ namespace MinecraftClient.Commands public class Reco : Command { public override string CMDName { get { return "reco"; } } - public override string CMDDesc { get { return "reco: restart and reconnect to the server."; } } + public override string CMDDesc { get { return "reco [account]: restart and reconnect to the server."; } } public override string Run(McTcpClient handler, string command) { + string[] args = getArgs(command); + if (args.Length > 0) + { + if (!Settings.setAccount(args[0])) + { + return "Unknown account '" + args[0] + "'."; + } + } Program.Restart(); return ""; } diff --git a/MinecraftClient/Commands/Send.cs b/MinecraftClient/Commands/Send.cs index f47e6450..4ca7b7b7 100644 --- a/MinecraftClient/Commands/Send.cs +++ b/MinecraftClient/Commands/Send.cs @@ -14,7 +14,7 @@ namespace MinecraftClient.Commands { if (hasArg(command)) { - handler.SendChatMessage(getArg(command)); + handler.SendText(getArg(command)); return ""; } else return CMDDesc; diff --git a/MinecraftClient/McTcpClient.cs b/MinecraftClient/McTcpClient.cs index 590e19af..d1c572c6 100644 --- a/MinecraftClient/McTcpClient.cs +++ b/MinecraftClient/McTcpClient.cs @@ -41,6 +41,7 @@ namespace MinecraftClient TcpClient client; IMinecraftCom handler; + Thread cmdprompt; /// /// Starts the main chat client @@ -126,10 +127,14 @@ namespace MinecraftClient { foreach (ChatBot bot in scripts_on_hold) { bots.Add(bot); } scripts_on_hold.Clear(); + Console.WriteLine("Server was successfully joined.\nType '" + (Settings.internalCmdChar == ' ' ? "" : "" + Settings.internalCmdChar) + "quit' to leave the server."); - StartTalk(); + + cmdprompt = new Thread(new ThreadStart(CommandPrompt)); + cmdprompt.Name = "MCC Command prompt"; + cmdprompt.Start(); } } } @@ -149,7 +154,7 @@ namespace MinecraftClient /// Allows the user to send chat messages, commands, and to leave the server. /// - private void StartTalk() + private void CommandPrompt() { try { @@ -183,14 +188,14 @@ namespace MinecraftClient string command = Settings.internalCmdChar == ' ' ? text : text.Substring(1); if (!performInternalCommand(Settings.expandVars(command), ref response_msg) && Settings.internalCmdChar == '/') { - SendChatMessage(text); + SendText(text); } else if (response_msg.Length > 0) { ConsoleIO.WriteLineFormatted("§8MCC: " + response_msg); } } - else SendChatMessage(text); + else SendText(text); } } } @@ -199,7 +204,7 @@ namespace MinecraftClient } /// - /// Perform an internal MCC command (not a server command, use SendChatMessage() instead for that!) + /// Perform an internal MCC command (not a server command, use SendText() instead for that!) /// /// The command /// Set to true if command was sent by the user using the command prompt @@ -277,6 +282,10 @@ namespace MinecraftClient handler.Disconnect(); handler.Dispose(); + + if (cmdprompt != null) + cmdprompt.Abort(); + Thread.Sleep(1000); if (client != null) { client.Close(); } @@ -351,7 +360,7 @@ namespace MinecraftClient /// Text to send to the server /// True if the text was sent with no error - public bool SendChatMessage(string text) + public bool SendText(string text) { if (text.Length > 100) //Message is too long? { diff --git a/MinecraftClient/Settings.cs b/MinecraftClient/Settings.cs index 653e730e..4f3f20db 100644 --- a/MinecraftClient/Settings.cs +++ b/MinecraftClient/Settings.cs @@ -84,8 +84,9 @@ namespace MinecraftClient //Remote Control public static bool RemoteCtrl_Enabled = false; - //Custom app variables + //Custom app variables and Minecraft accounts private static Dictionary AppVars = new Dictionary(); + private static Dictionary> Accounts = new Dictionary>(); private enum ParseMode { Default, Main, AppVars, Proxy, AntiAFK, Hangman, Alerts, ChatLog, AutoRelog, ScriptScheduler, RemoteControl }; @@ -157,7 +158,19 @@ namespace MinecraftClient case "backslash": internalCmdChar = '\\'; break; } break; - + case "accountsfile": + if (File.Exists(argValue)) + { + foreach (string account_line in File.ReadAllLines(argValue)) + { + //Each line contains account data: 'Alias,Login,Password' + string[] account_data = account_line.Split('#')[0].Trim().Split(','); + if (account_data.Length == 3) + Accounts[account_data[0].ToLower()] + = new KeyValuePair(account_data[1], account_data[2]); + } + } + break; } break; @@ -283,13 +296,14 @@ namespace MinecraftClient + "consoletitle=%username% - Minecraft Console Client\r\n" + "internalcmdchar=slash #use 'none', 'slash' or 'backslash'\r\n" + "mcversion=auto #use 'auto' or '1.X.X' values\r\n" + + "accountsfile=accounts.txt\r\n" + "exitonfailure=false\r\n" + "timestamps=false\r\n" + "\r\n" + "[AppVars]\r\n" + "#yourvar=yourvalue\r\n" - + "#can be used in other fields as %yourvar%\r\n" - + "#%username% and %serverip% are reserved variable names.\r\n" + + "#can be used in some other fields as %yourvar%\r\n" + + "#%username% and %serverip% are reserved variables.\r\n" + "\r\n" + "[Proxy]\r\n" + "enabled=false\r\n" @@ -341,26 +355,51 @@ namespace MinecraftClient public static int str2int(string str) { try { return Convert.ToInt32(str); } catch { return 0; } } public static bool str2bool(string str) { return str == "true" || str == "1"; } + /// + /// Load login/password using an account alias + /// + /// True if the account was found and loaded + + public static bool setAccount(string accountAlias) + { + accountAlias = accountAlias.ToLower(); + if (Accounts.ContainsKey(accountAlias)) + { + Settings.Login = Accounts[accountAlias].Key; + Settings.Password = Accounts[accountAlias].Value; + return true; + } + else return false; + } + /// /// Parse a "serverip:port" couple and store the values in ServerIP and ServerPort variables /// + /// True if the server IP was valid and loaded, false otherwise - public static void setServerIP(string serverIP) + public static bool setServerIP(string serverIP) { string[] sip = serverIP.Split(':'); - ServerIP = sip[0]; - if (sip.Length == 1) - { - ServerPort = 25565; - } - else + string host = sip[0]; + short port = 25565; + + if (sip.Length > 1) { try { - ServerPort = Convert.ToInt16(sip[1]); + port = Convert.ToInt16(sip[1]); } - catch (FormatException) { ServerPort = 25565; } + catch (FormatException) { return false; } } + + if (host == "localhost" || host.Contains('.')) + { + ServerIP = host; + ServerPort = port; + return true; + } + + return false; } /// diff --git a/MinecraftClient/config/sample-accounts.txt b/MinecraftClient/config/sample-accounts.txt new file mode 100644 index 00000000..f2326621 --- /dev/null +++ b/MinecraftClient/config/sample-accounts.txt @@ -0,0 +1,14 @@ +# Minecraft Console Client +# Account list file + +# Put account data as comma separated values +# Values are: Alias,Login,Password +# It allows a fast account switching +# without directly using the credentials + +# Usage examples: +# /tell reco Player2 +# /connect Player1 + +Player1,playerone@email.com,thepassword +Player2,TestBot,- \ No newline at end of file