Merge pull request #2332 from BentoBoxWorld/admin_range_multi_island

Adds support for multi islands to the admin range command
This commit is contained in:
tastybento 2024-03-24 18:47:39 -07:00 committed by GitHub
commit b734d579a1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 211 additions and 170 deletions

View File

@ -0,0 +1,103 @@
package world.bentobox.bentobox.api.commands.admin.range;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
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.util.Util;
/**
* @author Poslovitch
*/
public abstract class AbstractAdminRangeCommand extends CompositeCommand {
protected @Nullable UUID targetUUID;
protected Island targetIsland;
public AbstractAdminRangeCommand(CompositeCommand parent, String string) {
super(parent, string);
}
@Override
public boolean canExecute(User user, String label, @NonNull List<String> args) {
if (args.size() <= 1) {
showHelp(this, user);
return false;
}
targetUUID = Util.getUUID(args.get(0));
if (targetUUID == null) {
user.sendMessage("general.errors.unknown-player", TextVariables.NAME, args.get(0));
return false;
}
if (!Util.isInteger(args.get(1), true) || Integer.parseInt(args.get(1)) < 0) {
user.sendMessage("general.errors.must-be-positive-number", TextVariables.NUMBER, args.get(1));
return false;
}
// Check if the player has more than one island
Map<String, Island> islands = getIslandsXYZ(targetUUID);
if (islands.size() == 0) {
user.sendMessage("general.errors.player-has-no-island");
return false;
} else if (args.size() == 2) {
// If they only have one island, 2 args are fine
if (islands.size() == 1) {
targetIsland = islands.values().iterator().next();
return true;
} else {
// They need to specify which island
user.sendMessage("commands.admin.unregister.errors.player-has-more-than-one-island");
user.sendMessage("commands.admin.unregister.errors.specify-island-location");
return false;
}
} else if (args.size() != 3) {
// No location
user.sendMessage("commands.admin.unregister.errors.specify-island-location");
return false;
} else if (!islands.containsKey(args.get(2))) {
if (args.get(2).equalsIgnoreCase("help")) {
this.showHelp(this, user);
return false;
}
user.sendMessage("commands.admin.unregister.errors.unknown-island-location");
return false;
}
targetIsland = islands.get(args.get(2));
return true;
}
protected Map<String, Island> getIslandsXYZ(UUID target) {
return getIslands().getOwnedIslands(getWorld(), target).stream()
.collect(Collectors.toMap(island -> Util.xyz(island.getCenter().toVector()), island -> island));
}
@Override
public Optional<List<String>> tabComplete(User user, String alias, List<String> args) {
String lastArg = !args.isEmpty() ? args.get(args.size() - 1) : "";
if (args.isEmpty()) {
// Don't show every player on the server. Require at least the first letter
return Optional.empty();
} else if (args.size() == 3) {
List<String> options = new ArrayList<>(Util.getOnlinePlayerList(user));
return Optional.of(Util.tabLimit(options, lastArg));
} else if (args.size() > 4) {
// Find out which user
UUID uuid = getPlayers().getUUID(args.get(2));
if (uuid != null) {
return Optional.of(Util.tabLimit(new ArrayList<>(getIslandsXYZ(uuid).keySet()), lastArg));
}
}
return Optional.empty();
}
}

View File

