diff options
author | Cow <cow@volloeko.de> | 2020-07-26 23:41:58 +0200 |
---|---|---|
committer | Cow <cow@volloeko.de> | 2020-07-26 23:41:58 +0200 |
commit | 0027466a564a9a6bebbdeed05192a616175ea6f3 (patch) | |
tree | c6d8b5f61992bd1e418d83b1301cc087f673a2e1 /src/main/java/eu/olli/cowlection/util | |
parent | b9c5f23a671c50422303bf50e315d364b1354acf (diff) | |
download | Cowlection-0027466a564a9a6bebbdeed05192a616175ea6f3.tar.gz Cowlection-0027466a564a9a6bebbdeed05192a616175ea6f3.tar.bz2 Cowlection-0027466a564a9a6bebbdeed05192a616175ea6f3.zip |
Replaced 3rd party with official API
Diffstat (limited to 'src/main/java/eu/olli/cowlection/util')
-rw-r--r-- | src/main/java/eu/olli/cowlection/util/ApiUtils.java | 20 | ||||
-rw-r--r-- | src/main/java/eu/olli/cowlection/util/GsonUtils.java | 20 |
2 files changed, 29 insertions, 11 deletions
diff --git a/src/main/java/eu/olli/cowlection/util/ApiUtils.java b/src/main/java/eu/olli/cowlection/util/ApiUtils.java index b0a4cce..f4f23ff 100644 --- a/src/main/java/eu/olli/cowlection/util/ApiUtils.java +++ b/src/main/java/eu/olli/cowlection/util/ApiUtils.java @@ -9,7 +9,7 @@ import eu.olli.cowlection.config.MooConfig; import eu.olli.cowlection.data.Friend; import eu.olli.cowlection.data.HySkyBlockStats; import eu.olli.cowlection.data.HyStalkingData; -import eu.olli.cowlection.data.SlothStalkingData; +import eu.olli.cowlection.data.HyPlayerData; import org.apache.http.HttpStatus; import java.io.BufferedReader; @@ -25,9 +25,9 @@ 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 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() { @@ -75,7 +75,7 @@ public class ApiUtils { } private static HyStalkingData stalkPlayer(Friend friend) { - try (BufferedReader reader = makeApiCall(String.format(STALKING_URL_OFFICIAL, MooConfig.moo, UUIDTypeAdapter.fromUUID(friend.getUuid())))) { + try (BufferedReader reader = makeApiCall(String.format(ONLINE_STATUS_URL, MooConfig.moo, UUIDTypeAdapter.fromUUID(friend.getUuid())))) { if (reader != null) { return GsonUtils.fromJson(reader, HyStalkingData.class); } @@ -90,7 +90,7 @@ public class ApiUtils { } private static HySkyBlockStats stalkSkyBlockStats(Friend friend) { - try (BufferedReader reader = makeApiCall(String.format(SKYBLOCK_STATS_URL_OFFICIAL, MooConfig.moo, UUIDTypeAdapter.fromUUID(friend.getUuid())))) { + try (BufferedReader reader = makeApiCall(String.format(SKYBLOCK_STATS_URL, MooConfig.moo, UUIDTypeAdapter.fromUUID(friend.getUuid())))) { if (reader != null) { return GsonUtils.fromJson(reader, HySkyBlockStats.class); } @@ -100,14 +100,14 @@ public class ApiUtils { return null; } - public static void fetchPlayerOfflineStatus(Friend stalkedPlayer, ThrowingConsumer<SlothStalkingData> action) { + public static void fetchPlayerOfflineStatus(Friend stalkedPlayer, ThrowingConsumer<HyPlayerData> action) { pool.execute(() -> action.accept(stalkOfflinePlayer(stalkedPlayer))); } - private static SlothStalkingData stalkOfflinePlayer(Friend stalkedPlayer) { - try (BufferedReader reader = makeApiCall(String.format(STALKING_URL_UNOFFICIAL, UUIDTypeAdapter.fromUUID(stalkedPlayer.getUuid())))) { + 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, SlothStalkingData.class); + return GsonUtils.fromJson(reader, HyPlayerData.class); } } catch (IOException e) { e.printStackTrace(); diff --git a/src/main/java/eu/olli/cowlection/util/GsonUtils.java b/src/main/java/eu/olli/cowlection/util/GsonUtils.java index e0f1a64..030f42e 100644 --- a/src/main/java/eu/olli/cowlection/util/GsonUtils.java +++ b/src/main/java/eu/olli/cowlection/util/GsonUtils.java @@ -2,6 +2,7 @@ package eu.olli.cowlection.util; import com.google.gson.*; import com.mojang.util.UUIDTypeAdapter; +import eu.olli.cowlection.data.HyPlayerData; import net.minecraft.nbt.NBTBase; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; @@ -13,7 +14,7 @@ import java.lang.reflect.Type; import java.util.UUID; public final class GsonUtils { - private static final Gson gson = new GsonBuilder().registerTypeAdapter(UUID.class, new UUIDTypeAdapter()).create(); + private static final Gson gson = new GsonBuilder().registerTypeAdapter(UUID.class, new UUIDTypeAdapter()).registerTypeAdapter(HyPlayerData.class, new HyPlayerDataDeserializer()).create(); private static final Gson gsonPrettyPrinter = new GsonBuilder().registerTypeAdapter(UUID.class, new UUIDTypeAdapter()).setPrettyPrinting().create(); private GsonUtils() { @@ -73,4 +74,21 @@ public final class GsonUtils { } return new JsonObject(); } + + public static class HyPlayerDataDeserializer implements JsonDeserializer<HyPlayerData> { + @Override + public HyPlayerData deserialize(JsonElement json, Type type, JsonDeserializationContext jdc) throws JsonParseException { + if (!json.getAsJsonObject().get("success").getAsBoolean()) { + // status: failed + return null; + } + JsonElement player = json.getAsJsonObject().get("player"); + HyPlayerData hyPlayerData = gsonPrettyPrinter.fromJson(player, HyPlayerData.class); + if (hyPlayerData == null) { + // player hasn't played Hypixel before + return new HyPlayerData(); + } + return hyPlayerData; + } + } } |