Skip to content

Commit

Permalink
GH-913 Fix chat delay is not save to configuration. (#914)
Browse files Browse the repository at this point in the history
* Fix chat delay is not save to configuration.

* Remove useless (if equals 0s) because of new commons update.

* Fix.
  • Loading branch information
vLuckyyy authored Feb 14, 2025
1 parent 67101c8 commit 7d1b07b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 23 deletions.
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ object Versions {
const val SPIGOT_API = "1.21.3-R0.1-SNAPSHOT"
const val PAPER_API = "1.21.3-R0.1-SNAPSHOT"

const val ETERNALCODE_COMMONS = "1.1.5"
const val ETERNALCODE_COMMONS = "1.1.6"
const val MULTIFICATION = "1.2.1"

const val JETBRAINS_ANNOTATIONS = "26.0.2"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package com.eternalcode.core.configuration.composer;

import dev.rollczi.litecommands.time.DurationParser;
import panda.std.Result;

import com.eternalcode.commons.time.DurationParser;
import java.time.Duration;
import java.time.format.DateTimeParseException;
import java.util.Locale;
import panda.std.Result;

public class DurationComposer implements SimpleComposer<Duration> {

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

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.eternalcode.core.feature.chat;

import com.eternalcode.annotations.scan.command.DescriptionDocs;
import com.eternalcode.commons.scheduler.Scheduler;
import com.eternalcode.core.configuration.ConfigurationManager;
import com.eternalcode.core.configuration.implementation.PluginConfiguration;
import com.eternalcode.core.event.EventCaller;
import com.eternalcode.core.feature.chat.event.ClearChatEvent;
import com.eternalcode.core.feature.chat.event.DisableChatEvent;
Expand Down Expand Up @@ -28,19 +31,33 @@ class ChatCommand {
private final ChatSettings chatSettings;
private final EventCaller eventCaller;

private final PluginConfiguration config;
private final ConfigurationManager configManager;
private final Scheduler scheduler;

private final Supplier<Notice> clear;

@Inject
ChatCommand(
NoticeService noticeService,
ChatSettings chatSettings,
EventCaller eventCaller
EventCaller eventCaller,
PluginConfiguration config,
ConfigurationManager configManager, Scheduler scheduler
) {
this.noticeService = noticeService;
this.chatSettings = chatSettings;
this.eventCaller = eventCaller;

this.config = config;
this.configManager = configManager;
this.scheduler = scheduler;

this.clear = create(chatSettings);
this.eventCaller = eventCaller;
}

private static Supplier<Notice> create(ChatSettings settings) {
return () -> Notice.chat("<newline>".repeat(Math.max(0, settings.linesToClear())));
}

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

Duration currentDelay = this.chatSettings.getChatDelay();
EditSlowModeEvent event =
this.eventCaller.callEvent(new EditSlowModeEvent(currentDelay, duration, viewer.getUniqueId()));

if (event.isCancelled()) {
return;
}

this.chatSettings.setChatDelay(duration);
this.scheduler.runAsync(() -> this.configManager.save(this.config));

if (duration.isZero()) {
this.noticeService.create()
.notice(translation -> translation.chat().slowModeOff())
.placeholder("{PLAYER}", viewer.getName())
.onlinePlayers()
.send();

this.chatSettings.setChatDelay(duration);
return;
}

Duration chatDelay = this.chatSettings.getChatDelay();
EditSlowModeEvent event = this.eventCaller.callEvent(new EditSlowModeEvent(chatDelay, duration, viewer.getUniqueId()));

if (event.isCancelled()) {
return;
}

this.chatSettings.setChatDelay(duration);

this.noticeService.create()
.notice(translation -> translation.chat().slowModeSet())
.placeholder("{SLOWMODE}", DurationUtil.format(duration, true))
Expand All @@ -148,9 +165,5 @@ void slowmodeOff(@Context Viewer viewer) {
Duration noSlowMode = Duration.ZERO;
this.slowmode(viewer, noSlowMode);
}

private static Supplier<Notice> create(ChatSettings settings) {
return () -> Notice.chat("<newline>".repeat(Math.max(0, settings.linesToClear())));
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.eternalcode.commons.time.DurationParser;
import com.eternalcode.commons.time.TemporalAmountParser;
import java.math.RoundingMode;
import java.time.Duration;
import java.time.temporal.ChronoUnit;

Expand All @@ -12,7 +13,7 @@ public class DurationUtil {
.withUnit("m", ChronoUnit.MINUTES)
.withUnit("h", ChronoUnit.HOURS)
.withUnit("d", ChronoUnit.DAYS)
.roundOff(ChronoUnit.MILLIS);
.withRounded(ChronoUnit.MILLIS, RoundingMode.UP);

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

0 comments on commit 7d1b07b

Please sign in to comment.