aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/cowtipper/cowlection/util/ApiUtils.java
diff options
context:
space:
mode:
authorCow <cow@volloeko.de>2020-07-28 00:12:36 +0200
committerCow <cow@volloeko.de>2020-07-28 00:12:36 +0200
commitb393636cb3f7e05ef8b34804eeb06357f1b9cfbe (patch)
treed754561fd2e2f09ac66f41b2645ac5f351c1cace /src/main/java/de/cowtipper/cowlection/util/ApiUtils.java
parent023589c75ae72ddc5ff75fa7235bce4d102b2ad1 (diff)
downloadCowlection-b393636cb3f7e05ef8b34804eeb06357f1b9cfbe.tar.gz
Cowlection-b393636cb3f7e05ef8b34804eeb06357f1b9cfbe.tar.bz2
Cowlection-b393636cb3f7e05ef8b34804eeb06357f1b9cfbe.zip
Renamed package to match cowtipper.de
Diffstat (limited to 'src/main/java/de/cowtipper/cowlection/util/ApiUtils.java')
-rw-r--r--src/main/java/de/cowtipper/cowlection/util/ApiUtils.java139
1 files changed, 139 insertions, 0 deletions
diff --git a/src/main/java/de/cowtipper/cowlection/util/ApiUtils.java b/src/main/java/de/cowtipper/cowlection/util/ApiUtils.java
new file mode 100644
index 0000000..763084d
--- /dev/null
+++ b/src/main/java/de/cowtipper/cowlection/util/ApiUtils.java
@@ -0,0 +1,139 @@
+package de.cowtipper.cowlection.util;
+
+import com.google.gson.JsonArray;
+import com.google.gson.JsonParser;
+import com.google.gson.JsonSyntaxException;
+import com.mojang.util.UUIDTypeAdapter;
+import de.cowtipper.cowlection.Cowlection;
+import de.cowtipper.cowlection.command.exception.ThrowingConsumer;
+import de.cowtipper.cowlection.config.MooConfig;
+import de.cowtipper.cowlection.data.Friend;
+import de.cowtipper.cowlection.data.HyPlayerData;
+import de.cowtipper.cowlection.data.HySkyBlockStats;
+import de.cowtipper.cowlection.data.HyStalkingData;
+import org.apache.http.HttpStatus;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
+public class ApiUtils {
+ public static final String UUID_NOT_FOUND = "UUID-NOT-FOUND";
+ private static final String NAME_TO_UUID_URL = "https://api.mojang.com/users/profiles/minecraft/";
+ private static final String UUID_TO_NAME_URL = "https://api.mojang.com/user/profiles/%s/names";
+ private static final String ONLINE_STATUS_URL = "https://api.hypixel.net/status?key=%s&uuid=%s";
+ private static final String SKYBLOCK_STATS_URL = "https://api.hypixel.net/skyblock/profiles?key=%s&uuid=%s";
+ private static final String PLAYER_URL = "https://api.hypixel.net/player?key=%s&uuid=%s";
+ private static final ExecutorService pool = Executors.newCachedThreadPool();
+
+ private ApiUtils() {
+ }
+
+ public static void fetchFriendData(String name, ThrowingConsumer<Friend> action) {
+ pool.execute(() -> action.accept(getFriend(name)));
+ }
+
+ private static Friend getFriend(String name) {
+ try (BufferedReader reader = makeApiCall(NAME_TO_UUID_URL + name)) {
+ if (reader == null) {
+ return Friend.FRIEND_NOT_FOUND;
+ } else {
+ return GsonUtils.fromJson(reader, Friend.class);
+ }
+ } catch (IOException | JsonSyntaxException e) {
+ e.printStackTrace();
+ }
+ return null;
+ }
+
+ public static void fetchCurrentName(Friend friend, ThrowingConsumer<String> action) {
+ pool.execute(() -> action.accept(getCurrentName(friend)));
+ }
+
+ private static String getCurrentName(Friend friend) {
+ try (BufferedReader reader = makeApiCall(String.format(UUID_TO_NAME_URL, UUIDTypeAdapter.fromUUID(friend.getUuid())))) {
+ if (reader == null) {
+ return UUID_NOT_FOUND;
+ } else {
+ JsonArray nameHistoryData = new JsonParser().parse(reader).getAsJsonArray();
+ if (nameHistoryData.size() > 0) {
+ return nameHistoryData.get(nameHistoryData.size() - 1).getAsJsonObject().get("name").getAsString();
+ }
+ }
+ } catch (IOException | JsonSyntaxException e) {
+ e.printStackTrace();
+ }
+ return null;
+ }
+
+ public static void fetchPlayerStatus(Friend friend, ThrowingConsumer<HyStalkingData> action) {
+ pool.execute(() -> action.accept(stalkPlayer(friend)));
+ }
+
+ private static HyStalkingData stalkPlayer(Friend friend) {
+ try (BufferedReader reader = makeApiCall(String.format(ONLINE_STATUS_URL, MooConfig.moo, UUIDTypeAdapter.fromUUID(friend.getUuid())))) {
+ if (reader != null) {
+ return GsonUtils.fromJson(reader, HyStalkingData.class);
+ }
+ } catch (IOException | JsonSyntaxException e) {
+ e.printStackTrace();
+ }
+ return null;
+ }
+
+ public static void fetchSkyBlockStats(Friend friend, ThrowingConsumer<HySkyBlockStats> action) {
+ pool.execute(() -> action.accept(stalkSkyBlockStats(friend)));
+ }
+
+ private static HySkyBlockStats stalkSkyBlockStats(Friend friend) {
+ try (BufferedReader reader = makeApiCall(String.format(SKYBLOCK_STATS_URL, MooConfig.moo, UUIDTypeAdapter.fromUUID(friend.getUuid())))) {
+ if (reader != null) {
+ return GsonUtils.fromJson(reader, HySkyBlockStats.class);
+ }
+ } catch (IOException | JsonSyntaxException e) {
+ e.printStackTrace();
+ }
+ return null;
+ }
+
+ public static void fetchPlayerOfflineStatus(Friend stalkedPlayer, ThrowingConsumer<HyPlayerData> action) {
+ pool.execute(() -> action.accept(stalkOfflinePlayer(stalkedPlayer)));
+ }
+
+ private static HyPlayerData stalkOfflinePlayer(Friend stalkedPlayer) {
+ try (BufferedReader reader = makeApiCall(String.format(PLAYER_URL, MooConfig.moo, UUIDTypeAdapter.fromUUID(stalkedPlayer.getUuid())))) {
+ if (reader != null) {
+ return GsonUtils.fromJson(reader, HyPlayerData.class);
+ }
+ } catch (IOException | JsonSyntaxException e) {
+ e.printStackTrace();
+ }
+ return null;
+ }
+
+ private static BufferedReader makeApiCall(String url) throws IOException {
+ HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
+ connection.setConnectTimeout(5000);
+ connection.setReadTimeout(10000);
+ connection.addRequestProperty("User-Agent", "Forge Mod " + Cowlection.MODNAME + "/" + Cowlection.VERSION + " (" + Cowlection.GITURL + ")");
+
+ connection.getResponseCode();
+ if (connection.getResponseCode() == HttpStatus.SC_NO_CONTENT) { // http status 204
+ return null;
+ } else {
+ BufferedReader reader;
+ InputStream errorStream = connection.getErrorStream();
+ if (errorStream != null) {
+ reader = new BufferedReader(new InputStreamReader(errorStream));
+ } else {
+ reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
+ }
+ return reader;
+ }
+ }
+}