Merge pull request #2518 from BentoBoxWorld/2517_Admin_command_for_maximum_amount_of_homes

This commit is contained in:
tastybento 2024-09-29 08:24:42 -07:00 committed by GitHub
commit 83b7c66c4f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 906 additions and 11 deletions

View File

@ -88,7 +88,7 @@
<!-- Do not change unless you want different name for local builds. --> <!-- Do not change unless you want different name for local builds. -->
<build.number>-LOCAL</build.number> <build.number>-LOCAL</build.number>
<!-- This allows to change between versions. --> <!-- This allows to change between versions. -->
<build.version>2.5.4</build.version> <build.version>2.6.0</build.version>
<sonar.organization>bentobox-world</sonar.organization> <sonar.organization>bentobox-world</sonar.organization>
<sonar.host.url>https://sonarcloud.io</sonar.host.url> <sonar.host.url>https://sonarcloud.io</sonar.host.url>
<server.jars>${project.basedir}/lib</server.jars> <server.jars>${project.basedir}/lib</server.jars>

View File

@ -758,7 +758,7 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi
* @param user - the User * @param user - the User
* @return result of help command or false if no help defined * @return result of help command or false if no help defined
*/ */
protected boolean showHelp(CompositeCommand command, User user) { public boolean showHelp(CompositeCommand command, User user) {
return command.getSubCommand("help") return command.getSubCommand("help")
.map(helpCommand -> helpCommand.execute(user, helpCommand.getLabel(), new ArrayList<>())).orElse(false); .map(helpCommand -> helpCommand.execute(user, helpCommand.getLabel(), new ArrayList<>())).orElse(false);
} }

View File

@ -0,0 +1,169 @@
package world.bentobox.bentobox.api.commands.admin;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import com.google.common.primitives.Ints;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.commands.ConfirmableCommand;
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.util.Util;
/**
* Sets the maximum number of homes allowed on this island.
*
* Commands:
* <ul>
* <li><b>/bsb maxhomes &lt;player&gt; &lt;number&gt;</b> - Sets the maximum number of homes for each island where the player is the owner. This could apply to multiple islands.</li>
* <li><b>/bsb maxhomes &lt;player&gt; &lt;number&gt; [island name]</b> - Sets the maximum number of homes for a specific named island where the player is the owner.</li>
* <li><b>/bsb maxhomes &lt;number&gt;</b> - Sets the maximum number of homes for the island you are standing on (in-game only).</li>
* </ul>
*
* @author tastybento
* @since 2.6.0
*/
public class AdminMaxHomesCommand extends ConfirmableCommand {
Integer maxHomes;
Map<String, Island> islands = new HashMap<>();
public AdminMaxHomesCommand(CompositeCommand parent) {
super(parent, "setmaxhomes");
}
@Override
public void setup() {
setPermission("mod.maxhomes");
setOnlyPlayer(false);
setParametersHelp("commands.admin.maxhomes.parameters");
setDescription("commands.admin.maxhomes.description");
}
@Override
public boolean canExecute(User user, String label, List<String> args) {
islands.clear();
if (args.isEmpty()) {
showHelp(this, user);
return false;
}
// Check arguments
if (args.size() == 1) {
// Player must be in game
if (!user.isPlayer()) {
user.sendMessage("general.errors.use-in-game");
return false;
}
// Check world
if (user.getWorld() != getWorld()) {
user.sendMessage("general.errors.wrong-world");
return false;
}
// Arg must be an integer to return true, otherwise false
maxHomes = Ints.tryParse(args.get(0));
if (maxHomes == null || maxHomes < 1) {
user.sendMessage("general.errors.must-be-positive-number", TextVariables.NUMBER, args.get(0));
return false;
}
// Get the island the user is standing on
boolean onIsland = getIslands().getIslandAt(user.getLocation()).map(is -> {
islands.put("", is);
return true;
}).orElse(false);
if (!onIsland) {
user.sendMessage("general.errors.not-on-island");
return false;
}
return true;
}
// More than one argument
// First arg must be a valid player name
UUID targetUUID = getPlayers().getUUID(args.get(0));
if (targetUUID == null) {
user.sendMessage("general.errors.unknown-player", TextVariables.NAME, args.get(0));
return false;
}
// Second arg must be the max homes number
maxHomes = Ints.tryParse(args.get(1));
if (maxHomes == null) {
user.sendMessage("general.errors.must-be-positive-number", TextVariables.NUMBER, args.get(1));
return false;
}
// Get islands
islands = this.getNameIslandMap(User.getInstance(targetUUID));
if (islands.isEmpty()) {
user.sendMessage("general.errors.player-has-no-island");
return false;
}
if (args.size() > 2) {
// A specific island is mentioned. Parse which one it is and remove the others
final String name = String.join(" ", args.subList(2, args.size())); // Join all the args from here with spaces
islands.keySet().removeIf(n -> !name.equalsIgnoreCase(n));
if (islands.isEmpty()) {
// Failed name check - there are either
user.sendMessage("commands.admin.maxhomes.errors.unknown-island", TextVariables.NAME, name);
return false;
}
}
return true;
}
@Override
public boolean execute(User user, String label, List<String> args) {
if (islands.isEmpty() || maxHomes < 1) {
// Sanity check
return false;
}
islands.forEach((name, island) -> {
island.setMaxHomes(maxHomes);
user.sendMessage("commands.admin.maxhomes.max-homes-set", TextVariables.NAME, name, TextVariables.NUMBER,
String.valueOf(maxHomes));
});
return true;
}
@Override
public Optional<List<String>> tabComplete(User user, String alias, List<String> args) {
String lastArg = !args.isEmpty() ? args.get(args.size()-1) : "";
if (args.size() == 2) {
// Suggest player names
return Optional.of(Util.getOnlinePlayerList(user));
}
if (args.size() > 3) {
return Optional.of(Util.tabLimit(new ArrayList<>(getNameIslandMap(user).keySet()), lastArg));
}
return Optional.of(List.of("1"));
}
Map<String, Island> getNameIslandMap(User user) {
Map<String, Island> islandMap = new HashMap<>();
int index = 0;
for (Island island : getIslands().getIslands(getWorld(), user.getUniqueId())) {
index++;
if (island.getName() != null && !island.getName().isBlank()) {
// Name has been set
islandMap.put(island.getName(), island);
} else {
// Name has not been set
String text = user.getTranslation("protection.flags.ENTER_EXIT_MESSAGES.island", TextVariables.NAME,
user.getName(), TextVariables.DISPLAY_NAME, user.getDisplayName()) + " " + index;
islandMap.put(text, island);
}
}
return islandMap;
}
}

