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
This commit is contained in:
ORelio 2014-06-14 16:01:19 +02:00
parent f0b071ddea
commit 8b5ce567a6
5 changed files with 97 additions and 35 deletions

View file

@ -138,9 +138,35 @@ namespace MinecraftClient
}
//Get server 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...");
int protocolversion = 0; string version = "";
if (ProtocolHandler.GetServerInfo(Settings.ServerIP, ref protocolversion, ref version))
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

View file

@ -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 };

View file

@ -496,8 +496,9 @@ namespace MinecraftClient.Protocol.Handlers
/// </summary>
/// <returns>True if ping was successful</returns>
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;

View file

@ -19,36 +19,19 @@ namespace MinecraftClient.Protocol
/// Retrieve information about a Minecraft server
/// </summary>
/// <param name="serverIP">Server IP to ping</param>
/// <param name="serverPort">Server Port to ping</param>
/// <param name="protocolversion">Will contain protocol version, if ping successful</param>
/// <param name="version">Will contain minecraft version, if ping successful</param>
/// <returns>TRUE if ping was successful</returns>
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.");
}
/// <summary>
/// Convert a human-readable Minecraft version number to network protocol version number
/// </summary>
/// <param name="MCVersion">The Minecraft version number</param>
/// <returns>The protocol version number or 0 if could not determine protocol version: error, unknown, not supported</returns>
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 };

View file

@ -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<string, string> AppVars = new Dictionary<string, string>();
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"