mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2024-11-24 11:45:31 +01:00
Added /island banlist command and test class.
Note that preventing players from entering banned islands must still be implemented.
This commit is contained in:
parent
729974c1be
commit
76711f5d09
@ -173,6 +173,9 @@ commands:
|
||||
you-are-unbanned: "&b[owner]&a unbanned you from their island!"
|
||||
banlist:
|
||||
description: "list banned players"
|
||||
noone: "&aNo one is banned on this island"
|
||||
the-following: "&bThe following players are banned:"
|
||||
names: "&c[line]"
|
||||
lock:
|
||||
description: "lock/unlock your island so visitors cannot enter it"
|
||||
settings:
|
||||
|
@ -8,6 +8,7 @@ import us.tastybento.bskyblock.api.commands.CompositeCommand;
|
||||
import us.tastybento.bskyblock.api.user.User;
|
||||
import us.tastybento.bskyblock.commands.island.IslandAboutCommand;
|
||||
import us.tastybento.bskyblock.commands.island.IslandBanCommand;
|
||||
import us.tastybento.bskyblock.commands.island.IslandBanlistCommand;
|
||||
import us.tastybento.bskyblock.commands.island.IslandCreateCommand;
|
||||
import us.tastybento.bskyblock.commands.island.IslandGoCommand;
|
||||
import us.tastybento.bskyblock.commands.island.IslandLanguageCommand;
|
||||
@ -46,6 +47,7 @@ public class IslandCommand extends CompositeCommand {
|
||||
new IslandLanguageCommand(this);
|
||||
new IslandBanCommand(this);
|
||||
new IslandUnbanCommand(this);
|
||||
new IslandBanlistCommand(this);
|
||||
// Team commands
|
||||
new IslandTeamCommand(this);
|
||||
}
|
||||
|
@ -1,4 +1,71 @@
|
||||
package us.tastybento.bskyblock.commands.island;
|
||||
|
||||
public class IslandBanlistCommand {
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import us.tastybento.bskyblock.Constants;
|
||||
import us.tastybento.bskyblock.api.commands.CompositeCommand;
|
||||
import us.tastybento.bskyblock.api.user.User;
|
||||
import us.tastybento.bskyblock.database.objects.Island;
|
||||
|
||||
public class IslandBanlistCommand extends CompositeCommand {
|
||||
|
||||
public IslandBanlistCommand(CompositeCommand islandCommand) {
|
||||
super(islandCommand, "banlist");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setup() {
|
||||
setPermission(Constants.PERMPREFIX + "island.ban");
|
||||
setOnlyPlayer(true);
|
||||
setDescription("commands.island.banlist.description");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean execute(User user, List<String> args) {
|
||||
if (!args.isEmpty()) {
|
||||
// Show help
|
||||
showHelp(this, user);
|
||||
return false;
|
||||
}
|
||||
// Player issuing the command must have an island
|
||||
if (!getIslands().hasIsland(user.getUniqueId())) {
|
||||
user.sendMessage("general.errors.no-island");
|
||||
return false;
|
||||
}
|
||||
Island island = getIslands().getIsland(user.getUniqueId());
|
||||
// Show all the players banned on the island
|
||||
if (island.getBanned().isEmpty()) {
|
||||
user.sendMessage("commands.island.banlist.noone");
|
||||
return true;
|
||||
}
|
||||
// Title
|
||||
user.sendMessage("commands.island.banlist.the-following");
|
||||
// Create a nicely formatted list
|
||||
List<String> names = island.getBanned().stream().map(u -> getPlayers().getName(u)).sorted().collect(Collectors.toList());
|
||||
List<String> lines = new ArrayList<>();
|
||||
StringBuilder line = new StringBuilder();
|
||||
// Put the names into lines of no more than 40 characters long, separated by commas
|
||||
names.forEach(n -> {
|
||||
if (line.length() + n.length() < 41) {
|
||||
line.append(n);
|
||||
} else {
|
||||
lines.add(line.toString().trim());
|
||||
line.setLength(0);
|
||||
line.append(n);
|
||||
}
|
||||
line.append(", ");
|
||||
});
|
||||
// Remove trailing comma
|
||||
line.setLength(line.length() - 2);
|
||||
// Add the final line if it is not empty
|
||||
if (line.length() > 0) {
|
||||
lines.add(line.toString());
|
||||
}
|
||||
// Send the strings
|
||||
lines.forEach(l -> user.sendMessage("commands.island.banlist.names", "[line]", l));
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,168 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package us.tastybento.bskyblock.commands.island;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.scheduler.BukkitScheduler;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.stubbing.Answer;
|
||||
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 us.tastybento.bskyblock.BSkyBlock;
|
||||
import us.tastybento.bskyblock.Settings;
|
||||
import us.tastybento.bskyblock.api.user.User;
|
||||
import us.tastybento.bskyblock.commands.IslandCommand;
|
||||
import us.tastybento.bskyblock.database.objects.Island;
|
||||
import us.tastybento.bskyblock.managers.CommandsManager;
|
||||
import us.tastybento.bskyblock.managers.IslandsManager;
|
||||
import us.tastybento.bskyblock.managers.PlayersManager;
|
||||
|
||||
/**
|
||||
* @author tastybento
|
||||
*
|
||||
*/
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest({Bukkit.class, BSkyBlock.class, User.class })
|
||||
public class IslandBanlistCommandTest {
|
||||
|
||||
private BSkyBlock plugin;
|
||||
private IslandCommand ic;
|
||||
private UUID uuid;
|
||||
private User user;
|
||||
private Settings s;
|
||||
private IslandsManager im;
|
||||
private PlayersManager pm;
|
||||
private Island island;
|
||||
|
||||
/**
|
||||
* @throws java.lang.Exception
|
||||
*/
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
// Set up plugin
|
||||
plugin = mock(BSkyBlock.class);
|
||||
Whitebox.setInternalState(BSkyBlock.class, "instance", plugin);
|
||||
|
||||
// Command manager
|
||||
CommandsManager cm = mock(CommandsManager.class);
|
||||
when(plugin.getCommandsManager()).thenReturn(cm);
|
||||
|
||||
// Settings
|
||||
s = mock(Settings.class);
|
||||
when(s.getResetWait()).thenReturn(0L);
|
||||
when(s.getResetLimit()).thenReturn(3);
|
||||
when(plugin.getSettings()).thenReturn(s);
|
||||
|
||||
// Player
|
||||
Player p = mock(Player.class);
|
||||
// Sometimes use Mockito.withSettings().verboseLogging()
|
||||
user = mock(User.class);
|
||||
when(user.isOp()).thenReturn(false);
|
||||
uuid = UUID.randomUUID();
|
||||
when(user.getUniqueId()).thenReturn(uuid);
|
||||
when(user.getPlayer()).thenReturn(p);
|
||||
when(user.getName()).thenReturn("tastybento");
|
||||
|
||||
// Parent command has no aliases
|
||||
ic = mock(IslandCommand.class);
|
||||
when(ic.getSubCommandAliases()).thenReturn(new HashMap<>());
|
||||
|
||||
// No island for player to begin with (set it later in the tests)
|
||||
im = mock(IslandsManager.class);
|
||||
when(im.hasIsland(Mockito.eq(uuid))).thenReturn(false);
|
||||
when(im.isOwner(Mockito.eq(uuid))).thenReturn(false);
|
||||
when(plugin.getIslands()).thenReturn(im);
|
||||
|
||||
// Has team
|
||||
pm = mock(PlayersManager.class);
|
||||
when(pm.inTeam(Mockito.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);
|
||||
|
||||
// Island Banned list initialization
|
||||
island = mock(Island.class);
|
||||
when(island.getBanned()).thenReturn(new HashSet<>());
|
||||
when(island.isBanned(Mockito.any())).thenReturn(false);
|
||||
when(im.getIsland(Mockito.any(UUID.class))).thenReturn(island);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link us.tastybento.bskyblock.commands.island.IslandBanlistCommand#execute(us.tastybento.bskyblock.api.user.User, java.util.List)}.
|
||||
*/
|
||||
@Test
|
||||
public void testWithArgs() {
|
||||
IslandBanlistCommand iubc = new IslandBanlistCommand(ic);
|
||||
assertFalse(iubc.execute(user, Arrays.asList("bill")));
|
||||
// Verify show help
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoIsland() {
|
||||
IslandBanlistCommand iubc = new IslandBanlistCommand(ic);
|
||||
assertFalse(iubc.execute(user, new ArrayList<>()));
|
||||
Mockito.verify(user).sendMessage("general.errors.no-island");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBanlistNooneBanned() {
|
||||
IslandBanlistCommand iubc = new IslandBanlistCommand(ic);
|
||||
when(im.hasIsland(Mockito.eq(uuid))).thenReturn(true);
|
||||
assertTrue(iubc.execute(user, new ArrayList<>()));
|
||||
Mockito.verify(user).sendMessage("commands.island.banlist.noone");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBanlistBanned() {
|
||||
IslandBanlistCommand iubc = new IslandBanlistCommand(ic);
|
||||
when(im.hasIsland(Mockito.eq(uuid))).thenReturn(true);
|
||||
// Make a ban list
|
||||
String[] names = {"adam", "ben", "cara", "dave", "ed", "frank", "freddy", "george", "harry", "ian", "joe"};
|
||||
Set<UUID> banned = new HashSet<>();
|
||||
Map<UUID, String> uuidToName = new HashMap<>();
|
||||
for (int j = 0; j < names.length; j++) {
|
||||
UUID uuid = UUID.randomUUID();
|
||||
banned.add(uuid);
|
||||
uuidToName.put(uuid, names[j]);
|
||||
}
|
||||
when(island.getBanned()).thenReturn(banned);
|
||||
// Respond to name queries
|
||||
when(pm.getName(Mockito.any(UUID.class))).then(new Answer<String>() {
|
||||
|
||||
@Override
|
||||
public String answer(InvocationOnMock invocation) throws Throwable {
|
||||
return uuidToName.getOrDefault(invocation.getArgumentAt(0, UUID.class), "tastybento");
|
||||
}
|
||||
|
||||
});
|
||||
assertTrue(iubc.execute(user, new ArrayList<>()));
|
||||
Mockito.verify(user).sendMessage("commands.island.banlist.the-following");
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user