Skip to content

Commit 9c326e8

Browse files
committed
Deletes unused playerdata when purging
1 parent 388d381 commit 9c326e8

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

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

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.stream.Collectors;
2222

2323
import org.bukkit.Bukkit;
24+
import org.bukkit.OfflinePlayer;
2425
import org.bukkit.World;
2526
import org.bukkit.event.Listener;
2627

@@ -120,10 +121,10 @@ private boolean deleteEverything() {
120121
// Fail!
121122
getPlugin().logError("Not all region files could be deleted");
122123
}
123-
124124
// Delete islands and regions
125125
for (Set<String> islandIDs : deleteableRegions.values()) {
126126
for (String islandID : islandIDs) {
127+
deletePlayerFromWorldFolder(islandID);
127128
// Remove island from the cache
128129
getPlugin().getIslands().getIslandCache().deleteIslandFromCache(islandID);
129130
// Delete island from database using id
@@ -141,6 +142,46 @@ private boolean deleteEverything() {
141142

142143
}
143144

145+
private void deletePlayerFromWorldFolder(String islandID) {
146+
File base = getWorld().getWorldFolder();
147+
File playerData = new File(base, "playerdata");
148+
// Get the island from the cache
149+
getPlugin().getIslands().getIslandById(islandID).ifPresent(island -> {
150+
island.getMemberSet().forEach(uuid -> {
151+
// Check if the player has any islands left
152+
List<Island> memberOf = new ArrayList<>(getIslands().getIslands(getWorld(), uuid));
153+
deleteableRegions.values().forEach(ids -> memberOf.removeIf(i -> ids.contains(i.getUniqueId())));
154+
if (memberOf.isEmpty()) {
155+
// Do not remove this player if they are Op
156+
OfflinePlayer p = Bukkit.getOfflinePlayer(uuid);
157+
if (p.isOp()) {
158+
return;
159+
}
160+
// Do not remove if player logged in recently
161+
Long lastLogin = getPlugin().getPlayers().getLastLoginTimestamp(uuid);
162+
if (lastLogin == null) {
163+
lastLogin = Bukkit.getOfflinePlayer(uuid).getLastSeen();
164+
}
165+
long cutoffMillis = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(days);
166+
if (lastLogin >= cutoffMillis) {
167+
return;
168+
}
169+
// Remove the player from the world folder playerdata because they no longer have any island associated with them
170+
if (playerData.exists()) {
171+
File playerFile = new File(playerData, uuid + ".dat");
172+
if (playerFile.exists() && !playerFile.delete()) {
173+
getPlugin().logError("Failed to delete player data file: " + playerFile.getAbsolutePath());
174+
}
175+
playerFile = new File(playerData, uuid + ".dat_old");
176+
if (playerFile.exists() && !playerFile.delete()) {
177+
getPlugin().logError("Failed to delete player data backup file: " + playerFile.getAbsolutePath());
178+
}
179+
}
180+
}
181+
});
182+
});
183+
}
184+
144185
/**
145186
* Deletes all region files in deleteableRegions that are older than {@code days}.
146187
* First verifies that none of the overworld, nether or end region files (as

0 commit comments

Comments
 (0)