All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.chestmc.common.time.Cooldown.kt Maven / Gradle / Ivy

The newest version!
package net.chestmc.common.time

import net.chestmc.common.extensions.currentMillis
import net.chestmc.common.extensions.numbers.toDate
import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
import java.util.*
import java.util.concurrent.TimeUnit

/**
 * A cooldown class represents a duration that can be expired.
 */
@Serializable
data class Cooldown(val duration: Long, @Transient val unit: TimeUnit = TimeUnit.MILLISECONDS) : StartedTime {
  override var startMillis: Long = currentMillis
}

/**
 * Returns the time left to expires this cooldown.
 */
val Cooldown.expiresIn: Long
  get() = startMillis + unit.toMillis(duration) / currentMillis

/**
 * Returns the date to expires this cooldown.
 */
inline val Cooldown.expiresDate: Date
  get() = expiresIn.toDate()

/**
 * Verifies if this cooldown has expired.
 */
inline val Cooldown.isExpired: Boolean
  get() = expiresIn <= 0




© 2015 - 2025 Weber Informatics LLC | Privacy Policy