Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: formatted times #134

Open
wants to merge 2 commits into
base: v6
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,20 @@ If you prefer to use the Legacy Code format (i.e. &a, &b, etc.) then you can sti
with each element after that being usable as <prefix:index> with index being the items index in the list.
"""
.trim());

addDefault("Common.timeFormat.full", "<days><hours><minutes><seconds>");
addDefault("Common.timeFormat.days.singular", "<days> day, ");
addDefault("Common.timeFormat.days.plural", "<days> days, ");
addDefault("Common.timeFormat.hours.singular", "<hours> hour, ");
addDefault("Common.timeFormat.hours.plural", "<hours> hours, ");
addDefault("Common.timeFormat.minutes.singular", "<minutes> minute, ");
addDefault("Common.timeFormat.minutes.plural", "<minutes> minutes, ");
addDefault("Common.timeFormat.seconds.singular", "<seconds> second");
addDefault("Common.timeFormat.seconds.plural", "<seconds> seconds");

makeSectionLenient("Teleport");
addDefault(
"Teleport.eventBeforeTP",
"<prefix> <gray>Teleporting in <aqua><countdown> seconds</aqua>, please do not move!");
"<prefix> <gray>Teleporting in <aqua><countdown-formatted></aqua>, please do not move!");

addComment(
"Teleport.eventBeforeTP_title",
Expand Down Expand Up @@ -139,7 +147,7 @@ If you prefer to use the Legacy Code format (i.e. &a, &b, etc.) then you can sti

addDefault(
"Teleport.eventBeforeTPMovementAllowed",
"<prefix> <gray>Teleporting in <aqua><countdown></aqua> seconds!");
"<prefix> <gray>Teleporting in <aqua><countdown-formatted></aqua>!");
addDefault("Teleport.eventTeleport", "<prefix> <gray>Teleporting...");
addDefault(
"Teleport.eventMovement",
Expand Down Expand Up @@ -198,7 +206,7 @@ If you prefer to use the Legacy Code format (i.e. &a, &b, etc.) then you can sti
addDefault("Error.neverBlocked", "<prefix> <gray>This player was never blocked!");
addDefault(
"Error.onCooldown",
"<prefix> <gray>Please wait another <aqua><time></aqua> seconds to use this command!");
"<prefix> <gray>Please wait another <aqua><time-formatted></aqua> to use this command!");
addDefault(
"Error.requestSentToSelf", "<prefix> <gray>You can't send a request to yourself!");
addDefault(
Expand Down Expand Up @@ -436,7 +444,7 @@ If you prefer to use the Legacy Code format (i.e. &a, &b, etc.) then you can sti
"""
<prefix> <gray>The player <aqua><player></aqua> wants to teleport you to them!
<prefix> <gray>If you want to accept it, use <aqua>/tpayes</aqua>, but if not, use <aqua>/tpano</aqua>.
<prefix> <gray>You've got <aqua><lifetime> seconds</aqua> to respond to it!
<prefix> <gray>You've got <aqua><lifetime-formatted></aqua> to respond to it!

<click:run_command:'/tpayes <player>'><hover:show_text:'<green>Click here to accept the request.'><green><bold>[ACCEPT]</bold></hover></click> <click:run_command:'/tpano <player>'><hover:show_text:'<red>Click here to deny the request.'><red><bold>[DENY]</red></bold></hover></click>
"""
Expand Down Expand Up @@ -1307,6 +1315,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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,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)));
}
}

Expand Down
Loading