From 7d1b07b26afda7c58e85032095ca354a4d35b867 Mon Sep 17 00:00:00 2001 From: Martin Sulikowski Date: Fri, 14 Feb 2025 22:18:06 +0100 Subject: [PATCH] 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. --- buildSrc/src/main/kotlin/Versions.kt | 2 +- .../composer/DurationComposer.java | 7 +-- .../core/feature/chat/ChatCommand.java | 45 ++++++++++++------- .../eternalcode/core/util/DurationUtil.java | 3 +- 4 files changed, 34 insertions(+), 23 deletions(-) diff --git a/buildSrc/src/main/kotlin/Versions.kt b/buildSrc/src/main/kotlin/Versions.kt index 2452770a9..992cd6640 100644 --- a/buildSrc/src/main/kotlin/Versions.kt +++ b/buildSrc/src/main/kotlin/Versions.kt @@ -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" diff --git a/eternalcore-core/src/main/java/com/eternalcode/core/configuration/composer/DurationComposer.java b/eternalcore-core/src/main/java/com/eternalcode/core/configuration/composer/DurationComposer.java index a6c417e0a..b280e6542 100644 --- a/eternalcore-core/src/main/java/com/eternalcode/core/configuration/composer/DurationComposer.java +++ b/eternalcore-core/src/main/java/com/eternalcode/core/configuration/composer/DurationComposer.java @@ -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 { @@ -18,5 +16,4 @@ public Result deserialize(String source) { public Result serialize(Duration entity) { return Result.ok(DurationParser.TIME_UNITS.format(entity)); } - } diff --git a/eternalcore-core/src/main/java/com/eternalcode/core/feature/chat/ChatCommand.java b/eternalcore-core/src/main/java/com/eternalcode/core/feature/chat/ChatCommand.java index 464b44f61..0856fe88f 100644 --- a/eternalcore-core/src/main/java/com/eternalcode/core/feature/chat/ChatCommand.java +++ b/eternalcore-core/src/main/java/com/eternalcode/core/feature/chat/ChatCommand.java @@ -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; @@ -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 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 create(ChatSettings settings) { + return () -> Notice.chat("".repeat(Math.max(0, settings.linesToClear()))); } @Execute(name = "clear", aliases = "cc") @@ -111,10 +128,20 @@ 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()) @@ -122,19 +149,9 @@ void slowmode(@Context Viewer viewer, @Arg Duration duration) { .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)) @@ -148,9 +165,5 @@ void slowmodeOff(@Context Viewer viewer) { Duration noSlowMode = Duration.ZERO; this.slowmode(viewer, noSlowMode); } - - private static Supplier create(ChatSettings settings) { - return () -> Notice.chat("".repeat(Math.max(0, settings.linesToClear()))); - } } diff --git a/eternalcore-core/src/main/java/com/eternalcode/core/util/DurationUtil.java b/eternalcore-core/src/main/java/com/eternalcode/core/util/DurationUtil.java index 6d97ad6d6..9ddcab0ef 100644 --- a/eternalcore-core/src/main/java/com/eternalcode/core/util/DurationUtil.java +++ b/eternalcore-core/src/main/java/com/eternalcode/core/util/DurationUtil.java @@ -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; @@ -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 STANDARD_FORMAT = new DurationParser() .withUnit("d", ChronoUnit.DAYS)