Prevent players from obtaining more concurrent islands by owner transfer

This commit is contained in:
tastybento 2023-09-04 16:25:36 -07:00
parent aeaa807629
commit f970573102
3 changed files with 54 additions and 21 deletions

View File

@ -34,36 +34,41 @@ public class IslandTeamSetownerCommand extends CompositeCommand {
@Override
public boolean canExecute(User user, String label, List<String> args) {
UUID playerUUID = user.getUniqueId();
// If args are not right, show help
if (args.size() != 1) {
showHelp(this, user);
return false;
}
// Can use if in a team
boolean inTeam = getIslands().inTeam(getWorld(), playerUUID);
boolean inTeam = getIslands().inTeam(getWorld(), user.getUniqueId());
if (!inTeam) {
user.sendMessage("general.errors.no-team");
return false;
}
UUID ownerUUID = getIslands().getOwner(getWorld(), user.getUniqueId());
if (ownerUUID == null || !ownerUUID.equals(playerUUID)) {
if (ownerUUID == null || !ownerUUID.equals(user.getUniqueId())) {
user.sendMessage("general.errors.not-owner");
return false;
}
// If args are not right, show help
if (args.size() != 1) {
showHelp(this, user);
return false;
}
targetUUID = getPlayers().getUUID(args.get(0));
if (targetUUID == null) {
user.sendMessage("general.errors.unknown-player", TextVariables.NAME, args.get(0));
return false;
}
if (targetUUID.equals(playerUUID)) {
if (targetUUID.equals(user.getUniqueId())) {
user.sendMessage("commands.island.team.setowner.errors.cant-transfer-to-yourself");
return false;
}
if (!getIslands().getMembers(getWorld(), playerUUID).contains(targetUUID)) {
if (!getIslands().getMembers(getWorld(), user.getUniqueId()).contains(targetUUID)) {
user.sendMessage("commands.island.team.setowner.errors.target-is-not-member");
return false;
}
// Check how many islands target has
if (getIslands().getNumberOfConcurrentIslands(targetUUID, getWorld()) >= this.getIWM().getWorldSettings(getWorld()).getConcurrentIslands()) {
// Too many
user.sendMessage("commands.island.team.setowner.errors.at-max");
return false;
}
return true;
}

View File

@ -41,7 +41,6 @@ general:
you-must-wait: '&c You must wait [number]s before you can do that command again.'
must-be-positive-number: '&c [number] is not a valid positive number.'
not-on-island: '&c You are not on island!'
you-cannot-make: '&c You cannot make any more islands!'
worlds:
overworld: Overworld
nether: Nether
@ -532,6 +531,7 @@ commands:
unable-create-island: '&c Your island could not be generated, please contact
an administrator.'
creating-island: '&a Finding a spot for your island...'
you-cannot-make: '&c You cannot make any more islands!'
pasting:
estimated-time: '&a Estimated time: &b [number] &a seconds.'
blocks: '&a Building it block by block: &b [number] &a blocks in all...'
@ -729,6 +729,7 @@ commands:
&7 (&o Well, in fact, you could... But we don''t want you to. Because
it''s useless.&r &7 )'
target-is-not-member: '&c That player is not part of your island team!'
at-max: '&c That player already has the maximum number of islands they are allowed!'
name-is-the-owner: '&a [name] is now the island owner!'
parameters: <player>
you-are-the-owner: '&a You are now the island owner!'

View File

@ -12,6 +12,7 @@ import static org.mockito.Mockito.when;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
@ -20,6 +21,7 @@ import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginManager;
import org.bukkit.scheduler.BukkitScheduler;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.junit.After;
import org.junit.Before;
@ -35,6 +37,7 @@ import org.powermock.reflect.Whitebox;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.Settings;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.configuration.WorldSettings;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
@ -119,6 +122,10 @@ public class IslandTeamSetownerCommandTest {
// Island World Manager
when(plugin.getIWM()).thenReturn(iwm);
@NonNull
WorldSettings ws = mock(WorldSettings.class);
when(iwm.getWorldSettings(world)).thenReturn(ws);
when(ws.getConcurrentIslands()).thenReturn(3);
// Plugin Manager
PluginManager pim = mock(PluginManager.class);
@ -155,9 +162,9 @@ public class IslandTeamSetownerCommandTest {
* Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamSetownerCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringNullOwner() {
public void testCanExecuteUserStringListOfStringNullOwner() {
when(im.getOwner(any(), any())).thenReturn(null);
assertFalse(its.canExecute(user, "", Collections.emptyList()));
assertFalse(its.canExecute(user, "", List.of("gibby")));
verify(user).sendMessage("general.errors.not-owner");
}
@ -165,9 +172,9 @@ public class IslandTeamSetownerCommandTest {
* Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamSetownerCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringNotInTeam() {
public void testCanExecuteUserStringListOfStringNotInTeam() {
when(im.inTeam(any(), any())).thenReturn(false);
assertFalse(its.canExecute(user, "", Collections.emptyList()));
assertFalse(its.canExecute(user, "", List.of("gibby")));
verify(user).sendMessage("general.errors.no-team");
}
@ -175,10 +182,10 @@ public class IslandTeamSetownerCommandTest {
* Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamSetownerCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringNotOwner() {
public void testCanExecuteUserStringListOfStringNotOwner() {
when(im.inTeam(any(), any())).thenReturn(true);
when(im.getOwner(any(), any())).thenReturn(UUID.randomUUID());
assertFalse(its.canExecute(user, "", Collections.emptyList()));
assertFalse(its.canExecute(user, "", List.of("gibby")));
verify(user).sendMessage("general.errors.not-owner");
}
@ -186,7 +193,7 @@ public class IslandTeamSetownerCommandTest {
* Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamSetownerCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringShowHelp() {
public void testCanExecuteUserStringListOfStringShowHelp() {
when(im.inTeam(any(), any())).thenReturn(true);
when(im.getOwner(any(), any())).thenReturn(uuid);
assertFalse(its.canExecute(user, "", Collections.emptyList()));
@ -197,7 +204,7 @@ public class IslandTeamSetownerCommandTest {
* Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamSetownerCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringUnknownPlayer() {
public void testCanExecuteUserStringListOfStringUnknownPlayer() {
when(im.inTeam(any(), any())).thenReturn(true);
when(im.getOwner(any(), any())).thenReturn(uuid);
when(pm.getUUID(anyString())).thenReturn(null);
@ -209,7 +216,7 @@ public class IslandTeamSetownerCommandTest {
* Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamSetownerCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringSamePlayer() {
public void testCanExecuteUserStringListOfStringSamePlayer() {
when(im.inTeam(any(), any())).thenReturn(true);
when(im.getOwner(any(), any())).thenReturn(uuid);
when(pm.getUUID(anyString())).thenReturn(uuid);
@ -221,7 +228,27 @@ public class IslandTeamSetownerCommandTest {
* Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamSetownerCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringTargetNotInTeam() {
public void testCanExecuteUserStringListOfStringTargetAtMaxIslands() {
when(im.inTeam(any(), any())).thenReturn(true);
when(im.getOwner(any(), any())).thenReturn(uuid);
UUID target = UUID.randomUUID();
when(pm.getUUID(anyString())).thenReturn(target);
when(im.getMembers(any(), any())).thenReturn(Collections.singleton(target));
@Nullable
Island island = mock(Island.class);
when(im.getIsland(any(), any(User.class))).thenReturn(island);
when(im.getNumberOfConcurrentIslands(target, world)).thenReturn(3);
assertFalse(its.canExecute(user, "", Collections.singletonList("tastybento")));
verify(user).sendMessage("commands.island.team.setowner.errors.at-max");
}
/**
* Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamSetownerCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testCanExecuteUserStringListOfStringTargetNotInTeam() {
when(im.inTeam(any(), any())).thenReturn(true);
when(im.getOwner(any(), any())).thenReturn(uuid);
when(pm.getUUID(anyString())).thenReturn(UUID.randomUUID());