mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2024-11-01 00:10:40 +01:00
Improve purge by using our own last login value stored in the db.
This commit is contained in:
parent
68d53798e9
commit
2cb0651f95
@ -207,6 +207,7 @@ public class BentoBox extends JavaPlugin implements Listener {
|
||||
registerListeners();
|
||||
|
||||
// Load islands from database - need to wait until all the worlds are loaded
|
||||
log("Loading islands from database...");
|
||||
try {
|
||||
islandsManager.load();
|
||||
} catch (Exception e) {
|
||||
|
@ -5,6 +5,8 @@ import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.EventHandler;
|
||||
@ -21,8 +23,10 @@ import world.bentobox.bentobox.util.Util;
|
||||
|
||||
public class AdminPurgeCommand extends CompositeCommand implements Listener {
|
||||
|
||||
private static final Long YEAR2000 = 946713600L;
|
||||
private int count;
|
||||
private boolean inPurge;
|
||||
private boolean scanning;
|
||||
private boolean toBeConfirmed;
|
||||
private Iterator<String> it;
|
||||
private User user;
|
||||
@ -47,6 +51,10 @@ public class AdminPurgeCommand extends CompositeCommand implements Listener {
|
||||
|
||||
@Override
|
||||
public boolean canExecute(User user, String label, List<String> args) {
|
||||
if (scanning) {
|
||||
user.sendMessage("commands.admin.purge.scanning-in-progress");
|
||||
return false;
|
||||
}
|
||||
if (inPurge) {
|
||||
user.sendMessage("commands.admin.purge.purge-in-progress", TextVariables.LABEL, this.getTopLabel());
|
||||
return false;
|
||||
@ -75,13 +83,21 @@ public class AdminPurgeCommand extends CompositeCommand implements Listener {
|
||||
user.sendMessage("commands.admin.purge.days-one-or-more");
|
||||
return false;
|
||||
}
|
||||
islands = getOldIslands(days);
|
||||
user.sendMessage("commands.admin.purge.purgable-islands", TextVariables.NUMBER, String.valueOf(islands.size()));
|
||||
if (!islands.isEmpty()) {
|
||||
toBeConfirmed = true;
|
||||
user.sendMessage("commands.admin.purge.confirm", TextVariables.LABEL, this.getTopLabel());
|
||||
return false;
|
||||
}
|
||||
user.sendMessage("commands.admin.purge.scanning");
|
||||
scanning = true;
|
||||
getOldIslands(days).thenAccept(islandSet -> {
|
||||
user.sendMessage("commands.admin.purge.purgable-islands", TextVariables.NUMBER,
|
||||
String.valueOf(islandSet.size()));
|
||||
if (!islandSet.isEmpty()) {
|
||||
toBeConfirmed = true;
|
||||
user.sendMessage("commands.admin.purge.confirm", TextVariables.LABEL, this.getTopLabel());
|
||||
islands = islandSet;
|
||||
} else {
|
||||
user.sendMessage("commands.admin.purge.none-found");
|
||||
}
|
||||
scanning = false;
|
||||
});
|
||||
|
||||
} catch (NumberFormatException e) {
|
||||
user.sendMessage("commands.admin.purge.number-error");
|
||||
return false;
|
||||
@ -125,40 +141,61 @@ public class AdminPurgeCommand extends CompositeCommand implements Listener {
|
||||
* @param days days
|
||||
* @return set of islands
|
||||
*/
|
||||
Set<String> getOldIslands(int days) {
|
||||
long currentTimeMillis = System.currentTimeMillis();
|
||||
long daysInMilliseconds = (long) days * 1000 * 3600 * 24;
|
||||
Set<String> oldIslands = new HashSet<>();
|
||||
|
||||
CompletableFuture<Set<String>> getOldIslands(int days) {
|
||||
CompletableFuture<Set<String>> result = new CompletableFuture<>();
|
||||
// Process islands in one pass, logging and adding to the set if applicable
|
||||
getPlugin().getIslands().getIslands().stream()
|
||||
getPlugin().getIslands().getIslandsASync().thenAccept(list -> {
|
||||
user.sendMessage("commands.admin.purge.total-islands", TextVariables.NUMBER, String.valueOf(list.size()));
|
||||
Set<String> oldIslands = new HashSet<>();
|
||||
list.stream()
|
||||
.filter(i -> !i.isSpawn()).filter(i -> !i.getPurgeProtected())
|
||||
.filter(i -> i.getWorld() != null) // to handle currently unloaded world islands
|
||||
.filter(i -> i.getWorld().equals(this.getWorld())).filter(Island::isOwned).filter(
|
||||
i -> i.getMemberSet().stream()
|
||||
.allMatch(member -> (currentTimeMillis
|
||||
- Bukkit.getOfflinePlayer(member).getLastPlayed()) > daysInMilliseconds))
|
||||
.filter(i -> i.getWorld().equals(this.getWorld())) // Island needs to be in this world
|
||||
.filter(Island::isOwned) // The island needs to be owned
|
||||
.filter(i -> i.getMemberSet().stream().allMatch(member -> checkLastLoginTimestamp(days, member)))
|
||||
.forEach(i -> {
|
||||
// Add the unique island ID to the set
|
||||
oldIslands.add(i.getUniqueId());
|
||||
BentoBox.getInstance().log("Will purge island at " + Util.xyz(i.getCenter().toVector()) + " in "
|
||||
getPlugin().log("Will purge island at " + Util.xyz(i.getCenter().toVector()) + " in "
|
||||
+ i.getWorld().getName());
|
||||
// Log each member's last login information
|
||||
i.getMemberSet().forEach(member -> {
|
||||
Date lastLogin = new Date(Bukkit.getOfflinePlayer(member).getLastPlayed());
|
||||
Long timestamp = getPlayers().getLastLoginTimestamp(member);
|
||||
Date lastLogin = new Date(timestamp);
|
||||
BentoBox.getInstance()
|
||||
.log("Player " + BentoBox.getInstance().getPlayers().getName(member)
|
||||
+ " last logged in "
|
||||
+ (int) ((currentTimeMillis - Bukkit.getOfflinePlayer(member).getLastPlayed())
|
||||
/ 1000 / 3600 / 24)
|
||||
+ (int) ((System.currentTimeMillis() - timestamp) / 1000 / 3600 / 24)
|
||||
+ " days ago. " + lastLogin);
|
||||
});
|
||||
BentoBox.getInstance().log("+-----------------------------------------+");
|
||||
});
|
||||
|
||||
return oldIslands;
|
||||
result.complete(oldIslands);
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
private boolean checkLastLoginTimestamp(int days, UUID member) {
|
||||
long daysInMilliseconds = days * 24L * 3600 * 1000; // Calculate days in milliseconds
|
||||
Long lastLoginTimestamp = getPlayers().getLastLoginTimestamp(member);
|
||||
// If no valid last login time is found or it's before the year 2000, try to fetch from Bukkit
|
||||
if (lastLoginTimestamp == null || lastLoginTimestamp < YEAR2000) {
|
||||
lastLoginTimestamp = Bukkit.getOfflinePlayer(member).getLastPlayed();
|
||||
|
||||
// If still invalid, set the current timestamp to mark the user for eventual purging
|
||||
if (lastLoginTimestamp < YEAR2000) {
|
||||
getPlayers().setLoginTimeStamp(member, System.currentTimeMillis());
|
||||
return false; // User will be purged in the future
|
||||
} else {
|
||||
// Otherwise, update the last login timestamp with the valid value from Bukkit
|
||||
getPlayers().setLoginTimeStamp(member, lastLoginTimestamp);
|
||||
}
|
||||
}
|
||||
// Check if the difference between now and the last login is greater than the allowed days
|
||||
return System.currentTimeMillis() - lastLoginTimestamp > daysInMilliseconds;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return the inPurge
|
||||
*/
|
||||
|
@ -134,10 +134,31 @@ public abstract class AbstractDatabaseHandler<T> {
|
||||
@Nullable
|
||||
public abstract T loadObject(@NonNull String uniqueId) throws InstantiationException, IllegalAccessException, InvocationTargetException, ClassNotFoundException, IntrospectionException, NoSuchMethodException;
|
||||
|
||||
/**
|
||||
* Loads all the records in this table and returns a list of them async
|
||||
* @return CompletableFuture List of <T>
|
||||
* @since 2.7.0
|
||||
*/
|
||||
public CompletableFuture<List<T>> loadObjectsASync() {
|
||||
CompletableFuture<List<T>> completableFuture = new CompletableFuture<>();
|
||||
|
||||
Bukkit.getScheduler().runTaskAsynchronously(BentoBox.getInstance(), () -> {
|
||||
try {
|
||||
completableFuture.complete(loadObjects()); // Complete the future with the result
|
||||
} catch (Exception e) {
|
||||
completableFuture.completeExceptionally(e); // Complete exceptionally if an error occurs
|
||||
plugin.logError("Failed to load objects asynchronously: " + e.getMessage());
|
||||
}
|
||||
});
|
||||
|
||||
return completableFuture;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save T into the corresponding database
|
||||
*
|
||||
* @param instance that should be inserted into the database
|
||||
* @return completable future that is true if saved
|
||||
*/
|
||||
public abstract CompletableFuture<Boolean> saveObject(T instance) throws IllegalAccessException, InvocationTargetException, IntrospectionException ;
|
||||
|
||||
|
@ -166,6 +166,13 @@ public class Database<T> {
|
||||
return dataObjects;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load all objects async
|
||||
* @return CompletableFuture<List<T>>
|
||||
*/
|
||||
public @NonNull CompletableFuture<List<T>> loadObjectsASync() {
|
||||
return handler.loadObjectsASync();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -458,6 +458,16 @@ public class IslandsManager {
|
||||
return handler.loadObjects().stream().toList();
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads all existing islands from the database without caching async
|
||||
*
|
||||
* @return CompletableFuture<List> of every island
|
||||
* @since 2.7.0
|
||||
*/
|
||||
public CompletableFuture<List<Island>> getIslandsASync() {
|
||||
return handler.loadObjectsASync();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an <strong>unmodifiable collection</strong> of all the islands (even
|
||||
* those who may be unowned) in the specified world.
|
||||
|
@ -423,14 +423,25 @@ public class PlayersManager {
|
||||
/**
|
||||
* Records when the user last logged in. Called by the joinleave listener
|
||||
* @param user user
|
||||
* @since 2.7.0
|
||||
*/
|
||||
public void setLoginTimeStamp(User user) {
|
||||
if (user.isPlayer() && user.isOnline()) {
|
||||
Players p = this.getPlayer(user.getUniqueId());
|
||||
if (p != null) {
|
||||
p.setLastLogin(System.currentTimeMillis());
|
||||
this.savePlayer(user.getUniqueId());
|
||||
}
|
||||
setLoginTimeStamp(user.getUniqueId(), System.currentTimeMillis());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the player's last login time to a timestamp
|
||||
* @param playerUUID player UUID
|
||||
* @param timestamp timestamp to set
|
||||
* @since 2.7.0
|
||||
*/
|
||||
public void setLoginTimeStamp(UUID playerUUID, long timestamp) {
|
||||
Players p = this.getPlayer(playerUUID);
|
||||
if (p != null) {
|
||||
p.setLastLogin(timestamp);
|
||||
this.savePlayer(playerUUID);
|
||||
}
|
||||
}
|
||||
|
||||
@ -438,6 +449,7 @@ public class PlayersManager {
|
||||
* Get the last login time stamp for this player
|
||||
* @param uuid player's UUID
|
||||
* @return timestamp or null if unknown or not recorded yet
|
||||
* @since 2.7.0
|
||||
*/
|
||||
@Nullable
|
||||
public Long getLastLoginTimestamp(UUID uuid) {
|
||||
|
@ -98,6 +98,10 @@ commands:
|
||||
purgable-islands: '&a Found &b [number] &a purgable islands.'
|
||||
purge-in-progress: '&c Purging in progress. Use &b /[label] purge stop &c to
|
||||
cancel.'
|
||||
scanning: '&a Scanning islands in the database. This may take a while depending on how many you have...'
|
||||
scanning-in-progress: '&c Scanning in progress, please wait'
|
||||
none-found: '&c No islands found to purge.'
|
||||
total-islands: '&a You have [number] islands in your database in all worlds.'
|
||||
number-error: '&c Argument must be a number of days'
|
||||
confirm: '&d Type &b /[label] purge confirm &d to start purging'
|
||||
completed: '&a Purging stopped.'
|
||||
|
@ -6,14 +6,20 @@ import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
@ -38,6 +44,7 @@ import world.bentobox.bentobox.BentoBox;
|
||||
import world.bentobox.bentobox.api.addons.Addon;
|
||||
import world.bentobox.bentobox.api.commands.CompositeCommand;
|
||||
import world.bentobox.bentobox.api.events.island.IslandDeletedEvent;
|
||||
import world.bentobox.bentobox.api.localization.TextVariables;
|
||||
import world.bentobox.bentobox.api.user.User;
|
||||
import world.bentobox.bentobox.database.objects.Island;
|
||||
import world.bentobox.bentobox.managers.CommandsManager;
|
||||
@ -95,6 +102,7 @@ public class AdminPurgeCommandTest {
|
||||
when(plugin.getIslands()).thenReturn(im);
|
||||
// No islands by default
|
||||
when(im.getIslands()).thenReturn(Collections.emptyList());
|
||||
when(im.getIslandsASync()).thenReturn(CompletableFuture.completedFuture(Collections.emptyList()));
|
||||
|
||||
// IWM
|
||||
IslandWorldManager iwm = mock(IslandWorldManager.class);
|
||||
@ -286,13 +294,13 @@ public class AdminPurgeCommandTest {
|
||||
when(island.getOwner()).thenReturn(UUID.randomUUID());
|
||||
when(island.isOwned()).thenReturn(true);
|
||||
when(island.getMemberSet()).thenReturn(ImmutableSet.of(UUID.randomUUID()));
|
||||
when(im.getIslands()).thenReturn(Collections.singleton(island));
|
||||
OfflinePlayer op = mock(OfflinePlayer.class);
|
||||
when(op.getLastPlayed()).thenReturn(0L);
|
||||
when(Bukkit.getOfflinePlayer(any(UUID.class))).thenReturn(op);
|
||||
assertFalse(apc.execute(user, "", Collections.singletonList("10")));
|
||||
verify(user).sendMessage(eq("commands.admin.purge.purgable-islands"), eq("[number]"), eq("1"));
|
||||
verify(user).sendMessage(eq("commands.admin.purge.confirm"), eq("[label]"), eq("bsb"));
|
||||
when(im.getIslandsASync()).thenReturn(CompletableFuture.completedFuture(List.of(island)));
|
||||
when(pm.getLastLoginTimestamp(any())).thenReturn(962434800L);
|
||||
assertTrue(apc.execute(user, "", Collections.singletonList("10"))); // 10 days ago
|
||||
verify(user).sendMessage("commands.admin.purge.scanning");
|
||||
verify(user).sendMessage("commands.admin.purge.total-islands", "[number]", "1");
|
||||
verify(user, never()).sendMessage("commands.admin.purge.none-found");
|
||||
verify(user).sendMessage("commands.admin.purge.confirm", TextVariables.LABEL, "bsb");
|
||||
}
|
||||
|
||||
|
||||
@ -367,13 +375,26 @@ public class AdminPurgeCommandTest {
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.bentobox.api.commands.admin.purge.AdminPurgeCommand#getOldIslands(int)}
|
||||
* @throws TimeoutException
|
||||
* @throws ExecutionException
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
@Test
|
||||
public void testGetOldIslands() {
|
||||
assertTrue(apc.getOldIslands(10).isEmpty());
|
||||
public void testGetOldIslands() throws InterruptedException, ExecutionException, TimeoutException {
|
||||
assertTrue(apc.execute(user, "", Collections.singletonList("10"))); // 10 days ago
|
||||
// First, ensure that the result is empty
|
||||
CompletableFuture<Set<String>> result = apc.getOldIslands(10);
|
||||
Set<String> set = result.join();
|
||||
assertTrue(set.isEmpty());
|
||||
// Mocking Islands and their retrieval
|
||||
Island island1 = mock(Island.class);
|
||||
Island island2 = mock(Island.class);
|
||||
when(im.getIslands()).thenReturn(Set.of(island, island2));
|
||||
assertTrue(apc.getOldIslands(10).isEmpty());
|
||||
|
||||
when(im.getIslandsASync()).thenReturn(CompletableFuture.completedFuture(List.of(island1, island2)));
|
||||
// Now, check again after mocking islands
|
||||
CompletableFuture<Set<String>> futureWithIslands = apc.getOldIslands(10);
|
||||
assertTrue(futureWithIslands.get(5, TimeUnit.SECONDS).isEmpty()); // Adjust this assertion based on the expected behavior of getOldIslands
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user