Skip to content

Commit b6bbbae

Browse files
committed
Implemented chat delay.
Signed-off-by: Pavel Erokhin (MairwunNx) <MairwunNx@gmail.com>
1 parent 14aaad1 commit b6bbbae

File tree

5 files changed

+63
-3
lines changed

5 files changed

+63
-3
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.mairwunnx.projectessentials.chat
2+
3+
import java.time.Duration
4+
import java.time.ZonedDateTime
5+
import kotlin.time.ExperimentalTime
6+
import kotlin.time.toKotlinDuration
7+
8+
object ChatCooldown {
9+
private const val DEFAULT_COOLDOWN = 5
10+
private val cooldownMap = hashMapOf<String, ZonedDateTime>()
11+
12+
fun addCooldown(nickname: String) {
13+
removeCooldown(nickname)
14+
cooldownMap[nickname] = ZonedDateTime.now()
15+
}
16+
17+
fun removeCooldown(nickname: String) {
18+
if (cooldownMap[nickname] == null) return
19+
cooldownMap.remove(nickname)
20+
}
21+
22+
fun getCooldownIsExpired(
23+
nickname: String,
24+
minSecondsDuration: Int = DEFAULT_COOLDOWN
25+
): Boolean = getCooldownTimeLeft(nickname) >= minSecondsDuration
26+
27+
@UseExperimental(ExperimentalTime::class)
28+
fun getCooldownTimeLeft(nickname: String): Double {
29+
if (cooldownMap[nickname] != null) {
30+
val commandExecutionTime = cooldownMap[nickname]
31+
val dateTimeNow = ZonedDateTime.now()
32+
val duration = Duration.between(commandExecutionTime, dateTimeNow)
33+
return duration.toKotlinDuration().inSeconds
34+
}
35+
throw KotlinNullPointerException(
36+
"An error occurred while getting chat cooldown date time by nickname ($nickname)"
37+
)
38+
}
39+
}

src/main/kotlin/com/mairwunnx/projectessentials/chat/EntryPoint.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,24 @@ class EntryPoint : EssBase() {
7777
return
7878
}
7979

80+
if (!hasPermission(event.player, "ess.chat.chatdelay.bypass", 3)) {
81+
if (ChatCooldown.getCooldownIsExpired(
82+
event.player.name.string,
83+
ChatModelUtils.chatModel.moderation.messagingCooldown
84+
)
85+
) {
86+
ChatCooldown.addCooldown(event.player.name.string)
87+
} else {
88+
sendMsg(
89+
"chat",
90+
event.player.commandSource,
91+
"chat.cooldown_not_expired"
92+
)
93+
event.isCanceled = true
94+
return
95+
}
96+
}
97+
8098
if (event.message.contains("&")) {
8199
if (hasPermission(event.player, "ess.chat.color", 2)) {
82100
event.component = TextComponentUtils.toTextComponent {

src/main/kotlin/com/mairwunnx/projectessentials/chat/models/ChatModel.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ data class ChatModel(
1414
var blockedChars: List<String> = mutableListOf(),
1515
var modifyBlockedWords: Boolean = true,
1616
var blockedWordsMask: String = "**beep**",
17-
var maxMessageLength: Int = 128
17+
var maxMessageLength: Int = 128,
18+
var messagingCooldown: Int = 5
1819
)
1920

2021
@Serializable

src/main/resources/assets/projectessentialschat/lang/en_us.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
"project_essentials_chat.chat.blocked_word": "§cMessage not sent, your message contains blocked word: \"§7%s\".",
66
"project_essentials_chat.chat.blocked_char": "§cMessage not sent, your message contains blocked symbol.",
77
"project_essentials_chat.chat.message_maxlength": "§cMessage not sent, your message length exceeds maximum allowed length.",
8-
"project_essentials_chat.chat.mention_all_aborted": "§cMention not sent to all players, you §7don't have permission§c to mention all players."
8+
"project_essentials_chat.chat.mention_all_aborted": "§cMention not sent to all players, you §7don't have permission§c to mention all players.",
9+
"project_essentials_chat.chat.cooldown_not_expired": "§cMessage not sent, wait chat cooldown after your previous message."
910
}

src/main/resources/assets/projectessentialschat/lang/ru_ru.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
"project_essentials_chat.chat.blocked_word": "§cСообщение не отправлено, ваше сообщение содержит заблокированное слово: \"§7%s\".",
66
"project_essentials_chat.chat.blocked_char": "§cСообщение не отправлено, ваше сообщение содержит заблокированный символ.",
77
"project_essentials_chat.chat.message_maxlength": "§cСообщение не отправлено, ваше сообщение превышает максимально допустимую длину.",
8-
"project_essentials_chat.chat.mention_all_aborted": "§cОповещение не было отправлено всем игрокам, у вас §7нет прав§c на оповещение всех игроков."
8+
"project_essentials_chat.chat.mention_all_aborted": "§cОповещение не было отправлено всем игрокам, у вас §7нет прав§c на оповещение всех игроков.",
9+
"project_essentials_chat.chat.cooldown_not_expired": "§cСообщение не отправлено, дождитесь окончания задержки в чате после предыдущего Вашего сообщения."
910
}

0 commit comments

Comments
 (0)