Skip to content

Commit

Permalink
Nearby endpoint restructure
Browse files Browse the repository at this point in the history
  • Loading branch information
jwkerr committed Jul 27, 2024
1 parent cb88610 commit 9cea969
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
40 changes: 25 additions & 15 deletions src/main/java/net/earthmc/emcapi/endpoint/NearbyEndpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,35 @@ public NearbyContext getObjectOrNull(JsonElement element) {
JsonObject jsonObject = JSONUtil.getJsonElementAsJsonObjectOrNull(element);
if (jsonObject == null) throw new BadRequestResponse("Your query contains a value that is not a JSON object");

String targetTypeString = JSONUtil.getJsonElementAsStringOrNull(jsonObject.get("target_type"));
String searchTypeString = JSONUtil.getJsonElementAsStringOrNull(jsonObject.get("search_type"));
if (targetTypeString == null || searchTypeString == null) throw new BadRequestResponse("You did not specify a target or search type");

NearbyType targetType;
NearbyType searchType;
try {
NearbyType targetType = NearbyType.valueOf(jsonObject.get("target_type").getAsString().toUpperCase());
NearbyType searchType = NearbyType.valueOf(jsonObject.get("search_type").getAsString().toUpperCase());
targetType = NearbyType.valueOf(targetTypeString);
searchType = NearbyType.valueOf(searchTypeString);
} catch (IllegalArgumentException e) {
throw new BadRequestResponse("Your target or search type is invalid");
}

JsonElement targetElement = jsonObject.get("target");
int radius = jsonObject.get("radius").getAsInt();
if (targetType.equals(NearbyType.COORDINATE)) {
JsonArray jsonArray = JSONUtil.getJsonElementAsJsonArrayOrNull(targetElement);
if (jsonArray == null) throw new BadRequestResponse("Your target is not a valid JSON array");
Integer radius = JSONUtil.getJsonElementAsIntegerOrNull(jsonObject.get("radius"));
if (radius == null) throw new BadRequestResponse("You did not specify a radius");

Pair<Integer, Integer> pair = new Pair<>(jsonArray.get(0).getAsInt(), jsonArray.get(1).getAsInt());
JsonElement targetElement = jsonObject.get("target");
if (targetType.equals(NearbyType.COORDINATE)) {
JsonArray jsonArray = JSONUtil.getJsonElementAsJsonArrayOrNull(targetElement);
if (jsonArray == null) throw new BadRequestResponse("Your target is not a valid JSON array");

return new NearbyContext(targetType, pair, searchType, radius);
} else if (targetType.equals(NearbyType.TOWN)) {
String target = JSONUtil.getJsonElementAsStringOrNull(targetElement);
return new NearbyContext(targetType, target, searchType, radius);
}
} catch (Exception e) {
throw new BadRequestResponse("Your query contains an invalid JSON object");
Pair<Integer, Integer> pair = new Pair<>(jsonArray.get(0).getAsInt(), jsonArray.get(1).getAsInt());

return new NearbyContext(targetType, pair, searchType, radius);
} else if (targetType.equals(NearbyType.TOWN)) {
String target = JSONUtil.getJsonElementAsStringOrNull(targetElement);
if (target == null) throw new BadRequestResponse("Your target is not a valid string");

return new NearbyContext(targetType, target, searchType, radius);
}

return null;
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/net/earthmc/emcapi/util/JSONUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public static JsonObject getJsonObjectFromString(String string) {
}

public static String getJsonElementAsStringOrNull(JsonElement element) {
if (element == null) return null;

if (!element.isJsonPrimitive()) return null;

JsonPrimitive primitive = element.getAsJsonPrimitive();
Expand All @@ -23,6 +25,8 @@ public static String getJsonElementAsStringOrNull(JsonElement element) {
}

public static Integer getJsonElementAsIntegerOrNull(JsonElement element) {
if (element == null) return null;

if (!element.isJsonPrimitive()) return null;

JsonPrimitive primitive = element.getAsJsonPrimitive();
Expand All @@ -32,11 +36,15 @@ public static Integer getJsonElementAsIntegerOrNull(JsonElement element) {
}

public static JsonArray getJsonElementAsJsonArrayOrNull(JsonElement element) {
if (element == null) return null;

if (!element.isJsonArray()) return null;
return element.getAsJsonArray();
}

public static JsonObject getJsonElementAsJsonObjectOrNull(JsonElement element) {
if (element == null) return null;

if (!element.isJsonObject()) return null;
return element.getAsJsonObject();
}
Expand Down

0 comments on commit 9cea969

Please sign in to comment.