com.skillw.pouvoir.util.RunUtil.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of Pouvoir Show documentation
Show all versions of Pouvoir Show documentation
Bukkit Script Engine Plugin.
The newest version!
package com.skillw.pouvoir.util
import taboolib.common.platform.function.isPrimaryThread
import taboolib.common.util.sync
import kotlin.time.Duration
import kotlin.time.ExperimentalTime
import kotlin.time.TimeSource
/**
* @className Runnable
*
* @author Glom
* @date 2023/1/7 22:18 Copyright 2024 Glom.
*/
fun safe(run: () -> T): T? {
return runCatching { run() }.run {
if (isSuccess) getOrNull()
else {
exceptionOrNull()?.printStackTrace()
null
}
}
}
fun silent(run: () -> T): T? {
return runCatching { run() }.getOrNull()
}
fun syncRun(run: () -> T): T = if (!isPrimaryThread) sync { run() } else run()
@OptIn(ExperimentalTime::class)
fun time(times: Int = 1, exec: () -> Unit): Duration {
val mark = TimeSource.Monotonic.markNow()
repeat(times) {
exec()
}
return mark.elapsedNow()
}
@OptIn(ExperimentalTime::class)
fun time(exec: () -> R): Pair {
val mark = TimeSource.Monotonic.markNow()
return exec() to mark.elapsedNow()
}
@OptIn(ExperimentalTime::class)
fun timeSafe(exec: () -> R): Pair {
val mark = TimeSource.Monotonic.markNow()
return safe { exec() } to mark.elapsedNow()
}
@OptIn(ExperimentalTime::class)
fun timeSafeSilent(exec: () -> R): Pair {
val mark = TimeSource.Monotonic.markNow()
return silent { exec() } to mark.elapsedNow()
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy