From eafcc100ea31db8d6b7091b71b290986ba1aadcb Mon Sep 17 00:00:00 2001 From: Thatsmusic99 <25277367+thatsmusic99@users.noreply.github.com> Date: Sat, 23 Mar 2024 11:05:56 +0000 Subject: [PATCH] feat: add time formatting Signed-off-by: Thatsmusic99 <25277367+thatsmusic99@users.noreply.github.com> --- .../commands/TimedATCommand.java | 3 +- .../commands/teleport/TpAll.java | 3 +- .../commands/teleport/Tpa.java | 6 +- .../commands/teleport/TpaHere.java | 6 +- .../config/CustomMessages.java | 59 +++++++++++++++---- .../managers/MovementManager.java | 6 +- 6 files changed, 63 insertions(+), 20 deletions(-) diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/TimedATCommand.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/TimedATCommand.java index c9404ed3..81efc356 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/TimedATCommand.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/TimedATCommand.java @@ -27,7 +27,8 @@ default boolean canProceed(@NotNull CommandSender sender) { CustomMessages.sendMessage( sender, "Error.onCooldown", - Placeholder.unparsed("time", String.valueOf(cooldown))); + Placeholder.unparsed("time", String.valueOf(cooldown)), + Placeholder.component("time-formatted", CustomMessages.toTime(cooldown))); return false; } diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/teleport/TpAll.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/teleport/TpAll.java index 1abda1a8..dfcac5f4 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/teleport/TpAll.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/teleport/TpAll.java @@ -53,7 +53,8 @@ public boolean onCommand( target, "Info.tpaRequestHere", Placeholder.unparsed("player", sender.getName()), - Placeholder.unparsed("lifetime", String.valueOf(requestLifetime))); + Placeholder.unparsed("lifetime", String.valueOf(requestLifetime)), + Placeholder.component("lifetime-formatted", CustomMessages.toTime(requestLifetime))); BukkitRunnable run = new BukkitRunnable() { diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/teleport/Tpa.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/teleport/Tpa.java index de5bac6d..dedaf42c 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/teleport/Tpa.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/teleport/Tpa.java @@ -73,7 +73,8 @@ public boolean onCommand( "Info.requestSent", Placeholder.parsed( "player", MiniMessage.miniMessage().escapeTags(target.getName())), - Placeholder.unparsed("lifetime", String.valueOf(requestLifetime))); + Placeholder.unparsed("lifetime", String.valueOf(requestLifetime)), + Placeholder.component("lifetime-formatted", CustomMessages.toTime(requestLifetime))); CoreClass.playSound("tpa", "sent", player); @@ -88,7 +89,8 @@ public boolean onCommand( "Info.tpaRequestReceived", Placeholder.parsed( "player", MiniMessage.miniMessage().escapeTags(sender.getName())), - Placeholder.unparsed("lifetime", String.valueOf(requestLifetime))); + Placeholder.unparsed("lifetime", String.valueOf(requestLifetime)), + Placeholder.component("lifetime-formatted", CustomMessages.toTime(requestLifetime))); } CoreClass.playSound("tpa", "received", target); diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/teleport/TpaHere.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/teleport/TpaHere.java index 6d92c4fa..102bd679 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/teleport/TpaHere.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/commands/teleport/TpaHere.java @@ -63,7 +63,8 @@ public boolean onCommand( "Info.requestSent", Placeholder.parsed( "player", MiniMessage.miniMessage().escapeTags(target.getName())), - Placeholder.unparsed("lifetime", String.valueOf(requestLifetime))); + Placeholder.unparsed("lifetime", String.valueOf(requestLifetime)), + Placeholder.component("lifetime-formatted", CustomMessages.toTime(requestLifetime))); CoreClass.playSound("tpahere", "sent", player); ATPlayer targetPlayer = ATPlayer.getPlayer(target); @@ -76,7 +77,8 @@ public boolean onCommand( "Info.tpaRequestHere", Placeholder.parsed( "player", MiniMessage.miniMessage().escapeTags(sender.getName())), - Placeholder.unparsed("lifetime", String.valueOf(requestLifetime))); + Placeholder.unparsed("lifetime", String.valueOf(requestLifetime)), + Placeholder.component("lifetime-formatted", CustomMessages.toTime(requestLifetime))); } CoreClass.playSound("tpahere", "received", target); diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/config/CustomMessages.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/config/CustomMessages.java index 074a7921..63ba3c9c 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/config/CustomMessages.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/config/CustomMessages.java @@ -102,19 +102,19 @@ If you prefer to use the Legacy Code format (i.e. &a, &b, etc.) then you can sti """ .trim()); addDefault("Common.timeFormat.full", ""); - addDefault("Common.timeFormat.days.singular", " Day, "); - addDefault("Common.timeFormat.days.plural", " Days, "); - addDefault("Common.timeFormat.hours.singular", " Hour, "); - addDefault("Common.timeFormat.hours.plural", " Hours, "); - addDefault("Common.timeFormat.minutes.singular", " Minute, "); - addDefault("Common.timeFormat.minutes.plural", " Minutes, "); - addDefault("Common.timeFormat.seconds.singular", " Second"); - addDefault("Common.timeFormat.seconds.plural", " Seconds"); + addDefault("Common.timeFormat.days.singular", " day, "); + addDefault("Common.timeFormat.days.plural", " days, "); + addDefault("Common.timeFormat.hours.singular", " hour, "); + addDefault("Common.timeFormat.hours.plural", " hours, "); + addDefault("Common.timeFormat.minutes.singular", " minute, "); + addDefault("Common.timeFormat.minutes.plural", " minutes, "); + addDefault("Common.timeFormat.seconds.singular", " second"); + addDefault("Common.timeFormat.seconds.plural", " seconds"); makeSectionLenient("Teleport"); addDefault( "Teleport.eventBeforeTP", - " Teleporting in seconds, please do not move!"); + " Teleporting in , please do not move!"); addComment( "Teleport.eventBeforeTP_title", @@ -147,7 +147,7 @@ If you prefer to use the Legacy Code format (i.e. &a, &b, etc.) then you can sti addDefault( "Teleport.eventBeforeTPMovementAllowed", - " Teleporting in seconds!"); + " Teleporting in !"); addDefault("Teleport.eventTeleport", " Teleporting..."); addDefault( "Teleport.eventMovement", @@ -206,7 +206,7 @@ If you prefer to use the Legacy Code format (i.e. &a, &b, etc.) then you can sti addDefault("Error.neverBlocked", " This player was never blocked!"); addDefault( "Error.onCooldown", - " Please wait another seconds to use this command!"); + " Please wait another to use this command!"); addDefault( "Error.requestSentToSelf", " You can't send a request to yourself!"); addDefault( @@ -439,7 +439,7 @@ If you prefer to use the Legacy Code format (i.e. &a, &b, etc.) then you can sti """ The player wants to teleport you to them! If you want to accept it, use /tpayes, but if not, use /tpano. - You've got seconds to respond to it! + You've got to respond to it! '>Click here to accept the request.'>[ACCEPT] '>Click here to deny the request.'>[DENY] """ @@ -1298,6 +1298,41 @@ private static void sendSound( } } + public static @NotNull Component toTime(final int seconds) { + + // Calculate friendly time + final int days = seconds / (60 * 60 * 24); + + final int totalHours = seconds % (60 * 60 * 24); + final int hours = totalHours / (60 * 60); + + final int totalMinutes = totalHours % (60 * 60); + final int minutes = totalMinutes / 60; + + final int secondsRemain = seconds % 60; + + // Get individual placeholders + final var daysPart = getTimePart(days, "days"); + final var hoursPart = getTimePart(hours, "hours"); + final var minutesPart = getTimePart(minutes, "minutes"); + final var secondsPart = getTimePart(secondsRemain, "seconds"); + + return getComponent("Common.timeFormat.full", Placeholder.component("days", daysPart), + Placeholder.component("hours", hoursPart), + Placeholder.component("minutes", minutesPart), + Placeholder.component("seconds", secondsPart)); + } + + @ApiStatus.Internal + private static @NotNull Component getTimePart(final int value, final @NotNull String type) { + + final var placeholder = Placeholder.parsed(type, String.valueOf(value)); + + return value < 1 ? Component.empty() : value == 1 ? + getComponent("Common.timeFormat." + type + ".singular", placeholder) + : getComponent("Common.timeFormat." + type + ".plural", placeholder); + } + /** * Used to translate the legacy AT message format into the MiniMessage format. * diff --git a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/managers/MovementManager.java b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/managers/MovementManager.java index b70ba659..3f9cb0cd 100644 --- a/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/managers/MovementManager.java +++ b/AdvancedTeleport-Bukkit/src/main/java/io/github/niestrat99/advancedteleport/managers/MovementManager.java @@ -127,12 +127,14 @@ public void run() { CustomMessages.sendMessage( teleportingPlayer, "Teleport.eventBeforeTP", - Placeholder.unparsed("countdown", String.valueOf(warmUp))); + Placeholder.unparsed("countdown", String.valueOf(warmUp)), + Placeholder.component("countdown-formatted", CustomMessages.toTime(warmUp))); } else { CustomMessages.sendMessage( teleportingPlayer, "Teleport.eventBeforeTPMovementAllowed", - Placeholder.unparsed("countdown", String.valueOf(warmUp))); + Placeholder.unparsed("countdown", String.valueOf(warmUp)), + Placeholder.component("countdown-formatted", CustomMessages.toTime(warmUp))); } }