From 8b5ce567a698b03d2849229ea3bfc8cd8decbf27 Mon Sep 17 00:00:00 2001 From: ORelio Date: Sat, 14 Jun 2014 16:01:19 +0200 Subject: [PATCH] Add setting for manually choosing MC version - User can manually provide server version in config file - Server is not pinged if a minecraft version was manually provided - If the provided version isn't recognized, ping is re-enabled --- MinecraftClient/Program.cs | 41 +++++++--- .../Protocol/Handlers/Protocol16.cs | 3 +- .../Protocol/Handlers/Protocol17.cs | 3 +- MinecraftClient/Protocol/ProtocolHandler.cs | 80 ++++++++++++++----- MinecraftClient/Settings.cs | 5 +- 5 files changed, 97 insertions(+), 35 deletions(-) diff --git a/MinecraftClient/Program.cs b/MinecraftClient/Program.cs index af0716e5..629520bf 100644 --- a/MinecraftClient/Program.cs +++ b/MinecraftClient/Program.cs @@ -138,9 +138,35 @@ namespace MinecraftClient } //Get server version - Console.WriteLine("Retrieving Server Info..."); - int protocolversion = 0; string version = ""; - if (ProtocolHandler.GetServerInfo(Settings.ServerIP, ref protocolversion, ref version)) + int protocolversion = 0; + + if (Settings.ServerVersion != "" && Settings.ServerVersion.ToLower() != "auto") + { + protocolversion = Protocol.ProtocolHandler.MCVer2ProtocolVersion(Settings.ServerVersion); + if (protocolversion != 0) + { + ConsoleIO.WriteLineFormatted("§8Using Minecraft version " + Settings.ServerVersion + " (protocol v" + protocolversion + ')'); + } + else ConsoleIO.WriteLineFormatted("§8Unknown or not supported MC version '" + Settings.ServerVersion + "'.\nSwitching to autodetection mode."); + } + + if (protocolversion == 0) + { + Console.WriteLine("Retrieving Server Info..."); + if (!ProtocolHandler.GetServerInfo(Settings.ServerIP, Settings.ServerPort, ref protocolversion)) + { + Console.WriteLine("Failed to ping this IP."); + if (Settings.AutoRelog_Enabled) + { + ChatBots.AutoRelog bot = new ChatBots.AutoRelog(Settings.AutoRelog_Delay, Settings.AutoRelog_Retries); + if (!bot.OnDisconnect(ChatBot.DisconnectReason.ConnectionLost, "Failed to ping this IP.")) { ReadLineReconnect(); } + } + else ReadLineReconnect(); + return; + } + } + + if (protocolversion != 0) { try { @@ -159,13 +185,8 @@ namespace MinecraftClient } else { - Console.WriteLine("Failed to ping this IP."); - if (Settings.AutoRelog_Enabled) - { - ChatBots.AutoRelog bot = new ChatBots.AutoRelog(Settings.AutoRelog_Delay, Settings.AutoRelog_Retries); - if (!bot.OnDisconnect(ChatBot.DisconnectReason.ConnectionLost, "Failed to ping this IP.")) { ReadLineReconnect(); } - } - else ReadLineReconnect(); + Console.WriteLine("Failed to determine server version."); + ReadLineReconnect(); } } else diff --git a/MinecraftClient/Protocol/Handlers/Protocol16.cs b/MinecraftClient/Protocol/Handlers/Protocol16.cs index 1dc83fed..16cd92a1 100644 --- a/MinecraftClient/Protocol/Handlers/Protocol16.cs +++ b/MinecraftClient/Protocol/Handlers/Protocol16.cs @@ -647,10 +647,11 @@ namespace MinecraftClient.Protocol.Handlers return packet_data; } - public static bool doPing(string host, int port, ref int protocolversion, ref string version) + public static bool doPing(string host, int port, ref int protocolversion) { try { + string version = ""; TcpClient tcp = ProxyHandler.newTcpClient(host, port); tcp.ReceiveTimeout = 5000; //MC 1.7.2+ SpigotMC servers won't answer, so we need a reasonable timeout. byte[] ping = new byte[2] { 0xfe, 0x01 }; diff --git a/MinecraftClient/Protocol/Handlers/Protocol17.cs b/MinecraftClient/Protocol/Handlers/Protocol17.cs index a0eb33ab..42fa4a8d 100644 --- a/MinecraftClient/Protocol/Handlers/Protocol17.cs +++ b/MinecraftClient/Protocol/Handlers/Protocol17.cs @@ -496,8 +496,9 @@ namespace MinecraftClient.Protocol.Handlers /// /// True if ping was successful - public static bool doPing(string host, int port, ref int protocolversion, ref string version) + public static bool doPing(string host, int port, ref int protocolversion) { + string version = ""; TcpClient tcp = ProxyHandler.newTcpClient(host, port); tcp.ReceiveBufferSize = 1024 * 1024; diff --git a/MinecraftClient/Protocol/ProtocolHandler.cs b/MinecraftClient/Protocol/ProtocolHandler.cs index b715fda8..a35fa072 100644 --- a/MinecraftClient/Protocol/ProtocolHandler.cs +++ b/MinecraftClient/Protocol/ProtocolHandler.cs @@ -19,36 +19,19 @@ namespace MinecraftClient.Protocol /// Retrieve information about a Minecraft server /// /// Server IP to ping + /// Server Port to ping /// Will contain protocol version, if ping successful - /// Will contain minecraft version, if ping successful /// TRUE if ping was successful - public static bool GetServerInfo(string serverIP, ref int protocolversion, ref string version) + public static bool GetServerInfo(string serverIP, short serverPort, ref int protocolversion) { try { - string host; int port; - string[] sip = serverIP.Split(':'); - host = sip[0]; - - if (sip.Length == 1) - { - port = 25565; - } - else - { - try - { - port = Convert.ToInt32(sip[1]); - } - catch (FormatException) { port = 25565; } - } - - if (Protocol16Handler.doPing(host, port, ref protocolversion, ref version)) + if (Protocol16Handler.doPing(serverIP, serverPort, ref protocolversion)) { return true; } - else if (Protocol17Handler.doPing(host, port, ref protocolversion, ref version)) + else if (Protocol17Handler.doPing(serverIP, serverPort, ref protocolversion)) { return true; } @@ -81,7 +64,60 @@ namespace MinecraftClient.Protocol int[] supportedVersions_Protocol17 = { 4, 5 }; if (Array.IndexOf(supportedVersions_Protocol17, ProtocolVersion) > -1) return new Protocol17Handler(Client, ProtocolVersion, Handler); - throw new NotSupportedException("The protocol version '" + ProtocolVersion + "' is not supported."); + throw new NotSupportedException("The protocol version no." + ProtocolVersion + " is not supported."); + } + + /// + /// Convert a human-readable Minecraft version number to network protocol version number + /// + /// The Minecraft version number + /// The protocol version number or 0 if could not determine protocol version: error, unknown, not supported + + public static int MCVer2ProtocolVersion(string MCVersion) + { + if (MCVersion.Contains('.')) + { + switch (MCVersion.Split(' ')[0].Trim()) + { + case "1.4.6": + case "1.4.7": + return 51; + case "1.5.1": + return 60; + case "1.5.2": + return 61; + case "1.6.0": + return 72; + case "1.6.1": + case "1.6.2": + case "1.6.3": + case "1.6.4": + return 73; + case "1.7.2": + case "1.7.3": + case "1.7.4": + case "1.7.5": + return 4; + case "1.7.6": + case "1.7.7": + case "1.7.8": + case "1.7.9": + return 5; + default: + return 0; + } + } + else + { + try + { + return Int32.Parse(MCVersion); + } + catch + { + return -1; + } + } } public enum LoginResult { OtherError, ServiceUnavailable, SSLError, Success, WrongPassword, AccountMigrated, NotPremium }; diff --git a/MinecraftClient/Settings.cs b/MinecraftClient/Settings.cs index 6a7d11d6..f13d5f12 100644 --- a/MinecraftClient/Settings.cs +++ b/MinecraftClient/Settings.cs @@ -21,6 +21,7 @@ namespace MinecraftClient public static string Password = ""; public static string ServerIP = ""; public static short ServerPort = 25565; + public static string ServerVersion = ""; public static string SingleCommand = ""; public static string ConsoleTitle = ""; @@ -82,7 +83,7 @@ namespace MinecraftClient //Remote Control public static bool RemoteCtrl_Enabled = false; - //App variables + //Custom app variables private static Dictionary AppVars = new Dictionary(); private enum ParseMode { Default, Main, AppVars, Proxy, AntiAFK, Hangman, Alerts, ChatLog, AutoRelog, ScriptScheduler, RemoteControl }; @@ -140,6 +141,7 @@ namespace MinecraftClient case "consoletitle": ConsoleTitle = argValue; break; case "timestamps": chatTimeStamps = str2bool(argValue); break; case "exitonfailure": exitOnFailure = str2bool(argValue); break; + case "mcversion": ServerVersion = argValue; break; case "botowners": Bots_Owners.Clear(); foreach (string name in argValue.ToLower().Replace(" ", "").Split(',')) @@ -270,6 +272,7 @@ namespace MinecraftClient + "language=en_GB\r\n" + "botowners=Player1,Player2,Player3\r\n" + "consoletitle=%username% - Minecraft Console Client\r\n" + + "mcversion=auto #use 'auto' or '1.X.X' values\r\n" + "exitonfailure=false\r\n" + "timestamps=false\r\n" + "\r\n"