View File

@ -102,6 +102,8 @@ public abstract class DefaultAdminCommand extends CompositeCommand {
new AdminDeleteHomesCommand(this); new AdminDeleteHomesCommand(this);
// Reset name // Reset name
new AdminResetNameCommand(this); new AdminResetNameCommand(this);
// Max homes
new AdminMaxHomesCommand(this);
} }
/** /**

View File

@ -95,13 +95,7 @@ public class JoinLeaveListener implements Listener {
// Set island max members and homes based on permissions if this player is the // Set island max members and homes based on permissions if this player is the
// owner of an island // owner of an island
plugin.getIWM().getOverWorlds().stream().map(w -> plugin.getIslands().getIsland(w, playerUUID)) updateIslandMaxTeamAndHomeSize(user);
.filter(Objects::nonNull).filter(i -> playerUUID.equals(i.getOwner())).forEach(i -> {
plugin.getIslands().getMaxMembers(i, RanksManager.MEMBER_RANK);
plugin.getIslands().getMaxMembers(i, RanksManager.COOP_RANK);
plugin.getIslands().getMaxMembers(i, RanksManager.TRUSTED_RANK);
plugin.getIslands().getMaxHomes(i);
});
// Add a player to the bStats cache. // Add a player to the bStats cache.
plugin.getMetrics().ifPresent(bStats -> bStats.addPlayer(playerUUID)); plugin.getMetrics().ifPresent(bStats -> bStats.addPlayer(playerUUID));
@ -125,6 +119,18 @@ public class JoinLeaveListener implements Listener {
}); });
} }
private void updateIslandMaxTeamAndHomeSize(User user) {
plugin.getIWM().getOverWorlds().stream()
.flatMap(w -> plugin.getIslands().getIslands(w, user.getUniqueId()).stream()) // Flatten the List<Island> into a Stream<Island>
.filter(Objects::nonNull).filter(i -> user.getUniqueId().equals(i.getOwner())).forEach(i -> {
plugin.getIslands().getMaxMembers(i, RanksManager.MEMBER_RANK);
plugin.getIslands().getMaxMembers(i, RanksManager.COOP_RANK);
plugin.getIslands().getMaxMembers(i, RanksManager.TRUSTED_RANK);
plugin.getIslands().getMaxHomes(i);
});
}
private void firstTime(User user) { private void firstTime(User user) {
// Make sure the player is loaded into the cache or create the player if they // Make sure the player is loaded into the cache or create the player if they
// don't exist // don't exist
@ -206,6 +212,10 @@ public class JoinLeaveListener implements Listener {
} }
} }
/**
* Update island range using player perms
* @param user user
*/
private void updateIslandRange(User user) { private void updateIslandRange(User user) {
plugin.getIslands().getIslands(user.getUniqueId()).stream() plugin.getIslands().getIslands(user.getUniqueId()).stream()
.filter(island -> island.getOwner() != null && island.getOwner().equals(user.getUniqueId())) .filter(island -> island.getOwner() != null && island.getOwner().equals(user.getUniqueId()))

View File

@ -157,6 +157,15 @@ public enum GameModePlaceholder {
* @since 1.5.0 * @since 1.5.0
*/ */
ISLAND_VISITORS_COUNT("island_visitors_count", (addon, user, island) -> island == null ? "" : String.valueOf(island.getVisitors().size())), ISLAND_VISITORS_COUNT("island_visitors_count", (addon, user, island) -> island == null ? "" : String.valueOf(island.getVisitors().size())),
/**
* Returns the amount of players that are at least MEMBER on the island the player is standing on.
* @since 2.6.0
*/
ISLAND_MAX_HOMES("island_max_homes",
(addon, user, island) -> island == null ? ""
: String.valueOf(
island.getMaxHomes() == null ? addon.getPlugin().getIWM().getMaxHomes(island.getWorld())
: island.getMaxHomes())),
/** /**
* Returns the amount of players banned from the island. * Returns the amount of players banned from the island.
* @since 1.5.0 * @since 1.5.0
@ -281,6 +290,16 @@ public enum GameModePlaceholder {
*/ */
VISITED_ISLAND_MEMBERS_COUNT("visited_island_members_count", (addon, user, island) -> VISITED_ISLAND_MEMBERS_COUNT("visited_island_members_count", (addon, user, island) ->
getVisitedIsland(addon, user).map(value -> String.valueOf(value.getMemberSet().size())).orElse("")), getVisitedIsland(addon, user).map(value -> String.valueOf(value.getMemberSet().size())).orElse("")),
/**
* Returns the amount of players that are at least MEMBER on the island the player is standing on.
* @since 2.6.0
*/
VISITED_ISLAND_MAX_HOMES("visited_island_max_homes",
(addon, user,
island) -> getVisitedIsland(addon, user).map(value -> String.valueOf(
island.getMaxHomes() == null ? addon.getPlugin().getIWM().getMaxHomes(island.getWorld())
: island.getMaxHomes()))
.orElse("")),
/** /**
* Returns the amount of players that are TRUSTED on the island the player is standing on. * Returns the amount of players that are TRUSTED on the island the player is standing on.
* @since 1.5.2 * @since 1.5.2

View File

@ -143,6 +143,9 @@ public class IslandInfo {
// Show team members // Show team members
showMembers(user); showMembers(user);
} }
int maxHomes = island.getMaxHomes() == null ? plugin.getIWM().getMaxHomes(island.getWorld())
: island.getMaxHomes();
user.sendMessage("commands.admin.info.max-homes", TextVariables.NUMBER, String.valueOf(maxHomes));
Vector location = island.getProtectionCenter().toVector(); Vector location = island.getProtectionCenter().toVector();
user.sendMessage("commands.admin.info.island-center", TextVariables.XYZ, Util.xyz(location)); user.sendMessage("commands.admin.info.island-center", TextVariables.XYZ, Util.xyz(location));
user.sendMessage("commands.admin.info.protection-range", RANGE, String.valueOf(island.getProtectionRange())); user.sendMessage("commands.admin.info.protection-range", RANGE, String.valueOf(island.getProtectionRange()));

View File

@ -59,6 +59,13 @@ commands:
admin: admin:
help: help:
description: admin command description: admin command
maxhomes:
description: change the number of homes allowed on this island or player's island
parameters: <number / player> <number> <island name>
max-homes-set: '&a [name] - Set island max homes to [number]'
errors:
unknown-island: &c Unknown island! [name]
resets: resets:
description: edit player reset values description: edit player reset values
set: set:
@ -213,6 +220,7 @@ commands:
last-login-date-time-format: EEE MMM dd HH:mm:ss zzz yyyy last-login-date-time-format: EEE MMM dd HH:mm:ss zzz yyyy
deaths: 'Deaths: [number]' deaths: 'Deaths: [number]'
resets-left: 'Resets: [number] (Max: [total])' resets-left: 'Resets: [number] (Max: [total])'
max-homes: 'Max homes: [number]'
team-members-title: 'Team members:' team-members-title: 'Team members:'
team-owner-format: '&a [name] [rank]' team-owner-format: '&a [name] [rank]'
team-member-format: '&b [name] [rank]' team-member-format: '&b [name] [rank]'

View File

@ -68,8 +68,6 @@ public class AdminDeleteCommandTest {
@Mock @Mock
private @Nullable Island island; private @Nullable Island island;
/**
*/
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS); PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS);

View File

@ -0,0 +1,686 @@
package world.bentobox.bentobox.api.commands.admin;
import static org.junit.Assert.assertEquals;
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.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitScheduler;
import org.bukkit.scheduler.BukkitTask;
import org.eclipse.jdt.annotation.Nullable;
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;
import org.powermock.modules.junit4.PowerMockRunner;
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.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
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.PlayersManager;
import world.bentobox.bentobox.util.Util;
/**
* @author tastybento
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({ Bukkit.class, BentoBox.class, User.class, Util.class })
public class AdminMaxHomesCommandTest {
@Mock
private CompositeCommand ac;
@Mock
private User user;
@Mock
private IslandsManager im;
@Mock
private PlayersManager pm;
private UUID notUUID;
private UUID uuid;
@Mock
private World world;
@Mock
private @Nullable Island island;
private AdminMaxHomesCommand instance;
private String label;
private ArrayList<String> args = new ArrayList<>();
@Before
public void setUp() throws Exception {
PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS);
// Set up plugin
BentoBox plugin = mock(BentoBox.class);
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
// Util
Util.setPlugin(plugin);
// Command manager
CommandsManager cm = mock(CommandsManager.class);
when(plugin.getCommandsManager()).thenReturn(cm);
// Settings
Settings s = mock(Settings.class);
when(s.getResetCooldown()).thenReturn(0);
when(plugin.getSettings()).thenReturn(s);
// Player
Player p = mock(Player.class);
// Sometimes use Mockito.withSettings().verboseLogging()
when(user.isOp()).thenReturn(false);
uuid = UUID.randomUUID();
notUUID = UUID.randomUUID();
while (notUUID.equals(uuid)) {
notUUID = UUID.randomUUID();
}
when(user.getUniqueId()).thenReturn(uuid);
when(user.getPlayer()).thenReturn(p);
when(user.getName()).thenReturn("tastybento");
User.setPlugin(plugin);
// Parent command has no aliases
when(ac.getSubCommandAliases()).thenReturn(new HashMap<>());
when(ac.getTopLabel()).thenReturn("admin");
when(ac.getWorld()).thenReturn(world);
// Island World Manager
IslandWorldManager iwm = mock(IslandWorldManager.class);
when(plugin.getIWM()).thenReturn(iwm);
// Player has island to begin with
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(im.getIsland(world, user)).thenReturn(island);
when(im.getIslands(world, notUUID)).thenReturn(List.of(island));
when(plugin.getIslands()).thenReturn(im);
// Island
when(island.getOwner()).thenReturn(uuid);
when(island.hasTeam()).thenReturn(true);
// Has team
when(im.inTeam(any(), eq(uuid))).thenReturn(true);
when(plugin.getPlayers()).thenReturn(pm);
// Server & Scheduler
BukkitScheduler sch = mock(BukkitScheduler.class);
when(Bukkit.getScheduler()).thenReturn(sch);
BukkitTask task = mock(BukkitTask.class);
when(sch.runTaskLater(any(), any(Runnable.class), any(Long.class))).thenReturn(task);
// Locales
LocalesManager lm = mock(LocalesManager.class);
when(lm.get(any(), any())).thenReturn("mock translation");
when(plugin.getLocalesManager()).thenReturn(lm);
instance = spy(new AdminMaxHomesCommand(ac));
label = "island";
}
@After
public void tearDown() {
User.clearUsers();
Mockito.framework().clearInlineMocks();
}
/**
* Test method for {@link world.bentobox.bentobox.api.commands.admin.AdminMaxHomesCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testArgsIsEmpty() {
// Arrange: args is already empty
// Act
boolean result = instance.canExecute(user, label, args);
// Assert
assertFalse(result);
// Verify that showHelp was called
verify(instance).showHelp(instance, user);
}
/**
* Test method for {@link world.bentobox.bentobox.api.commands.admin.AdminMaxHomesCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testArgsSize1_UserNotPlayer() {
// Arrange
args.add("5");
when(user.isPlayer()).thenReturn(false);
// Act
boolean result = instance.canExecute(user, label, args);
// Assert
assertFalse(result);
verify(user).sendMessage("general.errors.use-in-game");
}
/**
* Test method for {@link world.bentobox.bentobox.api.commands.admin.AdminMaxHomesCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testArgsSize1_WrongWorld() {
// Arrange
args.add("5");
when(user.isPlayer()).thenReturn(true);
World userWorld = mock(World.class);
World expectedWorld = mock(World.class);
when(user.getWorld()).thenReturn(userWorld);
doReturn(expectedWorld).when(instance).getWorld();
// Act
boolean result = instance.canExecute(user, label, args);
// Assert
assertFalse(result);
verify(user).sendMessage("general.errors.wrong-world");
}
/**
* Test method for {@link world.bentobox.bentobox.api.commands.admin.AdminMaxHomesCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testArgsSize1_InvalidMaxHomes() {
// Arrange
args.add("notanumber");
when(user.isPlayer()).thenReturn(true);
World world = mock(World.class);
when(user.getWorld()).thenReturn(world);
doReturn(world).when(instance).getWorld();
// Act
boolean result = instance.canExecute(user, label, args);
// Assert
assertFalse(result);
verify(user).sendMessage("general.errors.must-be-positive-number", TextVariables.NUMBER, "notanumber");
}
/**
* Test method for {@link world.bentobox.bentobox.api.commands.admin.AdminMaxHomesCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testArgsSize1_UserNotOnIsland() {
// Arrange
args.add("5");
when(user.isPlayer()).thenReturn(true);
World world = mock(World.class);
when(user.getWorld()).thenReturn(world);
doReturn(world).when(instance).getWorld();
Location location = mock(Location.class);
when(user.getLocation()).thenReturn(location);
when(im.getIslandAt(location)).thenReturn(Optional.empty());
// Act
boolean result = instance.canExecute(user, label, args);
// Assert
assertFalse(result);
verify(user).sendMessage("general.errors.not-on-island");
}
/**
* Test method for {@link world.bentobox.bentobox.api.commands.admin.AdminMaxHomesCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testArgsSize1_Success() {
// Arrange
args.add("5");
when(user.isPlayer()).thenReturn(true);
World world = mock(World.class);
when(user.getWorld()).thenReturn(world);
doReturn(world).when(instance).getWorld();
Location location = mock(Location.class);
when(user.getLocation()).thenReturn(location);
Island island = mock(Island.class);
when(im.getIslandAt(location)).thenReturn(Optional.of(island));
// Act
boolean result = instance.canExecute(user, label, args);
// Assert
assertTrue(result);
}
/**
* Test method for {@link world.bentobox.bentobox.api.commands.admin.AdminMaxHomesCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testArgsSizeGreaterThan1_InvalidPlayer() {
// Arrange
args.add("UnknownPlayer");
args.add("5");
when(pm.getUUID("UnknownPlayer")).thenReturn(null);
// Act
boolean result = instance.canExecute(user, label, args);
// Assert
assertFalse(result);
verify(user).sendMessage("general.errors.unknown-player", TextVariables.NAME, "UnknownPlayer");
}
/**
* Test method for {@link world.bentobox.bentobox.api.commands.admin.AdminMaxHomesCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testArgsSizeGreaterThan1_InvalidMaxHomes() {
// Arrange
args.add("ValidPlayer");
args.add("notanumber");
UUID playerUUID = UUID.randomUUID();
when(pm.getUUID("ValidPlayer")).thenReturn(playerUUID);
// Act
boolean result = instance.canExecute(user, label, args);
// Assert
assertFalse(result);
verify(user).sendMessage("general.errors.must-be-positive-number", TextVariables.NUMBER, "notanumber");
}
/**
* Test method for {@link world.bentobox.bentobox.api.commands.admin.AdminMaxHomesCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testArgsSizeGreaterThan1_TargetPlayerHasNoIslands() {
// Arrange
args.add("ValidPlayer");
args.add("5");
UUID playerUUID = UUID.randomUUID();
when(pm.getUUID("ValidPlayer")).thenReturn(playerUUID);
User targetUser = mock(User.class);
// Mock static method User.getInstance(UUID)
// Assuming use of Mockito with inline mocking or PowerMockito
PowerMockito.mockStatic(User.class);
when(User.getInstance(playerUUID)).thenReturn(targetUser);
doReturn(new HashMap<String, Island>()).when(instance).getNameIslandMap(targetUser);
// Act
boolean result = instance.canExecute(user, label, args);
// Assert
assertFalse(result);
verify(user).sendMessage("general.errors.player-has-no-island");
}
/**
* Test method for {@link world.bentobox.bentobox.api.commands.admin.AdminMaxHomesCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testArgsSizeGreaterThan2_UnknownIsland() {
// Arrange
args.add("ValidPlayer");
args.add("5");
args.add("UnknownIsland");
UUID playerUUID = UUID.randomUUID();
when(pm.getUUID("ValidPlayer")).thenReturn(playerUUID);
User targetUser = mock(User.class);
// Mock static method User.getInstance(UUID)
// Assuming use of Mockito with inline mocking or PowerMockito
PowerMockito.mockStatic(User.class);
when(User.getInstance(playerUUID)).thenReturn(targetUser);
Map<String, Island> islandsMap = new HashMap<>();
islandsMap.put("Island1", mock(Island.class));
doReturn(islandsMap).when(instance).getNameIslandMap(targetUser);
// Act
boolean result = instance.canExecute(user, label, args);
// Assert
assertFalse(result);
verify(user).sendMessage("commands.admin.maxhomes.errors.unknown-island", TextVariables.NAME, "UnknownIsland");
}
/**
* Test method for {@link world.bentobox.bentobox.api.commands.admin.AdminMaxHomesCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testArgsSizeGreaterThan1_Success() {
// Arrange
args.add("ValidPlayer");
args.add("5");
UUID playerUUID = UUID.randomUUID();
when(pm.getUUID("ValidPlayer")).thenReturn(playerUUID);
User targetUser = mock(User.class);
// Mock static method User.getInstance(UUID)
PowerMockito.mockStatic(User.class);
when(User.getInstance(playerUUID)).thenReturn(targetUser);
Map<String, Island> islandsMap = new HashMap<>();
islandsMap.put("", mock(Island.class)); // Assuming empty string key as in code
doReturn(islandsMap).when(instance).getNameIslandMap(targetUser);
// Act
boolean result = instance.canExecute(user, label, args);
// Assert
assertTrue(result);
}
/**
* Test method for {@link world.bentobox.bentobox.api.commands.admin.AdminMaxHomesCommand#setup()}.
*/
@Test
public void testSetup() {
assertEquals("mod.maxhomes", instance.getPermission());
assertFalse(instance.isOnlyPlayer());
assertEquals("commands.admin.maxhomes.parameters", instance.getParameters());
assertEquals("commands.admin.maxhomes.description", instance.getDescription());
}
/**
* Test method for {@link world.bentobox.bentobox.api.commands.admin.AdminMaxHomesCommand#tabComplete(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testTabComplete_ArgsSize2_ReturnsPlayerNames() {
// Arrange
args.add("someArg"); // args.size() == 1
args.add(""); // args.size() == 2
// Mock Util.getOnlinePlayerList(user)
List<String> onlinePlayers = Arrays.asList("PlayerOne", "PlayerTwo");
PowerMockito.mockStatic(Util.class);
when(Util.getOnlinePlayerList(user)).thenReturn(onlinePlayers);
// Act
Optional<List<String>> result = instance.tabComplete(user, label, args);
// Assert
assertTrue(result.isPresent());
assertEquals(onlinePlayers, result.get());
}
/**
* Test method for {@link world.bentobox.bentobox.api.commands.admin.AdminMaxHomesCommand#tabComplete(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testTabComplete_ArgsSizeGreaterThan3_ReturnsIslandNames() {
// Arrange
args.add("someArg");
args.add("anotherArg");
args.add("thirdArg");
args.add(""); // args.size() == 4 (>3)
String lastArg = args.get(args.size() - 1);
Map<String, Island> nameIslandMap = new HashMap<>();
nameIslandMap.put("IslandOne", mock(Island.class));
nameIslandMap.put("IslandTwo", mock(Island.class));
doReturn(nameIslandMap).when(instance).getNameIslandMap(user);
// Create the list of island names
List<String> islandNames = new ArrayList<>(nameIslandMap.keySet());
// Mock Util.tabLimit()
List<String> limitedIslandNames = Arrays.asList("IslandOne", "IslandTwo");
PowerMockito.mockStatic(Util.class);
when(Util.tabLimit(islandNames, lastArg)).thenReturn(limitedIslandNames);
// Act
Optional<List<String>> result = instance.tabComplete(user, label, args);
// Assert
assertTrue(result.isPresent());
assertEquals(limitedIslandNames, result.get());
}
/**
* Test method for {@link world.bentobox.bentobox.api.commands.admin.AdminMaxHomesCommand#tabComplete(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testTabComplete_Otherwise_ReturnsListOfOne() {
// Arrange
args.add(""); // args.size() == 1
// Act
Optional<List<String>> result = instance.tabComplete(user, label, args);
// Assert
assertTrue(result.isPresent());
assertEquals(Collections.singletonList("1"), result.get());
}
/**
* Test method for {@link world.bentobox.bentobox.api.commands.admin.AdminMaxHomesCommand#tabComplete(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testTabComplete_ArgsSize3_ReturnsListOfOne() {
// Arrange
args.add("someArg");
args.add("anotherArg");
args.add(""); // args.size() == 3
// Act
Optional<List<String>> result = instance.tabComplete(user, label, args);
// Assert
assertTrue(result.isPresent());
assertEquals(Collections.singletonList("1"), result.get());
}
/**
* Test method for {@link world.bentobox.bentobox.api.commands.admin.AdminMaxHomesCommand#tabComplete(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteWithEmptyIslands_ShouldReturnFalse() {
// Arrange
instance.maxHomes = 5; // Set maxHomes to a valid number
instance.islands = new HashMap<>(); // Empty islands map
// Act
boolean result = instance.execute(user, label, args);
// Assert
assertFalse(result);
}
/**
* Test method for {@link world.bentobox.bentobox.api.commands.admin.AdminMaxHomesCommand#tabComplete(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteWithMaxHomesLessThanOne_ShouldReturnFalse() {
// Arrange
instance.maxHomes = 0; // Invalid maxHomes
Island island = mock(Island.class);
Map<String, Island> islandsMap = new HashMap<>();
islandsMap.put("TestIsland", island);
instance.islands = islandsMap;
// Act
boolean result = instance.execute(user, label, args);
// Assert
assertFalse(result);
}
/**
* Test method for {@link world.bentobox.bentobox.api.commands.admin.AdminMaxHomesCommand#tabComplete(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteSuccessful_SingleIsland() {
// Arrange
instance.maxHomes = 5;
Island island = mock(Island.class);
Map<String, Island> islandsMap = new HashMap<>();
islandsMap.put("TestIsland", island);
instance.islands = islandsMap;
// Act
boolean result = instance.execute(user, label, args);
// Assert
assertTrue(result);
verify(island).setMaxHomes(5);
verify(user).sendMessage("commands.admin.maxhomes.max-homes-set", TextVariables.NAME, "TestIsland",
TextVariables.NUMBER, "5");
}
/**
* Test method for {@link world.bentobox.bentobox.api.commands.admin.AdminMaxHomesCommand#tabComplete(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteSuccessful_MultipleIslands() {
// Arrange
instance.maxHomes = 3;
Island island1 = mock(Island.class);
Island island2 = mock(Island.class);
Map<String, Island> islandsMap = new HashMap<>();
islandsMap.put("IslandOne", island1);
islandsMap.put("IslandTwo", island2);
instance.islands = islandsMap;
// Act
boolean result = instance.execute(user, label, args);
// Assert
assertTrue(result);
verify(island1).setMaxHomes(3);
verify(island2).setMaxHomes(3);
verify(user).sendMessage("commands.admin.maxhomes.max-homes-set", TextVariables.NAME, "IslandOne",
TextVariables.NUMBER, "3");
verify(user).sendMessage("commands.admin.maxhomes.max-homes-set", TextVariables.NAME, "IslandTwo",
TextVariables.NUMBER, "3");
}
/**
* Test method for {@link world.bentobox.bentobox.api.commands.admin.AdminMaxHomesCommand#tabComplete(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteAfterSuccessfulCanExecute() {
// Arrange
args.add("5");
when(user.isPlayer()).thenReturn(true);
World world = mock(World.class);
when(user.getWorld()).thenReturn(world);
doReturn(world).when(instance).getWorld();
Location location = mock(Location.class);
when(user.getLocation()).thenReturn(location);
Island island = mock(Island.class);
when(im.getIslandAt(location)).thenReturn(Optional.of(island));
// Act
boolean canExecuteResult = instance.canExecute(user, label, args);
boolean executeResult = instance.execute(user, label, args);
// Assert
assertTrue(canExecuteResult);
assertTrue(executeResult);
verify(island).setMaxHomes(5);
verify(user).sendMessage("commands.admin.maxhomes.max-homes-set", TextVariables.NAME, "", TextVariables.NUMBER,
"5");
}
/**
* Test method for {@link world.bentobox.bentobox.api.commands.admin.AdminMaxHomesCommand#tabComplete(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteWithInvalidMaxHomesAfterCanExecute() {
// Arrange
args.add("-1");
when(user.isPlayer()).thenReturn(true);
World world = mock(World.class);
when(user.getWorld()).thenReturn(world);
doReturn(world).when(instance).getWorld();
// Act
boolean canExecuteResult = instance.canExecute(user, label, args);
boolean executeResult = instance.execute(user, label, args);
// Assert
assertFalse(canExecuteResult);
assertFalse(executeResult);
}
/**
* Test method for {@link world.bentobox.bentobox.api.commands.admin.AdminMaxHomesCommand#tabComplete(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteWithMultipleIslandsAfterCanExecute() {
// Arrange
args.add("ValidPlayer");
args.add("4");
UUID playerUUID = UUID.randomUUID();
when(pm.getUUID("ValidPlayer")).thenReturn(playerUUID);
User targetUser = mock(User.class);
PowerMockito.mockStatic(User.class);
when(User.getInstance(playerUUID)).thenReturn(targetUser);
Island island1 = mock(Island.class);
Island island2 = mock(Island.class);
Map<String, Island> islandsMap = new HashMap<>();
islandsMap.put("IslandA", island1);
islandsMap.put("IslandB", island2);
doReturn(islandsMap).when(instance).getNameIslandMap(targetUser);
// Act
boolean canExecuteResult = instance.canExecute(user, label, args);
boolean executeResult = instance.execute(user, label, args);
// Assert
assertTrue(canExecuteResult);
assertTrue(executeResult);
verify(island1).setMaxHomes(4);
verify(island2).setMaxHomes(4);
verify(user).sendMessage("commands.admin.maxhomes.max-homes-set", TextVariables.NAME, "IslandA",
TextVariables.NUMBER, "4");
verify(user).sendMessage("commands.admin.maxhomes.max-homes-set", TextVariables.NAME, "IslandB",
TextVariables.NUMBER, "4");
}
}