diff --git a/build.gradle b/build.gradle index ea5c756..d8d9869 100644 --- a/build.gradle +++ b/build.gradle @@ -14,7 +14,7 @@ buildscript { } apply plugin: 'net.minecraftforge.gradle.forge' -version = "1.5" +version = "1.5.2" group = "dev.gaminggeek.mojangstatus" // http://maven.apache.org/guides/mini/guide-naming-conventions.html archivesBaseName = "MojangStatus" @@ -40,6 +40,7 @@ repositories { dependencies { provided files("libs/modcore.jar") implementation "com.squareup.okhttp3:okhttp:4.5.0" + implementation "com.google.code.gson:gson:2.2.4" } processResources { diff --git a/libs/modcore.jar b/libs/modcore.jar index ec36e6b..7592ff2 100644 Binary files a/libs/modcore.jar and b/libs/modcore.jar differ diff --git a/src/main/java/dev/gaminggeek/mojangstatus/MojangStatus.java b/src/main/java/dev/gaminggeek/mojangstatus/MojangStatus.java index 18f71f4..6c59b07 100644 --- a/src/main/java/dev/gaminggeek/mojangstatus/MojangStatus.java +++ b/src/main/java/dev/gaminggeek/mojangstatus/MojangStatus.java @@ -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"; + public static final String VERSION = "1.5.2"; public static StatusCheck check = new StatusCheck(); public static StatusConfig statusConfig; diff --git a/src/main/java/dev/gaminggeek/mojangstatus/StatusCheck.java b/src/main/java/dev/gaminggeek/mojangstatus/StatusCheck.java index 49b32e8..342c4b7 100644 --- a/src/main/java/dev/gaminggeek/mojangstatus/StatusCheck.java +++ b/src/main/java/dev/gaminggeek/mojangstatus/StatusCheck.java @@ -14,22 +14,26 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Map; import java.util.Set; public class StatusCheck { public OkHttpClient client = new OkHttpClient(); public static final Notifications notifications = Notifications.INSTANCE; public JsonObject servicesConfig = getServicesConfig(); - public JsonArray services = servicesConfig.getAsJsonArray("services"); + public Set services = getServicesSet(); public JsonObject names = servicesConfig.getAsJsonObject("names"); - public JsonObject arrayToObject(JsonArray arr) { + public JsonObject getServiceStatus(JsonArray arr) { JsonObject obj = new JsonObject(); for (JsonElement element: arr.getAsJsonArray()) { JsonObject e = element.getAsJsonObject(); - Set key = e.keySet(); - for (String k: key) { - obj.addProperty(k, e.get(k).getAsString()); + for (String service: services) { + if (e.has(service)) { + obj.addProperty(service, e.get(service).getAsString()); + } } } return obj; @@ -44,10 +48,9 @@ public JsonObject getStatus() throws Exception { assert response.body() != null; JsonElement status = new JsonParser() .parse(response.body().string()); - JsonObject statusObj = new JsonObject(); if (status.isJsonArray()) { try { - return arrayToObject(status.getAsJsonArray()); + return getServiceStatus(status.getAsJsonArray()); } catch (Exception e) { e.printStackTrace(); } @@ -57,13 +60,14 @@ public JsonObject getStatus() throws Exception { } public void checkStatus(JsonObject lastStatus, Boolean notify) { - if (lastStatus.keySet().size() == 0) lastStatus = fakeStatus(); + if (getSize(lastStatus) == 0) lastStatus = fakeStatus(); StatusConfig config = MojangStatus.statusConfig; - JsonObject latestStatus = new JsonObject(); + JsonObject latestStatus; try { latestStatus = getStatus(); } catch (Exception e) { e.printStackTrace(); + return; } if (notify) { if (config.debug) lastStatus = fakeStatus(); @@ -131,21 +135,19 @@ public void notify(String service, String status) { public JsonArray getChanged(JsonObject before, JsonObject after) { JsonArray changed = new JsonArray(); - if (before.size() == 0 || after.size() == 0) return changed; + if (getSize(before) == 0 || getSize(after) == 0) return changed; StatusConfig config = MojangStatus.statusConfig; - JsonObject placebo = new JsonObject(); - Set keys = before.keySet(); - for (String k: keys) { + for (String k: services) { if (!(before.get(k).equals(after.get(k)))) { + JsonObject placebo = new JsonObject(); placebo.addProperty(k, after.get(k).getAsString()); - changed.add(placebo.deepCopy()); - placebo.remove(k); + changed.add(placebo); } else if (config.noChanges) { String status = after.get(k).getAsString(); if (status.equals("yellow") || status.equals("red")) { + JsonObject placebo = new JsonObject(); placebo.addProperty(k, after.get(k).getAsString()); - changed.add(placebo.deepCopy()); - placebo.remove(k); + changed.add(placebo); } } } @@ -155,12 +157,20 @@ public JsonArray getChanged(JsonObject before, JsonObject after) { public JsonObject fakeStatus() { StatusConfig config = MojangStatus.statusConfig; JsonObject statuses = new JsonObject(); - for (JsonElement service : services) { - statuses.addProperty(service.getAsString(), config.debug ? "grey" : "green"); + for (String service : services) { + statuses.addProperty(service, config.debug ? "grey" : "green"); } return statuses; } + public Integer getSize(JsonObject obj) { + Integer size = 0; + for (Map.Entry e: obj.entrySet()) { + size++; + } + return size; + } + public JsonObject getServicesConfig() { final InputStream inputStream = StatusCheck.class.getResourceAsStream("/services.json"); final StringBuilder sb = new StringBuilder(); @@ -178,4 +188,13 @@ public JsonObject getServicesConfig() { final String data = sb.toString(); return new JsonHolder(data).getObject(); } + + public Set getServicesSet() { + JsonArray services = servicesConfig.getAsJsonArray("services"); + Set keys = new HashSet<>(); + for (JsonElement s: services) { + keys.add(s.getAsString()); + } + return keys; + } } \ No newline at end of file