Skip to content

Commit ed452cf

Browse files
committed
Realms API: Handle HTTP request failure (#1921)
1 parent 0e2ebf4 commit ed452cf

File tree

1 file changed

+60
-36
lines changed

1 file changed

+60
-36
lines changed

MinecraftClient/Protocol/ProtocolHandler.cs

Lines changed: 60 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -660,41 +660,53 @@ public static bool SessionCheck(string uuid, string accesstoken, string serverha
660660
/// <returns>List of ID of available Realms worlds</returns>
661661
public static List<string> RealmsListWorlds(string username, string uuid, string accesstoken)
662662
{
663-
string result = "";
664-
string cookies = String.Format("sid=token:{0}:{1};user={2};version={3}", accesstoken, uuid, username, Program.MCHighestVersion);
665-
DoHTTPSGet("pc.realms.minecraft.net", "/worlds", cookies, ref result);
666-
Json.JSONData realmsWorlds = Json.ParseJson(result);
667663
List<string> realmsWorldsResult = new List<string>(); // Store world ID
668-
if (realmsWorlds.Properties.ContainsKey("servers")
669-
&& realmsWorlds.Properties["servers"].Type == Json.JSONData.DataType.Array
670-
&& realmsWorlds.Properties["servers"].DataArray.Count > 0)
664+
try
671665
{
672-
List<string> availableWorlds = new List<string>(); // Store string to print
673-
int index = 0;
674-
foreach (Json.JSONData realmsServer in realmsWorlds.Properties["servers"].DataArray)
666+
string result = "";
667+
string cookies = String.Format("sid=token:{0}:{1};user={2};version={3}", accesstoken, uuid, username, Program.MCHighestVersion);
668+
DoHTTPSGet("pc.realms.minecraft.net", "/worlds", cookies, ref result);
669+
Json.JSONData realmsWorlds = Json.ParseJson(result);
670+
if (realmsWorlds.Properties.ContainsKey("servers")
671+
&& realmsWorlds.Properties["servers"].Type == Json.JSONData.DataType.Array
672+
&& realmsWorlds.Properties["servers"].DataArray.Count > 0)
675673
{
676-
if (realmsServer.Properties.ContainsKey("name")
677-
&& realmsServer.Properties.ContainsKey("owner")
678-
&& realmsServer.Properties.ContainsKey("id")
679-
&& realmsServer.Properties.ContainsKey("expired"))
674+
List<string> availableWorlds = new List<string>(); // Store string to print
675+
int index = 0;
676+
foreach (Json.JSONData realmsServer in realmsWorlds.Properties["servers"].DataArray)
680677
{
681-
if (realmsServer.Properties["expired"].StringValue == "false")
678+
if (realmsServer.Properties.ContainsKey("name")
679+
&& realmsServer.Properties.ContainsKey("owner")
680+
&& realmsServer.Properties.ContainsKey("id")
681+
&& realmsServer.Properties.ContainsKey("expired"))
682682
{
683-
availableWorlds.Add(String.Format("[{0}] {2} ({3}) - {1}",
684-
index++,
685-
realmsServer.Properties["id"].StringValue,
686-
realmsServer.Properties["name"].StringValue,
687-
realmsServer.Properties["owner"].StringValue));
688-
realmsWorldsResult.Add(realmsServer.Properties["id"].StringValue);
683+
if (realmsServer.Properties["expired"].StringValue == "false")
684+
{
685+
availableWorlds.Add(String.Format("[{0}] {2} ({3}) - {1}",
686+
index++,
687+
realmsServer.Properties["id"].StringValue,
688+
realmsServer.Properties["name"].StringValue,
689+
realmsServer.Properties["owner"].StringValue));
690+
realmsWorldsResult.Add(realmsServer.Properties["id"].StringValue);
691+
}
689692
}
690693
}
694+
if (availableWorlds.Count > 0)
695+
{
696+
Translations.WriteLine("mcc.realms_available");
697+
foreach (var world in availableWorlds)
698+
ConsoleIO.WriteLine(world);
699+
Translations.WriteLine("mcc.realms_join");
700+
}
691701
}
692-
if (availableWorlds.Count > 0)
702+
703+
}
704+
catch (Exception e)
705+
{
706+
ConsoleIO.WriteLineFormatted("§8" + e.GetType().ToString() + ": " + e.Message);
707+
if (Settings.DebugMessages)
693708
{
694-
Translations.WriteLine("mcc.realms_available");
695-
foreach (var world in availableWorlds)
696-
ConsoleIO.WriteLine(world);
697-
Translations.WriteLine("mcc.realms_join");
709+
ConsoleIO.WriteLineFormatted("§8" + e.StackTrace);
698710
}
699711
}
700712
return realmsWorldsResult;
@@ -710,23 +722,35 @@ public static List<string> RealmsListWorlds(string username, string uuid, string
710722
/// <returns>Server address (host:port) or empty string if failure</returns>
711723
public static string GetRealmsWorldServerAddress(string worldId, string username, string uuid, string accesstoken)
712724
{
713-
string result = "";
714-
string cookies = String.Format("sid=token:{0}:{1};user={2};version={3}", accesstoken, uuid, username, Program.MCHighestVersion);
715-
int statusCode = DoHTTPSGet("pc.realms.minecraft.net", "/worlds/v1/" + worldId + "/join/pc", cookies, ref result);
716-
if (statusCode == 200)
725+
try
717726
{
718-
Json.JSONData serverAddress = Json.ParseJson(result);
719-
if (serverAddress.Properties.ContainsKey("address"))
720-
return serverAddress.Properties["address"].StringValue;
727+
string result = "";
728+
string cookies = String.Format("sid=token:{0}:{1};user={2};version={3}", accesstoken, uuid, username, Program.MCHighestVersion);
729+
int statusCode = DoHTTPSGet("pc.realms.minecraft.net", "/worlds/v1/" + worldId + "/join/pc", cookies, ref result);
730+
if (statusCode == 200)
731+
{
732+
Json.JSONData serverAddress = Json.ParseJson(result);
733+
if (serverAddress.Properties.ContainsKey("address"))
734+
return serverAddress.Properties["address"].StringValue;
735+
else
736+
{
737+
Translations.WriteLine("error.realms.ip_error");
738+
return "";
739+
}
740+
}
721741
else
722742
{
723-
Translations.WriteLine("error.realms.ip_error");
743+
Translations.WriteLine("error.realms.access_denied");
724744
return "";
725745
}
726746
}
727-
else
747+
catch (Exception e)
728748
{
729-
Translations.WriteLine("error.realms.access_denied");
749+
ConsoleIO.WriteLineFormatted("§8" + e.GetType().ToString() + ": " + e.Message);
750+
if (Settings.DebugMessages)
751+
{
752+
ConsoleIO.WriteLineFormatted("§8" + e.StackTrace);
753+
}
730754
return "";
731755
}
732756
}

0 commit comments

Comments
 (0)