Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. package com.mairwunnx.projectessentials.cooldowns
  2.  
  3. import com.google.common.collect.HashBasedTable
  4. import java.time.Duration
  5. import java.time.ZonedDateTime
  6. import kotlin.time.ExperimentalTime
  7. import kotlin.time.toKotlinDuration
  8.  
  9. class CooldownBase {
  10. val cooldownTable: HashBasedTable<String, String, ZonedDateTime> = HashBasedTable.create()
  11.  
  12. fun addCooldown(nickname: String, command: String) {
  13. if (cooldownTable.get(nickname, command) != null) {
  14. cooldownTable.remove(nickname, command)
  15. }
  16. cooldownTable.put(nickname, command, ZonedDateTime.now())
  17. }
  18.  
  19. @UseExperimental(ExperimentalTime::class)
  20. fun getCooldown(nickname: String, command: String): Double {
  21. if (cooldownTable.get(nickname, command) != null) {
  22. val commandExecutionTime = cooldownTable.get(nickname, command)
  23. val dateTimeNow: ZonedDateTime = ZonedDateTime.now()
  24. val duration = Duration.between(commandExecutionTime, dateTimeNow)
  25. return duration.toKotlinDuration().inSeconds
  26. }
  27. throw KotlinNullPointerException(
  28. "An error occurred while getting cooldown date time by nickname ($nickname) with command ($command)"
  29. )
  30. }
  31.  
  32. fun removeCooldown(nickname: String, command: String) {
  33. if (cooldownTable.get(nickname, command) == null) return
  34. cooldownTable.remove(nickname, command)
  35. }
  36.  
  37. fun getCooldownIsExpired(
  38. nickname: String,
  39. command: String,
  40. minSecondsDuration: Int
  41. ): Boolean = getCooldown(nickname, command) > minSecondsDuration
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement