Realms API: Handle HTTP request failure (#1921)

This commit is contained in:
ORelio 2022-01-31 20:56:37 +01:00
parent 0e2ebf484b
commit ed452cf632

View file

@ -660,41 +660,53 @@ namespace MinecraftClient.Protocol
/// <returns>List of ID of available Realms worlds</returns> /// <returns>List of ID of available Realms worlds</returns>
public static List<string> RealmsListWorlds(string username, string uuid, string accesstoken) public static List<string> RealmsListWorlds(string username, string uuid, string accesstoken)
{ {
string result = "";
string cookies = String.Format("sid=token:{0}:{1};user={2};version={3}", accesstoken, uuid, username, Program.MCHighestVersion);
DoHTTPSGet("pc.realms.minecraft.net", "/worlds", cookies, ref result);
Json.JSONData realmsWorlds = Json.ParseJson(result);
List<string> realmsWorldsResult = new List<string>(); // Store world ID List<string> realmsWorldsResult = new List<string>(); // Store world ID
if (realmsWorlds.Properties.ContainsKey("servers") try
&& realmsWorlds.Properties["servers"].Type == Json.JSONData.DataType.Array
&& realmsWorlds.Properties["servers"].DataArray.Count > 0)
{ {
List<string> availableWorlds = new List<string>(); // Store string to print string result = "";
int index = 0; string cookies = String.Format("sid=token:{0}:{1};user={2};version={3}", accesstoken, uuid, username, Program.MCHighestVersion);
foreach (Json.JSONData realmsServer in realmsWorlds.Properties["servers"].DataArray) DoHTTPSGet("pc.realms.minecraft.net", "/worlds", cookies, ref result);
Json.JSONData realmsWorlds = Json.ParseJson(result);
if (realmsWorlds.Properties.ContainsKey("servers")
&& realmsWorlds.Properties["servers"].Type == Json.JSONData.DataType.Array
&& realmsWorlds.Properties["servers"].DataArray.Count > 0)
{ {
if (realmsServer.Properties.ContainsKey("name") List<string> availableWorlds = new List<string>(); // Store string to print
&& realmsServer.Properties.ContainsKey("owner") int index = 0;
&& realmsServer.Properties.ContainsKey("id") foreach (Json.JSONData realmsServer in realmsWorlds.Properties["servers"].DataArray)
&& realmsServer.Properties.ContainsKey("expired"))
{ {
if (realmsServer.Properties["expired"].StringValue == "false") if (realmsServer.Properties.ContainsKey("name")
&& realmsServer.Properties.ContainsKey("owner")
&& realmsServer.Properties.ContainsKey("id")
&& realmsServer.Properties.ContainsKey("expired"))
{ {
availableWorlds.Add(String.Format("[{0}] {2} ({3}) - {1}", if (realmsServer.Properties["expired"].StringValue == "false")
index++, {
realmsServer.Properties["id"].StringValue, availableWorlds.Add(String.Format("[{0}] {2} ({3}) - {1}",
realmsServer.Properties["name"].StringValue, index++,
realmsServer.Properties["owner"].StringValue)); realmsServer.Properties["id"].StringValue,
realmsWorldsResult.Add(realmsServer.Properties["id"].StringValue); realmsServer.Properties["name"].StringValue,
realmsServer.Properties["owner"].StringValue));
realmsWorldsResult.Add(realmsServer.Properties["id"].StringValue);
}
} }
} }
if (availableWorlds.Count > 0)
{
Translations.WriteLine("mcc.realms_available");
foreach (var world in availableWorlds)
ConsoleIO.WriteLine(world);
Translations.WriteLine("mcc.realms_join");
}
} }
if (availableWorlds.Count > 0)
}
catch (Exception e)
{
ConsoleIO.WriteLineFormatted("§8" + e.GetType().ToString() + ": " + e.Message);
if (Settings.DebugMessages)
{ {
Translations.WriteLine("mcc.realms_available"); ConsoleIO.WriteLineFormatted("§8" + e.StackTrace);
foreach (var world in availableWorlds)
ConsoleIO.WriteLine(world);
Translations.WriteLine("mcc.realms_join");
} }
} }
return realmsWorldsResult; return realmsWorldsResult;
@ -710,23 +722,35 @@ namespace MinecraftClient.Protocol
/// <returns>Server address (host:port) or empty string if failure</returns> /// <returns>Server address (host:port) or empty string if failure</returns>
public static string GetRealmsWorldServerAddress(string worldId, string username, string uuid, string accesstoken) public static string GetRealmsWorldServerAddress(string worldId, string username, string uuid, string accesstoken)
{ {
string result = ""; try
string cookies = String.Format("sid=token:{0}:{1};user={2};version={3}", accesstoken, uuid, username, Program.MCHighestVersion);
int statusCode = DoHTTPSGet("pc.realms.minecraft.net", "/worlds/v1/" + worldId + "/join/pc", cookies, ref result);
if (statusCode == 200)
{ {
Json.JSONData serverAddress = Json.ParseJson(result); string result = "";
if (serverAddress.Properties.ContainsKey("address")) string cookies = String.Format("sid=token:{0}:{1};user={2};version={3}", accesstoken, uuid, username, Program.MCHighestVersion);
return serverAddress.Properties["address"].StringValue; int statusCode = DoHTTPSGet("pc.realms.minecraft.net", "/worlds/v1/" + worldId + "/join/pc", cookies, ref result);
if (statusCode == 200)
{
Json.JSONData serverAddress = Json.ParseJson(result);
if (serverAddress.Properties.ContainsKey("address"))
return serverAddress.Properties["address"].StringValue;
else
{
Translations.WriteLine("error.realms.ip_error");
return "";
}
}
else else
{ {
Translations.WriteLine("error.realms.ip_error"); Translations.WriteLine("error.realms.access_denied");
return ""; return "";
} }
} }
else catch (Exception e)
{ {
Translations.WriteLine("error.realms.access_denied"); ConsoleIO.WriteLineFormatted("§8" + e.GetType().ToString() + ": " + e.Message);
if (Settings.DebugMessages)
{
ConsoleIO.WriteLineFormatted("§8" + e.StackTrace);
}
return ""; return "";
} }
} }