Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Purges based on team members all being offline for too long #2362

Merged
merged 2 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
Expand All @@ -18,6 +17,7 @@
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.util.Util;

public class AdminPurgeCommand extends CompositeCommand implements Listener {

Expand Down Expand Up @@ -82,7 +82,7 @@ public boolean execute(User user, String label, List<String> args) {
user.sendMessage("commands.admin.purge.confirm", TextVariables.LABEL, this.getTopLabel());
return false;
}
} catch(Exception e) {
} catch (NumberFormatException e) {
user.sendMessage("commands.admin.purge.number-error");
return false;
}
Expand Down Expand Up @@ -120,29 +120,42 @@ void onIslandDeleted(IslandDeletedEvent e) {
}
}

/**
* Gets a set of islands that are older than the parameter in days
* @param days days
* @return set of islands
*/
Set<String> getOldIslands(int days) {
long currentTimeMillis = System.currentTimeMillis();
double daysInMilliseconds = days * 1000 * 3600 * 24;
Set<String> oldIslands = new HashSet<>();

// Process islands in one pass, logging and adding to the set if applicable
getPlugin().getIslands().getIslands().stream()
.filter(i -> !i.isSpawn())
.filter(i -> !i.getPurgeProtected())
.filter(i -> i.getWorld().equals(this.getWorld()))
.filter(Island::isOwned)
.filter(i -> i.getMembers().size() == 1)
.filter(i -> ((double)(System.currentTimeMillis() - Bukkit.getOfflinePlayer(i.getOwner()).getLastPlayed()) / 1000 / 3600 / 24) > days)
.forEach(i -> {
Date date = new Date(Bukkit.getOfflinePlayer(i.getOwner()).getLastPlayed());
BentoBox.getInstance().log("Will purge " +
BentoBox.getInstance().getPlayers().getName(i.getOwner()) +
" last logged in " + (int)((double)(System.currentTimeMillis() - Bukkit.getOfflinePlayer(i.getOwner()).getLastPlayed()) / 1000 / 3600 / 24) + " days ago. " + date);
});
return getPlugin().getIslands().getIslands().stream()
.filter(i -> !i.isSpawn())
.filter(i -> !i.getPurgeProtected())
.filter(i -> i.getWorld().equals(this.getWorld()))
.filter(Island::isOwned)
.filter(i -> i.getMembers().size() == 1)
.filter(i -> ((double)(System.currentTimeMillis() - Bukkit.getOfflinePlayer(i.getOwner()).getLastPlayed()) / 1000 / 3600 / 24) > days)
.map(Island::getUniqueId)
.collect(Collectors.toSet());
.filter(i -> !i.isSpawn()).filter(i -> !i.getPurgeProtected())
.filter(i -> i.getWorld().equals(this.getWorld())).filter(Island::isOwned).filter(
i -> i.getMemberSet().stream()
.allMatch(member -> (currentTimeMillis
- Bukkit.getOfflinePlayer(member).getLastPlayed()) > daysInMilliseconds))
.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 "
+ i.getWorld().getName());
// Log each member's last login information
i.getMemberSet().forEach(member -> {
Date lastLogin = new Date(Bukkit.getOfflinePlayer(member).getLastPlayed());
BentoBox.getInstance()
.log("Player " + BentoBox.getInstance().getPlayers().getName(member)
+ " last logged in "
+ (int) ((currentTimeMillis - Bukkit.getOfflinePlayer(member).getLastPlayed())
/ 1000 / 3600 / 24)
+ " days ago. " + lastLogin);
});
BentoBox.getInstance().log("+-----------------------------------------+");
});

return oldIslands;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
import static org.mockito.Mockito.when;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import org.bukkit.World;
import org.bukkit.util.Vector;
import org.eclipse.jdt.annotation.NonNull;
import org.junit.After;
import org.junit.Before;
Expand All @@ -31,6 +31,8 @@
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;

import com.google.common.collect.ImmutableSet;

