From 61fe218a16c76aacaf2acba82d2fe2845b211e27 Mon Sep 17 00:00:00 2001 From: tastybento Date: Sat, 2 May 2020 16:14:03 -0700 Subject: [PATCH] Purge was not calculated player logins correctly. https://github.com/BentoBoxWorld/BentoBox/issues/1333 --- .../commands/admin/purge/AdminPurgeCommand.java | 17 ++++++++++++++++- .../admin/purge/AdminPurgeCommandTest.java | 10 +++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/api/commands/admin/purge/AdminPurgeCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/admin/purge/AdminPurgeCommand.java index 4e15559e6..44ef8cd06 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/admin/purge/AdminPurgeCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/admin/purge/AdminPurgeCommand.java @@ -1,5 +1,6 @@ package world.bentobox.bentobox.api.commands.admin.purge; +import java.util.Date; import java.util.HashSet; import java.util.Iterator; import java.util.List; @@ -11,6 +12,7 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; +import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.api.commands.CompositeCommand; import world.bentobox.bentobox.api.events.island.IslandEvent.IslandDeletedEvent; import world.bentobox.bentobox.api.localization.TextVariables; @@ -119,13 +121,26 @@ public class AdminPurgeCommand extends CompositeCommand implements Listener { } Set getOldIslands(int days) { + 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 -> (System.currentTimeMillis() - Bukkit.getOfflinePlayer(i.getOwner()).getLastPlayed()) > days * 1000 * 24 * 3600) + .filter(i -> ((double)(System.currentTimeMillis() - Bukkit.getOfflinePlayer(i.getOwner()).getLastPlayed()) / 1000 / 3600 / 24) > days) .map(Island::getUniqueId) .collect(Collectors.toSet()); } diff --git a/src/test/java/world/bentobox/bentobox/api/commands/admin/purge/AdminPurgeCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/admin/purge/AdminPurgeCommandTest.java index 696e2c408..56dc75ecd 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/admin/purge/AdminPurgeCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/admin/purge/AdminPurgeCommandTest.java @@ -6,6 +6,7 @@ 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.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -39,6 +40,7 @@ import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.managers.CommandsManager; import world.bentobox.bentobox.managers.IslandWorldManager; import world.bentobox.bentobox.managers.IslandsManager; +import world.bentobox.bentobox.managers.PlayersManager; import world.bentobox.bentobox.managers.RanksManager; /** @@ -65,6 +67,8 @@ public class AdminPurgeCommandTest { private Island island; @Mock private World world; + @Mock + private PlayersManager pm; /** * @throws java.lang.Exception @@ -95,6 +99,10 @@ public class AdminPurgeCommandTest { // Island when(island.isOwned()).thenReturn(true); // Default owned + // Player manager + when(plugin.getPlayers()).thenReturn(pm); + when(pm.getName(any())).thenReturn("name"); + // Command apc = new AdminPurgeCommand(ac); } @@ -293,7 +301,7 @@ public class AdminPurgeCommandTest { testExecuteUserStringListOfStringIslandsFound(); assertTrue(apc.execute(user, "", Collections.singletonList("confirm"))); verify(im).deleteIsland(eq(island), eq(true), eq(null)); - verify(plugin).log(any()); + verify(plugin, times(2)).log(any()); verify(user).sendMessage(eq("commands.admin.purge.see-console-for-status"), eq("[label]"), eq("bsb")); }