aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main/java/at/hannibal2/skyhanni/config/storage/ProfileSpecificStorage.java3
-rw-r--r--src/main/java/at/hannibal2/skyhanni/features/garden/pests/PestAPI.kt136
2 files changed, 81 insertions, 58 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/config/storage/ProfileSpecificStorage.java b/src/main/java/at/hannibal2/skyhanni/config/storage/ProfileSpecificStorage.java
index 0ec04b195..3f62f3a1b 100644
--- a/src/main/java/at/hannibal2/skyhanni/config/storage/ProfileSpecificStorage.java
+++ b/src/main/java/at/hannibal2/skyhanni/config/storage/ProfileSpecificStorage.java
@@ -234,6 +234,9 @@ public class ProfileSpecificStorage {
public Map<Integer, GardenPlotAPI.PlotData> plotData = new HashMap<>();
@Expose
+ public int scoreboardPests = 0;
+
+ @Expose
public Map<CropType, LorenzVec> cropStartLocations = new HashMap<>();
@Expose
diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/pests/PestAPI.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/pests/PestAPI.kt
index 19f273899..bd0ee5407 100644
--- a/src/main/java/at/hannibal2/skyhanni/features/garden/pests/PestAPI.kt
+++ b/src/main/java/at/hannibal2/skyhanni/features/garden/pests/PestAPI.kt
@@ -1,8 +1,10 @@
package at.hannibal2.skyhanni.features.garden.pests
+import at.hannibal2.skyhanni.data.ScoreboardData
import at.hannibal2.skyhanni.events.DebugDataCollectEvent
import at.hannibal2.skyhanni.events.InventoryFullyOpenedEvent
import at.hannibal2.skyhanni.events.LorenzChatEvent
+import at.hannibal2.skyhanni.events.LorenzTickEvent
import at.hannibal2.skyhanni.events.LorenzWorldChangeEvent
import at.hannibal2.skyhanni.events.ScoreboardChangeEvent
import at.hannibal2.skyhanni.events.TabListUpdateEvent
@@ -18,7 +20,6 @@ import at.hannibal2.skyhanni.features.garden.GardenPlotAPI.pests
import at.hannibal2.skyhanni.features.garden.GardenPlotAPI.uncleared
import at.hannibal2.skyhanni.test.command.ErrorManager
import at.hannibal2.skyhanni.utils.ChatUtils
-import at.hannibal2.skyhanni.utils.DelayedRun
import at.hannibal2.skyhanni.utils.InventoryUtils
import at.hannibal2.skyhanni.utils.ItemUtils.getLore
import at.hannibal2.skyhanni.utils.LocationUtils.distanceSqToPlayer
@@ -37,8 +38,13 @@ import kotlin.time.Duration.Companion.seconds
object PestAPI {
val config get() = GardenAPI.config.pests
+ val storage get() = GardenAPI.storage
- var scoreboardPests = 0
+ var scoreboardPests: Int
+ get() = storage?.scoreboardPests ?: 0
+ set(value) {
+ storage?.scoreboardPests = value
+ }
var lastPestKillTime = SimpleTimeMark.farPast()
@@ -110,6 +116,9 @@ object PestAPI {
"§cThere are not any Pests on your Garden right now! Keep farming!"
)
+ var gardenJoinTime = SimpleTimeMark.farPast()
+ var firstScoreboardCheck = false
+
private fun fixPests(loop: Int = 2) {
val accurateAmount = getPlotsWithAccuratePests().sumOf { it.pests }
val inaccurateAmount = getPlotsWithInaccuratePests().size
@@ -119,10 +128,10 @@ object PestAPI {
plot.isPestCountInaccurate = false
}
} else if (inaccurateAmount == 1) { // if we can assume all the inaccurate pests are in the only inaccurate plot
- val plot = getPlotsWithInaccuratePests().firstOrNull()
- plot?.pests = scoreboardPests - accurateAmount
- plot?.isPestCountInaccurate = false
- } else if (accurateAmount + inaccurateAmount > scoreboardPests) {
+ val plot = getPlotsWithInaccuratePests().firstOrNull() ?: return
+ plot.pests = scoreboardPests - accurateAmount
+ plot.isPestCountInaccurate = false
+ } else if (accurateAmount + inaccurateAmount > scoreboardPests) { // when logic fails and we reach impossible pest counts
sendPestError(true)
getInfestedPlots().forEach {
it.pests = 0
@@ -134,11 +143,9 @@ object PestAPI {
}
private fun updatePests() {
+ if (!firstScoreboardCheck) return
+ fixPests()
PestUpdateEvent().postAndCatch()
- DelayedRun.runDelayed(3.seconds) {
- fixPests()
- PestUpdateEvent().postAndCatch()
- }
}
@SubscribeEvent
@@ -159,6 +166,7 @@ object PestAPI {
plot.isPestCountInaccurate = false
}
}
+ if (!event.unknownAmount) scoreboardPests += event.amountPests
updatePests()
}
@@ -182,14 +190,10 @@ object PestAPI {
@SubscribeEvent
fun onTabListUpdate(event: TabListUpdateEvent) {
if (!GardenAPI.inGarden()) return
- var previousLine = ""
for (line in event.tabList) {
infectedPlotsTablistPattern.matchMatcher(line) {
- if (previousLine == line) return
- val plotList = mutableListOf<Int>()
- group("plots").removeColor().split(", ").toMutableList().forEach {
- plotList.add(it.toInt())
- }
+ val plotList = group("plots").removeColor().split(", ").map { it.toInt() }
+ if (plotList.sorted() == getInfestedPlots().map { it.id }.sorted()) return
GardenPlotAPI.plots.forEach {
if (plotList.contains(it.id)) {
@@ -201,7 +205,6 @@ object PestAPI {
it.isPestCountInaccurate = false
}
}
- previousLine = line
updatePests()
}
}
@@ -210,47 +213,8 @@ object PestAPI {
@SubscribeEvent
fun onScoreboardChange(event: ScoreboardChangeEvent) {
if (!GardenAPI.inGarden()) return
-
- for (line in event.newList) {
- // gets the total amount of pests in the garden
- pestsInScoreboardPattern.matchMatcher(line) {
- val newPests = group("pests").formatInt()
- if (newPests != scoreboardPests) {
- scoreboardPests = newPests
- updatePests()
- }
- }
-
- // gets if there are no pests remaining in the garden
- noPestsInScoreboardPattern.matchMatcher(line) {
- if (scoreboardPests != 0) {
- resetAllPests()
- }
- }
-
- // gets the amount of pests in the current plot
- pestsInPlotScoreboardPattern.matchMatcher(line) {
- val plotName = group("plot")
- val pestsInPlot = group("pests").toInt()
- val plot = GardenPlotAPI.getPlotByName(plotName)
- if (pestsInPlot != plot?.pests || plot.isPestCountInaccurate) {
- plot?.pests = pestsInPlot
- plot?.isPestCountInaccurate = false
- updatePests()
- }
- }
-
- // gets if there are no pests remaining in the current plot
- noPestsInPlotScoreboardPattern.matchMatcher(line) {
- val plotName = group("plot")
- val plot = GardenPlotAPI.getPlotByName(plotName)
- if (plot?.pests != 0 || plot.isPestCountInaccurate) {
- GardenPlotAPI.getPlotByName(plotName)?.pests = 0
- GardenPlotAPI.getPlotByName(plotName)?.isPestCountInaccurate = false
- updatePests()
- }
- }
- }
+ if (!firstScoreboardCheck) return
+ checkScoreboardLines(event.newList)
}
@SubscribeEvent
@@ -266,8 +230,20 @@ object PestAPI {
}
@SubscribeEvent
+ fun onTick(event: LorenzTickEvent) {
+ if (!GardenAPI.inGarden()) return
+ if (!firstScoreboardCheck && gardenJoinTime.passedSince() > 5.seconds) {
+ checkScoreboardLines(ScoreboardData.sidebarLinesFormatted)
+ firstScoreboardCheck = true
+ updatePests()
+ }
+ }
+
+ @SubscribeEvent
fun onWorldChange(event: LorenzWorldChangeEvent) {
lastPestKillTime = SimpleTimeMark.farPast()
+ gardenJoinTime = SimpleTimeMark.now()
+ firstScoreboardCheck = false
}
private fun getPlotsWithAccuratePests() = GardenPlotAPI.plots.filter { it.pests > 0 && !it.isPestCountInaccurate }
@@ -316,6 +292,50 @@ object PestAPI {
)
}
+ private fun checkScoreboardLines(list: List<String>) {
+ for (line in list) {
+ // gets if there are no pests remaining in the garden
+ noPestsInScoreboardPattern.matchMatcher(line) {
+ if (scoreboardPests != 0 || getInfestedPlots().isNotEmpty()) {
+ resetAllPests()
+ }
+ return
+ }
+
+ // gets the total amount of pests in the garden
+ pestsInScoreboardPattern.matchMatcher(line) {
+ val newPests = group("pests").formatInt()
+ if (newPests != scoreboardPests) {
+ scoreboardPests = newPests
+ updatePests()
+ }
+ }
+
+ // gets the amount of pests in the current plot
+ pestsInPlotScoreboardPattern.matchMatcher(line) {
+ val plotName = group("plot")
+ val pestsInPlot = group("pests").toInt()
+ val plot = GardenPlotAPI.getPlotByName(plotName) ?: return
+ if (pestsInPlot != plot.pests || plot.isPestCountInaccurate) {
+ plot.pests = pestsInPlot
+ plot.isPestCountInaccurate = false
+ updatePests()
+ }
+ }
+
+ // gets if there are no pests remaining in the current plot
+ noPestsInPlotScoreboardPattern.matchMatcher(line) {
+ val plotName = group("plot")
+ val plot = GardenPlotAPI.getPlotByName(plotName) ?: return
+ if (plot.pests != 0 || plot.isPestCountInaccurate) {
+ plot.pests = 0
+ plot.isPestCountInaccurate = false
+ updatePests()
+ }
+ }
+ }
+ }
+
@SubscribeEvent
fun onDebugDataCollect(event: DebugDataCollectEvent) {
event.title("Garden Pests")