Skip to content

Commit 02d9bd8

Browse files
committed
Add notification cooldown, close #33
1 parent 6401712 commit 02d9bd8

9 files changed

Lines changed: 58 additions & 2 deletions

File tree

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## 2.6.3
44

55
- Fixed message parsing breaking on translation strings with indexed placeholders
6+
- Added a cooldown option for notifications
67

78
## 2.6.2
89

common/src/main/java/dev/terminalmc/chatnotify/ChatNotify.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ public static void afterClientTick(Minecraft mc) {
8383
ResponseUtil.tickResponses(mc);
8484
TimingUtil.tickActions();
8585

86+
// Cooldowns
87+
for (Notification notif : Config.get().getNotifs()) {
88+
if (notif.countdown > 0)
89+
notif.countdown -= 1;
90+
}
91+
8692
// Config reset warning toast
8793
if (hasResetConfig && mc.screen instanceof TitleScreen) {
8894
hasResetConfig = false;

common/src/main/java/dev/terminalmc/chatnotify/config/Notification.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ public class Notification implements StringSupplier {
5858
*/
5959
public transient boolean editing = false;
6060

61+
/**
62+
* The time, in ticks, remaining until this notification is eligible for triggering.
63+
*/
64+
public transient long countdown = 0;
65+
6166
// Options
6267

6368
/**
@@ -84,6 +89,13 @@ public enum CheckOwnMode {
8489
OFF,
8590
}
8691

92+
/**
93+
* The time, in ticks, for which this instance should be ineligible for activation after being
94+
* activated.
95+
*/
96+
public int cooldown;
97+
public static final int cooldownDefault = 0;
98+
8799
/**
88100
* Whether this instance allows use of inclusion triggers.
89101
*/
@@ -227,6 +239,7 @@ public enum CheckOwnMode {
227239
Notification(
228240
boolean enabled,
229241
CheckOwnMode checkOwnMode,
242+
int cooldown,
230243
boolean inclusionEnabled,
231244
boolean exclusionEnabled,
232245
boolean responseEnabled,
@@ -260,6 +273,7 @@ public enum CheckOwnMode {
260273
) {
261274
this.enabled = enabled;
262275
this.checkOwnMode = checkOwnMode;
276+
this.cooldown = cooldown;
263277
this.inclusionEnabled = inclusionEnabled;
264278
this.exclusionEnabled = exclusionEnabled;
265279
this.responseEnabled = responseEnabled;
@@ -300,6 +314,7 @@ static Notification createUser() {
300314
return new Notification(
301315
enabledDefault,
302316
CheckOwnMode.values()[0],
317+
cooldownDefault,
303318
inclusionEnabledDefault,
304319
exclusionEnabledDefault,
305320
responseEnabledDefault,
@@ -340,6 +355,7 @@ static Notification createBlank(Sound sound, TextStyle textStyle) {
340355
return new Notification(
341356
enabledDefault,
342357
CheckOwnMode.values()[0],
358+
cooldownDefault,
343359
inclusionEnabledDefault,
344360
exclusionEnabledDefault,
345361
responseEnabledDefault,
@@ -378,7 +394,7 @@ static Notification createBlank(Sound sound, TextStyle textStyle) {
378394
* user if {@code ownMsg} is {@code true}).
379395
*/
380396
public boolean canBeTriggered(boolean ownMsg) {
381-
if (enabled && !editing) {
397+
if (enabled && countdown <= 0 && !editing) {
382398
if (ownMsg) {
383399
return switch (checkOwnMode) {
384400
case DEFER -> Config.get().checkOwnMessages;
@@ -546,6 +562,13 @@ public Notification deserialize(
546562
silent
547563
);
548564

565+
int cooldown = JsonUtil.getOrDefault(
566+
obj,
567+
"cooldown",
568+
cooldownDefault,
569+
silent
570+
);
571+
549572
boolean responseEnabled = JsonUtil.getOrDefault(
550573
obj,
551574
"responseEnabled",
@@ -774,6 +797,7 @@ public Notification deserialize(
774797
return new Notification(
775798
enabled,
776799
checkOwnMode,
800+
cooldown,
777801
inclusionEnabled,
778802
exclusionEnabled,
779803
responseEnabled,

common/src/main/java/dev/terminalmc/chatnotify/gui/widget/list/root/notif/MiscOptionList.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public MiscOptionList(
6161
@Override
6262
protected void addEntries() {
6363
Minecraft mc = Minecraft.getInstance();
64+
resetLabelWidth();
6465

6566
addEntry(new OptionList.Entry.Text(
6667
entryX,
@@ -72,6 +73,20 @@ protected void addEntries() {
7273
));
7374
addEntry(new Entry.Controls(entryX, entryWidth, entryHeight, notif));
7475

76+
registerLabel(localized("options", "notif.misc.control.cooldown"));
77+
addEntry(new Entry.CustomLabeledMessageReset(
78+
dynWideEntryX,
79+
dynWideEntryWidth,
80+
entryHeight,
81+
() -> notif.cooldown,
82+
(s) -> notif.cooldown = Integer.parseInt(s.strip()),
83+
Notification.cooldownDefault,
84+
localized("option", "notif.misc.control.cooldown"),
85+
localized("option", "notif.misc.control.cooldown")
86+
.append(".\n")
87+
.append(localized("option", "notif.misc.control.cooldown.tooltip"))
88+
));
89+
7590
addEntry(new OptionList.Entry.Text(
7691
entryX,
7792
entryWidth,
@@ -189,7 +204,6 @@ protected void addEntries() {
189204
));
190205

191206
// register all label texts, so that they are properly aligned
192-
resetLabelWidth();
193207
registerLabel(localized("options", "notif.misc.timing.delay"));
194208
registerLabel(localized("option", "notif.misc.timing.title_fade_in"));
195209
registerLabel(localized("option", "notif.misc.timing.title_stay"));

common/src/main/java/dev/terminalmc/chatnotify/util/text/MessageUtil.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,9 @@ private static String checkOwner(String cleanStr) {
304304
// Trigger notification
305305
anyTriggered = true;
306306

307+
// Start countdown
308+
notif.countdown = notif.cooldown;
309+
307310
// Play sound
308311
if (!anySoundPlayed || Config.get().notifMode.equals(Config.NotifMode.ALL)) {
309312
anySoundPlayed = playSound(notif);

common/src/main/resources/assets/chatnotify/lang/en_us.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@
112112
"option.chatnotify.notif.list.tooltip": "A notification will be activated when an incoming chat message matches one of the notification's triggers.",
113113
"option.chatnotify.notif.misc": "Misc",
114114
"option.chatnotify.notif.misc.control": "Misc Controls",
115+
"option.chatnotify.notif.misc.control.cooldown": "Notification cooldown",
116+
"option.chatnotify.notif.misc.control.cooldown.tooltip": "Time in ticks the notification should be disabled after being activated.",
115117
"option.chatnotify.notif.misc.control.self_notify.status.DEFER": "Defer",
116118
"option.chatnotify.notif.misc.control.self_notify.status.DEFER.tooltip": "Global option will be used.",
117119
"option.chatnotify.notif.misc.control.self_notify.status.OFF": "Override - OFF",

common/src/main/resources/assets/chatnotify/lang/ru_ru.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@
112112
"option.chatnotify.notif.list.tooltip": "Уведомление будет активировано, если входящее сообщение в чате соответствует одному из триггеров уведомления.",
113113
"option.chatnotify.notif.misc": "Дополнительные параметры",
114114
"option.chatnotify.notif.misc.control": "Дополнительные элементы управления",
115+
"option.chatnotify.notif.misc.control.cooldown": "Время между уведомлениями",
116+
"option.chatnotify.notif.misc.control.cooldown.tooltip": "Продолжительность (в тиках) между последовательными активациями этого уведомления.",
115117
"option.chatnotify.notif.misc.control.self_notify.status.DEFER": "Отложить",
116118
"option.chatnotify.notif.misc.control.self_notify.status.DEFER.tooltip": "Будет использована глобальная настройка.",
117119
"option.chatnotify.notif.misc.control.self_notify.status.OFF": "Переопределить - ВЫКЛ",

common/src/main/resources/assets/chatnotify/lang/zh_cn.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@
112112
"option.chatnotify.notif.list.tooltip": "传入消息将激活第一个启用且与触发器匹配的通知",
113113
"option.chatnotify.notif.misc": "高级选项",
114114
"option.chatnotify.notif.misc.control": "高级控制",
115+
"option.chatnotify.notif.misc.control.cooldown": "Notification cooldown",
116+
"option.chatnotify.notif.misc.control.cooldown.tooltip": "Time in ticks the notification should be disabled after being activated.",
115117
"option.chatnotify.notif.misc.control.self_notify.status.DEFER": "推迟",
116118
"option.chatnotify.notif.misc.control.self_notify.status.DEFER.tooltip": "将使用全局选项",
117119
"option.chatnotify.notif.misc.control.self_notify.status.OFF": "覆盖 - 关闭",

common/src/main/resources/assets/chatnotify/lang/zh_tw.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@
112112
"option.chatnotify.notif.list.tooltip": "傳入訊息將會啟動第一個啟用且觸發條件相符的通知。",
113113
"option.chatnotify.notif.misc": "進階選項",
114114
"option.chatnotify.notif.misc.control": "Advanced Controls",
115+
"option.chatnotify.notif.misc.control.cooldown": "Notification cooldown",
116+
"option.chatnotify.notif.misc.control.cooldown.tooltip": "Time in ticks the notification should be disabled after being activated.",
115117
"option.chatnotify.notif.misc.control.self_notify.status.DEFER": "Defer",
116118
"option.chatnotify.notif.misc.control.self_notify.status.DEFER.tooltip": "Global option will be used.",
117119
"option.chatnotify.notif.misc.control.self_notify.status.OFF": "Override - OFF",

0 commit comments

Comments
 (0)