21
21
import java .util .stream .Collectors ;
22
22
23
23
import org .bukkit .Bukkit ;
24
+ import org .bukkit .OfflinePlayer ;
24
25
import org .bukkit .World ;
25
26
import org .bukkit .event .Listener ;
26
27
@@ -120,10 +121,10 @@ private boolean deleteEverything() {
120
121
// Fail!
121
122
getPlugin ().logError ("Not all region files could be deleted" );
122
123
}
123
-
124
124
// Delete islands and regions
125
125
for (Set <String > islandIDs : deleteableRegions .values ()) {
126
126
for (String islandID : islandIDs ) {
127
+ deletePlayerFromWorldFolder (islandID );
127
128
// Remove island from the cache
128
129
getPlugin ().getIslands ().getIslandCache ().deleteIslandFromCache (islandID );
129
130
// Delete island from database using id
@@ -141,6 +142,46 @@ private boolean deleteEverything() {
141
142
142
143
}
143
144
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
+
144
185
/**
145
186
* Deletes all region files in deleteableRegions that are older than {@code days}.
146
187
* First verifies that none of the overworld, nether or end region files (as
0 commit comments