From b49c4b47c2910ab6ae75b62acf9fcc455d1da501 Mon Sep 17 00:00:00 2001 From: hannibal2 <24389977+hannibal00212@users.noreply.github.com> Date: Thu, 30 Nov 2023 12:50:31 +0100 Subject: fixed ConcurrentModificationException in DelayedRun --- .../at/hannibal2/skyhanni/data/MinecraftData.kt | 2 +- .../java/at/hannibal2/skyhanni/utils/DelayedRun.kt | 25 +++++++++++++++------- 2 files changed, 18 insertions(+), 9 deletions(-) (limited to 'src/main/java') 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 } } } -- cgit