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 | e09e8bcdc718a08d57abea55339ecbbb082d9c8a (patch) | |
| tree | 612213eed0614d34edea3bb31ea64904f45b5bca /src/main/java/at/hannibal2/skyhanni/data | |
| parent | 0be6c7f900fe2003b0692e714b613292595d6974 (diff) | |
| download | SkyHanni-e09e8bcdc718a08d57abea55339ecbbb082d9c8a.tar.gz SkyHanni-e09e8bcdc718a08d57abea55339ecbbb082d9c8a.tar.bz2 SkyHanni-e09e8bcdc718a08d57abea55339ecbbb082d9c8a.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/skyhanni/data')
| -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 |
2 files changed, 98 insertions, 5 deletions
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 |
