diff --git a/src/main/java/net/frozenorb/apiv3/route/servers/POSTServerHeartbeat.java b/src/main/java/net/frozenorb/apiv3/route/servers/POSTServerHeartbeat.java index baa664f..afb492c 100644 --- a/src/main/java/net/frozenorb/apiv3/route/servers/POSTServerHeartbeat.java +++ b/src/main/java/net/frozenorb/apiv3/route/servers/POSTServerHeartbeat.java @@ -107,32 +107,36 @@ public final class POSTServerHeartbeat implements Handler { if (result.failed()) { callback.fail(result.cause()); } else { - Map users = result.result().result(0); - Map> grants = result.result().result(1); - Map> punishments = result.result().result(2); - Map response = new HashMap<>(); + try { + Map users = result.result().result(0); + Map> grants = result.result().result(1); + Map> punishments = result.result().result(2); + Map response = new HashMap<>(); - for (Map.Entry userEntry : users.entrySet()) { - UUID uuid = userEntry.getKey(); - User user = userEntry.getValue(); + for (Map.Entry userEntry : users.entrySet()) { + UUID uuid = userEntry.getKey(); + User user = userEntry.getValue(); - if (user == null) { - String username = playerNames.get(uuid); - user = new User(uuid, username); - user.insert(); - users.put(uuid, user); + if (user == null) { + String username = playerNames.get(uuid); + user = new User(uuid, username); + user.insert(); + users.put(uuid, user); + } + + // Only save if needed + if (user.seenOnServer(server)) { + user.save(); + } + + // TODO: Provide IPs for ip ban lookup + response.put(uuid.toString(), user.createLoginInfo(server, punishments.get(uuid), ImmutableList.of(), grants.get(uuid))); } - // Only save if needed - if (user.seenOnServer(server)) { - user.save(); - } - - // TODO: Provide IPs for ip ban lookup - response.put(uuid.toString(), user.createLoginInfo(server, punishments.get(uuid), ImmutableList.of(), grants.get(uuid))); + callback.complete(response); + } catch (Exception ex) { + callback.fail(ex); } - - callback.complete(response); } }); diff --git a/src/main/java/net/frozenorb/apiv3/util/MojangUtils.java b/src/main/java/net/frozenorb/apiv3/util/MojangUtils.java index 9b554f8..a2a2fcf 100644 --- a/src/main/java/net/frozenorb/apiv3/util/MojangUtils.java +++ b/src/main/java/net/frozenorb/apiv3/util/MojangUtils.java @@ -4,6 +4,7 @@ import com.mongodb.async.SingleResultCallback; import lombok.experimental.UtilityClass; import net.frozenorb.apiv3.APIv3; import org.bson.Document; +import org.bson.json.JsonParseException; import java.io.IOException; import java.util.UUID; @@ -14,13 +15,17 @@ public class MojangUtils { public static void getName(UUID id, SingleResultCallback callback) { APIv3.getHttpClient().get("sessionserver.mojang.com", "/session/minecraft/profile/" + id.toString().replace("-", ""), (response) -> { response.bodyHandler((body) -> { - Document resJson = Document.parse(body.toString()); - String name = resJson.getString("name"); + try { + Document resJson = Document.parse(body.toString()); + String name = resJson.getString("name"); - if (name == null) { - callback.onResult(null, new IOException("Hit Mojang API rate limit: " + resJson.toJson())); - } else { - callback.onResult(name, null); + if (name == null) { + callback.onResult(null, new IOException("Hit Mojang API rate limit: " + resJson.toJson())); + } else { + callback.onResult(name, null); + } + } catch (JsonParseException ex) { + callback.onResult(null, new RuntimeException(body.toString(), ex)); } });