From 1dc0e0571570680263d2626be642989644e14917 Mon Sep 17 00:00:00 2001 From: Telesphoreo Date: Sun, 10 Apr 2022 18:30:21 -0500 Subject: [PATCH] Closes #26 --- build.gradle | 2 +- .../java/dev/plex/command/impl/FlatlandsCMD.java | 1 - .../plex/command/impl/MasterbuilderworldCMD.java | 1 - .../dev/plex/command/impl/NameHistoryCMD.java | 4 +--- src/main/java/dev/plex/command/impl/NotesCMD.java | 4 +--- src/main/java/dev/plex/punishment/Punishment.java | 6 +++--- src/main/java/dev/plex/util/PlexUtils.java | 15 ++++++++++++++- src/main/resources/config.yml | 2 ++ 8 files changed, 22 insertions(+), 13 deletions(-) diff --git a/build.gradle b/build.gradle index 697a126..4fcd2c0 100644 --- a/build.gradle +++ b/build.gradle @@ -103,7 +103,7 @@ String getBuildNumber() { } static def getDate() { - return new Date().format("MM/dd/yyyy 'at' hh:mm:ss a") + return new Date().format("MM/dd/yyyy 'at' hh:mm:ss a z") } task buildProperties { diff --git a/src/main/java/dev/plex/command/impl/FlatlandsCMD.java b/src/main/java/dev/plex/command/impl/FlatlandsCMD.java index 433051e..eaa9401 100644 --- a/src/main/java/dev/plex/command/impl/FlatlandsCMD.java +++ b/src/main/java/dev/plex/command/impl/FlatlandsCMD.java @@ -17,7 +17,6 @@ import org.jetbrains.annotations.Nullable; @CommandParameters(name = "flatlands", description = "Teleport to the flatlands") public class FlatlandsCMD extends PlexCommand { - @Override protected Component execute(@NotNull CommandSender sender, @Nullable Player playerSender, String[] args) { diff --git a/src/main/java/dev/plex/command/impl/MasterbuilderworldCMD.java b/src/main/java/dev/plex/command/impl/MasterbuilderworldCMD.java index cb8b4c2..6af7d22 100644 --- a/src/main/java/dev/plex/command/impl/MasterbuilderworldCMD.java +++ b/src/main/java/dev/plex/command/impl/MasterbuilderworldCMD.java @@ -17,7 +17,6 @@ import org.jetbrains.annotations.Nullable; @CommandParameters(name = "masterbuilderworld", aliases = "mbw", description = "Teleport to the Master Builder world") public class MasterbuilderworldCMD extends PlexCommand { - @Override protected Component execute(@NotNull CommandSender sender, @Nullable Player playerSender, String[] args) { diff --git a/src/main/java/dev/plex/command/impl/NameHistoryCMD.java b/src/main/java/dev/plex/command/impl/NameHistoryCMD.java index 8b02301..0d23538 100644 --- a/src/main/java/dev/plex/command/impl/NameHistoryCMD.java +++ b/src/main/java/dev/plex/command/impl/NameHistoryCMD.java @@ -24,8 +24,6 @@ import org.jetbrains.annotations.Nullable; @CommandPermissions(level = Rank.OP, permission = "plex.namehistory") public class NameHistoryCMD extends PlexCommand { - private static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("MM/dd/yyyy 'at' hh:mm:ss a"); - @Override protected Component execute(@NotNull CommandSender sender, @Nullable Player playerSender, String[] args) { @@ -50,7 +48,7 @@ public class NameHistoryCMD extends PlexCommand historyList.add( messageComponent("nameHistoryBody", history.getUsername(), - DATE_FORMAT.format(history.getLocalDateTime()))); + PlexUtils.useTimezone(history.getLocalDateTime()))); } else { diff --git a/src/main/java/dev/plex/command/impl/NotesCMD.java b/src/main/java/dev/plex/command/impl/NotesCMD.java index 88d694d..944c46d 100644 --- a/src/main/java/dev/plex/command/impl/NotesCMD.java +++ b/src/main/java/dev/plex/command/impl/NotesCMD.java @@ -30,8 +30,6 @@ import java.util.concurrent.atomic.AtomicReference; @CommandPermissions(level = Rank.ADMIN, permission = "plex.notes") public class NotesCMD extends PlexCommand { - private static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("MM/dd/yyyy 'at' hh:mm:ss a"); - @Override protected Component execute(@NotNull CommandSender sender, @Nullable Player playerSender, String[] args) { @@ -173,7 +171,7 @@ public class NotesCMD extends PlexCommand AtomicReference noteList = new AtomicReference<>(Component.text("Player notes for: " + plexPlayer.getName()).color(NamedTextColor.GREEN)); for (Note note : notes) { - Component noteLine = mmString("" + note.getId() + " - Written by: " + DataUtils.getPlayer(note.getWrittenBy()).getName() + " on " + DATE_FORMAT.format(note.getTimestamp())); + Component noteLine = mmString("" + note.getId() + " - Written by: " + DataUtils.getPlayer(note.getWrittenBy()).getName() + " on " + PlexUtils.useTimezone(note.getTimestamp())); noteLine = noteLine.append(mmString("# " + note.getNote())); noteList.set(noteList.get().append(Component.newline())); noteList.set(noteList.get().append(noteLine)); diff --git a/src/main/java/dev/plex/punishment/Punishment.java b/src/main/java/dev/plex/punishment/Punishment.java index 4c42bf4..d9250ae 100644 --- a/src/main/java/dev/plex/punishment/Punishment.java +++ b/src/main/java/dev/plex/punishment/Punishment.java @@ -8,7 +8,6 @@ import dev.plex.util.PlexUtils; import dev.plex.util.adapter.LocalDateTimeDeserializer; import dev.plex.util.adapter.LocalDateTimeSerializer; import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; import java.util.UUID; import lombok.Getter; import lombok.Setter; @@ -20,7 +19,6 @@ import net.kyori.adventure.text.Component; public class Punishment { private static final String banUrl = Plex.get().config.getString("banning.ban_url"); - private static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("MM/dd/yyyy 'at' hh:mm:ss a"); private final UUID punished; private final UUID punisher; private String ip; @@ -45,8 +43,10 @@ public class Punishment public static Component generateBanMessage(Punishment punishment) { + return PlexUtils.messageComponent("banMessage", banUrl, punishment.getReason(), - DATE_FORMAT.format(punishment.getEndDate()), punishment.getPunisher() == null ? "CONSOLE" : MojangUtils.getInfo(punishment.getPunisher().toString()).getUsername()); + PlexUtils.useTimezone(punishment.getEndDate()), + punishment.getPunisher() == null ? "CONSOLE" : MojangUtils.getInfo(punishment.getPunisher().toString()).getUsername()); } public static Component generateIndefBanMessage(String type) diff --git a/src/main/java/dev/plex/util/PlexUtils.java b/src/main/java/dev/plex/util/PlexUtils.java index b597a17..1ce9b5a 100644 --- a/src/main/java/dev/plex/util/PlexUtils.java +++ b/src/main/java/dev/plex/util/PlexUtils.java @@ -6,11 +6,11 @@ import dev.plex.Plex; import dev.plex.PlexBase; import dev.plex.config.Config; import dev.plex.storage.StorageType; +import java.time.format.DateTimeFormatter; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; import net.kyori.adventure.text.minimessage.tag.standard.*; -import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; import org.apache.commons.lang.math.NumberUtils; import org.bukkit.*; @@ -58,6 +58,9 @@ public class PlexUtils extends PlexBase "ca83b658-c03b-4106-9edc-72f70a80656d", // ayunami2000 "2e06e049-24c8-42e4-8bcf-d35372af31e6" //Fleek ); + private static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("MM/dd/yyyy 'at' hh:mm:ss a z"); + private static final Set TIMEZONES = Set.of(TimeZone.getAvailableIDs()); + private static String TIMEZONE = Plex.get().config.getString("server.timezone"); static { @@ -240,6 +243,16 @@ public class PlexUtils extends PlexBase return (unit != null) ? unit : TimeUnit.DAY; } + public static String useTimezone(LocalDateTime date) + { + // Use UTC if the timezone is null or not set correctly + if (TIMEZONE == null || !TIMEZONES.contains(TIMEZONE)) + { + TIMEZONE = "Etc/UTC"; + } + return DATE_FORMAT.withZone(ZoneId.of(TIMEZONE)).format(date); + } + public static LocalDateTime parseDateOffset(String... time) { Instant instant = Instant.now(); diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index e3955a7..d0ade40 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -7,6 +7,8 @@ server: colorize_motd: true sample: - "&cForums: https://forum.plex.us.org" + # What timezone should various messages appear in (e.g. ban message end date) + timezone: Etc/UTC titles: masterbuilders: [ ]