Skip to content

Commit

Permalink
adding support for grabbing the api key from other mods: neu, cow, ds…
Browse files Browse the repository at this point in the history
…m, dg, st, soopy and sbe
  • Loading branch information
hannibal002 committed Aug 20, 2022
1 parent 0be6c7f commit e09e8bc
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/main/java/at/hannibal2/skyhanni/SkyHanniMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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;
Expand Down Expand Up @@ -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;
Expand All @@ -67,7 +67,7 @@ public void preInit(FMLPreInitializationEvent event) {
registerEvent(new HypixelData());
registerEvent(new DungeonData());
registerEvent(new ScoreboardData());
registerEvent(new ApiData());
registerEvent(new ApiKeyGrabber());
registerEvent(new SeaCreatureManager());
registerEvent(new ItemRenderBackground());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""

Expand All @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -83,7 +135,6 @@ class ApiData {
if (entry.key == playerUuid) {
val profileData = entry.value.asJsonObject
ProfileApiDataLoadedEvent(profileData).postAndCatch()

}
}
}
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/at/hannibal2/skyhanni/data/OtherMod.kt
Original file line number Diff line number Diff line change
@@ -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)
}
21 changes: 17 additions & 4 deletions src/main/java/at/hannibal2/skyhanni/utils/APIUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 ->
Expand All @@ -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))
}
}

0 comments on commit e09e8bc

Please sign in to comment.