@ -1,23 +1,18 @@
package world.bentobox.bentobox.api.commands.admin.range; package world.bentobox.bentobox.api.commands.admin.range;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.UUID;
import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.NonNull;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.events.island.IslandEvent; import world.bentobox.bentobox.api.events.island.IslandEvent;
import world.bentobox.bentobox.api.localization.TextVariables; import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.util.Util;
/** /**
* @since 1.10.0 * @since 1.10.0
* @author Poslovitch * @author Poslovitch
*/ */
public class AdminRangeAddCommand extends CompositeCommand { public class AdminRangeAddCommand extends AbstractAdminRangeCommand {
public AdminRangeAddCommand(AdminRangeCommand parent) { public AdminRangeAddCommand(AdminRangeCommand parent) {
super(parent, "add"); super(parent, "add");
@ -32,53 +27,28 @@ public class AdminRangeAddCommand extends CompositeCommand {
@Override @Override
public boolean execute(User user, String label, @NonNull List<String> args) { public boolean execute(User user, String label, @NonNull List<String> args) {
if (args.size() != 2) { int newRange = targetIsland.getProtectionRange() + Integer.parseInt(args.get(1));
showHelp(this, user);
return false;
}
UUID targetUUID = Util.getUUID(args.get(0)); if (newRange > targetIsland.getRange()) {
if (targetUUID == null) { user.sendMessage("commands.admin.range.invalid-value.too-high", TextVariables.NUMBER,
user.sendMessage("general.errors.unknown-player", TextVariables.NAME, args.get(0)); String.valueOf(targetIsland.getRange()));
return false; return false;
} } else if (newRange == targetIsland.getProtectionRange()) {
if (!(getIslands().hasIsland(getWorld(), targetUUID) || getIslands().inTeam(getWorld(), targetUUID))) {
user.sendMessage("general.errors.player-has-no-island");
return false;
}
if (!Util.isInteger(args.get(1), true) || Integer.parseInt(args.get(1)) < 0) {
user.sendMessage("general.errors.must-be-positive-number", TextVariables.NUMBER, args.get(1));
return false;
}
Island island = Objects.requireNonNull(getIslands().getIsland(getWorld(), targetUUID));
int newRange = island.getProtectionRange() + Integer.parseInt(args.get(1));
if (newRange > island.getRange()) {
user.sendMessage("commands.admin.range.invalid-value.too-high", TextVariables.NUMBER, String.valueOf(island.getRange()));
return false;
} else if (newRange == island.getProtectionRange()) {
user.sendMessage("commands.admin.range.invalid-value.same-as-before", TextVariables.NUMBER, args.get(1)); user.sendMessage("commands.admin.range.invalid-value.same-as-before", TextVariables.NUMBER, args.get(1));
return false; return false;
} }
// Get old range for event // Get old range for event
int oldRange = island.getProtectionRange(); int oldRange = targetIsland.getProtectionRange();
// Well, now it can be applied without taking any risks! // Well, now it can be applied without taking any risks!
island.setProtectionRange(newRange); targetIsland.setProtectionRange(newRange);
// Call Protection Range Change event. Does not support cancelling. // Call Protection Range Change event. Does not support cancelling.
IslandEvent.builder() IslandEvent.builder()
.island(island) .island(targetIsland).location(targetIsland.getCenter())
.location(island.getCenter()) .reason(IslandEvent.Reason.RANGE_CHANGE).involvedPlayer(targetUUID).admin(true)
.reason(IslandEvent.Reason.RANGE_CHANGE) .protectionRange(newRange, oldRange).build();
.involvedPlayer(targetUUID)
.admin(true)
.protectionRange(newRange, oldRange)
.build();
user.sendMessage("commands.admin.range.add.success", user.sendMessage("commands.admin.range.add.success",
TextVariables.NAME, args.get(0), TextVariables.NUMBER, args.get(1), TextVariables.NAME, args.get(0), TextVariables.NUMBER, args.get(1),
@ -86,4 +56,6 @@ public class AdminRangeAddCommand extends CompositeCommand {
return true; return true;
} }
} }

View File

@ -31,4 +31,5 @@ public class AdminRangeCommand extends CompositeCommand {
showHelp(this, user); showHelp(this, user);
return true; return true;
} }
}
}

View File

@ -1,23 +1,19 @@
package world.bentobox.bentobox.api.commands.admin.range; package world.bentobox.bentobox.api.commands.admin.range;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.UUID;
import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.NonNull;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.events.island.IslandEvent; import world.bentobox.bentobox.api.events.island.IslandEvent;
import world.bentobox.bentobox.api.localization.TextVariables; import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.util.Util; import world.bentobox.bentobox.util.Util;
/** /**
* @since 1.10.0 * @since 1.10.0
* @author Poslovitch * @author Poslovitch
*/ */
public class AdminRangeRemoveCommand extends CompositeCommand { public class AdminRangeRemoveCommand extends AbstractAdminRangeCommand {
public AdminRangeRemoveCommand(AdminRangeCommand parent) { public AdminRangeRemoveCommand(AdminRangeCommand parent) {
super(parent, "remove"); super(parent, "remove");
@ -32,48 +28,31 @@ public class AdminRangeRemoveCommand extends CompositeCommand {
@Override @Override
public boolean execute(User user, String label, @NonNull List<String> args) { public boolean execute(User user, String label, @NonNull List<String> args) {
if (args.size() != 2) {
showHelp(this, user);
return false;
}
UUID targetUUID = Util.getUUID(args.get(0));
if (targetUUID == null) {
user.sendMessage("general.errors.unknown-player", TextVariables.NAME, args.get(0));
return false;
}
if (!(getIslands().hasIsland(getWorld(), targetUUID) || getIslands().inTeam(getWorld(), targetUUID))) {
user.sendMessage("general.errors.player-has-no-island");
return false;
}
if (!Util.isInteger(args.get(1), true) || Integer.parseInt(args.get(1)) < 0) { if (!Util.isInteger(args.get(1), true) || Integer.parseInt(args.get(1)) < 0) {
user.sendMessage("general.errors.must-be-positive-number", TextVariables.NUMBER, args.get(1)); user.sendMessage("general.errors.must-be-positive-number", TextVariables.NUMBER, args.get(1));
return false; return false;
} }
Island island = Objects.requireNonNull(getIslands().getIsland(getWorld(), targetUUID)); int newRange = targetIsland.getProtectionRange() - Integer.parseInt(args.get(1));
int newRange = island.getProtectionRange() - Integer.parseInt(args.get(1));
if (newRange <= 1) { if (newRange <= 1) {
user.sendMessage("commands.admin.range.invalid-value.too-low", TextVariables.NUMBER, String.valueOf(island.getRange())); user.sendMessage("commands.admin.range.invalid-value.too-low", TextVariables.NUMBER,
String.valueOf(targetIsland.getRange()));
return false; return false;
} else if (newRange == island.getProtectionRange()) { } else if (newRange == targetIsland.getProtectionRange()) {
user.sendMessage("commands.admin.range.invalid-value.same-as-before", TextVariables.NUMBER, args.get(1)); user.sendMessage("commands.admin.range.invalid-value.same-as-before", TextVariables.NUMBER, args.get(1));
return false; return false;
} }
// Get old range for event // Get old range for event
int oldRange = island.getProtectionRange(); int oldRange = targetIsland.getProtectionRange();
// Well, now it can be applied without taking any risks! // Well, now it can be applied without taking any risks!
island.setProtectionRange(newRange); targetIsland.setProtectionRange(newRange);
// Call Protection Range Change event. Does not support cancelling. // Call Protection Range Change event. Does not support cancelling.
IslandEvent.builder() IslandEvent.builder()
.island(island) .island(targetIsland).location(targetIsland.getCenter())
.location(island.getCenter())
.reason(IslandEvent.Reason.RANGE_CHANGE) .reason(IslandEvent.Reason.RANGE_CHANGE)
.involvedPlayer(targetUUID) .involvedPlayer(targetUUID)
.admin(true) .admin(true)

View File

@ -1,19 +1,14 @@
package world.bentobox.bentobox.api.commands.admin.range; package world.bentobox.bentobox.api.commands.admin.range;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import world.bentobox.bentobox.api.commands.CompositeCommand; import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.events.island.IslandEvent; import world.bentobox.bentobox.api.events.island.IslandEvent;
import world.bentobox.bentobox.api.localization.TextVariables; import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.util.Util; import world.bentobox.bentobox.util.Util;
public class AdminRangeSetCommand extends CompositeCommand { public class AdminRangeSetCommand extends AbstractAdminRangeCommand {
public AdminRangeSetCommand(CompositeCommand parent) { public AdminRangeSetCommand(CompositeCommand parent) {
super(parent, "set"); super(parent, "set");
@ -28,23 +23,6 @@ public class AdminRangeSetCommand extends CompositeCommand {
@Override @Override
public boolean execute(User user, String label, List<String> args) { public boolean execute(User user, String label, List<String> args) {
if (args.size() != 2) {
// Show help
showHelp(this, user);
return false;
}
// Get target player
UUID targetUUID = Util.getUUID(args.get(0));
if (targetUUID == null) {
user.sendMessage("general.errors.unknown-player", TextVariables.NAME, args.get(0));
return false;
}
if (!(getIslands().hasIsland(getWorld(), targetUUID) || getIslands().inTeam(getWorld(), targetUUID))) {
user.sendMessage("general.errors.player-has-no-island");
return false;
}
// Get new range // Get new range
if (!Util.isInteger(args.get(1), true) || Integer.parseInt(args.get(1)) < 0) { if (!Util.isInteger(args.get(1), true) || Integer.parseInt(args.get(1)) < 0) {
user.sendMessage("general.errors.must-be-positive-number", TextVariables.NUMBER, args.get(1)); user.sendMessage("general.errors.must-be-positive-number", TextVariables.NUMBER, args.get(1));
@ -52,33 +30,30 @@ public class AdminRangeSetCommand extends CompositeCommand {
} }
int range = Integer.parseInt(args.get(1)); int range = Integer.parseInt(args.get(1));
// Get island
Island island = Objects.requireNonNull(getIslands().getIsland(getWorld(), targetUUID));
// Do some sanity checks to make sure the new protection range won't cause problems // Do some sanity checks to make sure the new protection range won't cause problems
if (range < 1) { if (range < 1) {
user.sendMessage("commands.admin.range.invalid-value.too-low", TextVariables.NUMBER, args.get(1)); user.sendMessage("commands.admin.range.invalid-value.too-low", TextVariables.NUMBER, args.get(1));
return false; return false;
} }
if (range > island.getRange() * 2) { if (range > targetIsland.getRange() * 2) {
user.sendMessage("commands.admin.range.invalid-value.too-high", TextVariables.NUMBER, String.valueOf(2 * island.getRange())); user.sendMessage("commands.admin.range.invalid-value.too-high", TextVariables.NUMBER,
String.valueOf(2 * targetIsland.getRange()));
return false; return false;
} }
if (range == island.getProtectionRange()) { if (range == targetIsland.getProtectionRange()) {
user.sendMessage("commands.admin.range.invalid-value.same-as-before", TextVariables.NUMBER, args.get(1)); user.sendMessage("commands.admin.range.invalid-value.same-as-before", TextVariables.NUMBER, args.get(1));
return false; return false;
} }
// Get old range for event // Get old range for event
int oldRange = island.getProtectionRange(); int oldRange = targetIsland.getProtectionRange();
// Well, now it can be applied without taking any risks! // Well, now it can be applied without taking any risks!
island.setProtectionRange(range); targetIsland.setProtectionRange(range);
// Call Protection Range Change event. Does not support canceling. // Call Protection Range Change event. Does not support canceling.
IslandEvent.builder() IslandEvent.builder()
.island(island) .island(targetIsland).location(targetIsland.getCenter())
.location(island.getCenter())
.reason(IslandEvent.Reason.RANGE_CHANGE) .reason(IslandEvent.Reason.RANGE_CHANGE)
.involvedPlayer(targetUUID) .involvedPlayer(targetUUID)
.admin(true) .admin(true)
@ -90,14 +65,4 @@ public class AdminRangeSetCommand extends CompositeCommand {
return true; 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.isEmpty()) {
// Don't show every player on the server. Require at least the first letter
return Optional.empty();
}
List<String> options = new ArrayList<>(Util.getOnlinePlayerList(user));
return Optional.of(Util.tabLimit(options, lastArg));
}
} }

View File

@ -81,8 +81,10 @@ public class DefaultPasteUtil {
*/ */
public static BlockData createBlockData(BlueprintBlock block) { public static BlockData createBlockData(BlueprintBlock block) {
try { try {
return Bukkit.createBlockData(block.getBlockData()); return Material.STONE.createBlockData();
//return Bukkit.createBlockData(block.getBlockData());
} catch (Exception e) { } catch (Exception e) {
BentoBox.getInstance().logStacktrace(e);
return convertBlockData(block); return convertBlockData(block);
} }
} }

View File

@ -166,7 +166,7 @@ commands:
&a Green Particles &f show the default protected range if the island protection range differs from it. &a Green Particles &f show the default protected range if the island protection range differs from it.
showing: '&2 Showing range indicators' showing: '&2 Showing range indicators'
set: set:
parameters: <player> <range> parameters: <player> <range> [island location]
description: sets the island protected range description: sets the island protected range
success: '&a Set island protection range to &b [number]&a .' success: '&a Set island protection range to &b [number]&a .'
reset: reset:
@ -175,12 +175,12 @@ commands:
success: '&a Reset island protection range to &b [number]&a .' success: '&a Reset island protection range to &b [number]&a .'
add: add:
description: increases the island protected range description: increases the island protected range
parameters: <player> <range> parameters: <player> <range> [island location]
success: '&a Successfully increased &b [name]&a ''s island protected range success: '&a Successfully increased &b [name]&a ''s island protected range
to &b [total] &7 (&b +[number]&7 )&a .' to &b [total] &7 (&b +[number]&7 )&a .'
remove: remove:
description: decreases the island protected range description: decreases the island protected range
parameters: <player> <range> parameters: <player> <range> [island location]
success: '&a Successfully decreased &b [name]&a ''s island protected range success: '&a Successfully decreased &b [name]&a ''s island protected range
to &b [total] &7 (&b -[number]&7 )&a .' to &b [total] &7 (&b -[number]&7 )&a .'
register: register:

View File

@ -1,7 +1,11 @@
package world.bentobox.bentobox.api.commands.admin.range; package world.bentobox.bentobox.api.commands.admin.range;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock; 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 static org.mockito.Mockito.when;
import java.util.ArrayList; import java.util.ArrayList;
@ -9,14 +13,18 @@ import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.Set;
import java.util.UUID; import java.util.UUID;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.PluginManager;
import org.bukkit.scheduler.BukkitScheduler; import org.bukkit.scheduler.BukkitScheduler;
import org.bukkit.util.Vector;
import org.eclipse.jdt.annotation.NonNull;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -56,6 +64,8 @@ public class AdminRangeSetCommandTest {
private PlayersManager pm; private PlayersManager pm;
@Mock @Mock
private PluginManager pim; private PluginManager pim;
@Mock
private @NonNull Location location;
/** /**
*/ */
@ -92,23 +102,25 @@ public class AdminRangeSetCommandTest {
// Island World Manager // Island World Manager
IslandWorldManager iwm = mock(IslandWorldManager.class); IslandWorldManager iwm = mock(IslandWorldManager.class);
when(iwm.getFriendlyName(Mockito.any())).thenReturn("BSkyBlock"); when(iwm.getFriendlyName(any())).thenReturn("BSkyBlock");
when(iwm.getIslandProtectionRange(Mockito.any())).thenReturn(200); when(iwm.getIslandProtectionRange(any())).thenReturn(200);
when(plugin.getIWM()).thenReturn(iwm); when(plugin.getIWM()).thenReturn(iwm);
// Player has island to begin with // Player has island to begin with
im = mock(IslandsManager.class); im = mock(IslandsManager.class);
when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(true); when(im.hasIsland(any(), any(UUID.class))).thenReturn(true);
when(im.hasIsland(Mockito.any(), Mockito.any(User.class))).thenReturn(true); when(im.hasIsland(any(), any(User.class))).thenReturn(true);
Island island = mock(Island.class); Island island = mock(Island.class);
when(island.getRange()).thenReturn(50); when(island.getRange()).thenReturn(50);
when(island.getProtectionRange()).thenReturn(50); when(island.getProtectionRange()).thenReturn(50);
when(im.getIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(island); when(location.toVector()).thenReturn(new Vector(2, 3, 4));
when(island.getCenter()).thenReturn(location);
when(im.getOwnedIslands(any(), any(UUID.class))).thenReturn(Set.of(island));
when(plugin.getIslands()).thenReturn(im); when(plugin.getIslands()).thenReturn(im);
// Has team // Has team
pm = mock(PlayersManager.class); pm = mock(PlayersManager.class);
when(im.inTeam(Mockito.any(), Mockito.eq(uuid))).thenReturn(true); when(im.inTeam(any(), Mockito.eq(uuid))).thenReturn(true);
when(plugin.getPlayers()).thenReturn(pm); when(plugin.getPlayers()).thenReturn(pm);
@ -122,11 +134,11 @@ public class AdminRangeSetCommandTest {
LocalesManager lm = mock(LocalesManager.class); LocalesManager lm = mock(LocalesManager.class);
Answer<String> answer = invocation -> invocation.getArgument(1, String.class); Answer<String> answer = invocation -> invocation.getArgument(1, String.class);
when(lm.get(Mockito.any(), Mockito.any())).thenAnswer(answer); when(lm.get(any(), any())).thenAnswer(answer);
when(plugin.getLocalesManager()).thenReturn(lm); when(plugin.getLocalesManager()).thenReturn(lm);
// Addon // Addon
when(iwm.getAddon(Mockito.any())).thenReturn(Optional.empty()); when(iwm.getAddon(any())).thenReturn(Optional.empty());
} }
@After @After
@ -137,69 +149,72 @@ public class AdminRangeSetCommandTest {
/** /**
* Test method for * Test method for
* {@link world.bentobox.bentobox.api.commands.admin.range.AdminRangeSetCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. * {@link world.bentobox.bentobox.api.commands.admin.range.AdminRangeSetCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/ */
@Test @Test
public void testExecuteConsoleNoArgs() { public void testExecuteConsoleNoArgs() {
AdminRangeSetCommand arc = new AdminRangeSetCommand(ac); AdminRangeSetCommand arc = new AdminRangeSetCommand(ac);
CommandSender sender = mock(CommandSender.class); CommandSender sender = mock(CommandSender.class);
User console = User.getInstance(sender); User console = User.getInstance(sender);
arc.execute(console, "", new ArrayList<>()); assertFalse(arc.canExecute(console, "", new ArrayList<>()));
// Show help // Show help
Mockito.verify(sender).sendMessage("commands.help.header"); verify(sender).sendMessage("commands.help.header");
} }
/** /**
* Test method for * Test method for
* {@link world.bentobox.bentobox.api.commands.admin.range.AdminRangeSetCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. * {@link world.bentobox.bentobox.api.commands.admin.range.AdminRangeSetCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/ */
@Test @Test
public void testExecutePlayerNoArgs() { public void testExecutePlayerNoArgs() {
AdminRangeSetCommand arc = new AdminRangeSetCommand(ac); AdminRangeSetCommand arc = new AdminRangeSetCommand(ac);
arc.execute(user, "", new ArrayList<>()); assertFalse(arc.canExecute(user, "", List.of()));
// Show help // Show help
Mockito.verify(user).sendMessage("commands.help.header", "[label]", "BSkyBlock"); verify(user).sendMessage("commands.help.header", "[label]", "BSkyBlock");
} }
/** /**
* Test method for * Test method for
* {@link world.bentobox.bentobox.api.commands.admin.range.AdminRangeSetCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. * {@link world.bentobox.bentobox.api.commands.admin.range.AdminRangeSetCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/ */
@Test @Test
public void testExecuteUnknownPlayer() { public void testExecuteUnknownPlayer() {
AdminRangeSetCommand arc = new AdminRangeSetCommand(ac); AdminRangeSetCommand arc = new AdminRangeSetCommand(ac);
String[] args = { "tastybento", "100" }; String[] args = { "tastybento", "100" };
arc.execute(user, "", Arrays.asList(args)); assertFalse(arc.canExecute(user, "", Arrays.asList(args)));
Mockito.verify(user).sendMessage("general.errors.unknown-player", "[name]", args[0]); verify(user).sendMessage("general.errors.unknown-player", "[name]", args[0]);
} }
/** /**
* Test method for {@link world.bentobox.bentobox.api.commands.admin.range.AdminRangeSetCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. * Test method for {@link world.bentobox.bentobox.api.commands.admin.range.AdminRangeSetCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/ */
@Test @Test
public void testExecuteKnownPlayerNotOwnerNoTeam() { public void testExecuteKnownPlayerNotOwnerNoTeam() {
when(pm.getUUID(Mockito.anyString())).thenReturn(uuid); when(pm.getUUID(anyString())).thenReturn(uuid);
when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(false); when(im.getOwnedIslands(any(), any(UUID.class))).thenReturn(Set.of());
when(im.inTeam(Mockito.any(), Mockito.any(UUID.class))).thenReturn(false); when(im.inTeam(any(), any(UUID.class))).thenReturn(false);
AdminRangeSetCommand arc = new AdminRangeSetCommand(ac); AdminRangeSetCommand arc = new AdminRangeSetCommand(ac);
List<String> args = new ArrayList<>(); List<String> args = new ArrayList<>();
args.add("tastybento"); args.add("tastybento");
args.add("100"); args.add("100");
arc.execute(user, "", args); assertFalse(arc.canExecute(user, "", args));
Mockito.verify(user).sendMessage("general.errors.player-has-no-island"); verify(user).sendMessage("general.errors.player-has-no-island");
} }
/**
* Test method for {@link world.bentobox.bentobox.api.commands.admin.range.AdminRangeSetCommand#canExecute(User, String, List)}
*/
@Test @Test
public void testExecuteKnownPlayerNotOwnerButInTeam() { public void testExecuteKnownPlayerNotOwnerButInTeam() {
when(pm.getUUID(Mockito.anyString())).thenReturn(uuid); when(pm.getUUID(anyString())).thenReturn(uuid);
when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(false); when(im.getOwnedIslands(any(), any(UUID.class))).thenReturn(Set.of());
when(im.inTeam(Mockito.any(), Mockito.any(UUID.class))).thenReturn(true); when(im.inTeam(any(), any(UUID.class))).thenReturn(true);
AdminRangeSetCommand arc = new AdminRangeSetCommand(ac); AdminRangeSetCommand arc = new AdminRangeSetCommand(ac);
List<String> args = new ArrayList<>(); List<String> args = new ArrayList<>();
args.add("tastybento"); args.add("tastybento");
args.add("100"); args.add("100");
arc.execute(user, "", args); assertFalse(arc.canExecute(user, "", args));
Mockito.verify(user, never()).sendMessage("general.errors.player-has-no-island"); verify(user).sendMessage("general.errors.player-has-no-island");
} }
/** /**
@ -207,41 +222,42 @@ public class AdminRangeSetCommandTest {
*/ */
@Test @Test
public void testExecuteTooHigh() { public void testExecuteTooHigh() {
when(pm.getUUID(Mockito.anyString())).thenReturn(uuid); when(pm.getUUID(anyString())).thenReturn(uuid);
AdminRangeSetCommand arc = new AdminRangeSetCommand(ac); AdminRangeSetCommand arc = new AdminRangeSetCommand(ac);
List<String> args = new ArrayList<>(); List<String> args = new ArrayList<>();
args.add("tastybento"); args.add("tastybento");
args.add("1000"); args.add("1000");
arc.execute(user, "", args); assertTrue(arc.canExecute(user, "", args));
Mockito.verify(user).sendMessage("commands.admin.range.invalid-value.too-high", TextVariables.NUMBER, "100"); assertFalse(arc.execute(user, "", args));
verify(user).sendMessage("commands.admin.range.invalid-value.too-high", TextVariables.NUMBER, "100");
} }
/** /**
* Test method for {@link world.bentobox.bentobox.api.commands.admin.range.AdminRangeSetCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. * Test method for {@link world.bentobox.bentobox.api.commands.admin.range.AdminRangeSetCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/ */
@Test @Test
public void testExecuteNotANumber() { public void testExecuteNotANumber() {
when(pm.getUUID(Mockito.anyString())).thenReturn(uuid); when(pm.getUUID(anyString())).thenReturn(uuid);
AdminRangeSetCommand arc = new AdminRangeSetCommand(ac); AdminRangeSetCommand arc = new AdminRangeSetCommand(ac);
List<String> args = new ArrayList<>(); List<String> args = new ArrayList<>();
args.add("tastybento"); args.add("tastybento");
args.add("NAN"); args.add("NAN");
arc.execute(user, "", args); assertFalse(arc.canExecute(user, "", args));
Mockito.verify(user).sendMessage("general.errors.must-be-positive-number", TextVariables.NUMBER, "NAN"); verify(user).sendMessage("general.errors.must-be-positive-number", TextVariables.NUMBER, "NAN");
} }
/** /**
* Test method for {@link world.bentobox.bentobox.api.commands.admin.range.AdminRangeSetCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. * Test method for {@link world.bentobox.bentobox.api.commands.admin.range.AdminRangeSetCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/ */
@Test() @Test()
public void testExecuteDoubleNumber() { public void testExecuteDoubleNumber() {
when(pm.getUUID(Mockito.anyString())).thenReturn(uuid); when(pm.getUUID(anyString())).thenReturn(uuid);
AdminRangeSetCommand arc = new AdminRangeSetCommand(ac); AdminRangeSetCommand arc = new AdminRangeSetCommand(ac);
List<String> args = new ArrayList<>(); List<String> args = new ArrayList<>();
args.add("tastybento"); args.add("tastybento");
args.add("3.141592654"); args.add("3.141592654");
arc.execute(user, "", args); assertFalse(arc.canExecute(user, "", args));
Mockito.verify(user).sendMessage("general.errors.must-be-positive-number", TextVariables.NUMBER, "3.141592654"); verify(user).sendMessage("general.errors.must-be-positive-number", TextVariables.NUMBER, "3.141592654");
} }
/** /**
@ -249,13 +265,14 @@ public class AdminRangeSetCommandTest {
*/ */
@Test @Test
public void testExecuteZero() { public void testExecuteZero() {
when(pm.getUUID(Mockito.anyString())).thenReturn(uuid); when(pm.getUUID(anyString())).thenReturn(uuid);
AdminRangeSetCommand arc = new AdminRangeSetCommand(ac); AdminRangeSetCommand arc = new AdminRangeSetCommand(ac);
List<String> args = new ArrayList<>(); List<String> args = new ArrayList<>();
args.add("tastybento"); args.add("tastybento");
args.add("0"); args.add("0");
arc.execute(user, "", args); assertTrue(arc.canExecute(user, "", args));
Mockito.verify(user).sendMessage("commands.admin.range.invalid-value.too-low", TextVariables.NUMBER, "0"); assertFalse(arc.execute(user, "", args));
verify(user).sendMessage("commands.admin.range.invalid-value.too-low", TextVariables.NUMBER, "0");
} }
/** /**
@ -263,13 +280,13 @@ public class AdminRangeSetCommandTest {
*/ */
@Test @Test
public void testExecuteNegNumber() { public void testExecuteNegNumber() {
when(pm.getUUID(Mockito.anyString())).thenReturn(uuid); when(pm.getUUID(anyString())).thenReturn(uuid);
AdminRangeSetCommand arc = new AdminRangeSetCommand(ac); AdminRangeSetCommand arc = new AdminRangeSetCommand(ac);
List<String> args = new ArrayList<>(); List<String> args = new ArrayList<>();
args.add("tastybento"); args.add("tastybento");
args.add("-437645"); args.add("-437645");
arc.execute(user, "", args); assertFalse(arc.canExecute(user, "", args));
Mockito.verify(user).sendMessage("general.errors.must-be-positive-number", TextVariables.NUMBER, "-437645"); verify(user).sendMessage("general.errors.must-be-positive-number", TextVariables.NUMBER, "-437645");
} }
/** /**
@ -277,13 +294,14 @@ public class AdminRangeSetCommandTest {
*/ */
@Test @Test
public void testExecuteSame() { public void testExecuteSame() {
when(pm.getUUID(Mockito.anyString())).thenReturn(uuid); when(pm.getUUID(anyString())).thenReturn(uuid);
AdminRangeSetCommand arc = new AdminRangeSetCommand(ac); AdminRangeSetCommand arc = new AdminRangeSetCommand(ac);
List<String> args = new ArrayList<>(); List<String> args = new ArrayList<>();
args.add("tastybento"); args.add("tastybento");
args.add("50"); args.add("50");
arc.execute(user, "", args); assertTrue(arc.canExecute(user, "", args));
Mockito.verify(user).sendMessage("commands.admin.range.invalid-value.same-as-before", TextVariables.NUMBER, "50"); assertFalse(arc.execute(user, "", args));
verify(user).sendMessage("commands.admin.range.invalid-value.same-as-before", TextVariables.NUMBER, "50");
} }
/** /**
@ -291,13 +309,14 @@ public class AdminRangeSetCommandTest {
*/ */
@Test @Test
public void testExecute() { public void testExecute() {
when(pm.getUUID(Mockito.anyString())).thenReturn(uuid); when(pm.getUUID(anyString())).thenReturn(uuid);
AdminRangeSetCommand arc = new AdminRangeSetCommand(ac); AdminRangeSetCommand arc = new AdminRangeSetCommand(ac);
List<String> args = new ArrayList<>(); List<String> args = new ArrayList<>();
args.add("tastybento"); args.add("tastybento");
args.add("48"); args.add("48");
arc.execute(user, "", args); assertTrue(arc.canExecute(user, "", args));
Mockito.verify(user).sendMessage("commands.admin.range.set.success", TextVariables.NUMBER, "48"); assertTrue(arc.execute(user, "", args));
verify(user).sendMessage("commands.admin.range.set.success", TextVariables.NUMBER, "48");
} }
} }