diff --git a/src/main/java/world/bentobox/bentobox/api/commands/admin/team/AdminTeamDisbandCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/admin/team/AdminTeamDisbandCommand.java index c28598174..8af0a0cfe 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/admin/team/AdminTeamDisbandCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/admin/team/AdminTeamDisbandCommand.java @@ -57,7 +57,7 @@ public class AdminTeamDisbandCommand extends CompositeCommand { mUser.sendMessage("commands.admin.team.disband.disbanded"); // The owner gets to keep the island if (!m.equals(targetUUID)) { - island.removeMember(m); + getIslands().removePlayer(island, m); TeamEvent.builder() .island(island) .reason(TeamEvent.Reason.KICK) diff --git a/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamLeaveCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamLeaveCommand.java index 21d40f3bf..618bf4068 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamLeaveCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamLeaveCommand.java @@ -84,7 +84,7 @@ public class IslandTeamLeaveCommand extends ConfirmableCommand { if (ownerUUID != null) { User.getInstance(ownerUUID).sendMessage("commands.island.team.leave.left-your-island", TextVariables.NAME, user.getName(), TextVariables.DISPLAY_NAME, user.getDisplayName()); } - island.removeMember(user.getUniqueId()); + getIslands().removePlayer(island, user.getUniqueId()); // Clean the player getPlayers().cleanLeavingPlayer(getWorld(), user, false, island); diff --git a/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamUncoopCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamUncoopCommand.java index 5df6a1133..af397ee71 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamUncoopCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamUncoopCommand.java @@ -83,7 +83,7 @@ public class IslandTeamUncoopCommand extends CompositeCommand { } Island island = getIslands().getIsland(getWorld(), user.getUniqueId()); if (island != null) { - island.removeMember(targetUUID); + getIslands().removePlayer(island, targetUUID); user.sendMessage("commands.island.team.uncoop.success", TextVariables.NAME, target.getName(), TextVariables.DISPLAY_NAME, target.getDisplayName()); target.sendMessage("commands.island.team.uncoop.you-are-no-longer-a-coop-member", TextVariables.NAME, user.getName(), TextVariables.DISPLAY_NAME, user.getDisplayName()); // Set cooldown diff --git a/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamUntrustCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamUntrustCommand.java index 57034bf66..4c81cae1d 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamUntrustCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamUntrustCommand.java @@ -83,7 +83,7 @@ public class IslandTeamUntrustCommand extends CompositeCommand { } Island island = getIslands().getIsland(getWorld(), user.getUniqueId()); if (island != null) { - island.removeMember(targetUUID); + getIslands().removePlayer(island, targetUUID); user.sendMessage("commands.island.team.untrust.success", TextVariables.NAME, target.getName(), TextVariables.DISPLAY_NAME, target.getDisplayName()); target.sendMessage("commands.island.team.untrust.you-are-no-longer-trusted", TextVariables.NAME, user.getName(), TextVariables.DISPLAY_NAME, user.getDisplayName()); // Set cooldown diff --git a/src/main/java/world/bentobox/bentobox/managers/IslandsManager.java b/src/main/java/world/bentobox/bentobox/managers/IslandsManager.java index d861d1866..6554c0d80 100644 --- a/src/main/java/world/bentobox/bentobox/managers/IslandsManager.java +++ b/src/main/java/world/bentobox/bentobox/managers/IslandsManager.java @@ -1378,6 +1378,15 @@ public class IslandsManager { islandCache.removePlayer(world, uuid).forEach(handler::saveObjectAsync); } + /** + * Remove this player from this island + * @param island island + * @param uuid uuid of member + */ + public void removePlayer(Island island, UUID uuid) { + islandCache.removePlayer(island, uuid); + } + /** * This teleports players away from an island - used when reseting or deleting an island * @param island to remove players from @@ -1512,7 +1521,7 @@ public class IslandsManager { public void setOwner(User user, UUID targetUUID, Island island) { islandCache.setOwner(island, targetUUID); // Remove the old owner from the island - island.removeMember(user.getUniqueId()); + plugin.getIslands().removePlayer(island, user.getUniqueId()); user.sendMessage("commands.island.team.setowner.name-is-the-owner", "[name]", plugin.getPlayers().getName(targetUUID)); plugin.getIWM().getAddon(island.getWorld()).ifPresent(addon -> { diff --git a/src/main/java/world/bentobox/bentobox/managers/island/IslandCache.java b/src/main/java/world/bentobox/bentobox/managers/island/IslandCache.java index 194bfbe99..07d4bcfea 100644 --- a/src/main/java/world/bentobox/bentobox/managers/island/IslandCache.java +++ b/src/main/java/world/bentobox/bentobox/managers/island/IslandCache.java @@ -303,6 +303,19 @@ public class IslandCache { return islandSet; } + /** + * Removes player from island and removes the cache reference + * @param island member's island + * @param uuid uuid of member to remove + */ + public void removePlayer(@NonNull Island island, @NonNull UUID uuid) { + Set islandSet = islandsByUUID.get(uuid); + if (islandSet != null) { + islandSet.remove(island); + } + island.removeMember(uuid); + } + /** * Get the number of islands in the cache * @return the number of islands diff --git a/src/test/java/world/bentobox/bentobox/api/commands/admin/AdminUnregisterCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/admin/AdminUnregisterCommandTest.java index ea9762317..1594c85b4 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/admin/AdminUnregisterCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/admin/AdminUnregisterCommandTest.java @@ -18,6 +18,7 @@ import java.util.UUID; import org.bukkit.Bukkit; import org.bukkit.Location; +import org.bukkit.World; import org.bukkit.entity.Player; import org.bukkit.plugin.PluginManager; import org.bukkit.scheduler.BukkitScheduler; @@ -27,6 +28,7 @@ import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.Mock; import org.mockito.Mockito; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; @@ -58,11 +60,17 @@ import world.bentobox.bentobox.util.Util; @PrepareForTest({Bukkit.class, BentoBox.class, User.class }) public class AdminUnregisterCommandTest { + @Mock private CompositeCommand ac; + @Mock private User user; + @Mock private IslandsManager im; + @Mock private PlayersManager pm; private UUID notUUID; + @Mock + private World world; /** */ @@ -84,8 +92,6 @@ public class AdminUnregisterCommandTest { // Player Player p = mock(Player.class); - // Sometimes use Mockito.withSettings().verboseLogging() - user = mock(User.class); when(user.isOp()).thenReturn(false); UUID uuid = UUID.randomUUID(); notUUID = UUID.randomUUID(); @@ -98,8 +104,8 @@ public class AdminUnregisterCommandTest { User.setPlugin(plugin); // Parent command has no aliases - ac = mock(CompositeCommand.class); when(ac.getSubCommandAliases()).thenReturn(new HashMap<>()); + when(ac.getWorld()).thenReturn(world); // Island World Manager IslandWorldManager iwm = mock(IslandWorldManager.class); @@ -107,7 +113,6 @@ public class AdminUnregisterCommandTest { // Player has island to begin with - im = mock(IslandsManager.class); when(im.hasIsland(any(), any(UUID.class))).thenReturn(true); when(im.hasIsland(any(), any(User.class))).thenReturn(true); when(im.isOwner(any(),any())).thenReturn(true); @@ -115,7 +120,6 @@ public class AdminUnregisterCommandTest { when(plugin.getIslands()).thenReturn(im); // Has team - pm = mock(PlayersManager.class); when(im.inTeam(any(), eq(uuid))).thenReturn(true); when(plugin.getPlayers()).thenReturn(pm); @@ -227,9 +231,9 @@ public class AdminUnregisterCommandTest { itl.unregisterPlayer(user, "name", targetUUID); verify(user).sendMessage("commands.admin.unregister.unregistered-island", TextVariables.XYZ, "1,2,3", TextVariables.NAME, "name"); assertTrue(map.isEmpty()); - verify(im).removePlayer(any(), eq(uuid1)); - verify(im).removePlayer(any(), eq(uuid2)); - verify(im).removePlayer(any(), eq(uuid3)); - verify(im, never()).removePlayer(any(), eq(uuid4)); + verify(im).removePlayer(any(World.class), eq(uuid1)); + verify(im).removePlayer(any(World.class), eq(uuid2)); + verify(im).removePlayer(any(World.class), eq(uuid3)); + verify(im, never()).removePlayer(any(World.class), eq(uuid4)); } } diff --git a/src/test/java/world/bentobox/bentobox/api/commands/admin/team/AdminTeamDisbandCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/admin/team/AdminTeamDisbandCommandTest.java index cb6601244..7a630c095 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/admin/team/AdminTeamDisbandCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/admin/team/AdminTeamDisbandCommandTest.java @@ -21,9 +21,11 @@ import java.util.Set; import java.util.UUID; import org.bukkit.Bukkit; +import org.bukkit.World; import org.bukkit.entity.Player; import org.bukkit.plugin.PluginManager; import org.bukkit.scheduler.BukkitScheduler; +import org.eclipse.jdt.annotation.Nullable; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -72,6 +74,8 @@ public class AdminTeamDisbandCommandTest { @Mock private PluginManager pim; private UUID notUUID; + @Mock + private @Nullable Island island; /** */ @@ -119,6 +123,7 @@ public class AdminTeamDisbandCommandTest { when(im.hasIsland(any(), any(User.class))).thenReturn(true); when(im.isOwner(any(),any())).thenReturn(true); when(im.getOwner(any(),any())).thenReturn(uuid); + when(im.getIsland(any(World.class), any(UUID.class))).thenReturn(island); when(plugin.getIslands()).thenReturn(im); // Has team @@ -214,8 +219,7 @@ public class AdminTeamDisbandCommandTest { @Test public void testExecuteSuccess() { when(im.inTeam(any(), any())).thenReturn(true); - Island is = mock(Island.class); - when(im.getIsland(any(), any(UUID.class))).thenReturn(is); + when(im.getIsland(any(), any(UUID.class))).thenReturn(island); String[] name = {"tastybento"}; when(pm.getUUID(any())).thenReturn(notUUID); when(pm.getName(any())).thenReturn(name[0]); @@ -229,8 +233,8 @@ public class AdminTeamDisbandCommandTest { AdminTeamDisbandCommand itl = new AdminTeamDisbandCommand(ac); assertTrue(itl.execute(user, itl.getLabel(), Arrays.asList(name))); - verify(is, never()).removeMember(notUUID); - verify(is).removeMember(uuid); + verify(im, never()).removePlayer(island, notUUID); + verify(im).removePlayer(island, uuid); verify(user).sendMessage("commands.admin.team.disband.success", TextVariables.NAME, name[0]); verify(p).sendMessage("commands.admin.team.disband.disbanded"); verify(p2).sendMessage("commands.admin.team.disband.disbanded"); diff --git a/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamKickCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamKickCommandTest.java index 29c98a69e..b11245fc0 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamKickCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamKickCommandTest.java @@ -21,6 +21,7 @@ import java.util.UUID; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; +import org.bukkit.World; import org.bukkit.entity.Player; import org.bukkit.plugin.PluginManager; import org.bukkit.scheduler.BukkitScheduler; @@ -86,6 +87,8 @@ public class IslandTeamKickCommandTest { private Island island; @Mock private Addon addon; + @Mock + private World world; /** */ @@ -132,6 +135,7 @@ public class IslandTeamKickCommandTest { when(ic.getAddon()).thenReturn(addon); AddonDescription desc = new AddonDescription.Builder("main", "name", "version").build(); when(addon.getDescription()).thenReturn(desc); + when(ic.getWorld()).thenReturn(world); // Player has island to begin with im = mock(IslandsManager.class); @@ -264,7 +268,7 @@ public class IslandTeamKickCommandTest { IslandTeamKickCommand itl = new IslandTeamKickCommand(ic); assertTrue(itl.execute(user, itl.getLabel(), Collections.singletonList("poslovitch"))); - verify(im).removePlayer(any(), eq(notUUID)); + verify(im).removePlayer(any(World.class), eq(notUUID)); verify(user).sendMessage("commands.island.team.kick.success", TextVariables.NAME, "poslovitch", TextVariables.DISPLAY_NAME, "&Cposlovich"); } @@ -354,7 +358,7 @@ public class IslandTeamKickCommandTest { IslandTeamKickCommand itl = new IslandTeamKickCommand(ic); assertTrue(itl.execute(user, itl.getLabel(), Collections.singletonList("poslovitch"))); - verify(im).removePlayer(any(), eq(notUUID)); + verify(im).removePlayer(any(World.class), eq(notUUID)); verify(user).sendMessage("commands.island.team.kick.success", TextVariables.NAME, "poslovitch", TextVariables.DISPLAY_NAME, "&Cposlovich"); } @@ -376,7 +380,7 @@ public class IslandTeamKickCommandTest { IslandTeamKickCommand itl = new IslandTeamKickCommand(ic); assertTrue(itl.execute(user, itl.getLabel(), Collections.singletonList("poslovitch"))); - verify(im).removePlayer(any(), eq(notUUID)); + verify(im).removePlayer(any(World.class), eq(notUUID)); verify(user).sendMessage("commands.island.team.kick.success", TextVariables.NAME, "poslovitch", TextVariables.DISPLAY_NAME, "&Cposlovich"); verify(target, Mockito.never()).getInventory(); @@ -404,7 +408,7 @@ public class IslandTeamKickCommandTest { IslandTeamKickCommand itl = new IslandTeamKickCommand(ic); assertTrue(itl.execute(user, itl.getLabel(), Collections.singletonList("poslovitch"))); - verify(im).removePlayer(any(), eq(notUUID)); + verify(im).removePlayer(any(World.class), eq(notUUID)); verify(user).sendMessage("commands.island.team.kick.success", TextVariables.NAME, "poslovitch", TextVariables.DISPLAY_NAME, "&Cposlovich"); verify(target, Mockito.never()).getInventory(); verify(pm).cleanLeavingPlayer(any(), any(User.class), eq(true), eq(island)); diff --git a/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamLeaveCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamLeaveCommandTest.java index 4b5cb65ab..71c79716a 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamLeaveCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamLeaveCommandTest.java @@ -162,7 +162,7 @@ public class IslandTeamLeaveCommandTest { IslandTeamLeaveCommand itl = new IslandTeamLeaveCommand(ic); assertTrue(itl.execute(user, itl.getLabel(), new ArrayList<>())); - verify(island).removeMember(uuid); + verify(im).removePlayer(island,uuid); verify(user).sendMessage(eq("commands.island.team.leave.success")); } @@ -221,7 +221,7 @@ public class IslandTeamLeaveCommandTest { IslandTeamLeaveCommand itl = new IslandTeamLeaveCommand(ic); assertTrue(itl.execute(user, itl.getLabel(), new ArrayList<>())); - verify(island).removeMember(uuid); + verify(im).removePlayer(island, uuid); verify(user).sendMessage("commands.island.team.leave.success"); verify(pm).addReset(eq(world), eq(uuid)); verify(user).sendMessage("commands.island.reset.resets-left", TextVariables.NUMBER, "100");