Skip to content

Commit 5ca428b

Browse files
AlexanderTrysMinemd5sha256
authored andcommitted
Add rental time-left placeholder
1 parent 002980d commit 5ca428b

6 files changed

Lines changed: 66 additions & 1 deletion

File tree

realty-api/src/main/java/io/github/md5sha256/realty/api/DurationFormatter.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package io.github.md5sha256.realty.api;
22

33
import org.jetbrains.annotations.NotNull;
4+
import org.jetbrains.annotations.Nullable;
45

56
import java.time.Duration;
7+
import java.time.LocalDateTime;
68

79
public final class DurationFormatter {
810

@@ -57,4 +59,27 @@ private DurationFormatter() {}
5759
}
5860
return sb.toString();
5961
}
62+
63+
/**
64+
* Formats the remaining time until the given end date.
65+
*
66+
* <p>Returns {@code "N/A"} when no end date exists and {@code "Expired"}
67+
* when the end date has already passed.</p>
68+
*/
69+
public static @NotNull String formatTimeLeft(@Nullable LocalDateTime endDate) {
70+
return formatTimeLeft(endDate, LocalDateTime.now());
71+
}
72+
73+
static @NotNull String formatTimeLeft(@Nullable LocalDateTime endDate, @NotNull LocalDateTime now) {
74+
if (endDate == null) {
75+
return "N/A";
76+
}
77+
78+
Duration remaining = Duration.between(now, endDate);
79+
if (remaining.isZero() || remaining.isNegative()) {
80+
return "Expired";
81+
}
82+
83+
return format(remaining);
84+
}
6085
}

realty-common/src/main/java/io/github/md5sha256/realty/database/RealtyApiImpl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,7 @@ public int deleteRegion(@NotNull String worldGuardRegionId, @NotNull UUID worldI
839839
placeholders.put("duration", DurationFormatter.format(Duration.ofSeconds(lease.durationSeconds())));
840840
placeholders.put("start_date", lease.startDate() != null ? dateFormatter.apply(lease.startDate()) : "N/A");
841841
placeholders.put("end_date", lease.endDate() != null ? dateFormatter.apply(lease.endDate()) : "N/A");
842+
placeholders.put("time_left", DurationFormatter.formatTimeLeft(lease.endDate()));
842843
if (lease.maxExtensions() != null) {
843844
placeholders.put("extensions", lease.currentMaxExtensions() + "/" + lease.maxExtensions());
844845
} else {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package io.github.md5sha256.realty.api;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.DisplayName;
5+
import org.junit.jupiter.api.Test;
6+
7+
import java.time.LocalDateTime;
8+
9+
class DurationFormatterTest {
10+
11+
@Test
12+
@DisplayName("formatTimeLeft returns N/A when no end date exists")
13+
void noEndDate() {
14+
String result = DurationFormatter.formatTimeLeft(null, LocalDateTime.of(2026, 3, 25, 1, 0, 0));
15+
Assertions.assertEquals("N/A", result);
16+
}
17+
18+
@Test
19+
@DisplayName("formatTimeLeft returns Expired when end date has passed")
20+
void expired() {
21+
String result = DurationFormatter.formatTimeLeft(
22+
LocalDateTime.of(2026, 3, 25, 1, 0, 0),
23+
LocalDateTime.of(2026, 3, 25, 1, 0, 1)
24+
);
25+
Assertions.assertEquals("Expired", result);
26+
}
27+
28+
@Test
29+
@DisplayName("formatTimeLeft formats remaining time using the standard duration formatter")
30+
void remainingTime() {
31+
String result = DurationFormatter.formatTimeLeft(
32+
LocalDateTime.of(2026, 3, 27, 4, 30, 15),
33+
LocalDateTime.of(2026, 3, 25, 1, 0, 0)
34+
);
35+
Assertions.assertEquals("2d 3h 30m 15s", result);
36+
}
37+
}

realty-paper/src/main/java/io/github/md5sha256/realty/command/InfoCommand.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ private void appendLeaseholdInfo(@NotNull TextComponent.Builder builder,
182182
Placeholder.unparsed("end_date", leasehold.endDate() != null
183183
? DateFormatter.format(settings.get(), leasehold.endDate())
184184
: "N/A"),
185+
Placeholder.unparsed("time_left", DurationFormatter.formatTimeLeft(leasehold.endDate())),
185186
Placeholder.unparsed("extensions", extensions)));
186187
}
187188

realty-paper/src/main/resources/messages.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ info:
118118
- "Duration: <duration>"
119119
- "Start Date: <start_date>"
120120
- "End Date: <end_date>"
121+
- "Time Left: <time_left>"
121122
- "Extensions: <extensions>"
122123
auction-active: "<#00aaff>►</#00aaff> Active Auction: <yellow><has_auction></yellow>"
123124
error: "Failed to retrieve region info: <error>"

realty-paper/src/main/resources/profiles.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
# - "<dark_red><bold>[Leased]"
6363
# - "<region>"
6464
# - "<tenant>"
65-
# - "<end_date>"
65+
# - "<time_left>"
6666
# left-click-commands:
6767
# - "realty info <region>"
6868
# grouped:

0 commit comments

Comments
 (0)