Skip to content

Commit

Permalink
Migrate TickTask to use new events api
Browse files Browse the repository at this point in the history
  • Loading branch information
Sychic committed Nov 8, 2023
1 parent 7de7892 commit 7a74140
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
8 changes: 8 additions & 0 deletions events/src/main/kotlin/gg/skytils/event/Events.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,12 @@ object Events {

suspend inline fun <reified T : Event> on(noinline block: suspend (T) -> Unit) =
events.filterIsInstance<T>().onEach(block).launchIn(CoroutineScope(currentCoroutineContext()))

suspend inline fun <reified T : Event> await() =
events.filterIsInstance<T>().first()

suspend inline fun <reified T : Event> await(repetitions: Int) =
repeat(repetitions) {
await<T>()
}
}
3 changes: 2 additions & 1 deletion src/main/kotlin/gg/skytils/skytilsmod/Skytils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package gg.skytils.skytilsmod

import gg.essential.universal.UChat
import gg.essential.universal.UKeyboard
import gg.skytils.event.Events
import gg.skytils.skytilsmod.commands.impl.*
import gg.skytils.skytilsmod.commands.stats.impl.CataCommand
import gg.skytils.skytilsmod.commands.stats.impl.SlayerCommand
Expand Down Expand Up @@ -440,7 +441,7 @@ class Skytils {
}

init {
TickTask(20, repeats = true) {
tickTimer(20) {
if (mc.thePlayer != null) {
if (deobfEnvironment) {
if (DevTools.toggles.getOrDefault("forcehypixel", false)) Utils.isOnHypixel = true
Expand Down
37 changes: 37 additions & 0 deletions src/main/kotlin/gg/skytils/skytilsmod/core/tick.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Skytils - Hypixel Skyblock Quality of Life Mod
* Copyright (C) 2020-2023 Skytils
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package gg.skytils.skytilsmod.core

import gg.skytils.event.Events
import gg.skytils.event.impl.TickEvent
import kotlinx.coroutines.*

object Tick : CoroutineScope {
@OptIn(DelicateCoroutinesApi::class)
val dispatcher = newFixedThreadPoolContext(5, "Skytils Tick")
override val coroutineContext = dispatcher + SupervisorJob()
}

fun tickTimer(ticks: Int, task: () -> Unit) =
Tick.launch {
while (true) {
Events.await<TickEvent>(ticks)
task()
}
}

0 comments on commit 7a74140

Please sign in to comment.