Skip to content

Commit d106d79

Browse files
committed
Fixed bug with not synchronized cooldowns of command and command aliases.
1 parent 99cefea commit d106d79

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed

src/main/kotlin/com/mairwunnx/projectessentialscooldown/CooldownAPI.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.mairwunnx.projectessentialscooldown
22

33
import com.google.common.collect.HashBasedTable
4+
import com.mairwunnx.projectessentialscooldown.essentials.CommandsAliases
45
import java.time.Duration
56
import java.time.ZonedDateTime
67
import kotlin.time.ExperimentalTime
@@ -42,6 +43,16 @@ object CooldownAPI {
4243
*/
4344
fun addCooldown(nickname: String, command: String) {
4445
removeCooldown(nickname, command)
46+
CommandsAliases.aliases.keys.forEach { baseCommand ->
47+
val aliasesOfCommands = CommandsAliases.aliases[baseCommand]
48+
if (aliasesOfCommands != null &&
49+
aliasesOfCommands.contains(command)
50+
) {
51+
aliasesOfCommands.forEach {
52+
cooldownTable.put(nickname, it, ZonedDateTime.now())
53+
}
54+
}
55+
}
4556
cooldownTable.put(nickname, command, ZonedDateTime.now())
4657
}
4758

src/main/kotlin/com/mairwunnx/projectessentialscooldown/CooldownUtils.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import org.apache.logging.log4j.Logger
1212
internal object CooldownUtils {
1313
private val logger: Logger = LogManager.getLogger()
1414

15-
internal fun processCooldownsCommandsList(
15+
private fun processCooldownsCommandsList(
1616
cooldowns: List<String>
1717
): HashMap<String, Int> {
1818
val hashMap = hashMapOf<String, Int>()
@@ -47,12 +47,15 @@ internal object CooldownUtils {
4747
?: CommandsAliases.searchForAliasesForCooldown(
4848
commandName, cooldownsMap
4949
).let {
50+
logger.info("original command: ${it.b} and cooldown: ${it.a}")
5051
originalCommand = it.b
5152
return@let it.a
5253
}
53-
?: cooldownsMap[CooldownAPI.defaultCooldownLiterals[0]]
54+
?: cooldownsMap[CooldownAPI.defaultCooldownLiterals.iterator().next()]
5455
?: CooldownAPI.DEFAULT_COOLDOWN
5556

57+
logger.info("Getting cooldown expired for command: $command when raw command: $commandName")
58+
logger.info("Cooldown for $command when raw command $commandName is $commandCooldown")
5659
if (CooldownAPI.getCooldownIsExpired(commandSenderNickName, command, commandCooldown)) {
5760
CooldownAPI.addCooldown(commandSenderNickName, command)
5861
return false

src/main/kotlin/com/mairwunnx/projectessentialscooldown/essentials/CommandsAliases.kt

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package com.mairwunnx.projectessentialscooldown.essentials
22

33
import com.mairwunnx.projectessentialscore.extensions.empty
44
import net.minecraft.util.Tuple
5+
import org.apache.logging.log4j.LogManager
6+
import org.apache.logging.log4j.Logger
57

68
/**
79
* Command aliases basically for working with
@@ -12,6 +14,7 @@ import net.minecraft.util.Tuple
1214
* @since 1.14.4-1.0.0.0
1315
*/
1416
object CommandsAliases {
17+
private val logger: Logger = LogManager.getLogger()
1518
/**
1619
* This stores all aliases, just add your alias here.
1720
*
@@ -26,18 +29,15 @@ object CommandsAliases {
2629
command: String,
2730
cooldownsMap: HashMap<String, Int>
2831
): Tuple<Int?, String> {
29-
return try {
30-
aliases.keys.forEach { baseCommand ->
31-
val aliasesOfCommands = aliases[baseCommand]
32-
if (aliasesOfCommands != null &&
33-
aliasesOfCommands.contains(command)
34-
) {
35-
Tuple(cooldownsMap[baseCommand]!!, baseCommand)
36-
}
32+
aliases.keys.forEach { baseCommand ->
33+
val aliasesOfCommands = aliases[baseCommand]
34+
if (aliasesOfCommands != null &&
35+
command in aliasesOfCommands
36+
) {
37+
logger.info("Command: $baseCommand; Cooldown: ${cooldownsMap[baseCommand]}")
38+
return Tuple(cooldownsMap[baseCommand], baseCommand)
3739
}
38-
Tuple(null, String.empty)
39-
} catch (ex: KotlinNullPointerException) {
40-
Tuple(null, String.empty)
4140
}
41+
return Tuple(null, String.empty)
4242
}
4343
}

0 commit comments

Comments
 (0)