diff options
author | Lorenz <lo.scherf@gmail.com> | 2022-08-20 02:25:39 +0200 |
---|---|---|
committer | Lorenz <lo.scherf@gmail.com> | 2022-08-20 02:25:39 +0200 |
commit | 427402e8c65a78621e365e5921d18d66917e00e4 (patch) | |
tree | 612213eed0614d34edea3bb31ea64904f45b5bca /src/main/java/at/hannibal2 | |
parent | bd8cc161ab959bf0cacf3784371f0d53efd39d2a (diff) | |
download | skyhanni-427402e8c65a78621e365e5921d18d66917e00e4.tar.gz skyhanni-427402e8c65a78621e365e5921d18d66917e00e4.tar.bz2 skyhanni-427402e8c65a78621e365e5921d18d66917e00e4.zip |
adding support for grabbing the api key from other mods: neu, cow, dsm, dg, st, soopy and sbe
Diffstat (limited to 'src/main/java/at/hannibal2')
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/SkyHanniMod.java | 6 | ||||
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/data/ApiKeyGrabber.kt (renamed from src/main/java/at/hannibal2/skyhanni/data/ApiData.kt) | 61 | ||||
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/data/OtherMod.kt | 42 | ||||
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/utils/APIUtil.kt | 21 |
4 files changed, 118 insertions, 12 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.java b/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.java index 35ed2eba9..ed0d3812c 100644 --- a/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.java +++ b/src/main/java/at/hannibal2/skyhanni/SkyHanniMod.java @@ -2,7 +2,7 @@ package at.hannibal2.skyhanni; import at.hannibal2.skyhanni.config.Features; import at.hannibal2.skyhanni.config.gui.commands.Commands; -import at.hannibal2.skyhanni.data.ApiData; +import at.hannibal2.skyhanni.data.ApiKeyGrabber; import at.hannibal2.skyhanni.data.HypixelData; import at.hannibal2.skyhanni.data.ItemRenderBackground; import at.hannibal2.skyhanni.data.ScoreboardData; @@ -54,7 +54,7 @@ public class SkyHanniMod { public static Features feature; private File configFile; - private final Gson gson = new GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation().create(); + public static final Gson gson = new GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation().create(); public static File configDirectory; public static RepoManager repo; @@ -67,7 +67,7 @@ public class SkyHanniMod { registerEvent(new HypixelData()); registerEvent(new DungeonData()); registerEvent(new ScoreboardData()); - registerEvent(new ApiData()); + registerEvent(new ApiKeyGrabber()); registerEvent(new SeaCreatureManager()); registerEvent(new ItemRenderBackground()); diff --git a/src/main/java/at/hannibal2/skyhanni/data/ApiData.kt b/src/main/java/at/hannibal2/skyhanni/data/ApiKeyGrabber.kt index 9e47d5d0f..c3a7dada5 100644 --- a/src/main/java/at/hannibal2/skyhanni/data/ApiData.kt +++ b/src/main/java/at/hannibal2/skyhanni/data/ApiKeyGrabber.kt @@ -8,8 +8,9 @@ import at.hannibal2.skyhanni.utils.APIUtil import at.hannibal2.skyhanni.utils.LorenzUtils import net.minecraft.client.Minecraft import net.minecraftforge.fml.common.eventhandler.SubscribeEvent +import java.io.File -class ApiData { +class ApiKeyGrabber { private var currentProfileName = "" @@ -35,11 +36,20 @@ class ApiData { private fun updateApiData() { val uuid = Minecraft.getMinecraft().thePlayer.uniqueID.toString().replace("-", "") - val apiKey = SkyHanniMod.feature.hidden.apiKey + var apiKey = SkyHanniMod.feature.hidden.apiKey + if (!verifyKey(apiKey)) { + LorenzUtils.chat("§c[SkyHanni] Invalid api key detected, deleting it!") + apiKey = "" + SkyHanniMod.feature.hidden.apiKey = "" + } if (apiKey.isEmpty()) { - LorenzUtils.error("SkyHanni has no API Key set. Type /api new to reload.") - return + readApiKeyFromOtherMods() + apiKey = SkyHanniMod.feature.hidden.apiKey + if (apiKey.isEmpty()) { + LorenzUtils.warning("SkyHanni has no API Key set. Type /api new to reload.") + return + } } val url = "https://api.hypixel.net/player?key=$apiKey&uuid=$uuid" @@ -71,6 +81,48 @@ class ApiData { } } + private fun readApiKeyFromOtherMods() { + println("Trying to find the API Key from the config of other mods..") + + var found = false + for (mod in OtherMod.values()) { + val modName = mod.modName + val file = File(mod.configPath) + if (file.exists()) { + val reader = APIUtil.readFile(file) + try { + val key = mod.readKey(reader).replace("\n", "").replace(" ", "") + if (verifyKey(key)) { + println("- $modName: good key!") + if (!found) { + found = true + LorenzUtils.chat("§e[SkyHanni] Grabbed the API key from $modName!") + SkyHanniMod.feature.hidden.apiKey = key + } + } else { + println("- $modName: wrong key!") + } + } catch (e: Throwable) { + println("- $modName: wrong config format! (" + e.message + ")") + continue + } + } else { + println("- $modName: no config found!") + } + } + } + + private fun verifyKey(key: String): Boolean { + return try { + val url = "https://api.hypixel.net/key?key=$key" + val bazaarData = APIUtil.getJSONResponse(url, silentError = true) + return bazaarData.get("success").asBoolean + } catch (e: Throwable) { + e.printStackTrace() + false + } + } + private fun loadProfile(playerUuid: String, profileId: String) { val apiKey = SkyHanniMod.feature.hidden.apiKey val url = "https://api.hypixel.net/skyblock/profile?key=$apiKey&profile=$profileId" @@ -83,7 +135,6 @@ class ApiData { if (entry.key == playerUuid) { val profileData = entry.value.asJsonObject ProfileApiDataLoadedEvent(profileData).postAndCatch() - } } } diff --git a/src/main/java/at/hannibal2/skyhanni/data/OtherMod.kt b/src/main/java/at/hannibal2/skyhanni/data/OtherMod.kt new file mode 100644 index 000000000..04f2a4d4f --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/data/OtherMod.kt @@ -0,0 +1,42 @@ +package at.hannibal2.skyhanni.data + +import at.hannibal2.skyhanni.SkyHanniMod +import com.google.gson.JsonObject +import java.io.BufferedReader + +enum class OtherMod(val modName: String, val configPath: String, val readKey: (BufferedReader) -> (String)) { + NEU("Not Enough Updates", "config/notenoughupdates/configNew.json", { reader -> + getJson(reader)["apiData"].asJsonObject["apiKey"].asString + }), + COW("Cowlection", "config/cowlection/do-not-share-me-with-other-players.cfg", { reader -> + val lines = reader.readText().split(System.lineSeparator()) + val line = lines.find { it.startsWith(" S:moo=") }!! + line.split("=")[1] + }), + DSM("Dankers SkyBlock Mod", "config/Danker's Skyblock Mod.cfg", { reader -> + val lines = reader.readText().split(System.lineSeparator()) + val line = lines.find { it.startsWith(" S:APIKey=") }!! + line.split("=")[1] + }), + DG("Dungeons Guide", "config/dungeonsguide/config.json", { reader -> + getJson(reader)["partykicker.apikey"].asJsonObject["apikey"].asString + }), + SKYTILS("Skytils", "config/skytils/config.toml", { reader -> + val lines = reader.readText().split(System.lineSeparator()) + val line = lines.find { it.startsWith(" hypixel_api_key = \"") }!! + line.split("\"")[1] + }), + HYPIXEL_API_KEY_MANAGER("Hypixel API Key Manager", "HypixelApiKeyManager/localdata.json", { reader -> + getJson(reader)["key"].asString + }), + SOOPY("Soopy Addons", "soopyAddonsData/apikey.txt", { reader -> + reader.readText() + }), + SBE("SkyBlock Extras", "config/SkyblockExtras.cfg", { reader -> + getJson(reader)["values"].asJsonObject["apiKey"].asString + }), +} + +fun getJson(reader: BufferedReader): JsonObject { + return SkyHanniMod.gson.fromJson(reader, com.google.gson.JsonObject::class.java) +}
\ No newline at end of file diff --git a/src/main/java/at/hannibal2/skyhanni/utils/APIUtil.kt b/src/main/java/at/hannibal2/skyhanni/utils/APIUtil.kt index c321cf2d5..b66120f82 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/APIUtil.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/APIUtil.kt @@ -8,6 +8,11 @@ import org.apache.http.impl.client.HttpClientBuilder import org.apache.http.impl.client.HttpClients import org.apache.http.message.BasicHeader import org.apache.http.util.EntityUtils +import java.io.BufferedReader +import java.io.File +import java.io.FileInputStream +import java.io.InputStreamReader +import java.nio.charset.StandardCharsets object APIUtil { @@ -27,7 +32,7 @@ object APIUtil { ) .useSystemProperties() - fun getJSONResponse(urlString: String): JsonObject { + fun getJSONResponse(urlString: String, silentError: Boolean = false): JsonObject { val client = builder.build() try { client.execute(HttpGet(urlString)).use { response -> @@ -37,12 +42,20 @@ object APIUtil { return parser.parse(retSrc) as JsonObject } } - } catch (ex: Throwable) { - ex.printStackTrace() - LorenzUtils.error("SkyHanni ran into an ${ex::class.simpleName ?: "error"} whilst fetching a resource. See logs for more details.") + } catch (throwable: Throwable) { + if (silentError) { + throw throwable + } else { + throwable.printStackTrace() + LorenzUtils.error("SkyHanni ran into an ${throwable::class.simpleName ?: "error"} whilst fetching a resource. See logs for more details.") + } } finally { client.close() } return JsonObject() } + + fun readFile(file: File): BufferedReader { + return BufferedReader(InputStreamReader(FileInputStream(file), StandardCharsets.UTF_8)) + } }
\ No newline at end of file |