Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement