Added admin unregister island command and test.

Unit test passes. Not tested in-game yet.
2474km to San Francisco. 11552m high above Des Moines, Iowa.
This commit is contained in:
tastybento 2018-05-08 23:26:01 -04:00
parent 62d8249468
commit 4f9c3e49df
5 changed files with 250 additions and 10 deletions

View File

@ -70,6 +70,10 @@ commands:
parameters: "[player]"
description: "make player the team's leader"
already-leader: "&cPlayer is already the leader!"
unregister:
parameters: "[owner]"
description: "unregister owner from island, but keep island blocks"
unregistered-island: "&aUnregistered player from island at [xyz]."
info:
parameters: "<player>"
description: "get info on where you are or player's island"

View File

@ -1,4 +1,52 @@
package us.tastybento.bskyblock.commands.admin;
public class AdminUnregisterCommand {
}
import java.util.List;
import java.util.UUID;
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;
import us.tastybento.bskyblock.util.Util;
public class AdminUnregisterCommand extends CompositeCommand {
public AdminUnregisterCommand(CompositeCommand parent) {
super(parent, "unregister");
}
@Override
public void setup() {
setPermission(Constants.PERMPREFIX + "admin.unregister");
setParameters("commands.admin.unregister.parameters");
setDescription("commands.admin.unregister.description");
}
@Override
public boolean execute(User user, List<String> args) {
// If args are not right, show help
if (args.size() != 1) {
showHelp(this, user);
return false;
}
// Get target
UUID targetUUID = getPlayers().getUUID(args.get(0));
if (targetUUID == null) {
user.sendMessage("general.errors.unknown-player");
return false;
}
if (!getIslands().hasIsland(targetUUID)) {
user.sendMessage("general.errors.player-has-no-island");
return false;
}
if (getIslands().inTeam(targetUUID)) {
user.sendMessage("commands.admin.unregister.cannot-unregister-team-player");
return false;
}
// Unregister island
user.sendMessage("commands.admin.unregister.unregistered-island", "[xyz]", Util.xyz(getIslands().getIsland(targetUUID).getCenter().toVector()));
getIslands().removePlayer(targetUUID);
user.sendMessage("general.success");
return true;
}
}

View File

@ -667,14 +667,14 @@ public class Island implements DataObject {
// Show team members
showMembers(plugin, user);
Vector location = center.toVector();
user.sendMessage("commands.admin.info.island-location", "[xyz]", xyz(location));
user.sendMessage("commands.admin.info.island-location", "[xyz]", Util.xyz(location));
Vector from = center.toVector().subtract(new Vector(range, 0, range)).setY(0);
Vector to = center.toVector().add(new Vector(range-1, 0, range-1)).setY(center.getWorld().getMaxHeight());
user.sendMessage("commands.admin.info.island-coords", "[xz1]", xyz(from), "[xz2]", xyz(to));
user.sendMessage("commands.admin.info.island-coords", "[xz1]", Util.xyz(from), "[xz2]", Util.xyz(to));
user.sendMessage("commands.admin.info.protection-range", "[range]", String.valueOf(range));
Vector pfrom = center.toVector().subtract(new Vector(protectionRange, 0, protectionRange)).setY(0);
Vector pto = center.toVector().add(new Vector(protectionRange-1, 0, protectionRange-1)).setY(center.getWorld().getMaxHeight());;
user.sendMessage("commands.admin.info.protection-coords", "[xz1]", xyz(pfrom), "[xz2]", xyz(pto));
user.sendMessage("commands.admin.info.protection-coords", "[xz1]", Util.xyz(pfrom), "[xz2]", Util.xyz(pto));
if (spawn) {
user.sendMessage("commands.admin.info.is-spawn");
}
@ -685,11 +685,7 @@ public class Island implements DataObject {
}
return true;
}
private String xyz(Vector location) {
return location.getBlockX() + "," + location.getBlockY() + "," + location.getBlockZ();
}
/**
* Shows the members of this island
* @param plugin

View File

@ -16,6 +16,7 @@ import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.permissions.PermissionAttachmentInfo;
import org.bukkit.util.Vector;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.user.User;
@ -251,4 +252,8 @@ public class Util {
return permValue;
}
public static String xyz(Vector location) {
return location.getBlockX() + "," + location.getBlockY() + "," + location.getBlockZ();
}
}

View File

@ -0,0 +1,187 @@
package us.tastybento.bskyblock.commands.admin;
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.UUID;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitScheduler;
import org.bukkit.util.Vector;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
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 us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.Settings;
import us.tastybento.bskyblock.api.user.User;
import us.tastybento.bskyblock.commands.AdminCommand;
import us.tastybento.bskyblock.database.objects.Island;
import us.tastybento.bskyblock.managers.CommandsManager;
import us.tastybento.bskyblock.managers.IslandsManager;
import us.tastybento.bskyblock.managers.LocalesManager;
import us.tastybento.bskyblock.managers.PlayersManager;
/**
* @author tastybento
*
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({Bukkit.class, BSkyBlock.class, User.class })
public class AdminUnregisterCommandTest {
private BSkyBlock plugin;
private AdminCommand ac;
private UUID uuid;
private User user;
private Settings s;
private IslandsManager im;
private PlayersManager pm;
private UUID notUUID;
/**
* @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();
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
ac = mock(AdminCommand.class);
when(ac.getSubCommandAliases()).thenReturn(new HashMap<>());
// Player has island to begin with
im = mock(IslandsManager.class);
when(im.hasIsland(Mockito.any())).thenReturn(true);
when(im.isOwner(Mockito.any())).thenReturn(true);
when(im.getTeamLeader(Mockito.any())).thenReturn(uuid);
when(plugin.getIslands()).thenReturn(im);
// Has team
pm = mock(PlayersManager.class);
when(im.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);
// Locales
LocalesManager lm = mock(LocalesManager.class);
when(lm.get(Mockito.any(), Mockito.any())).thenReturn("mock translation");
when(plugin.getLocalesManager()).thenReturn(lm);
}
/**
* Test method for {@link us.tastybento.bskyblock.commands.admin.teams.AdminUnregisterCommand#execute(us.tastybento.bskyblock.api.user.User, java.util.List)}.
*/
@Test
public void testExecuteNoTarget() {
AdminUnregisterCommand itl = new AdminUnregisterCommand(ac);
assertFalse(itl.execute(user, new ArrayList<>()));
// Show help
}
/**
* Test method for {@link us.tastybento.bskyblock.commands.admin.teams.AdminUnregisterCommand#execute(us.tastybento.bskyblock.api.user.User, java.util.List)}.
*/
@Test
public void testExecuteUnknownPlayer() {
AdminUnregisterCommand itl = new AdminUnregisterCommand(ac);
String[] name = {"tastybento"};
when(pm.getUUID(Mockito.any())).thenReturn(null);
assertFalse(itl.execute(user, Arrays.asList(name)));
Mockito.verify(user).sendMessage(Mockito.eq("general.errors.unknown-player"));
}
/**
* Test method for {@link us.tastybento.bskyblock.commands.admin.teams.AdminUnregisterCommand#execute(us.tastybento.bskyblock.api.user.User, java.util.List)}.
*/
@Test
public void testExecutePlayerNoIsland() {
AdminUnregisterCommand itl = new AdminUnregisterCommand(ac);
String[] name = {"tastybento"};
when(pm.getUUID(Mockito.any())).thenReturn(notUUID);
when(im.hasIsland(Mockito.any())).thenReturn(false);
assertFalse(itl.execute(user, Arrays.asList(name)));
Mockito.verify(user).sendMessage(Mockito.eq("general.errors.player-has-no-island"));
}
/**
* Test method for {@link us.tastybento.bskyblock.commands.admin.teams.AdminUnregisterCommand#execute(us.tastybento.bskyblock.api.user.User, java.util.List)}.
*/
@Test
public void testExecuteInTeam() {
when(im.inTeam(Mockito.any())).thenReturn(true);
String[] name = {"tastybento"};
when(pm.getUUID(Mockito.any())).thenReturn(notUUID);
AdminUnregisterCommand itl = new AdminUnregisterCommand(ac);
assertFalse(itl.execute(user, Arrays.asList(name)));
Mockito.verify(user).sendMessage("commands.admin.unregister.cannot-unregister-team-player");
}
/**
* Test method for {@link us.us.tastybento.bskyblock.commands.admin.teams.AdminUnregisterCommand#execute(us.tastybento.bskyblock.api.user.User, java.util.List)}.
*/
@Test
public void testExecuteSuccess() {
when(im.inTeam(Mockito.any())).thenReturn(false);
Island is = mock(Island.class);
Location loc = mock(Location.class);
when(loc.toVector()).thenReturn(new Vector(123,123,432));
when(is.getCenter()).thenReturn(loc);
when(im.getIsland(Mockito.any())).thenReturn(is);
String[] name = {"tastybento"};
when(pm.getUUID(Mockito.any())).thenReturn(notUUID);
AdminUnregisterCommand itl = new AdminUnregisterCommand(ac);
assertTrue(itl.execute(user, Arrays.asList(name)));
// Add other verifications
Mockito.verify(user).sendMessage("commands.admin.unregister.unregistered-island", "[xyz]", "123,123,432");
Mockito.verify(user).sendMessage(Mockito.eq("general.success"));
}
}