diff options
author | syeyoung <cyoung06@naver.com> | 2023-09-17 22:34:33 +0900 |
---|---|---|
committer | syeyoung <cyoung06@naver.com> | 2023-09-17 22:34:33 +0900 |
commit | 32982d788065c36c25fccabcffe095492c6e8ba9 (patch) | |
tree | e75fb84da05565b9616490bd390b93df82d7fabf | |
parent | e58907019e2eea057586841d625fe018ce42cd02 (diff) | |
download | Skyblock-Dungeons-Guide-32982d788065c36c25fccabcffe095492c6e8ba9.tar.gz Skyblock-Dungeons-Guide-32982d788065c36c25fccabcffe095492c6e8ba9.tar.bz2 Skyblock-Dungeons-Guide-32982d788065c36c25fccabcffe095492c6e8ba9.zip |
- use dg backend api for fetching profiles
Signed-off-by: syeyoung <cyoung06@naver.com>
5 files changed, 28 insertions, 150 deletions
diff --git a/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/features/FeatureRegistry.java b/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/features/FeatureRegistry.java index 4363e963..21267a61 100644 --- a/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/features/FeatureRegistry.java +++ b/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/features/FeatureRegistry.java @@ -35,7 +35,6 @@ import kr.syeyoung.dungeonsguide.mod.features.impl.discord.onlinealarm.PlayingDG import kr.syeyoung.dungeonsguide.mod.features.impl.dungeon.*; import kr.syeyoung.dungeonsguide.mod.features.impl.etc.*; import kr.syeyoung.dungeonsguide.mod.features.impl.etc.ability.FeatureAbilityCooldown; -import kr.syeyoung.dungeonsguide.mod.features.impl.party.APIKey; import kr.syeyoung.dungeonsguide.mod.features.impl.party.FeaturePartyList; import kr.syeyoung.dungeonsguide.mod.features.impl.party.FeaturePartyReady; import kr.syeyoung.dungeonsguide.mod.features.impl.party.customgui.FeatureCustomPartyFinder; @@ -200,7 +199,6 @@ public class FeatureRegistry { public static final SimpleFeature DEBUG_BLOCK_CACHING = register(new SimpleFeature("Debug","Enable block getBlockCaching", "Cache all world.getBlockState callls", "debug.blockcache")); // Dungeon Party - public static final APIKey PARTYKICKER_APIKEY = register(new APIKey()); // Decide where to go public static final FeatureAutoReparty BOSSFIGHT_AUTOREPARTY = register(new FeatureAutoReparty()); public static final FeatureViewPlayerStatsOnJoin PARTYKICKER_VIEWPLAYER = register(new FeatureViewPlayerStatsOnJoin()); public static final FeatureCustomPartyFinder PARTYKICKER_CUSTOM = register(new FeatureCustomPartyFinder()); diff --git a/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/features/impl/party/APIKey.java b/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/features/impl/party/APIKey.java deleted file mode 100644 index 221b1d18..00000000 --- a/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/features/impl/party/APIKey.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Dungeons Guide - The most intelligent Hypixel Skyblock Dungeons Mod - * Copyright (C) 2021 cyoung06 - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <https://www.gnu.org/licenses/>. - */ - -package kr.syeyoung.dungeonsguide.mod.features.impl.party; - - -import kr.syeyoung.dungeonsguide.mod.chat.ChatTransmitter; -import kr.syeyoung.dungeonsguide.mod.config.types.TCString; -import kr.syeyoung.dungeonsguide.mod.events.annotations.DGEventHandler; -import kr.syeyoung.dungeonsguide.mod.features.FeatureParameter; -import kr.syeyoung.dungeonsguide.mod.features.SimpleFeature; -import kr.syeyoung.dungeonsguide.mod.utils.TextUtils; -import net.minecraft.util.ChatComponentText; -import net.minecraftforge.client.event.ClientChatReceivedEvent; - -public class APIKey extends SimpleFeature { - - public APIKey() { - super("Dungeon Party", "API KEY", "Sets api key","partykicker.apikey"); - addParameter("apikey", new FeatureParameter<String>("apikey", "API Key", "API key", "", TCString.INSTANCE)); - } - - public String getAPIKey() { - return this.<String>getParameter("apikey").getValue(); - } - - - @DGEventHandler(triggerOutOfSkyblock = true) - public void onChat(ClientChatReceivedEvent clientChatReceivedEvent) { - if (clientChatReceivedEvent.type == 2) return; - String str = clientChatReceivedEvent.message.getFormattedText(); - if (str.startsWith("§aYour new API key is §r§b")) { - String apiKeys = TextUtils.stripColor(str.split(" ")[5]); - ChatTransmitter.addToQueue(new ChatComponentText("§eDungeons Guide §7:: §fAutomatically Configured Hypixel API Key")); - this.<String>getParameter("apikey").setValue(apiKeys); - } - } - - @Override - public boolean isDisableable() { - return false; - } -} diff --git a/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/features/impl/party/playerpreview/FeatureViewPlayerStatsOnJoin.java b/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/features/impl/party/playerpreview/FeatureViewPlayerStatsOnJoin.java index 188b6175..230224f9 100644 --- a/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/features/impl/party/playerpreview/FeatureViewPlayerStatsOnJoin.java +++ b/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/features/impl/party/playerpreview/FeatureViewPlayerStatsOnJoin.java @@ -110,7 +110,7 @@ public class FeatureViewPlayerStatsOnJoin extends SimpleFeature { } - ApiFetcher.fetchMostRecentProfileAsync(a.get(), FeatureRegistry.PARTYKICKER_APIKEY.getAPIKey()); + ApiFetcher.fetchMostRecentProfileAsync(a.get()); IChatComponent comp = new ChatComponentText("§eDungeons Guide §7:: §e" + username + "§f's Profile ") .appendSibling(new ChatComponentText("§7view").setChatStyle(new ChatStyle().setChatHoverEvent(new HoverEventRenderPlayer( @@ -141,7 +141,7 @@ public class FeatureViewPlayerStatsOnJoin extends SimpleFeature { double width = 220 * scaledResolution.getScaleFactor(); double height = 220 * scaledResolution.getScaleFactor(); widget = new OverlayWidget( - new WidgetProfileViewer(gameProfile, FeatureRegistry.PARTYKICKER_APIKEY.getAPIKey(), () -> { + new WidgetProfileViewer(gameProfile, () -> { if (widget != null) { OverlayManager.getInstance().removeOverlay(widget); widget = null; diff --git a/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/features/impl/party/playerpreview/api/ApiFetcher.java b/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/features/impl/party/playerpreview/api/ApiFetcher.java index 010530f6..966c47e8 100644 --- a/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/features/impl/party/playerpreview/api/ApiFetcher.java +++ b/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/features/impl/party/playerpreview/api/ApiFetcher.java @@ -24,6 +24,9 @@ import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.mojang.authlib.GameProfile; +import kr.syeyoung.dungeonsguide.launcher.Main; +import kr.syeyoung.dungeonsguide.launcher.auth.AuthManager; +import kr.syeyoung.dungeonsguide.launcher.auth.DgAuthUtil; import kr.syeyoung.dungeonsguide.mod.DungeonsGuide; import kr.syeyoung.dungeonsguide.mod.VersionInfo; import kr.syeyoung.dungeonsguide.mod.features.impl.party.playerpreview.api.playerprofile.PlayerProfile; @@ -57,7 +60,6 @@ public class ApiFetcher { .setThreadFactory(DungeonsGuide.THREAD_FACTORY) .setNameFormat("DG-APIFetcher-%d").build())); - private static final Set<String> invalidKeys = new HashSet<>(); public static void purgeCache() { playerProfileCache.clear(); @@ -69,7 +71,6 @@ public class ApiFetcher { completableFutureMap2.clear(); completableFutureMap3.clear(); completableFutureMap4.clear(); - invalidKeys.clear(); PlayerProfileParser.constants = null; ex.submit(PlayerProfileParser::getLilyWeightConstants); @@ -88,6 +89,16 @@ public class ApiFetcher { String servers = IOUtils.toString(inputStreamReader); return gson.fromJson(servers, JsonObject.class); } + public static JsonObject getJsonWithAuth(String url, String token) throws IOException { + URLConnection connection = new URL(url).openConnection(); + connection.setRequestProperty("User-Agent", "DungeonsGuide/"+ VersionInfo.VERSION); + connection.addRequestProperty("Authorization", "Bearer "+token); + connection.setConnectTimeout(10000); + connection.setReadTimeout(10000); + InputStreamReader inputStreamReader = new InputStreamReader(connection.getInputStream()); + String servers = IOUtils.toString(inputStreamReader); + return gson.fromJson(servers, JsonObject.class); + } public static JsonArray getJsonArr(String url) throws IOException { URLConnection connection = new URL(url).openConnection(); @@ -142,7 +153,7 @@ public class ApiFetcher { private static final Map<String, CompletableFuture<Optional<PlayerSkyblockData>>> completableFutureMap = new ConcurrentHashMap<>(); - public static CompletableFuture<Optional<PlayerSkyblockData>> fetchMostRecentProfileAsync(String uid, String apiKey) { + public static CompletableFuture<Optional<PlayerSkyblockData>> fetchMostRecentProfileAsync(String uid) { if (playerProfileCache.containsKey(uid)) { CachedData<PlayerSkyblockData> cachedData = playerProfileCache.get(uid); if (cachedData.getExpire() > System.currentTimeMillis()) { @@ -153,15 +164,10 @@ public class ApiFetcher { if (completableFutureMap.containsKey(uid)) { return completableFutureMap.get(uid); } - if (invalidKeys.contains(apiKey)) { - CompletableFuture cf = new CompletableFuture(); - cf.completeExceptionally(new IOException("403 for url")); - return cf; - } CompletableFuture<Optional<PlayerSkyblockData>> completableFuture = new CompletableFuture<>(); ex.submit(() -> { try { - Optional<PlayerSkyblockData> playerProfile = fetchPlayerProfiles(uid, apiKey); + Optional<PlayerSkyblockData> playerProfile = fetchPlayerProfiles(uid); playerProfileCache.put(uid, new CachedData<>(System.currentTimeMillis() + 1000 * 60 * 30, playerProfile.orElse(null))); completableFuture.complete(playerProfile); completableFutureMap.remove(uid); @@ -169,7 +175,6 @@ public class ApiFetcher { if (e.getMessage().contains("403 for URL")) { completableFuture.completeExceptionally(e); completableFutureMap.remove(uid); - invalidKeys.add(apiKey); } else { completableFuture.completeExceptionally(e); completableFutureMap.remove(uid); @@ -268,21 +273,6 @@ public class ApiFetcher { } - public static Optional<Integer> getNumberOfSecretsFromAchievement(String uid, String apiKey) throws IOException { - JsonObject response = getJson("https://api.hypixel.net/player?uuid=" + uid + "&key=" + apiKey); - if (response.has("player")) { - JsonObject treasures = response.getAsJsonObject("player"); - if (treasures.has("achievements")) { - treasures = treasures.getAsJsonObject("achievements"); - if (treasures.has("skyblock_treasure_hunter")) { - return Optional.of(treasures.get("skyblock_treasure_hunter").getAsInt()); - } - } - } - return Optional.empty(); - } - - public static int getArrayIndex(Object[] arr,Object value) { int k=0; for(int i=0;i<arr.length;i++){ @@ -294,10 +284,14 @@ public class ApiFetcher { return k; } - public static Optional<PlayerSkyblockData> fetchPlayerProfiles(String uid, String apiKey) throws IOException { + public static Optional<PlayerSkyblockData> fetchPlayerProfiles(String uid) throws IOException { + String dgAPIToken = AuthManager.getInstance().getWorkingTokenOrThrow(); + System.out.println("Fetching player profiles"); - JsonObject json = getJson("https://api.hypixel.net/skyblock/profiles?uuid=" + uid + "&key=" + apiKey); - if (!json.get("success").getAsBoolean()) return Optional.empty(); + JsonObject json = getJsonWithAuth(Main.DOMAIN+"/skyblock/player/"+uid, dgAPIToken); + + int secrets = json.get("secrets").getAsInt(); + System.out.println("Downloaded data from api"); JsonArray profiles = json.getAsJsonArray("profiles"); String dashTrimmed = uid.replace("-", ""); @@ -321,10 +315,7 @@ public class ApiFetcher { System.out.println("Finished Parsing Profile"); - System.out.println("Getting nm of secrets from achievement"); - getNumberOfSecretsFromAchievement(uid, apiKey).ifPresent(e::setTotalSecrets); - System.out.println("finished getting secrets from achievement"); - + e.setTotalSecrets(secrets); System.out.println("Getting selected profile"); if (e.isSelected()) { @@ -339,49 +330,5 @@ public class ApiFetcher { return Optional.of(pp); } - public static Optional<PlayerProfile> fetchMostRecentProfile(String uid, String apiKey) throws IOException { - JsonObject json = getJson("https://api.hypixel.net/skyblock/profiles?uuid=" + uid + "&key=" + apiKey); - if (!json.get("success").getAsBoolean()) return Optional.empty(); - JsonArray profiles = json.getAsJsonArray("profiles"); - String dashTrimmed = uid.replace("-", ""); - - JsonObject profile = null; - float lastSave = Long.MIN_VALUE; - for (JsonElement jsonElement : profiles) { - JsonObject semiProfile = jsonElement.getAsJsonObject(); - if (!semiProfile.getAsJsonObject("members").has(dashTrimmed)) { - continue; - } - JsonElement last_save = semiProfile.get("last_save"); - - JsonElement cute_name = semiProfile.get("cute_name"); - if (cute_name != null) { - System.out.println(cute_name.getAsString()); - } else { - System.out.println("THIS SHOULD NOT HAPPEN"); - } - - if (last_save == null) { - return Optional.empty(); - } - float lastSave2 = last_save.getAsLong(); - if (lastSave2 > lastSave) { - - profile = semiProfile; - lastSave = lastSave2; - } - } - - - if (profile == null) { - return Optional.empty(); - } - - PlayerProfile pp = PlayerProfileParser.parseProfile(profile, dashTrimmed); - - getNumberOfSecretsFromAchievement(uid, apiKey).ifPresent(pp::setTotalSecrets); - - return Optional.of(pp); - } } diff --git a/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/features/impl/party/playerpreview/widget/WidgetProfileViewer.java b/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/features/impl/party/playerpreview/widget/WidgetProfileViewer.java index 71486ce6..35c88c9c 100644 --- a/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/features/impl/party/playerpreview/widget/WidgetProfileViewer.java +++ b/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/features/impl/party/playerpreview/widget/WidgetProfileViewer.java @@ -38,16 +38,10 @@ public class WidgetProfileViewer extends AnnotatedWidget { @Bind(variableName = "visible") public final BindableAttribute<String> visiblePage = new BindableAttribute<>(String.class, "fetching"); private GameProfile gameProfile; - private String apiKey; private Runnable close; - public WidgetProfileViewer(GameProfile gameProfile, String apiKey, Runnable close) { + public WidgetProfileViewer(GameProfile gameProfile, Runnable close) { super(new ResourceLocation("dungeonsguide:gui/features/profileViewer/pv.gui")); - if (apiKey.isEmpty()) { - visiblePage.setValue("apiKeyInvalid"); - return; - } this.gameProfile = gameProfile; - this.apiKey = apiKey; this.close = close; refresh(); } @@ -55,12 +49,9 @@ public class WidgetProfileViewer extends AnnotatedWidget { @On(functionName = "refresh") public void refresh() { actualPV.setValue(null); - if (apiKey.isEmpty()) { - visiblePage.setValue("apiKeyInvalid"); - return; - } + visiblePage.setValue("fetching"); - ApiFetcher.fetchMostRecentProfileAsync(gameProfile.getId().toString(), apiKey) + ApiFetcher.fetchMostRecentProfileAsync(gameProfile.getId().toString()) .whenComplete((a,e) -> { if (e != null) { e.printStackTrace(); |