aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/eu/olli/cowmoonication/util/ApiUtils.java
diff options
context:
space:
mode:
authorCow <cow@volloeko.de>2020-05-29 12:30:31 +0200
committerCow <cow@volloeko.de>2020-05-29 12:30:31 +0200
commitb9d6b75423ea24c4947b3a655f199c3b34aa167a (patch)
tree7df92efa2a2c4533326e9f37e9062db08cc13e51 /src/main/java/eu/olli/cowmoonication/util/ApiUtils.java
parent4d45e0bc9afacc8408295aef50b8fd6530f97104 (diff)
downloadCowlection-b9d6b75423ea24c4947b3a655f199c3b34aa167a.tar.gz
Cowlection-b9d6b75423ea24c4947b3a655f199c3b34aa167a.tar.bz2
Cowlection-b9d6b75423ea24c4947b3a655f199c3b34aa167a.zip
Added /moo stalkskyblock <playerName>
- Now using CommandExceptions instead of just red chat color for command error messages - Simplified creation of chat message
Diffstat (limited to 'src/main/java/eu/olli/cowmoonication/util/ApiUtils.java')
-rw-r--r--src/main/java/eu/olli/cowmoonication/util/ApiUtils.java27
1 files changed, 22 insertions, 5 deletions
diff --git a/src/main/java/eu/olli/cowmoonication/util/ApiUtils.java b/src/main/java/eu/olli/cowmoonication/util/ApiUtils.java
index aca1819..8515593 100644
--- a/src/main/java/eu/olli/cowmoonication/util/ApiUtils.java
+++ b/src/main/java/eu/olli/cowmoonication/util/ApiUtils.java
@@ -4,8 +4,10 @@ import com.google.gson.JsonArray;
import com.google.gson.JsonParser;
import com.mojang.util.UUIDTypeAdapter;
import eu.olli.cowmoonication.Cowmoonication;
+import eu.olli.cowmoonication.command.exception.ThrowingConsumer;
import eu.olli.cowmoonication.config.MooConfig;
import eu.olli.cowmoonication.data.Friend;
+import eu.olli.cowmoonication.data.HySkyBlockStats;
import eu.olli.cowmoonication.data.HyStalkingData;
import eu.olli.cowmoonication.data.SlothStalkingData;
import org.apache.http.HttpStatus;
@@ -18,20 +20,20 @@ import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
-import java.util.function.Consumer;
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 STALKING_URL_OFFICIAL = "https://api.hypixel.net/status?key=%s&uuid=%s";
+ private static final String SKYBLOCK_STATS_URL_OFFICIAL = "https://api.hypixel.net/skyblock/profiles?key=%s&uuid=%s";
private static final String STALKING_URL_UNOFFICIAL = "https://api.slothpixel.me/api/players/%s";
private static ExecutorService pool = Executors.newCachedThreadPool();
private ApiUtils() {
}
- public static void fetchFriendData(String name, Consumer<Friend> action) {
+ public static void fetchFriendData(String name, ThrowingConsumer<Friend> action) {
pool.execute(() -> action.accept(getFriend(name)));
}
@@ -48,7 +50,7 @@ public class ApiUtils {
return null;
}
- public static void fetchCurrentName(Friend friend, Consumer<String> action) {
+ public static void fetchCurrentName(Friend friend, ThrowingConsumer<String> action) {
pool.execute(() -> action.accept(getCurrentName(friend)));
}
@@ -68,7 +70,7 @@ public class ApiUtils {
return null;
}
- public static void fetchPlayerStatus(Friend friend, Consumer<HyStalkingData> action) {
+ public static void fetchPlayerStatus(Friend friend, ThrowingConsumer<HyStalkingData> action) {
pool.execute(() -> action.accept(stalkPlayer(friend)));
}
@@ -83,7 +85,22 @@ public class ApiUtils {
return null;
}
- public static void fetchPlayerOfflineStatus(Friend stalkedPlayer, Consumer<SlothStalkingData> action) {
+ 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_OFFICIAL, MooConfig.moo, UUIDTypeAdapter.fromUUID(friend.getUuid())))) {
+ if (reader != null) {
+ return GsonUtils.fromJson(reader, HySkyBlockStats.class);
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ return null;
+ }
+
+ public static void fetchPlayerOfflineStatus(Friend stalkedPlayer, ThrowingConsumer<SlothStalkingData> action) {
pool.execute(() -> action.accept(stalkOfflinePlayer(stalkedPlayer)));
}