Fix ServerList bug

This commit is contained in:
BruceChen 2022-11-05 21:15:16 +08:00
parent 0dbc6086d1
commit fc30e38e78

View file

@ -346,36 +346,32 @@ namespace MinecraftClient
/// <returns>True if the server IP was valid and loaded, false otherwise</returns> /// <returns>True if the server IP was valid and loaded, false otherwise</returns>
public bool SetServerIP(ServerInfoConfig serverInfo, bool checkAlias) public bool SetServerIP(ServerInfoConfig serverInfo, bool checkAlias)
{ {
string serverStr = ToLowerIfNeed(serverInfo.Host); string[] sip = serverInfo.Host.Split(new[] { ":", "" }, StringSplitOptions.None);
string[] sip = serverStr.Split(new[] { ":", "" }, StringSplitOptions.None); string host = ToLowerIfNeed(sip[0]);
string host = sip[0];
ushort port = 25565; ushort port = 25565;
if (sip.Length > 1) if (serverInfo.Port.HasValue)
{ {
if (serverInfo.Port != null) port = serverInfo.Port.Value;
{
port = (ushort)serverInfo.Port;
} }
else else if (sip.Length > 1)
{ {
try { port = Convert.ToUInt16(sip[1]); } try { port = Convert.ToUInt16(sip[1]); }
catch (FormatException) { return false; } catch (FormatException) { return false; }
} }
}
if (host == "localhost" || host.Contains('.')) if (host == "localhost" || host.Contains('.'))
{ {
//Server IP (IP or domain names contains at least a dot) //Server IP (IP or domain names contains at least a dot)
if (sip.Length == 1 && serverInfo.Port == null && host.Contains('.') && host.Any(c => char.IsLetter(c)) && if (sip.Length == 1 && !serverInfo.Port.HasValue && host.Contains('.') && host.Any(c => char.IsLetter(c)) &&
Settings.Config.Main.Advanced.ResolveSrvRecords != MainConfigHealper.MainConfig.AdvancedConfig.ResolveSrvRecordType.no) Settings.Config.Main.Advanced.ResolveSrvRecords != ResolveSrvRecordType.no)
//Domain name without port may need Minecraft SRV Record lookup //Domain name without port may need Minecraft SRV Record lookup
ProtocolHandler.MinecraftServiceLookup(ref host, ref port); ProtocolHandler.MinecraftServiceLookup(ref host, ref port);
InternalConfig.ServerIP = host; InternalConfig.ServerIP = host;
InternalConfig.ServerPort = port; InternalConfig.ServerPort = port;
return true; return true;
} }
else if (checkAlias && Advanced.ServerList.TryGetValue(serverStr, out ServerInfoConfig serverStr2)) else if (checkAlias && Advanced.ServerList.TryGetValue(sip[0], out ServerInfoConfig serverStr2))
{ {
return SetServerIP(serverStr2, false); return SetServerIP(serverStr2, false);
} }
@ -428,7 +424,7 @@ namespace MinecraftClient
if (!string.IsNullOrWhiteSpace(General.Server.Host)) if (!string.IsNullOrWhiteSpace(General.Server.Host))
{ {
string[] sip = General.Server.Host.Split(new[] { ":", "" }, StringSplitOptions.None); string[] sip = General.Server.Host.Split(new[] { ":", "" }, StringSplitOptions.None);
General.Server.Host = sip[0]; General.Server.Host = ToLowerIfNeed(sip[0]);
InternalConfig.ServerIP = General.Server.Host; InternalConfig.ServerIP = General.Server.Host;
if (sip.Length > 1) if (sip.Length > 1)