Skip to content

Commit 7d1b07b

Browse files
authored
GH-913 Fix chat delay is not save to configuration. (#914)
* Fix chat delay is not save to configuration. * Remove useless (if equals 0s) because of new commons update. * Fix.
1 parent 67101c8 commit 7d1b07b

File tree

4 files changed

+34
-23
lines changed

4 files changed

+34
-23
lines changed

buildSrc/src/main/kotlin/Versions.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ object Versions {
33
const val SPIGOT_API = "1.21.3-R0.1-SNAPSHOT"
44
const val PAPER_API = "1.21.3-R0.1-SNAPSHOT"
55

6-
const val ETERNALCODE_COMMONS = "1.1.5"
6+
const val ETERNALCODE_COMMONS = "1.1.6"
77
const val MULTIFICATION = "1.2.1"
88

99
const val JETBRAINS_ANNOTATIONS = "26.0.2"
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package com.eternalcode.core.configuration.composer;
22

3-
import dev.rollczi.litecommands.time.DurationParser;
4-
import panda.std.Result;
5-
3+
import com.eternalcode.commons.time.DurationParser;
64
import java.time.Duration;
75
import java.time.format.DateTimeParseException;
8-
import java.util.Locale;
6+
import panda.std.Result;
97

108
public class DurationComposer implements SimpleComposer<Duration> {
119

@@ -18,5 +16,4 @@ public Result<Duration, Exception> deserialize(String source) {
1816
public Result<String, Exception> serialize(Duration entity) {
1917
return Result.ok(DurationParser.TIME_UNITS.format(entity));
2018
}
21-
2219
}

eternalcore-core/src/main/java/com/eternalcode/core/feature/chat/ChatCommand.java

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.eternalcode.core.feature.chat;
22

33
import com.eternalcode.annotations.scan.command.DescriptionDocs;
4+
import com.eternalcode.commons.scheduler.Scheduler;
5+
import com.eternalcode.core.configuration.ConfigurationManager;
6+
import com.eternalcode.core.configuration.implementation.PluginConfiguration;
47
import com.eternalcode.core.event.EventCaller;
58
import com.eternalcode.core.feature.chat.event.ClearChatEvent;
69
import com.eternalcode.core.feature.chat.event.DisableChatEvent;
@@ -28,19 +31,33 @@ class ChatCommand {
2831
private final ChatSettings chatSettings;
2932
private final EventCaller eventCaller;
3033

34+
private final PluginConfiguration config;
35+
private final ConfigurationManager configManager;
36+
private final Scheduler scheduler;
37+
3138
private final Supplier<Notice> clear;
3239

3340
@Inject
3441
ChatCommand(
3542
NoticeService noticeService,
3643
ChatSettings chatSettings,
37-
EventCaller eventCaller
44+
EventCaller eventCaller,
45+
PluginConfiguration config,
46+
ConfigurationManager configManager, Scheduler scheduler
3847
) {
3948
this.noticeService = noticeService;
4049
this.chatSettings = chatSettings;
50+
this.eventCaller = eventCaller;
51+
52+
this.config = config;
53+
this.configManager = configManager;
54+
this.scheduler = scheduler;
4155

4256
this.clear = create(chatSettings);
43-
this.eventCaller = eventCaller;
57+
}
58+
59+
private static Supplier<Notice> create(ChatSettings settings) {
60+
return () -> Notice.chat("<newline>".repeat(Math.max(0, settings.linesToClear())));
4461
}
4562

4663
@Execute(name = "clear", aliases = "cc")
@@ -111,30 +128,30 @@ void disable(@Context Viewer viewer, @Context CommandSender sender) {
111128
void slowmode(@Context Viewer viewer, @Arg Duration duration) {
112129
if (duration.isNegative()) {
113130
this.noticeService.viewer(viewer, translation -> translation.argument().numberBiggerThanOrEqualZero());
131+
return;
132+
}
114133

134+
Duration currentDelay = this.chatSettings.getChatDelay();
135+
EditSlowModeEvent event =
136+
this.eventCaller.callEvent(new EditSlowModeEvent(currentDelay, duration, viewer.getUniqueId()));
137+
138+
if (event.isCancelled()) {
115139
return;
116140
}
117141

142+
this.chatSettings.setChatDelay(duration);
143+
this.scheduler.runAsync(() -> this.configManager.save(this.config));
144+
118145
if (duration.isZero()) {
119146
this.noticeService.create()
120147
.notice(translation -> translation.chat().slowModeOff())
121148
.placeholder("{PLAYER}", viewer.getName())
122149
.onlinePlayers()
123150
.send();
124151

125-
this.chatSettings.setChatDelay(duration);
126-
return;
127-
}
128-
129-
Duration chatDelay = this.chatSettings.getChatDelay();
130-
EditSlowModeEvent event = this.eventCaller.callEvent(new EditSlowModeEvent(chatDelay, duration, viewer.getUniqueId()));
131-
132-
if (event.isCancelled()) {
133152
return;
134153
}
135154

136-
this.chatSettings.setChatDelay(duration);
137-
138155
this.noticeService.create()
139156
.notice(translation -> translation.chat().slowModeSet())
140157
.placeholder("{SLOWMODE}", DurationUtil.format(duration, true))
@@ -148,9 +165,5 @@ void slowmodeOff(@Context Viewer viewer) {
148165
Duration noSlowMode = Duration.ZERO;
149166
this.slowmode(viewer, noSlowMode);
150167
}
151-
152-
private static Supplier<Notice> create(ChatSettings settings) {
153-
return () -> Notice.chat("<newline>".repeat(Math.max(0, settings.linesToClear())));
154-
}
155168
}
156169

eternalcore-core/src/main/java/com/eternalcode/core/util/DurationUtil.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.eternalcode.commons.time.DurationParser;
44
import com.eternalcode.commons.time.TemporalAmountParser;
5+
import java.math.RoundingMode;
56
import java.time.Duration;
67
import java.time.temporal.ChronoUnit;
78

@@ -12,7 +13,7 @@ public class DurationUtil {
1213
.withUnit("m", ChronoUnit.MINUTES)
1314
.withUnit("h", ChronoUnit.HOURS)
1415
.withUnit("d", ChronoUnit.DAYS)
15-
.roundOff(ChronoUnit.MILLIS);
16+
.withRounded(ChronoUnit.MILLIS, RoundingMode.UP);
1617

1718
private static final TemporalAmountParser<Duration> STANDARD_FORMAT = new DurationParser()
1819
.withUnit("d", ChronoUnit.DAYS)

0 commit comments

Comments
 (0)