Skip to content

Commit

Permalink
Backend: Use on DelayedRun to add scheduled tasks (hannibal002#2462)
Browse files Browse the repository at this point in the history
  • Loading branch information
CalMWolfs authored and MTOnline69 committed Sep 10, 2024
1 parent 8094356 commit 5dc023f
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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++
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/at/hannibal2/skyhanni/data/repo/RepoManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -169,7 +169,7 @@ class RepoManager(private val configLocation: File) {
val comp = CompletableFuture<Void?>()
if (!atomicShouldManuallyReload.get()) return comp
ErrorManager.resetCache()
Minecraft.getMinecraft().addScheduledTask {
DelayedRun.onThread.execute {
error = false
successfulConstants.clear()
unsuccessfulConstants.clear()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -113,7 +113,7 @@ object UpdateManager {
} else if (forceDownload) {
ChatUtils.chat("§aSkyHanni didn't find a new update.")
}
}, MinecraftExecutor.OnThread)
}, DelayedRun.onThread)
}

fun queueUpdate() {
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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) {
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/at/hannibal2/skyhanni/utils/DelayedRun.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}
}
6 changes: 3 additions & 3 deletions src/main/java/at/hannibal2/skyhanni/utils/SoundUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -33,7 +33,7 @@ object SoundUtils {
e.message?.let {
if (it.startsWith("value already present:")) {
println("SkyHanni Sound error: $it")
return@addScheduledTask
return@execute
}
}
}
Expand Down

0 comments on commit 5dc023f

Please sign in to comment.