import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.addons.Addon;
import world.bentobox.bentobox.api.commands.CompositeCommand;
Expand All @@ -41,7 +43,6 @@
import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.managers.PlayersManager;
import world.bentobox.bentobox.managers.RanksManager;

/**
* @author tastybento
Expand Down Expand Up @@ -69,6 +70,8 @@ public class AdminPurgeCommandTest {
private World world;
@Mock
private PlayersManager pm;
@Mock
private @NonNull Location location;

/**
*/
Expand Down Expand Up @@ -97,6 +100,8 @@ public void setUp() throws Exception {

// Island
when(island.isOwned()).thenReturn(true); // Default owned
when(location.toVector()).thenReturn(new Vector(1, 2, 3));
when(island.getCenter()).thenReturn(location);

// Player manager
when(plugin.getPlayers()).thenReturn(pm);
Expand Down Expand Up @@ -237,11 +242,15 @@ public void testExecuteUserStringListOfStringNoIslandsTeamIsland() {
when(island.getPurgeProtected()).thenReturn(false);
when(island.getWorld()).thenReturn(world);
when(island.getOwner()).thenReturn(UUID.randomUUID());
Map<UUID, Integer> team = new HashMap<>();
team.put(UUID.randomUUID(), RanksManager.OWNER_RANK);
team.put(UUID.randomUUID(), RanksManager.MEMBER_RANK);
when(island.getMembers()).thenReturn(team);
when(island.getMemberSet()).thenReturn(ImmutableSet.of(UUID.randomUUID(), UUID.randomUUID()));
when(im.getIslands()).thenReturn(Collections.singleton(island));

// All players are up to date
PowerMockito.mockStatic(Bukkit.class);
OfflinePlayer op = mock(OfflinePlayer.class);
when(op.getLastPlayed()).thenReturn(System.currentTimeMillis());
when(Bukkit.getOfflinePlayer(any(UUID.class))).thenReturn(op);

assertTrue(apc.execute(user, "", Collections.singletonList("10")));
verify(user).sendMessage(eq("commands.admin.purge.purgable-islands"), eq("[number]"), eq("0"));
}
Expand All @@ -254,9 +263,7 @@ public void testExecuteUserStringListOfStringNoIslandsRecentLogin() {
when(island.getPurgeProtected()).thenReturn(false);
when(island.getWorld()).thenReturn(world);
when(island.getOwner()).thenReturn(UUID.randomUUID());
Map<UUID, Integer> team = new HashMap<>();
team.put(UUID.randomUUID(), RanksManager.OWNER_RANK);
when(island.getMembers()).thenReturn(team);
when(island.getMemberSet()).thenReturn(ImmutableSet.of(UUID.randomUUID()));
when(im.getIslands()).thenReturn(Collections.singleton(island));
PowerMockito.mockStatic(Bukkit.class);
OfflinePlayer op = mock(OfflinePlayer.class);
Expand All @@ -275,9 +282,7 @@ public void testExecuteUserStringListOfStringIslandsFound() {
when(island.getWorld()).thenReturn(world);
when(island.getOwner()).thenReturn(UUID.randomUUID());
when(island.isOwned()).thenReturn(true);
Map<UUID, Integer> team = new HashMap<>();
team.put(UUID.randomUUID(), RanksManager.OWNER_RANK);
when(island.getMembers()).thenReturn(team);
when(island.getMemberSet()).thenReturn(ImmutableSet.of(UUID.randomUUID()));
when(im.getIslands()).thenReturn(Collections.singleton(island));
PowerMockito.mockStatic(Bukkit.class);
OfflinePlayer op = mock(OfflinePlayer.class);
Expand All @@ -300,7 +305,7 @@ public void testRemoveIslands() {
testExecuteUserStringListOfStringIslandsFound();
assertTrue(apc.execute(user, "", Collections.singletonList("confirm")));
verify(im).deleteIsland(eq(island), eq(true), eq(null));
verify(plugin, times(2)).log(any());
verify(plugin, times(4)).log(any());
verify(user).sendMessage(eq("commands.admin.purge.see-console-for-status"), eq("[label]"), eq("bsb"));
}

Expand Down
Loading