aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main/java/at/hannibal2/skyhanni/features/garden/farming/FarmingWeightDisplay.kt33
1 files changed, 29 insertions, 4 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/farming/FarmingWeightDisplay.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/farming/FarmingWeightDisplay.kt
index 71b69107b..30473f39f 100644
--- a/src/main/java/at/hannibal2/skyhanni/features/garden/farming/FarmingWeightDisplay.kt
+++ b/src/main/java/at/hannibal2/skyhanni/features/garden/farming/FarmingWeightDisplay.kt
@@ -66,6 +66,11 @@ class FarmingWeightDisplay {
if (!isEnabled()) return
if (!event.isMod(5)) return
update()
+
+ SkyHanniMod.coroutineScope.launch {
+ getCropWeights()
+ hasFetchedCropWeights = true
+ }
}
companion object {
@@ -403,7 +408,9 @@ class FarmingWeightDisplay {
private fun CropType.getLocalCounter() = localCounter[this] ?: 0L
- private fun CropType.getFactor() = factorPerCrop[this]!!
+ private fun CropType.getFactor(): Double {
+ return factorPerCrop[this] ?: backupFactors[this]!!
+ }
fun lookUpCommand(it: Array<String>) {
val name = if (it.size == 1) it[0] else LorenzUtils.getPlayerName()
@@ -411,7 +418,25 @@ class FarmingWeightDisplay {
LorenzUtils.chat("§e[SkyHanni] Opening Farming Profile from §b$name")
}
- private val factorPerCrop by lazy {
+ private val factorPerCrop = mutableMapOf<CropType, Double>()
+ private var attemptingCropWeightFetch = false
+ private var hasFetchedCropWeights = false
+
+ private suspend fun getCropWeights() {
+ if (attemptingCropWeightFetch || hasFetchedCropWeights) return
+ attemptingCropWeightFetch = true
+
+ val url = "https://api.elitebot.dev/weights"
+ val result = withContext(Dispatchers.IO) { APIUtil.getJSONResponse(url) }.asJsonObject
+
+ for (crop in result.entrySet()) {
+ val cropType = CropType.entries.firstOrNull { it.cropName == crop.key } ?: continue
+ factorPerCrop[cropType] = crop.value.asDouble
+ }
+ }
+
+ // still needed when first joining garden and if they cant make https requests
+ private val backupFactors by lazy {
mapOf(
CropType.WHEAT to 100_000.0,
CropType.CARROT to 302_061.86,
@@ -425,7 +450,7 @@ class FarmingWeightDisplay {
CropType.CACTUS to 177_254.45,
)
}
- }
- class UpcomingPlayer(val name: String, val weight: Double)
+ class UpcomingPlayer(val name: String, val weight: Double)
+ }
} \ No newline at end of file