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

Commit

Permalink
Fuck you gson
Browse files Browse the repository at this point in the history
  • Loading branch information
GamingGeek committed Jun 12, 2020
1 parent 95490f4 commit 2ab6d71
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 21 deletions.
3 changes: 2 additions & 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"
version = "1.5.2"
group = "dev.gaminggeek.mojangstatus" // http://maven.apache.org/guides/mini/guide-naming-conventions.html
archivesBaseName = "MojangStatus"

Expand All @@ -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 {
Expand Down
Binary file modified libs/modcore.jar
Binary file not shown.
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";
public static final String VERSION = "1.5.2";
public static StatusCheck check = new StatusCheck();
public static StatusConfig statusConfig;

Expand Down
57 changes: 38 additions & 19 deletions src/main/java/dev/gaminggeek/mojangstatus/StatusCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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<String> 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;
Expand All @@ -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();
}
Expand All @@ -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();
Expand Down Expand Up @@ -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<String> 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);
}
}
}
Expand All @@ -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<String, JsonElement> e: obj.entrySet()) {
size++;
}
return size;
}

public JsonObject getServicesConfig() {
final InputStream inputStream = StatusCheck.class.getResourceAsStream("/services.json");
final StringBuilder sb = new StringBuilder();
Expand All @@ -178,4 +188,13 @@ public JsonObject getServicesConfig() {
final String data = sb.toString();
return new JsonHolder(data).getObject();
}

public Set<String> getServicesSet() {
JsonArray services = servicesConfig.getAsJsonArray("services");
Set<String> keys = new HashSet<>();
for (JsonElement s: services) {
keys.add(s.getAsString());
}
return keys;
}
}

0 comments on commit 2ab6d71

Please sign in to comment.