Implements kicked-keep-inventory config setting.

https://github.com/BentoBoxWorld/BentoBox/issues/738
This commit is contained in:
tastybento 2019-06-07 22:18:34 -07:00
parent ccdfccdf09
commit efdb9a25dc
5 changed files with 122 additions and 56 deletions

View File

@ -81,11 +81,10 @@ public class IslandTeamKickCommand extends ConfirmableCommand {
getPlayers().save(targetUUID);
}
}
if (getIWM().isOnLeaveResetInventory(getWorld())) {
if (getIWM().isOnLeaveResetInventory(getWorld()) && !getIWM().isKickedKeepInventory(getWorld())) {
if (target.isOnline()) {
target.getPlayer().getInventory().clear();
}
else {
} else {
getPlayers().getPlayer(targetUUID).addToPendingKick(getWorld());
getPlayers().save(targetUUID);
}

View File

@ -287,7 +287,7 @@ public interface WorldSettings extends ConfigObject {
boolean isLeaversLoseReset();
/**
* @return whether players keep their inventory if they are kicked or not
* @return whether players keep their inventory when they are kicked
*/
boolean isKickedKeepInventory();

View File

@ -709,7 +709,7 @@ public class IslandWorldManager {
}
/**
* @return whether players keep their inventory if they are kicked or not
* @return whether players keep their inventory if they are kicked. Overrides leave inventory clearing
*/
public boolean isKickedKeepInventory(@NonNull World world) {
return gameModes.get(world).getWorldSettings().isKickedKeepInventory();

View File

@ -4,6 +4,9 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.any;
import java.util.Collections;
import java.util.HashMap;
@ -22,6 +25,7 @@ import org.bukkit.scheduler.BukkitScheduler;
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;
@ -34,6 +38,7 @@ import world.bentobox.bentobox.api.commands.CompositeCommand;
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.database.objects.Players;
import world.bentobox.bentobox.managers.CommandsManager;
import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.managers.IslandsManager;
@ -48,15 +53,25 @@ import world.bentobox.bentobox.managers.PlayersManager;
@PrepareForTest({Bukkit.class, BentoBox.class, User.class })
public class IslandTeamKickCommandTest {
@Mock
private CompositeCommand ic;
private UUID uuid;
@Mock
private User user;
@Mock
private Settings s;
@Mock
private IslandsManager im;
@Mock
private PlayersManager pm;
private UUID notUUID;
@Mock
private IslandWorldManager iwm;
@Mock
private Player player;
@Mock
private Player target;
@Mock
private CompositeCommand subCommand;
/**
@ -73,19 +88,21 @@ public class IslandTeamKickCommandTest {
when(plugin.getCommandsManager()).thenReturn(cm);
// Settings
s = mock(Settings.class);
when(plugin.getSettings()).thenReturn(s);
// Player
player = mock(Player.class);
// Sometimes use Mockito.withSettings().verboseLogging()
user = mock(User.class);
when(user.isOp()).thenReturn(false);
uuid = UUID.randomUUID();
notUUID = UUID.randomUUID();
while(notUUID.equals(uuid)) {
notUUID = UUID.randomUUID();
}
when(target.getUniqueId()).thenReturn(notUUID);
when(target.isOnline()).thenReturn(true);
when(target.getName()).thenReturn("poslovitch");
// Set the target user
User.getInstance(target);
when(user.getUniqueId()).thenReturn(uuid);
when(user.getPlayer()).thenReturn(player);
when(user.getName()).thenReturn("tastybento");
@ -100,14 +117,14 @@ public class IslandTeamKickCommandTest {
// Player has island to begin with
im = mock(IslandsManager.class);
when(im.hasIsland(Mockito.any(), Mockito.any(UUID.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.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);
@ -119,21 +136,21 @@ public class IslandTeamKickCommandTest {
// 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);
// Placeholders
PlaceholdersManager placeholdersManager = mock(PlaceholdersManager.class);
when(plugin.getPlaceholdersManager()).thenReturn(placeholdersManager);
when(placeholdersManager.replacePlaceholders(Mockito.any(), Mockito.any())).thenReturn("mock translation");
when(placeholdersManager.replacePlaceholders(any(), any())).thenReturn("mock translation");
// IWM friendly name
iwm = mock(IslandWorldManager.class);
when(iwm.getFriendlyName(Mockito.any())).thenReturn("BSkyBlock");
when(iwm.getFriendlyName(any())).thenReturn("BSkyBlock");
when(plugin.getIWM()).thenReturn(iwm);
// Addon
when(iwm.getAddon(Mockito.any())).thenReturn(Optional.empty());
when(iwm.getAddon(any())).thenReturn(Optional.empty());
// Plugin Manager
Server server = mock(Server.class);
@ -144,7 +161,7 @@ public class IslandTeamKickCommandTest {
// Island
Island island = mock(Island.class);
when(island.getUniqueId()).thenReturn("uniqueid");
when(im.getIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(island);
when(im.getIsland(any(), any(UUID.class))).thenReturn(island);
}
@ -153,10 +170,10 @@ public class IslandTeamKickCommandTest {
*/
@Test
public void testExecuteNoTeam() {
when(im.inTeam(Mockito.any(), Mockito.eq(uuid))).thenReturn(false);
when(im.inTeam(any(), eq(uuid))).thenReturn(false);
IslandTeamKickCommand itl = new IslandTeamKickCommand(ic);
assertFalse(itl.execute(user, itl.getLabel(), Collections.emptyList()));
Mockito.verify(user).sendMessage(Mockito.eq("general.errors.no-team"));
verify(user).sendMessage(eq("general.errors.no-team"));
}
/**
@ -164,10 +181,10 @@ public class IslandTeamKickCommandTest {
*/
@Test
public void testExecuteNotTeamOwner() {
when(im.getOwner(Mockito.any(), Mockito.any())).thenReturn(notUUID);
when(im.getOwner(any(), any())).thenReturn(notUUID);
IslandTeamKickCommand itl = new IslandTeamKickCommand(ic);
assertFalse(itl.execute(user, itl.getLabel(), Collections.emptyList()));
Mockito.verify(user).sendMessage(Mockito.eq("general.errors.not-owner"));
verify(user).sendMessage(eq("general.errors.not-owner"));
}
/**
@ -186,9 +203,9 @@ public class IslandTeamKickCommandTest {
@Test
public void testExecuteUnknownPlayer() {
IslandTeamKickCommand itl = new IslandTeamKickCommand(ic);
when(pm.getUUID(Mockito.any())).thenReturn(null);
when(pm.getUUID(any())).thenReturn(null);
assertFalse(itl.execute(user, itl.getLabel(), Collections.singletonList("poslovitch")));
Mockito.verify(user).sendMessage("general.errors.unknown-player", "[name]", "poslovitch");
verify(user).sendMessage("general.errors.unknown-player", "[name]", "poslovitch");
}
/**
@ -197,9 +214,9 @@ public class IslandTeamKickCommandTest {
@Test
public void testExecuteSamePlayer() {
IslandTeamKickCommand itl = new IslandTeamKickCommand(ic);
when(pm.getUUID(Mockito.any())).thenReturn(uuid);
when(pm.getUUID(any())).thenReturn(uuid);
assertFalse(itl.execute(user, itl.getLabel(), Collections.singletonList("poslovitch")));
Mockito.verify(user).sendMessage(Mockito.eq("commands.island.kick.cannot-kick"));
verify(user).sendMessage(eq("commands.island.kick.cannot-kick"));
}
/**
@ -208,10 +225,10 @@ public class IslandTeamKickCommandTest {
@Test
public void testExecuteDifferentPlayerNotInTeam() {
IslandTeamKickCommand itl = new IslandTeamKickCommand(ic);
when(pm.getUUID(Mockito.any())).thenReturn(notUUID);
when(im.getMembers(Mockito.any(), Mockito.any())).thenReturn(new HashSet<>());
when(pm.getUUID(any())).thenReturn(notUUID);
when(im.getMembers(any(), any())).thenReturn(new HashSet<>());
assertFalse(itl.execute(user, itl.getLabel(), Collections.singletonList("poslovitch")));
Mockito.verify(user).sendMessage(Mockito.eq("general.errors.not-in-team"));
verify(user).sendMessage(eq("general.errors.not-in-team"));
}
/**
@ -221,17 +238,72 @@ public class IslandTeamKickCommandTest {
public void testExecuteNoConfirmation() {
when(s.isKickConfirmation()).thenReturn(false);
when(pm.getUUID(Mockito.any())).thenReturn(notUUID);
when(pm.getUUID(any())).thenReturn(notUUID);
when(pm.getName(notUUID)).thenReturn("poslovitch");
Set<UUID> members = new HashSet<>();
members.add(notUUID);
when(im.getMembers(Mockito.any(), Mockito.any())).thenReturn(members);
when(im.getMembers(any(), any())).thenReturn(members);
IslandTeamKickCommand itl = new IslandTeamKickCommand(ic);
assertTrue(itl.execute(user, itl.getLabel(), Collections.singletonList("poslovitch")));
Mockito.verify(im).removePlayer(Mockito.any(), Mockito.eq(notUUID));
Mockito.verify(user).sendMessage("commands.island.team.kick.success", TextVariables.NAME, "poslovitch");
verify(im).removePlayer(any(), eq(notUUID));
verify(user).sendMessage("commands.island.team.kick.success", TextVariables.NAME, "poslovitch");
}
/**
* Test method for {@link IslandTeamKickCommand#execute(User, String, java.util.List)}
*/
@Test
public void testExecuteNoConfirmationKeepInventory() {
when(iwm.isOnLeaveResetInventory(any())).thenReturn(true);
when(iwm.isKickedKeepInventory(any())).thenReturn(true);
when(s.isKickConfirmation()).thenReturn(false);
when(pm.getUUID(any())).thenReturn(notUUID);
when(pm.getName(notUUID)).thenReturn("poslovitch");
Set<UUID> members = new HashSet<>();
members.add(notUUID);
when(im.getMembers(any(), any())).thenReturn(members);
IslandTeamKickCommand itl = new IslandTeamKickCommand(ic);
assertTrue(itl.execute(user, itl.getLabel(), Collections.singletonList("poslovitch")));
verify(im).removePlayer(any(), eq(notUUID));
verify(user).sendMessage("commands.island.team.kick.success", TextVariables.NAME, "poslovitch");
verify(target, Mockito.never()).getInventory();
}
/**
* Test method for {@link IslandTeamKickCommand#execute(User, String, java.util.List)}
*/
@Test
public void testExecuteNoConfirmationLoseInventoryOffline() {
when(iwm.isOnLeaveResetInventory(any())).thenReturn(true);
when(iwm.isKickedKeepInventory(any())).thenReturn(false);
when(s.isKickConfirmation()).thenReturn(false);
when(pm.getUUID(any())).thenReturn(notUUID);
when(pm.getName(notUUID)).thenReturn("poslovitch");
Players targetPlayer = mock(Players.class);
when(pm.getPlayer(eq(notUUID))).thenReturn(targetPlayer);
when(target.isOnline()).thenReturn(false);
Set<UUID> members = new HashSet<>();
members.add(notUUID);
when(im.getMembers(any(), any())).thenReturn(members);
IslandTeamKickCommand itl = new IslandTeamKickCommand(ic);
assertTrue(itl.execute(user, itl.getLabel(), Collections.singletonList("poslovitch")));
verify(im).removePlayer(any(), eq(notUUID));
verify(user).sendMessage("commands.island.team.kick.success", TextVariables.NAME, "poslovitch");
verify(target, Mockito.never()).getInventory();
verify(pm).getPlayer(notUUID);
verify(targetPlayer).addToPendingKick(any());
}
/**
@ -241,16 +313,16 @@ public class IslandTeamKickCommandTest {
public void testExecuteWithConfirmation() {
when(s.isKickConfirmation()).thenReturn(true);
when(pm.getUUID(Mockito.any())).thenReturn(notUUID);
when(pm.getUUID(any())).thenReturn(notUUID);
Set<UUID> members = new HashSet<>();
members.add(notUUID);
when(im.getMembers(Mockito.any(), Mockito.any())).thenReturn(members);
when(im.getMembers(any(), any())).thenReturn(members);
IslandTeamKickCommand itl = new IslandTeamKickCommand(ic);
assertFalse(itl.execute(user, itl.getLabel(), Collections.singletonList("poslovitch")));
// Confirmation required
Mockito.verify(user).sendMessage(Mockito.eq("commands.confirmation.confirm"), Mockito.eq("[seconds]"), Mockito.eq("0"));
verify(user).sendMessage(eq("commands.confirmation.confirm"), eq("[seconds]"), eq("0"));
}
/**
@ -260,38 +332,34 @@ public class IslandTeamKickCommandTest {
public void testExecuteTestResets() {
when(s.isKickConfirmation()).thenReturn(false);
// Create the target user
when(pm.getUUID(Mockito.any())).thenReturn(notUUID);
Player targetPlayer = mock(Player.class);
when(targetPlayer.getUniqueId()).thenReturn(notUUID);
when(targetPlayer.isOnline()).thenReturn(true);
when(targetPlayer.getName()).thenReturn("poslovitch");
User.getInstance(targetPlayer);
when(pm.getUUID(any())).thenReturn(notUUID);
when(target.getName()).thenReturn("poslovitch");
// Target's inventory
PlayerInventory inv = mock(PlayerInventory.class);
when(targetPlayer.getInventory()).thenReturn(inv);
when(target.getInventory()).thenReturn(inv);
Inventory enderChest = mock(Inventory.class);
when(targetPlayer.getEnderChest()).thenReturn(enderChest);
when(target.getEnderChest()).thenReturn(enderChest);
// Set the user in Users
Set<UUID> members = new HashSet<>();
// Add the team members
members.add(notUUID);
when(im.getMembers(Mockito.any(), Mockito.any())).thenReturn(members);
when(im.getMembers(any(), any())).thenReturn(members);
// Require resets
when(iwm.isOnLeaveResetEnderChest(Mockito.any())).thenReturn(true);
when(iwm.isOnLeaveResetInventory(Mockito.any())).thenReturn(true);
when(iwm.isOnLeaveResetMoney(Mockito.any())).thenReturn(true);
when(iwm.isOnLeaveResetEnderChest(any())).thenReturn(true);
when(iwm.isOnLeaveResetInventory(any())).thenReturn(true);
when(iwm.isOnLeaveResetMoney(any())).thenReturn(true);
IslandTeamKickCommand itl = new IslandTeamKickCommand(ic);
// Kick the team member
assertTrue(itl.execute(user, itl.getLabel(), Collections.singletonList("poslovitch")));
// Verify
Mockito.verify(im).removePlayer(Mockito.any(), Mockito.eq(notUUID));
Mockito.verify(user).sendMessage("commands.island.team.kick.success", TextVariables.NAME, "poslovitch");
Mockito.verify(enderChest).clear();
Mockito.verify(inv).clear();
verify(im).removePlayer(any(), eq(notUUID));
verify(user).sendMessage("commands.island.team.kick.success", TextVariables.NAME, "poslovitch");
verify(enderChest).clear();
verify(inv).clear();
}
/**
@ -302,6 +370,6 @@ public class IslandTeamKickCommandTest {
// 10 minutes = 600 seconds
when(s.getInviteCooldown()).thenReturn(10);
testExecuteNoConfirmation();
Mockito.verify(subCommand).setCooldown("uniqueid", notUUID.toString(), 600);
verify(subCommand).setCooldown("uniqueid", notUUID.toString(), 600);
}
}

View File

@ -2,12 +2,11 @@ package world.bentobox.bentobox.api.commands.island.team;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.HashMap;