Skip to content

Commit c735103

Browse files
committed
Adds check for last login to owner or any team mates on the island
1 parent d1dde90 commit c735103

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

src/main/java/world/bentobox/bentobox/api/commands/admin/purge/AdminPurgeRegionsCommand.java

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
import java.io.IOException;
66
import java.nio.ByteBuffer;
77
import java.nio.ByteOrder;
8+
import java.time.Instant;
9+
import java.time.ZoneId;
10+
import java.time.format.DateTimeFormatter;
811
import java.util.ArrayList;
912
import java.util.HashMap;
1013
import java.util.HashSet;
@@ -249,7 +252,7 @@ private void findIslands(World world, int days) {
249252
.anyMatch(optIsland ->
250253
// If missing (empty) → treat as undeletable (true) - this is a bit conservative but maybe the database is messed up
251254
// If present, checkIsland(...) == true means “cannot delete”
252-
optIsland.map(this::checkIsland)
255+
optIsland.map(this::canDeleteIsland)
253256
.orElse(true)
254257
)
255258
);
@@ -284,7 +287,29 @@ private void findIslands(World world, int days) {
284287

285288
private void displayIsland(Island island) {
286289
// Log the island data
287-
getPlugin().log("Island at " + Util.xyz(island.getCenter().toVector()) + " in world " + getWorld().getName() + " owned by " + getPlugin().getPlayers().getName(island.getOwner()) + " will be deleted");
290+
getPlugin().log("Island at " + Util.xyz(island.getCenter().toVector()) + " in world " + getWorld().getName()
291+
+ " owned by " + getPlugin().getPlayers().getName(island.getOwner())
292+
+ " who last logged in " + formatLocalTimestamp(getPlugin().getPlayers().getLastLoginTimestamp(island.getOwner()))
293+
+ " will be deleted");
294+
}
295+
296+
/**
297+
* Formats a millisecond timestamp into a human-readable string
298+
* using the system's local time zone.
299+
*
300+
* @param millis the timestamp in milliseconds
301+
* @return formatted string in the form "yyyy-MM-dd HH:mm"
302+
*/
303+
public String formatLocalTimestamp(Long millis) {
304+
if (millis == null) {
305+
return "(unknown or never recorded)";
306+
}
307+
Instant instant = Instant.ofEpochMilli(millis);
308+
309+
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")
310+
.withZone(ZoneId.systemDefault()); // Uses the machine's local time zone
311+
312+
return formatter.format(instant);
288313
}
289314

290315
/**
@@ -293,7 +318,12 @@ private void displayIsland(Island island) {
293318
* @param island island
294319
* @return true means “cannot delete”
295320
*/
296-
private boolean checkIsland(Island island) {
321+
private boolean canDeleteIsland(Island island) {
322+
long cutoffMillis = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(days);
323+
// Check if owner or team members logged in recently
324+
if (island.getMemberSet().stream().map(uuid -> getPlugin().getPlayers().getLastLoginTimestamp(uuid) >= cutoffMillis).findFirst().orElse(false)) {
325+
return false;
326+
}
297327
// Level check
298328
boolean levelCheck = getPlugin().getAddonsManager().getAddonByName("Level").map(l ->
299329
((Level) l).getIslandLevel(getWorld(), island.getOwner()) >= getPlugin().getSettings().getIslandPurgeLevel()).orElse(false);

0 commit comments

Comments
 (0)