From ab98222f068e196d79a2731212b7d9152413b242 Mon Sep 17 00:00:00 2001 From: Teriuihi Date: Sun, 24 Mar 2024 17:24:24 +0100 Subject: [PATCH] Refactor April 1st check in ChatListener class A new method, `isWithinApril1st`, is introduced to simplify April 1st date check logic in the `ChatListener` class. This new method calculates the start and end of April 1st for all timezones in UTC and determines if the current time falls within this range. --- .../com/alttd/chat/listeners/ChatListener.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/galaxy/src/main/java/com/alttd/chat/listeners/ChatListener.java b/galaxy/src/main/java/com/alttd/chat/listeners/ChatListener.java index bebfd04..7bf95a6 100755 --- a/galaxy/src/main/java/com/alttd/chat/listeners/ChatListener.java +++ b/galaxy/src/main/java/com/alttd/chat/listeners/ChatListener.java @@ -29,8 +29,7 @@ import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.jetbrains.annotations.NotNull; -import java.time.LocalDate; -import java.time.Month; +import java.time.*; import java.util.HashSet; import java.util.Set; import java.util.regex.Pattern; @@ -61,6 +60,14 @@ public class ChatListener implements Listener { event.result(formatComponent.replaceText(TextReplacementConfig.builder().match("%message%").replacement(message).build())); } + ZonedDateTime aprilFirstUTCMidnight = ZonedDateTime.of(LocalDate.now().getYear(), 4, 1, 0, 0, 0, 0, ZoneId.of("UTC")); + ZonedDateTime aprilFirstStart = aprilFirstUTCMidnight.minusHours(24); + ZonedDateTime aprilFirstEnd = aprilFirstUTCMidnight.plusHours(24); + private boolean isWithinApril1st() { + ZonedDateTime now = ZonedDateTime.now(ZoneId.of("UTC")); + return now.isAfter(aprilFirstStart) && now.isBefore(aprilFirstEnd); + } + private final Component mention = MiniMessage.miniMessage().deserialize(Config.MENTIONPLAYERTAG); @EventHandler(ignoreCancelled = true) public void onPlayerChat(AsyncChatEvent event) { @@ -109,8 +116,7 @@ public class ChatListener implements Listener { Set playersToPing = new HashSet<>(); pingPlayers(playersToPing, modifiableString, player); - LocalDate now = LocalDate.now(); - if (now.getMonth().equals(Month.APRIL) && now.getDayOfMonth() == 1) { + if (isWithinApril1st()) { if (modifiableString.string().startsWith(Config.APRIL_FOOLS_RESET + " ")) { modifiableString.removeStringAtStart(Config.APRIL_FOOLS_RESET + " "); } else {