Skip to content

Commit e09e8bc

Browse files
committed
adding support for grabbing the api key from other mods: neu, cow, dsm, dg, st, soopy and sbe
1 parent 0be6c7f commit e09e8bc

File tree

4 files changed

+118
-12
lines changed

4 files changed

+118
-12
lines changed

src/main/java/at/hannibal2/skyhanni/SkyHanniMod.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import at.hannibal2.skyhanni.config.Features;
44
import at.hannibal2.skyhanni.config.gui.commands.Commands;
5-
import at.hannibal2.skyhanni.data.ApiData;
5+
import at.hannibal2.skyhanni.data.ApiKeyGrabber;
66
import at.hannibal2.skyhanni.data.HypixelData;
77
import at.hannibal2.skyhanni.data.ItemRenderBackground;
88
import at.hannibal2.skyhanni.data.ScoreboardData;
@@ -54,7 +54,7 @@ public class SkyHanniMod {
5454
public static Features feature;
5555
private File configFile;
5656

57-
private final Gson gson = new GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation().create();
57+
public static final Gson gson = new GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation().create();
5858

5959
public static File configDirectory;
6060
public static RepoManager repo;
@@ -67,7 +67,7 @@ public void preInit(FMLPreInitializationEvent event) {
6767
registerEvent(new HypixelData());
6868
registerEvent(new DungeonData());
6969
registerEvent(new ScoreboardData());
70-
registerEvent(new ApiData());
70+
registerEvent(new ApiKeyGrabber());
7171
registerEvent(new SeaCreatureManager());
7272
registerEvent(new ItemRenderBackground());
7373

src/main/java/at/hannibal2/skyhanni/data/ApiData.kt renamed to src/main/java/at/hannibal2/skyhanni/data/ApiKeyGrabber.kt

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import at.hannibal2.skyhanni.utils.APIUtil
88
import at.hannibal2.skyhanni.utils.LorenzUtils
99
import net.minecraft.client.Minecraft
1010
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
11+
import java.io.File
1112

12-
class ApiData {
13+
class ApiKeyGrabber {
1314

1415
private var currentProfileName = ""
1516

@@ -35,11 +36,20 @@ class ApiData {
3536
private fun updateApiData() {
3637
val uuid = Minecraft.getMinecraft().thePlayer.uniqueID.toString().replace("-", "")
3738

38-
val apiKey = SkyHanniMod.feature.hidden.apiKey
39+
var apiKey = SkyHanniMod.feature.hidden.apiKey
40+
if (!verifyKey(apiKey)) {
41+
LorenzUtils.chat("§c[SkyHanni] Invalid api key detected, deleting it!")
42+
apiKey = ""
43+
SkyHanniMod.feature.hidden.apiKey = ""
44+
}
3945

4046
if (apiKey.isEmpty()) {
41-
LorenzUtils.error("SkyHanni has no API Key set. Type /api new to reload.")
42-
return
47+
readApiKeyFromOtherMods()
48+
apiKey = SkyHanniMod.feature.hidden.apiKey
49+
if (apiKey.isEmpty()) {
50+
LorenzUtils.warning("SkyHanni has no API Key set. Type /api new to reload.")
51+
return
52+
}
4353
}
4454

4555
val url = "https://api.hypixel.net/player?key=$apiKey&uuid=$uuid"
@@ -71,6 +81,48 @@ class ApiData {
7181
}
7282
}
7383

84+
private fun readApiKeyFromOtherMods() {
85+
println("Trying to find the API Key from the config of other mods..")
86+
87+
var found = false
88+
for (mod in OtherMod.values()) {
89+
val modName = mod.modName
90+
val file = File(mod.configPath)
91+
if (file.exists()) {
92+
val reader = APIUtil.readFile(file)
93+
try {
94+
val key = mod.readKey(reader).replace("\n", "").replace(" ", "")
95+
if (verifyKey(key)) {
96+
println("- $modName: good key!")
97+
if (!found) {
98+
found = true
99+
LorenzUtils.chat("§e[SkyHanni] Grabbed the API key from $modName!")
100+
SkyHanniMod.feature.hidden.apiKey = key
101+
}
102+
} else {
103+
println("- $modName: wrong key!")
104+
}
105+
} catch (e: Throwable) {
106+
println("- $modName: wrong config format! (" + e.message + ")")
107+
continue
108+
}
109+
} else {
110+
println("- $modName: no config found!")
111+
}
112+
}
113+
}
114+
115+
private fun verifyKey(key: String): Boolean {
116+
return try {
117+
val url = "https://api.hypixel.net/key?key=$key"
118+
val bazaarData = APIUtil.getJSONResponse(url, silentError = true)
119+
return bazaarData.get("success").asBoolean
120+
} catch (e: Throwable) {
121+
e.printStackTrace()
122+
false
123+
}
124+
}
125+
74126
private fun loadProfile(playerUuid: String, profileId: String) {
75127
val apiKey = SkyHanniMod.feature.hidden.apiKey
76128
val url = "https://api.hypixel.net/skyblock/profile?key=$apiKey&profile=$profileId"
@@ -83,7 +135,6 @@ class ApiData {
83135
if (entry.key == playerUuid) {
84136
val profileData = entry.value.asJsonObject
85137
ProfileApiDataLoadedEvent(profileData).postAndCatch()
86-
87138
}
88139
}
89140
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package at.hannibal2.skyhanni.data
2+
3+
import at.hannibal2.skyhanni.SkyHanniMod
4+
import com.google.gson.JsonObject
5+
import java.io.BufferedReader
6+
7+
enum class OtherMod(val modName: String, val configPath: String, val readKey: (BufferedReader) -> (String)) {
8+
NEU("Not Enough Updates", "config/notenoughupdates/configNew.json", { reader ->
9+
getJson(reader)["apiData"].asJsonObject["apiKey"].asString
10+
}),
11+
COW("Cowlection", "config/cowlection/do-not-share-me-with-other-players.cfg", { reader ->
12+
val lines = reader.readText().split(System.lineSeparator())
13+
val line = lines.find { it.startsWith(" S:moo=") }!!
14+
line.split("=")[1]
15+
}),
16+
DSM("Dankers SkyBlock Mod", "config/Danker's Skyblock Mod.cfg", { reader ->
17+
val lines = reader.readText().split(System.lineSeparator())
18+
val line = lines.find { it.startsWith(" S:APIKey=") }!!
19+
line.split("=")[1]
20+
}),
21+
DG("Dungeons Guide", "config/dungeonsguide/config.json", { reader ->
22+
getJson(reader)["partykicker.apikey"].asJsonObject["apikey"].asString
23+
}),
24+
SKYTILS("Skytils", "config/skytils/config.toml", { reader ->
25+
val lines = reader.readText().split(System.lineSeparator())
26+
val line = lines.find { it.startsWith(" hypixel_api_key = \"") }!!
27+
line.split("\"")[1]
28+
}),
29+
HYPIXEL_API_KEY_MANAGER("Hypixel API Key Manager", "HypixelApiKeyManager/localdata.json", { reader ->
30+
getJson(reader)["key"].asString
31+
}),
32+
SOOPY("Soopy Addons", "soopyAddonsData/apikey.txt", { reader ->
33+
reader.readText()
34+
}),
35+
SBE("SkyBlock Extras", "config/SkyblockExtras.cfg", { reader ->
36+
getJson(reader)["values"].asJsonObject["apiKey"].asString
37+
}),
38+
}
39+
40+
fun getJson(reader: BufferedReader): JsonObject {
41+
return SkyHanniMod.gson.fromJson(reader, com.google.gson.JsonObject::class.java)
42+
}

src/main/java/at/hannibal2/skyhanni/utils/APIUtil.kt

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ import org.apache.http.impl.client.HttpClientBuilder
88
import org.apache.http.impl.client.HttpClients
99
import org.apache.http.message.BasicHeader
1010
import org.apache.http.util.EntityUtils
11+
import java.io.BufferedReader
12+
import java.io.File
13+
import java.io.FileInputStream
14+
import java.io.InputStreamReader
15+
import java.nio.charset.StandardCharsets
1116

1217

1318
object APIUtil {
@@ -27,7 +32,7 @@ object APIUtil {
2732
)
2833
.useSystemProperties()
2934

30-
fun getJSONResponse(urlString: String): JsonObject {
35+
fun getJSONResponse(urlString: String, silentError: Boolean = false): JsonObject {
3136
val client = builder.build()
3237
try {
3338
client.execute(HttpGet(urlString)).use { response ->
@@ -37,12 +42,20 @@ object APIUtil {
3742
return parser.parse(retSrc) as JsonObject
3843
}
3944
}
40-
} catch (ex: Throwable) {
41-
ex.printStackTrace()
42-
LorenzUtils.error("SkyHanni ran into an ${ex::class.simpleName ?: "error"} whilst fetching a resource. See logs for more details.")
45+
} catch (throwable: Throwable) {
46+
if (silentError) {
47+
throw throwable
48+
} else {
49+
throwable.printStackTrace()
50+
LorenzUtils.error("SkyHanni ran into an ${throwable::class.simpleName ?: "error"} whilst fetching a resource. See logs for more details.")
51+
}
4352
} finally {
4453
client.close()
4554
}
4655
return JsonObject()
4756
}
57+
58+
fun readFile(file: File): BufferedReader {
59+
return BufferedReader(InputStreamReader(FileInputStream(file), StandardCharsets.UTF_8))
60+
}
4861
}

0 commit comments

Comments
 (0)