mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2025-02-10 09:21:31 +01:00
Fix issue where players with more than one island couldn't leave a team
This commit is contained in:
parent
26e6310fba
commit
54ebf2dfd2
@ -16,97 +16,92 @@ import world.bentobox.bentobox.managers.RanksManager;
|
||||
public class IslandTeamLeaveCommand extends ConfirmableCommand {
|
||||
|
||||
public IslandTeamLeaveCommand(CompositeCommand islandTeamCommand) {
|
||||
super(islandTeamCommand, "leave");
|
||||
super(islandTeamCommand, "leave");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setup() {
|
||||
setPermission("island.team.leave");
|
||||
setOnlyPlayer(true);
|
||||
setDescription("commands.island.team.leave.description");
|
||||
setPermission("island.team.leave");
|
||||
setOnlyPlayer(true);
|
||||
setDescription("commands.island.team.leave.description");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean execute(User user, String label, List<String> args) {
|
||||
if (!getIslands().inTeam(getWorld(), user.getUniqueId())) {
|
||||
user.sendMessage("general.errors.no-team");
|
||||
return false;
|
||||
}
|
||||
if (getIslands().hasIsland(getWorld(), user.getUniqueId())) {
|
||||
user.sendMessage("commands.island.team.leave.cannot-leave");
|
||||
return false;
|
||||
}
|
||||
if (!getSettings().isLeaveConfirmation()) {
|
||||
leave(user);
|
||||
return true;
|
||||
} else {
|
||||
// Check resets
|
||||
if (getIWM().isLeaversLoseReset(getWorld())) {
|
||||
showResets(user);
|
||||
}
|
||||
this.askConfirmation(user, () -> leave(user));
|
||||
return false;
|
||||
}
|
||||
if (!getIslands().inTeam(getWorld(), user.getUniqueId())) {
|
||||
user.sendMessage("general.errors.no-team");
|
||||
return false;
|
||||
}
|
||||
if (user.getUniqueId().equals(getIslands().getPrimaryIsland(getWorld(), user.getUniqueId()).getOwner())) {
|
||||
user.sendMessage("commands.island.team.leave.cannot-leave");
|
||||
return false;
|
||||
}
|
||||
if (!getSettings().isLeaveConfirmation()) {
|
||||
leave(user);
|
||||
return true;
|
||||
} else {
|
||||
// Check resets
|
||||
if (getIWM().isLeaversLoseReset(getWorld())) {
|
||||
showResets(user);
|
||||
}
|
||||
this.askConfirmation(user, () -> leave(user));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private void showResets(User user) {
|
||||
int resetsLeft = getPlayers().getResetsLeft(getWorld(), user.getUniqueId());
|
||||
if (resetsLeft != -1) {
|
||||
// Resets are not unlimited here
|
||||
if (resetsLeft == 0) {
|
||||
// No resets allowed
|
||||
user.sendMessage("commands.island.reset.none-left");
|
||||
} else {
|
||||
// Still some resets left
|
||||
// Notify how many resets are left
|
||||
user.sendMessage("commands.island.reset.resets-left", TextVariables.NUMBER, String.valueOf(resetsLeft));
|
||||
}
|
||||
}
|
||||
int resetsLeft = getPlayers().getResetsLeft(getWorld(), user.getUniqueId());
|
||||
if (resetsLeft != -1) {
|
||||
// Resets are not unlimited here
|
||||
if (resetsLeft == 0) {
|
||||
// No resets allowed
|
||||
user.sendMessage("commands.island.reset.none-left");
|
||||
} else {
|
||||
// Still some resets left
|
||||
// Notify how many resets are left
|
||||
user.sendMessage("commands.island.reset.resets-left", TextVariables.NUMBER, String.valueOf(resetsLeft));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void leave(User user) {
|
||||
Island island = getIslands().getIsland(getWorld(), user);
|
||||
if (island == null) {
|
||||
user.sendMessage("general.errors.no-island");
|
||||
return;
|
||||
}
|
||||
// Fire event
|
||||
IslandBaseEvent event = TeamEvent.builder()
|
||||
.island(island)
|
||||
.reason(TeamEvent.Reason.LEAVE)
|
||||
.involvedPlayer(user.getUniqueId())
|
||||
.build();
|
||||
if (event.isCancelled()) {
|
||||
return;
|
||||
}
|
||||
UUID ownerUUID = island.getOwner();
|
||||
if (ownerUUID != null) {
|
||||
User.getInstance(ownerUUID).sendMessage("commands.island.team.leave.left-your-island", TextVariables.NAME, user.getName(), TextVariables.DISPLAY_NAME, user.getDisplayName());
|
||||
}
|
||||
getIslands().removePlayer(island, user.getUniqueId());
|
||||
// Clean the player
|
||||
getPlayers().cleanLeavingPlayer(getWorld(), user, false, island);
|
||||
Island island = getIslands().getIsland(getWorld(), user);
|
||||
if (island == null) {
|
||||
user.sendMessage("general.errors.no-island");
|
||||
return;
|
||||
}
|
||||
// Fire event
|
||||
IslandBaseEvent event = TeamEvent.builder().island(island).reason(TeamEvent.Reason.LEAVE)
|
||||
.involvedPlayer(user.getUniqueId()).build();
|
||||
if (event.isCancelled()) {
|
||||
return;
|
||||
}
|
||||
UUID ownerUUID = island.getOwner();
|
||||
if (ownerUUID != null) {
|
||||
User.getInstance(ownerUUID).sendMessage("commands.island.team.leave.left-your-island", TextVariables.NAME,
|
||||
user.getName(), TextVariables.DISPLAY_NAME, user.getDisplayName());
|
||||
}
|
||||
getIslands().removePlayer(island, user.getUniqueId());
|
||||
// Clean the player
|
||||
getPlayers().cleanLeavingPlayer(getWorld(), user, false, island);
|
||||
|
||||
// Add cooldown for this player and target
|
||||
if (getSettings().getInviteCooldown() > 0 && getParent() != null) {
|
||||
// Get the invite class from the parent
|
||||
getParent().getSubCommand("invite").ifPresent(c -> c.setCooldown(island.getUniqueId(), user.getUniqueId().toString(), getSettings().getInviteCooldown() * 60));
|
||||
}
|
||||
// Remove reset if required
|
||||
if (getIWM().isLeaversLoseReset(getWorld())) {
|
||||
// Add a reset
|
||||
getPlayers().addReset(getWorld(), user.getUniqueId());
|
||||
// Notify how many resets are left
|
||||
showResets(user);
|
||||
}
|
||||
user.sendMessage("commands.island.team.leave.success");
|
||||
IslandEvent.builder()
|
||||
.island(island)
|
||||
.involvedPlayer(user.getUniqueId())
|
||||
.admin(false)
|
||||
.reason(IslandEvent.Reason.RANK_CHANGE)
|
||||
.rankChange(island.getRank(user), RanksManager.VISITOR_RANK)
|
||||
.build();
|
||||
// Add cooldown for this player and target
|
||||
if (getSettings().getInviteCooldown() > 0 && getParent() != null) {
|
||||
// Get the invite class from the parent
|
||||
getParent().getSubCommand("invite").ifPresent(c -> c.setCooldown(island.getUniqueId(),
|
||||
user.getUniqueId().toString(), getSettings().getInviteCooldown() * 60));
|
||||
}
|
||||
// Remove reset if required
|
||||
if (getIWM().isLeaversLoseReset(getWorld())) {
|
||||
// Add a reset
|
||||
getPlayers().addReset(getWorld(), user.getUniqueId());
|
||||
// Notify how many resets are left
|
||||
showResets(user);
|
||||
}
|
||||
user.sendMessage("commands.island.team.leave.success");
|
||||
IslandEvent.builder().island(island).involvedPlayer(user.getUniqueId()).admin(false)
|
||||
.reason(IslandEvent.Reason.RANK_CHANGE).rankChange(island.getRank(user), RanksManager.VISITOR_RANK)
|
||||
.build();
|
||||
}
|
||||
}
|
@ -38,6 +38,8 @@ 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.LocalesManager;
|
||||
import world.bentobox.bentobox.managers.PlaceholdersManager;
|
||||
import world.bentobox.bentobox.managers.PlayersManager;
|
||||
|
||||
/**
|
||||
@ -48,87 +50,98 @@ import world.bentobox.bentobox.managers.PlayersManager;
|
||||
@PrepareForTest({ Bukkit.class, BentoBox.class, User.class })
|
||||
public class IslandTeamLeaveCommandTest {
|
||||
|
||||
@Mock
|
||||
private CompositeCommand ic;
|
||||
private UUID uuid;
|
||||
@Mock
|
||||
private User user;
|
||||
@Mock
|
||||
private Settings s;
|
||||
@Mock
|
||||
private IslandsManager im;
|
||||
@Mock
|
||||
private IslandWorldManager iwm;
|
||||
@Mock
|
||||
private Player player;
|
||||
@Mock
|
||||
private CompositeCommand subCommand;
|
||||
@Mock
|
||||
private PlayersManager pm;
|
||||
@Mock
|
||||
private World world;
|
||||
@Mock
|
||||
private @Nullable Island island;
|
||||
@Mock
|
||||
private CompositeCommand ic;
|
||||
private UUID uuid;
|
||||
@Mock
|
||||
private User user;
|
||||
@Mock
|
||||
private Settings s;
|
||||
@Mock
|
||||
private IslandsManager im;
|
||||
@Mock
|
||||
private IslandWorldManager iwm;
|
||||
@Mock
|
||||
private Player player;
|
||||
@Mock
|
||||
private CompositeCommand subCommand;
|
||||
@Mock
|
||||
private PlayersManager pm;
|
||||
@Mock
|
||||
private World world;
|
||||
@Mock
|
||||
private @Nullable Island island;
|
||||
|
||||
/**
|
||||
*/
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
// Set up plugin
|
||||
BentoBox plugin = mock(BentoBox.class);
|
||||
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
|
||||
/**
|
||||
*/
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
// Set up plugin
|
||||
BentoBox plugin = mock(BentoBox.class);
|
||||
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
|
||||
|
||||
// Command manager
|
||||
CommandsManager cm = mock(CommandsManager.class);
|
||||
when(plugin.getCommandsManager()).thenReturn(cm);
|
||||
// Command manager
|
||||
CommandsManager cm = mock(CommandsManager.class);
|
||||
when(plugin.getCommandsManager()).thenReturn(cm);
|
||||
|
||||
// Settings
|
||||
when(s.getResetCooldown()).thenReturn(0);
|
||||
when(plugin.getSettings()).thenReturn(s);
|
||||
// Settings
|
||||
when(s.getResetCooldown()).thenReturn(0);
|
||||
when(plugin.getSettings()).thenReturn(s);
|
||||
|
||||
// Player
|
||||
// Sometimes use Mockito.withSettings().verboseLogging()
|
||||
when(user.isOp()).thenReturn(false);
|
||||
uuid = UUID.randomUUID();
|
||||
when(user.getUniqueId()).thenReturn(uuid);
|
||||
when(user.getPlayer()).thenReturn(player);
|
||||
when(user.getName()).thenReturn("tastybento");
|
||||
// Player
|
||||
// Sometimes use Mockito.withSettings().verboseLogging()
|
||||
when(user.isOp()).thenReturn(false);
|
||||
uuid = UUID.randomUUID();
|
||||
when(user.getUniqueId()).thenReturn(uuid);
|
||||
when(user.getPlayer()).thenReturn(player);
|
||||
when(user.getName()).thenReturn("tastybento");
|
||||
User.setPlugin(plugin);
|
||||
|
||||
// Parent command has no aliases
|
||||
ic = mock(CompositeCommand.class);
|
||||
when(ic.getSubCommandAliases()).thenReturn(new HashMap<>());
|
||||
Optional<CompositeCommand> optionalCommand = Optional.of(subCommand);
|
||||
when(ic.getSubCommand(Mockito.anyString())).thenReturn(optionalCommand);
|
||||
when(ic.getWorld()).thenReturn(world);
|
||||
// Parent command has no aliases
|
||||
ic = mock(CompositeCommand.class);
|
||||
when(ic.getSubCommandAliases()).thenReturn(new HashMap<>());
|
||||
Optional<CompositeCommand> optionalCommand = Optional.of(subCommand);
|
||||
when(ic.getSubCommand(Mockito.anyString())).thenReturn(optionalCommand);
|
||||
when(ic.getWorld()).thenReturn(world);
|
||||
|
||||
// Player has island to begin with
|
||||
when(im.hasIsland(any(), Mockito.any(UUID.class))).thenReturn(true);
|
||||
// when(im.isOwner(any(), any())).thenReturn(true);
|
||||
when(plugin.getIslands()).thenReturn(im);
|
||||
// Player has island to begin with
|
||||
when(island.getOwner()).thenReturn(UUID.randomUUID());
|
||||
when(im.getPrimaryIsland(world, uuid)).thenReturn(island);
|
||||
// when(im.isOwner(any(), any())).thenReturn(true);
|
||||
when(plugin.getIslands()).thenReturn(im);
|
||||
|
||||
// Has team
|
||||
when(im.inTeam(any(), eq(uuid))).thenReturn(true);
|
||||
when(plugin.getPlayers()).thenReturn(pm);
|
||||
// Has team
|
||||
when(im.inTeam(any(), eq(uuid))).thenReturn(true);
|
||||
when(plugin.getPlayers()).thenReturn(pm);
|
||||
|
||||
// Server & Scheduler
|
||||
BukkitScheduler sch = mock(BukkitScheduler.class);
|
||||
PowerMockito.mockStatic(Bukkit.class);
|
||||
when(Bukkit.getScheduler()).thenReturn(sch);
|
||||
// Server & Scheduler
|
||||
BukkitScheduler sch = mock(BukkitScheduler.class);
|
||||
PowerMockito.mockStatic(Bukkit.class);
|
||||
when(Bukkit.getScheduler()).thenReturn(sch);
|
||||
|
||||
// Island World Manager
|
||||
when(plugin.getIWM()).thenReturn(iwm);
|
||||
// Island World Manager
|
||||
when(plugin.getIWM()).thenReturn(iwm);
|
||||
|
||||
// Plugin Manager
|
||||
PluginManager pim = mock(PluginManager.class);
|
||||
when(Bukkit.getPluginManager()).thenReturn(pim);
|
||||
// Plugin Manager
|
||||
PluginManager pim = mock(PluginManager.class);
|
||||
when(Bukkit.getPluginManager()).thenReturn(pim);
|
||||
|
||||
// Island
|
||||
when(island.getUniqueId()).thenReturn("uniqueid");
|
||||
when(im.getIsland(world, user)).thenReturn(island);
|
||||
// Island
|
||||
when(island.getUniqueId()).thenReturn("uniqueid");
|
||||
when(im.getIsland(world, user)).thenReturn(island);
|
||||
|
||||
}
|
||||
// Locales
|
||||
LocalesManager lm = mock(LocalesManager.class);
|
||||
when(lm.get(any(), any())).thenAnswer(invocation -> invocation.getArgument(1, String.class));
|
||||
when(plugin.getLocalesManager()).thenReturn(lm);
|
||||
PlaceholdersManager phm = mock(PlaceholdersManager.class);
|
||||
when(phm.replacePlaceholders(any(), any())).thenAnswer(invocation -> invocation.getArgument(1, String.class));
|
||||
// Placeholder manager
|
||||
when(plugin.getPlaceholdersManager()).thenReturn(phm);
|
||||
|
||||
/**
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamLeaveCommand#execute(User, String, java.util.List)}
|
||||
*/
|
||||
@Test
|
||||
@ -139,27 +152,24 @@ public class IslandTeamLeaveCommandTest {
|
||||
verify(user).sendMessage(eq("general.errors.no-team"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for
|
||||
* {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamLeaveCommand#execute(User, String, java.util.List)}
|
||||
*/
|
||||
@Test
|
||||
public void testExecuteInOwner() {
|
||||
IslandTeamLeaveCommand itl = new IslandTeamLeaveCommand(ic);
|
||||
assertFalse(itl.execute(user, itl.getLabel(), new ArrayList<>()));
|
||||
verify(user).sendMessage(eq("commands.island.team.leave.cannot-leave"));
|
||||
}
|
||||
/**
|
||||
* Test method for
|
||||
* {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamLeaveCommand#execute(User, String, java.util.List)}
|
||||
*/
|
||||
@Test
|
||||
public void testExecuteIsOwner() {
|
||||
when(island.getOwner()).thenReturn(uuid);
|
||||
IslandTeamLeaveCommand itl = new IslandTeamLeaveCommand(ic);
|
||||
assertFalse(itl.execute(user, itl.getLabel(), new ArrayList<>()));
|
||||
verify(user).sendMessage(eq("commands.island.team.leave.cannot-leave"));
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamLeaveCommand#execute(User, String, java.util.List)}
|
||||
*/
|
||||
@Test
|
||||
public void testExecuteNoConfirmation() {
|
||||
when(s.isLeaveConfirmation()).thenReturn(false);
|
||||
when(im.hasIsland(any(), eq(uuid))).thenReturn(false);
|
||||
//when(im.isOwner(any(), eq(uuid))).thenReturn(false);
|
||||
// Add a team owner - null
|
||||
///when(im.getOwner(any(), any())).thenReturn(null);
|
||||
|
||||
IslandTeamLeaveCommand itl = new IslandTeamLeaveCommand(ic);
|
||||
assertTrue(itl.execute(user, itl.getLabel(), new ArrayList<>()));
|
||||
@ -167,7 +177,7 @@ public class IslandTeamLeaveCommandTest {
|
||||
verify(user).sendMessage(eq("commands.island.team.leave.success"));
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamLeaveCommand#execute(User, String, java.util.List)}
|
||||
*/
|
||||
@Test
|
||||
@ -175,18 +185,13 @@ public class IslandTeamLeaveCommandTest {
|
||||
when(s.isLeaveConfirmation()).thenReturn(true);
|
||||
// 3 second timeout
|
||||
when(s.getConfirmationTime()).thenReturn(3);
|
||||
when(im.hasIsland(any(), eq(uuid))).thenReturn(false);
|
||||
//when(im.isOwner(any(), eq(uuid))).thenReturn(false);
|
||||
// Add a team owner - null
|
||||
//when(im.getOwner(any(), any())).thenReturn(null);
|
||||
|
||||
IslandTeamLeaveCommand itl = new IslandTeamLeaveCommand(ic);
|
||||
assertFalse(itl.execute(user, itl.getLabel(), new ArrayList<>()));
|
||||
// Confirmation required
|
||||
verify(user).sendMessage(eq("commands.confirmation.confirm"), eq("[seconds]"), eq("3"));
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamLeaveCommand#execute(User, String, java.util.List)}
|
||||
*/
|
||||
@Test
|
||||
@ -195,17 +200,13 @@ public class IslandTeamLeaveCommandTest {
|
||||
when(iwm.isLeaversLoseReset(any())).thenReturn(true);
|
||||
|
||||
when(s.isLeaveConfirmation()).thenReturn(false);
|
||||
when(im.hasIsland(any(), eq(uuid))).thenReturn(false);
|
||||
//when(im.isOwner(any(), eq(uuid))).thenReturn(false);
|
||||
// Add a team owner - null
|
||||
//when(im.getOwner(any(), any())).thenReturn(null);
|
||||
|
||||
IslandTeamLeaveCommand itl = new IslandTeamLeaveCommand(ic);
|
||||
assertTrue(itl.execute(user, itl.getLabel(), new ArrayList<>()));
|
||||
verify(user).sendMessage("commands.island.reset.none-left");
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamLeaveCommand#execute(User, String, java.util.List)}
|
||||
*/
|
||||
@Test
|
||||
@ -215,10 +216,6 @@ public class IslandTeamLeaveCommandTest {
|
||||
when(pm.getResetsLeft(any(),any(UUID.class))).thenReturn(100);
|
||||
|
||||
when(s.isLeaveConfirmation()).thenReturn(false);
|
||||
when(im.hasIsland(any(), eq(uuid))).thenReturn(false);
|
||||
//when(im.isOwner(any(), eq(uuid))).thenReturn(false);
|
||||
// Add a team owner - null
|
||||
//when(im.getOwner(any(), any())).thenReturn(null);
|
||||
|
||||
IslandTeamLeaveCommand itl = new IslandTeamLeaveCommand(ic);
|
||||
assertTrue(itl.execute(user, itl.getLabel(), new ArrayList<>()));
|
||||
@ -228,7 +225,7 @@ public class IslandTeamLeaveCommandTest {
|
||||
verify(user).sendMessage("commands.island.reset.resets-left", TextVariables.NUMBER, "100");
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* Test method for {@link IslandTeamLeaveCommand#execute(User, String, java.util.List)}
|
||||
*/
|
||||
@Test
|
||||
|
Loading…
Reference in New Issue
Block a user