Created
September 8, 2019 18:41
-
-
Save MairwunNx/cab68ce51055fd5cf115be5b43604b82 to your computer and use it in GitHub Desktop.
Forge cooldown implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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