aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni')
-rw-r--r--src/main/java/at/hannibal2/skyhanni/data/MinecraftData.kt2
-rw-r--r--src/main/java/at/hannibal2/skyhanni/utils/DelayedRun.kt25
2 files changed, 18 insertions, 9 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/data/MinecraftData.kt b/src/main/java/at/hannibal2/skyhanni/data/MinecraftData.kt
index 56d565299..2ec0ea667 100644
--- a/src/main/java/at/hannibal2/skyhanni/data/MinecraftData.kt
+++ b/src/main/java/at/hannibal2/skyhanni/data/MinecraftData.kt
@@ -72,11 +72,11 @@ object MinecraftData {
Minecraft.getMinecraft().thePlayer ?: return
totalTicks++
LorenzTickEvent(totalTicks).postAndCatch()
- DelayedRun.checkRuns()
}
@SubscribeEvent
fun onTick(event: LorenzTickEvent) {
+ DelayedRun.checkRuns()
if (!LorenzUtils.inSkyBlock) return
val hand = InventoryUtils.getItemInHand()
val newItem = hand?.getInternalName() ?: NEUInternalName.NONE
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/DelayedRun.kt b/src/main/java/at/hannibal2/skyhanni/utils/DelayedRun.kt
index 2af59435d..f90237588 100644
--- a/src/main/java/at/hannibal2/skyhanni/utils/DelayedRun.kt
+++ b/src/main/java/at/hannibal2/skyhanni/utils/DelayedRun.kt
@@ -1,25 +1,34 @@
package at.hannibal2.skyhanni.utils
+import at.hannibal2.skyhanni.utils.LorenzUtils.editCopy
import kotlin.time.Duration
+// TODO find better sync bug fix than creating a new map for each use
object DelayedRun {
- val map = mutableMapOf<() -> Any, SimpleTimeMark>()
+ var map = mapOf<() -> Any, SimpleTimeMark>()
fun runDelayed(duration: Duration, run: () -> Unit) {
- map[run] = SimpleTimeMark.now() + duration
+ map = map.editCopy {
+ this[run] = SimpleTimeMark.now() + duration
+ }
}
fun runNextTick(run: () -> Unit) {
- map[run] = SimpleTimeMark.now()
+ map = map.editCopy {
+ this[run] = SimpleTimeMark.now()
+ }
}
fun checkRuns() {
- map.entries.removeIf { (runnable, time) ->
- val inPast = time.isInPast()
- if (inPast) {
- runnable()
+ if (map.isEmpty()) return
+ map = map.editCopy {
+ entries.removeIf { (runnable, time) ->
+ val inPast = time.isInPast()
+ if (inPast) {
+ runnable()
+ }
+ inPast
}
- inPast
}
}
}