Removes only team members from island.

Fixes https://github.com/BentoBoxWorld/BentoBox/issues/1096
This commit is contained in:
tastybento 2020-01-07 13:12:53 -08:00
parent aa5c253e03
commit a3a0efb3a3
2 changed files with 74 additions and 24 deletions

View File

@ -5,8 +5,6 @@ import java.util.List;
import java.util.Optional;
import java.util.UUID;
import com.google.common.collect.ImmutableSet;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.commands.ConfirmableCommand;
import world.bentobox.bentobox.api.events.island.IslandEvent;
@ -57,9 +55,10 @@ public class AdminUnregisterCommand extends ConfirmableCommand {
return true;
}
private void unregisterPlayer(User user, UUID targetUUID) {
void unregisterPlayer(User user, UUID targetUUID) {
// Unregister island
Island oldIsland = getIslands().getIsland(getWorld(), targetUUID);
if (oldIsland == null) return;
IslandEvent.builder()
.island(oldIsland)
.location(oldIsland.getCenter())
@ -68,10 +67,13 @@ public class AdminUnregisterCommand extends ConfirmableCommand {
.admin(true)
.build();
// Remove all island members
new ImmutableSet.Builder<UUID>().addAll(oldIsland.getMembers().keySet()).build().forEach(m -> {
oldIsland.getMemberSet().forEach(m -> {
getIslands().removePlayer(getWorld(), m);
getPlayers().clearHomeLocations(getWorld(), m);
});
// Remove all island players that reference this island
oldIsland.getMembers().clear();
getIslands().save(oldIsland);
user.sendMessage("commands.admin.unregister.unregistered-island", "[xyz]", Util.xyz(oldIsland.getCenter().toVector()));
}

View File

@ -2,12 +2,18 @@ package world.bentobox.bentobox.api.commands.admin;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.framework;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.bukkit.Bukkit;
@ -16,6 +22,7 @@ import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginManager;
import org.bukkit.scheduler.BukkitScheduler;
import org.bukkit.util.Vector;
import org.eclipse.jdt.annotation.Nullable;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@ -26,6 +33,8 @@ import org.powermock.core.classloader.annotations.PrepareForTest;
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.Settings;
import world.bentobox.bentobox.api.commands.CompositeCommand;
@ -36,6 +45,7 @@ import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.managers.LocalesManager;
import world.bentobox.bentobox.managers.PlayersManager;
import world.bentobox.bentobox.managers.RanksManager;
/**
@ -96,15 +106,15 @@ public class AdminUnregisterCommandTest {
// Player has island to begin with
im = mock(IslandsManager.class);
when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(true);
when(im.hasIsland(Mockito.any(), Mockito.any(User.class))).thenReturn(true);
when(im.isOwner(Mockito.any(),Mockito.any())).thenReturn(true);
when(im.getOwner(Mockito.any(),Mockito.any())).thenReturn(uuid);
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);
when(im.getOwner(any(),any())).thenReturn(uuid);
when(plugin.getIslands()).thenReturn(im);
// Has team
pm = mock(PlayersManager.class);
when(im.inTeam(Mockito.any(), Mockito.eq(uuid))).thenReturn(true);
when(im.inTeam(any(), eq(uuid))).thenReturn(true);
when(plugin.getPlayers()).thenReturn(pm);
@ -115,7 +125,7 @@ public class AdminUnregisterCommandTest {
// Locales
LocalesManager lm = mock(LocalesManager.class);
when(lm.get(Mockito.any(), Mockito.any())).thenReturn("mock translation");
when(lm.get(any(), any())).thenReturn("mock translation");
when(plugin.getLocalesManager()).thenReturn(lm);
// Plugin Manager
@ -127,11 +137,11 @@ public class AdminUnregisterCommandTest {
@After
public void tearDown() {
User.clearUsers();
Mockito.framework().clearInlineMocks();
framework().clearInlineMocks();
}
/**
* Test method for .
* Test method for {@link AdminUnregisterCommand#canExecute(User, String, java.util.List)}.
*/
@Test
public void testExecuteNoTarget() {
@ -141,48 +151,86 @@ public class AdminUnregisterCommandTest {
}
/**
* Test method for .
* Test method for {@link AdminUnregisterCommand#canExecute(User, String, java.util.List)}.
*/
@Test
public void testExecuteUnknownPlayer() {
AdminUnregisterCommand itl = new AdminUnregisterCommand(ac);
String[] name = {"tastybento"};
when(pm.getUUID(Mockito.any())).thenReturn(null);
when(pm.getUUID(any())).thenReturn(null);
assertFalse(itl.canExecute(user, itl.getLabel(), Arrays.asList(name)));
Mockito.verify(user).sendMessage("general.errors.unknown-player", "[name]", name[0]);
verify(user).sendMessage("general.errors.unknown-player", "[name]", name[0]);
}
/**
* Test method for .
* Test method for {@link AdminUnregisterCommand#canExecute(User, String, java.util.List)}.
*/
@Test
public void testExecutePlayerNoIsland() {
AdminUnregisterCommand itl = new AdminUnregisterCommand(ac);
String[] name = {"tastybento"};
when(pm.getUUID(Mockito.any())).thenReturn(notUUID);
when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(false);
when(pm.getUUID(any())).thenReturn(notUUID);
when(im.hasIsland(any(), any(UUID.class))).thenReturn(false);
assertFalse(itl.canExecute(user, itl.getLabel(), Arrays.asList(name)));
Mockito.verify(user).sendMessage(Mockito.eq("general.errors.player-has-no-island"));
verify(user).sendMessage(Mockito.eq("general.errors.player-has-no-island"));
}
/**
* Test method for .
* Test method for {@link AdminUnregisterCommand#execute(User, String, java.util.List)}.
*/
@Test
public void testExecuteSuccess() {
when(im.inTeam(Mockito.any(), Mockito.any())).thenReturn(false);
when(im.inTeam(any(), any())).thenReturn(false);
Island is = mock(Island.class);
Location loc = mock(Location.class);
when(loc.toVector()).thenReturn(new Vector(123,123,432));
when(is.getCenter()).thenReturn(loc);
when(im.getIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(is);
when(im.getIsland(any(), any(UUID.class))).thenReturn(is);
String[] name = {"tastybento"};
when(pm.getUUID(Mockito.any())).thenReturn(notUUID);
when(pm.getUUID(any())).thenReturn(notUUID);
AdminUnregisterCommand itl = new AdminUnregisterCommand(ac);
assertTrue(itl.execute(user, itl.getLabel(), Arrays.asList(name)));
// Add other verifications
Mockito.verify(user).sendMessage("commands.confirmation.confirm", "[seconds]", "0");
verify(user).sendMessage("commands.confirmation.confirm", "[seconds]", "0");
}
/**
* Test method for {@link AdminUnregisterCommand#unregisterPlayer(User, UUID)}.
*/
@Test
public void testUnregisterPlayer() {
@Nullable
Island oldIsland = mock(Island.class);
@Nullable
Location center = mock(Location.class);
when(oldIsland.getCenter()).thenReturn(center);
when(center.toVector()).thenReturn(new Vector(1,2,3));
// Members
UUID uuid1 = UUID.randomUUID();
UUID uuid2 = UUID.randomUUID();
UUID uuid3 = UUID.randomUUID();
ImmutableSet<UUID> imSet = ImmutableSet.of(uuid1, uuid2, uuid3);
when(oldIsland.getMemberSet()).thenReturn(imSet);
// Trusted member
UUID uuid4 = UUID.randomUUID();
// Map must be mutable because it is cleared
Map<UUID, Integer> map = new HashMap<>();
map.put(uuid4, RanksManager.TRUSTED_RANK);
when(oldIsland.getMembers()).thenReturn(map);
// Island
when(im.getIsland(any(), any(UUID.class))).thenReturn(oldIsland);
AdminUnregisterCommand itl = new AdminUnregisterCommand(ac);
UUID targetUUID = UUID.randomUUID();
itl.unregisterPlayer(user, targetUUID);
verify(user).sendMessage("commands.admin.unregister.unregistered-island", "[xyz]", "1,2,3");
assertTrue(map.isEmpty());
verify(im).removePlayer(any(), eq(uuid1));
verify(im).removePlayer(any(), eq(uuid2));
verify(im).removePlayer(any(), eq(uuid3));
verify(pm).clearHomeLocations(any(), eq(uuid1));
verify(pm).clearHomeLocations(any(), eq(uuid2));
verify(pm).clearHomeLocations(any(), eq(uuid3));
verify(im, never()).removePlayer(any(), eq(uuid4));
}
}