Skip to content

Instantly share code, notes, and snippets.

@MairwunNx
Created September 8, 2019 18:41
Show Gist options
  • Save MairwunNx/cab68ce51055fd5cf115be5b43604b82 to your computer and use it in GitHub Desktop.
Save MairwunNx/cab68ce51055fd5cf115be5b43604b82 to your computer and use it in GitHub Desktop.
Forge cooldown implementation
package com.mairwunnx.projectessentials.cooldowns
import com.google.common.collect.HashBasedTable
import java.time.Duration
import java.time.ZonedDateTime
import kotlin.time.ExperimentalTime
import kotlin.time.toKotlinDuration
class CooldownBase {
val cooldownTable: HashBasedTable<String, String, ZonedDateTime> = HashBasedTable.create()
fun addCooldown(nickname: String, command: String) {
if (cooldownTable.get(nickname, command) != null) {
cooldownTable.remove(nickname, command)
}
cooldownTable.put(nickname, command, ZonedDateTime.now())
}
@UseExperimental(ExperimentalTime::class)
fun getCooldown(nickname: String, command: String): Double {
if (cooldownTable.get(nickname, command) != null) {
val commandExecutionTime = cooldownTable.get(nickname, command)
val dateTimeNow: ZonedDateTime = ZonedDateTime.now()
val duration = Duration.between(commandExecutionTime, dateTimeNow)
return duration.toKotlinDuration().inSeconds
}
throw KotlinNullPointerException(
"An error occurred while getting cooldown date time by nickname ($nickname) with command ($command)"
)
}
fun removeCooldown(nickname: String, command: String) {
if (cooldownTable.get(nickname, command) == null) return
cooldownTable.remove(nickname, command)
}
fun getCooldownIsExpired(
nickname: String,
command: String,
minSecondsDuration: Int
): Boolean = getCooldown(nickname, command) > minSecondsDuration
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment