Skip to content
This repository has been archived by the owner on Feb 10, 2022. It is now read-only.

Commit

Permalink
Make requests async
Browse files Browse the repository at this point in the history
  • Loading branch information
GamingGeek committed Jun 16, 2020
1 parent 2ab6d71 commit fcabec0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ buildscript {
}
apply plugin: 'net.minecraftforge.gradle.forge'

version = "1.5.2"
version = "1.6"
group = "dev.gaminggeek.mojangstatus" // http://maven.apache.org/guides/mini/guide-naming-conventions.html
archivesBaseName = "MojangStatus"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
@Mod(name = "Mojang Status", modid = MojangStatus.MODID, version = MojangStatus.VERSION)
public class MojangStatus {
public static final String MODID = "mojang_status";
public static final String VERSION = "1.5.2";
public static final String VERSION = "1.6";
public static StatusCheck check = new StatusCheck();
public static StatusConfig statusConfig;

Expand Down
32 changes: 22 additions & 10 deletions src/main/java/dev/gaminggeek/mojangstatus/StatusCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.*;
import org.jetbrains.annotations.NotNull;

import java.io.BufferedReader;
import java.io.IOException;
Expand Down Expand Up @@ -44,17 +43,30 @@ public JsonObject getStatus() throws Exception {
.url("https://status.mojang.com/check")
.build();

try (Response response = client.newCall(request).execute()) {
assert response.body() != null;
JsonElement status = new JsonParser()
.parse(response.body().string());
if (status.isJsonArray()) {
try {
return getServiceStatus(status.getAsJsonArray());
final JsonElement[] status = new JsonElement[1];
client.newCall(request).enqueue(new Callback() {
@Override public void onFailure(@NotNull Call call, @NotNull IOException e) {
e.printStackTrace();
}

@Override public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
try (ResponseBody responseBody = response.body()) {
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

assert responseBody != null;
status[0] = new JsonParser()
.parse(responseBody.string());
} catch (Exception e) {
e.printStackTrace();
}
}
});
if (status[0].isJsonArray()) {
try {
return getServiceStatus(status[0].getAsJsonArray());
} catch (Exception e) {
e.printStackTrace();
}
}
throw new Exception("Failed to retrieve status");
}
Expand Down

0 comments on commit fcabec0

Please sign in to comment.