Skip to content
This repository was archived by the owner on Feb 4, 2018. It is now read-only.

Commit baf17c0

Browse files
committed
💎 Updated Status Item to support custom services
1 parent c9f1968 commit baf17c0

3 files changed

Lines changed: 56 additions & 15 deletions

File tree

‎src/main/java/uk/co/hexeption/darkforge/gui/screen/GuiStatusSlot.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ public GuiStatusSlot(GuiStatus gui) {
4646
this.statusItems.add(new StatusItem("CDN", "textures.minecraft.net"));
4747
this.statusItems.add(new StatusItem("API", "api.mojang.com"));
4848

49+
/**
50+
* Test Code
51+
* this.statusItems.add(new StatusItem("Capes", "capesapi.com", "http://status.wizardhaxmc.com/api/v1/components/2", "Operational"));
52+
*/
53+
4954
updateList();
5055

5156
}

‎src/main/java/uk/co/hexeption/darkforge/gui/screen/StatusItem.java‎

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ public class StatusItem {
3232

3333
private String service;
3434

35+
private String customAPI;
36+
37+
private String statusName;
38+
3539
private int status;
3640

3741
private long ping;
@@ -44,24 +48,53 @@ public StatusItem(String name, String service) {
4448
this.ping = -1L;
4549
}
4650

47-
public void check() {
51+
public StatusItem(String name, String service, String customAPI, String statusName) {
4852

49-
new Thread(() -> {
53+
this.name = name;
54+
this.service = service;
55+
this.customAPI = customAPI;
56+
this.statusName = statusName;
57+
this.status = 3;
58+
this.ping = -1L;
59+
}
60+
61+
public void check() {
62+
if (customAPI == null) {
63+
new Thread(() -> {
64+
65+
try {
66+
long startTime = System.currentTimeMillis();
67+
this.status = 3;
68+
if (((String) URLUtils.getWebsiteContents(new URL("http://status.mojang.com/check?service=" + this.service)).get(0)).contains("green")) {
69+
this.status = 1;
70+
this.ping = (System.currentTimeMillis() - startTime);
71+
} else {
72+
this.status = 0;
73+
}
74+
} catch (Exception e) {
75+
this.status = 2;
76+
}
5077

51-
try {
52-
long startTime = System.currentTimeMillis();
53-
StatusItem.this.status = 3;
54-
if (((String) URLUtils.getWebsiteContents(new URL("http://status.mojang.com/check?service=" + StatusItem.this.service)).get(0)).contains("green")) {
55-
StatusItem.this.status = 1;
56-
StatusItem.this.ping = (System.currentTimeMillis() - startTime);
57-
} else {
58-
StatusItem.this.status = 0;
78+
}).start();
79+
} else {
80+
new Thread(() -> {
81+
82+
try {
83+
long startTime = System.currentTimeMillis();
84+
this.status = 3;
85+
if (((String) URLUtils.getWebsiteContents(new URL(this.customAPI)).get(0)).contains(this.statusName)) {
86+
this.status = 1;
87+
this.ping = (System.currentTimeMillis() - startTime);
88+
} else {
89+
this.status = 0;
90+
}
91+
} catch (Exception e) {
92+
this.status = 2;
5993
}
60-
} catch (Exception e) {
61-
StatusItem.this.status = 2;
62-
}
6394

64-
}).start();
95+
}).start();
96+
}
97+
6598
}
6699

67100
public String getStatus() {

‎src/main/java/uk/co/hexeption/darkforge/utils/URLUtils.java‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.BufferedReader;
2222
import java.io.InputStreamReader;
2323
import java.net.URL;
24+
import java.net.URLConnection;
2425
import java.util.ArrayList;
2526

2627
/**
@@ -31,7 +32,9 @@ public class URLUtils {
3132
public static ArrayList getWebsiteContents(URL url) throws Exception {
3233

3334
ArrayList fileContents = new ArrayList();
34-
BufferedReader fileReader = new BufferedReader(new InputStreamReader(url.openStream()));
35+
URLConnection openConnection = url.openConnection();
36+
openConnection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36");
37+
BufferedReader fileReader = new BufferedReader(new InputStreamReader(openConnection.getInputStream()));
3538

3639
String fileLine = "";
3740
while ((fileLine = fileReader.readLine()) != null) {

0 commit comments

Comments
 (0)