diff --git a/MinecraftClient/ChatBots/RemoteControl.cs b/MinecraftClient/ChatBots/RemoteControl.cs index df5cde1b..93a07dbc 100644 --- a/MinecraftClient/ChatBots/RemoteControl.cs +++ b/MinecraftClient/ChatBots/RemoteControl.cs @@ -37,7 +37,7 @@ namespace MinecraftClient.ChatBots case "connect": if (command.Length >= 9) { - Settings.ServerIP = command.Substring(8); + Settings.setServerIP(command.Substring(8)); ReconnectToTheServer(); } break; diff --git a/MinecraftClient/ChatBots/Script.cs b/MinecraftClient/ChatBots/Script.cs index 3517a827..a4e9167b 100644 --- a/MinecraftClient/ChatBots/Script.cs +++ b/MinecraftClient/ChatBots/Script.cs @@ -111,7 +111,7 @@ namespace MinecraftClient.ChatBots case "connect": if (instruction_line.Length >= 9) { - Settings.ServerIP = instruction_line.Substring(8); + Settings.setServerIP(instruction_line.Substring(8)); ReconnectToTheServer(); } break; diff --git a/MinecraftClient/McTcpClient.cs b/MinecraftClient/McTcpClient.cs index 88b0fbd5..89054b8e 100644 --- a/MinecraftClient/McTcpClient.cs +++ b/MinecraftClient/McTcpClient.cs @@ -44,25 +44,31 @@ namespace MinecraftClient /// Starts the main chat client /// /// The chosen username of a premium Minecraft Account - /// A valid sessionID obtained with MinecraftCom.GetLogin() - /// The server IP (serveradress or serveradress:port) + /// The player's UUID for online-mode authentication + /// A valid sessionID obtained after logging in + /// The server IP + /// The server port to use + /// Minecraft protocol version to use - public McTcpClient(string username, string uuid, string sessionID, int protocolversion, string server_port) + public McTcpClient(string username, string uuid, string sessionID, int protocolversion, string server_ip, short port) { - StartClient(username, uuid, sessionID, server_port, protocolversion, false, ""); + StartClient(username, uuid, sessionID, server_ip, port, protocolversion, false, ""); } /// /// Starts the main chat client in single command sending mode /// /// The chosen username of a premium Minecraft Account - /// A valid sessionID obtained with MinecraftCom.GetLogin() - /// The server IP (serveradress or serveradress:port) + /// The player's UUID for online-mode authentication + /// A valid sessionID obtained after logging in + /// The server IP + /// The server port to use + /// Minecraft protocol version to use /// The text or command to send. - public McTcpClient(string username, string uuid, string sessionID, string server_port, int protocolversion, string command) + public McTcpClient(string username, string uuid, string sessionID, string server_ip, short port, int protocolversion, string command) { - StartClient(username, uuid, sessionID, server_port, protocolversion, true, command); + StartClient(username, uuid, sessionID, server_ip, port, protocolversion, true, command); } /// @@ -70,41 +76,30 @@ namespace MinecraftClient /// /// The chosen username of a premium Minecraft Account /// A valid sessionID obtained with MinecraftCom.GetLogin() - /// The server IP (serveradress or serveradress:port)/param> + /// The server IP + /// The server port to use + /// Minecraft protocol version to use + /// The player's UUID for online-mode authentication /// If set to true, the client will send a single command and then disconnect from the server /// The text or command to send. Will only be sent if singlecommand is set to true. - private void StartClient(string user, string uuid, string sessionID, string server_port, int protocolversion, bool singlecommand, string command) + private void StartClient(string user, string uuid, string sessionID, string server_ip, short port, int protocolversion, bool singlecommand, string command) { - string[] sip = server_port.Split(':'); - this.sessionid = sessionID; this.uuid = uuid; this.username = user; - this.host = sip[0]; - - if (sip.Length == 1) - { - port = 25565; - } - else - { - try - { - port = Convert.ToInt32(sip[1]); - } - catch (FormatException) { port = 25565; } - } + this.host = server_ip; + this.port = port; if (!singlecommand) { if (Settings.AntiAFK_Enabled) { BotLoad(new ChatBots.AntiAFK(Settings.AntiAFK_Delay)); } if (Settings.Hangman_Enabled) { BotLoad(new ChatBots.HangmanGame(Settings.Hangman_English)); } if (Settings.Alerts_Enabled) { BotLoad(new ChatBots.Alerts()); } - if (Settings.ChatLog_Enabled) { BotLoad(new ChatBots.ChatLog(Settings.replaceVars(Settings.ChatLog_File), Settings.ChatLog_Filter, Settings.ChatLog_DateTime)); } - if (Settings.PlayerLog_Enabled) { BotLoad(new ChatBots.PlayerListLogger(Settings.PlayerLog_Delay, Settings.replaceVars(Settings.PlayerLog_File))); } + if (Settings.ChatLog_Enabled) { BotLoad(new ChatBots.ChatLog(Settings.expandVars(Settings.ChatLog_File), Settings.ChatLog_Filter, Settings.ChatLog_DateTime)); } + if (Settings.PlayerLog_Enabled) { BotLoad(new ChatBots.PlayerListLogger(Settings.PlayerLog_Delay, Settings.expandVars(Settings.PlayerLog_File))); } if (Settings.AutoRelog_Enabled) { BotLoad(new ChatBots.AutoRelog(Settings.AutoRelog_Delay, Settings.AutoRelog_Retries)); } - if (Settings.ScriptScheduler_Enabled) { BotLoad(new ChatBots.ScriptScheduler(Settings.replaceVars(Settings.ScriptScheduler_TasksFile))); } + if (Settings.ScriptScheduler_Enabled) { BotLoad(new ChatBots.ScriptScheduler(Settings.expandVars(Settings.ScriptScheduler_TasksFile))); } if (Settings.RemoteCtrl_Enabled) { BotLoad(new ChatBots.RemoteControl()); } } @@ -191,7 +186,7 @@ namespace MinecraftClient } else if (text.ToLower().StartsWith("/connect ")) { - Settings.ServerIP = text.Substring(9); + Settings.setServerIP(text.Substring(9)); Program.Restart(); } else if (text != "") diff --git a/MinecraftClient/Program.cs b/MinecraftClient/Program.cs index 95a2760f..af0716e5 100644 --- a/MinecraftClient/Program.cs +++ b/MinecraftClient/Program.cs @@ -59,7 +59,7 @@ namespace MinecraftClient Settings.Password = args[1]; if (args.Length >= 3) { - Settings.ServerIP = args[2]; + Settings.setServerIP(args[2]); //Single command? if (args.Length >= 4) @@ -73,7 +73,7 @@ namespace MinecraftClient if (Settings.ConsoleTitle != "") { Settings.Username = "New Window"; - Console.Title = Settings.replaceVars(Settings.ConsoleTitle); + Console.Title = Settings.expandVars(Settings.ConsoleTitle); } //Asking the user to type in missing data such as Username and Password @@ -127,14 +127,14 @@ namespace MinecraftClient { if (Settings.ConsoleTitle != "") { - Console.Title = Settings.replaceVars(Settings.ConsoleTitle); + Console.Title = Settings.expandVars(Settings.ConsoleTitle); } Console.WriteLine("Success. (session ID: " + sessionID + ')'); if (Settings.ServerIP == "") { Console.Write("Server IP : "); - Settings.ServerIP = Console.ReadLine(); + Settings.setServerIP(Console.ReadLine()); } //Get server version @@ -147,9 +147,9 @@ namespace MinecraftClient //Start the main TCP client if (Settings.SingleCommand != "") { - Client = new McTcpClient(Settings.Username, UUID, sessionID, Settings.ServerIP, protocolversion, Settings.SingleCommand); + Client = new McTcpClient(Settings.Username, UUID, sessionID, Settings.ServerIP, Settings.ServerPort, protocolversion, Settings.SingleCommand); } - else Client = new McTcpClient(Settings.Username, UUID, sessionID, protocolversion, Settings.ServerIP); + else Client = new McTcpClient(Settings.Username, UUID, sessionID, protocolversion, Settings.ServerIP, Settings.ServerPort); } catch (NotSupportedException) { diff --git a/MinecraftClient/Settings.cs b/MinecraftClient/Settings.cs index 41aeb198..6a7d11d6 100644 --- a/MinecraftClient/Settings.cs +++ b/MinecraftClient/Settings.cs @@ -20,6 +20,7 @@ namespace MinecraftClient public static string Username = ""; public static string Password = ""; public static string ServerIP = ""; + public static short ServerPort = 25565; public static string SingleCommand = ""; public static string ConsoleTitle = ""; @@ -133,7 +134,7 @@ namespace MinecraftClient { case "login": Login = argValue; break; case "password": Password = argValue; break; - case "serverip": ServerIP = argValue; break; + case "serverip": setServerIP(argValue); break; case "singlecommand": SingleCommand = argValue; break; case "language": Language = argValue; break; case "consoletitle": ConsoleTitle = argValue; break; @@ -327,56 +328,74 @@ 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"; } + /// + /// Parse a "serverip:port" couple and store the values in ServerIP and ServerPort variables + /// + + public static void setServerIP(string serverIP) + { + string[] sip = serverIP.Split(':'); + ServerIP = sip[0]; + if (sip.Length == 1) + { + ServerPort = 25565; + } + else + { + try + { + ServerPort = Convert.ToInt16(sip[1]); + } + catch (FormatException) { ServerPort = 25565; } + } + } + /// /// Replace %variables% with their value /// /// String to parse /// Modifier string - public static string replaceVars(string str) + public static string expandVars(string str) { StringBuilder result = new StringBuilder(); for (int i = 0; i < str.Length; i++) { if (str[i] == '%') { - bool varname_ok = true; + bool varname_ok = false; StringBuilder var_name = new StringBuilder(); + for (int j = i + 1; j < str.Length; j++) { if (!char.IsLetterOrDigit(str[j])) { if (str[j] == '%') - { varname_ok = var_name.Length > 0; - break; - } - else - { - varname_ok = false; - break; - } + break; } else var_name.Append(str[j]); } + if (varname_ok) { string varname = var_name.ToString(); string varname_lower = varname.ToLower(); i = i + varname.Length + 1; - if (varname_lower == "username") + + switch (varname_lower) { - result.Append(Username); + case "username": result.Append(Username); break; + case "serverip": result.Append(ServerIP); break; + case "serverport": result.Append(ServerPort); break; + default: + if (AppVars.ContainsKey(varname_lower)) + { + result.Append(AppVars[varname_lower]); + } + else result.Append("%" + varname + '%'); + break; } - else if (varname_lower == "serverip") - { - result.Append(ServerIP.Split(':')[0]); - } - else if (AppVars.ContainsKey(varname_lower)) - { - result.Append(AppVars[varname_lower]); - } - else result.Append("%" + varname + '%'); } else result.Append(str[i]); }