From d09480c3d272f270fb1b45c51bc3540b5416c09a Mon Sep 17 00:00:00 2001 From: CalMWolfs <94038482+CalMWolfs@users.noreply.github.com> Date: Sun, 8 Sep 2024 07:03:51 +1000 Subject: Backend: Use on DelayedRun to add scheduled tasks (#2462) --- .../java/at/hannibal2/skyhanni/data/FixedRateTimerManager.kt | 6 +++--- src/main/java/at/hannibal2/skyhanni/data/repo/RepoManager.kt | 4 ++-- .../hannibal2/skyhanni/features/misc/update/UpdateManager.kt | 6 +++--- .../at/hannibal2/skyhanni/test/hotswap/HotswapSupportImpl.kt | 4 ++-- src/main/java/at/hannibal2/skyhanni/utils/DelayedRun.kt | 12 ++++++++++++ src/main/java/at/hannibal2/skyhanni/utils/SoundUtils.kt | 6 +++--- 6 files changed, 25 insertions(+), 13 deletions(-) (limited to 'src/main') diff --git a/src/main/java/at/hannibal2/skyhanni/data/FixedRateTimerManager.kt b/src/main/java/at/hannibal2/skyhanni/data/FixedRateTimerManager.kt index 4d825b2e9..36dc3c6f0 100644 --- a/src/main/java/at/hannibal2/skyhanni/data/FixedRateTimerManager.kt +++ b/src/main/java/at/hannibal2/skyhanni/data/FixedRateTimerManager.kt @@ -2,8 +2,8 @@ package at.hannibal2.skyhanni.data import at.hannibal2.skyhanni.events.SecondPassedEvent import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule +import at.hannibal2.skyhanni.utils.DelayedRun import at.hannibal2.skyhanni.utils.LorenzUtils -import net.minecraft.client.Minecraft import kotlin.concurrent.fixedRateTimer @SkyHanniModule @@ -12,8 +12,8 @@ object FixedRateTimerManager { init { fixedRateTimer(name = "skyhanni-fixed-rate-timer-manager", period = 1000L) { - Minecraft.getMinecraft().addScheduledTask { - if (!LorenzUtils.onHypixel) return@addScheduledTask + DelayedRun.onThread.execute { + if (!LorenzUtils.onHypixel) return@execute SecondPassedEvent(totalSeconds).postAndCatch() totalSeconds++ } diff --git a/src/main/java/at/hannibal2/skyhanni/data/repo/RepoManager.kt b/src/main/java/at/hannibal2/skyhanni/data/repo/RepoManager.kt index 79d329f22..551f44699 100644 --- a/src/main/java/at/hannibal2/skyhanni/data/repo/RepoManager.kt +++ b/src/main/java/at/hannibal2/skyhanni/data/repo/RepoManager.kt @@ -7,12 +7,12 @@ import at.hannibal2.skyhanni.events.NeuRepositoryReloadEvent import at.hannibal2.skyhanni.events.RepositoryReloadEvent import at.hannibal2.skyhanni.test.command.ErrorManager import at.hannibal2.skyhanni.utils.ChatUtils +import at.hannibal2.skyhanni.utils.DelayedRun import at.hannibal2.skyhanni.utils.SimpleTimeMark import at.hannibal2.skyhanni.utils.chat.Text import at.hannibal2.skyhanni.utils.chat.Text.asComponent import at.hannibal2.skyhanni.utils.chat.Text.send import com.google.gson.JsonObject -import net.minecraft.client.Minecraft import net.minecraft.util.IChatComponent import net.minecraftforge.fml.common.eventhandler.SubscribeEvent import org.apache.commons.io.FileUtils @@ -169,7 +169,7 @@ class RepoManager(private val configLocation: File) { val comp = CompletableFuture() if (!atomicShouldManuallyReload.get()) return comp ErrorManager.resetCache() - Minecraft.getMinecraft().addScheduledTask { + DelayedRun.onThread.execute { error = false successfulConstants.clear() unsuccessfulConstants.clear() diff --git a/src/main/java/at/hannibal2/skyhanni/features/misc/update/UpdateManager.kt b/src/main/java/at/hannibal2/skyhanni/features/misc/update/UpdateManager.kt index fae931ff2..040b6741a 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/misc/update/UpdateManager.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/misc/update/UpdateManager.kt @@ -7,10 +7,10 @@ import at.hannibal2.skyhanni.events.LorenzTickEvent import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule import at.hannibal2.skyhanni.utils.ChatUtils import at.hannibal2.skyhanni.utils.ConditionalUtils.onToggle +import at.hannibal2.skyhanni.utils.DelayedRun import at.hannibal2.skyhanni.utils.LorenzLogger import com.google.gson.JsonElement import io.github.moulberry.notenoughupdates.util.ApiUtil -import io.github.moulberry.notenoughupdates.util.MinecraftExecutor import io.github.notenoughupdates.moulconfig.observer.Property import io.github.notenoughupdates.moulconfig.processor.MoulConfigProcessor import moe.nea.libautoupdate.CurrentVersion @@ -113,7 +113,7 @@ object UpdateManager { } else if (forceDownload) { ChatUtils.chat("§aSkyHanni didn't find a new update.") } - }, MinecraftExecutor.OnThread) + }, DelayedRun.onThread) } fun queueUpdate() { @@ -130,7 +130,7 @@ object UpdateManager { potentialUpdate!!.executePreparedUpdate() ChatUtils.chat("Download of update complete. ") ChatUtils.chat("§aThe update will be installed after your next restart.") - }, MinecraftExecutor.OnThread) + }, DelayedRun.onThread) } private val context = UpdateContext( diff --git a/src/main/java/at/hannibal2/skyhanni/test/hotswap/HotswapSupportImpl.kt b/src/main/java/at/hannibal2/skyhanni/test/hotswap/HotswapSupportImpl.kt index a42a10faf..6e497efca 100644 --- a/src/main/java/at/hannibal2/skyhanni/test/hotswap/HotswapSupportImpl.kt +++ b/src/main/java/at/hannibal2/skyhanni/test/hotswap/HotswapSupportImpl.kt @@ -2,12 +2,12 @@ package at.hannibal2.skyhanni.test.hotswap import at.hannibal2.skyhanni.SkyHanniMod import at.hannibal2.skyhanni.utils.ChatUtils +import at.hannibal2.skyhanni.utils.DelayedRun import at.hannibal2.skyhanni.utils.ReflectionUtils.makeAccessible import at.hannibal2.skyhanni.utils.ReflectionUtils.removeFinal import moe.nea.hotswapagentforge.forge.ClassDefinitionEvent import moe.nea.hotswapagentforge.forge.HotswapEvent import moe.nea.hotswapagentforge.forge.HotswapFinishedEvent -import net.minecraft.client.Minecraft import net.minecraftforge.common.MinecraftForge import net.minecraftforge.fml.common.eventhandler.SubscribeEvent @@ -22,7 +22,7 @@ class HotswapSupportImpl : HotswapSupportHandle { fun onHotswapClass(event: ClassDefinitionEvent.Redefinition) { val instance = SkyHanniMod.modules.find { it.javaClass.name == event.fullyQualifiedName } ?: return val primaryConstructor = runCatching { instance.javaClass.getDeclaredConstructor() }.getOrNull() - Minecraft.getMinecraft().addScheduledTask { + DelayedRun.onThread.execute { ChatUtils.chat("Refreshing event subscriptions for module $instance!") MinecraftForge.EVENT_BUS.unregister(instance) if (primaryConstructor == null) { diff --git a/src/main/java/at/hannibal2/skyhanni/utils/DelayedRun.kt b/src/main/java/at/hannibal2/skyhanni/utils/DelayedRun.kt index eb83bce3a..4642124b7 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/DelayedRun.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/DelayedRun.kt @@ -2,7 +2,9 @@ package at.hannibal2.skyhanni.utils import at.hannibal2.skyhanni.test.command.ErrorManager import at.hannibal2.skyhanni.utils.CollectionUtils.drainTo +import net.minecraft.client.Minecraft import java.util.concurrent.ConcurrentLinkedQueue +import java.util.concurrent.Executor import kotlin.time.Duration object DelayedRun { @@ -35,4 +37,14 @@ object DelayedRun { } futureTasks.drainTo(tasks) } + + @JvmField + val onThread = Executor { + val mc = Minecraft.getMinecraft() + if (mc.isCallingFromMinecraftThread) { + it.run() + } else { + Minecraft.getMinecraft().addScheduledTask(it) + } + } } diff --git a/src/main/java/at/hannibal2/skyhanni/utils/SoundUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/SoundUtils.kt index 79e320949..f001cfadb 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/SoundUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/SoundUtils.kt @@ -19,8 +19,8 @@ object SoundUtils { val centuryActiveTimerAlert by lazy { createSound("skyhanni:centurytimer.active", 1f) } fun ISound.playSound() { - Minecraft.getMinecraft().addScheduledTask { - if (Minecraft.getMinecraft().soundHandler.isSoundPlaying(this)) return@addScheduledTask + DelayedRun.onThread.execute { + if (Minecraft.getMinecraft().soundHandler.isSoundPlaying(this)) return@execute val gameSettings = Minecraft.getMinecraft().gameSettings val oldLevel = gameSettings.getSoundLevel(SoundCategory.PLAYERS) if (!SkyHanniMod.feature.misc.maintainGameVolume) { @@ -33,7 +33,7 @@ object SoundUtils { e.message?.let { if (it.startsWith("value already present:")) { println("SkyHanni Sound error: $it") - return@addScheduledTask + return@execute } } } -- cgit