Added admin Make Leader command and test.

Unit test passes. Not tested in-game because I'm 9930m above Chicago,
it's 2858km to San Francisco, it's dark, I'm wearing sunglasses, and I
have no Wi-Fi!
This commit is contained in:
tastybento 2018-05-08 22:56:24 -04:00
parent 5eb6447f01
commit 62d8249468
8 changed files with 288 additions and 37 deletions

View File

@ -66,6 +66,10 @@ commands:
description: "kick a player from a team" description: "kick a player from a team"
cannot-kick-leader: "&cYou cannot kick the team leader. Kick members first" cannot-kick-leader: "&cYou cannot kick the team leader. Kick members first"
admin-kicked: "&cThe admin kicked you from the team." admin-kicked: "&cThe admin kicked you from the team."
makeleader:
parameters: "[player]"
description: "make player the team's leader"
already-leader: "&cPlayer is already the leader!"
info: info:
parameters: "<player>" parameters: "<player>"
description: "get info on where you are or player's island" description: "get info on where you are or player's island"

View File

@ -14,6 +14,7 @@ import us.tastybento.bskyblock.commands.admin.AdminVersionCommand;
import us.tastybento.bskyblock.commands.admin.teams.AdminTeamAddCommand; import us.tastybento.bskyblock.commands.admin.teams.AdminTeamAddCommand;
import us.tastybento.bskyblock.commands.admin.teams.AdminTeamDisbandCommand; import us.tastybento.bskyblock.commands.admin.teams.AdminTeamDisbandCommand;
import us.tastybento.bskyblock.commands.admin.teams.AdminTeamKickCommand; import us.tastybento.bskyblock.commands.admin.teams.AdminTeamKickCommand;
import us.tastybento.bskyblock.commands.admin.teams.AdminTeamMakeLeaderCommand;
public class AdminCommand extends CompositeCommand { public class AdminCommand extends CompositeCommand {
@ -39,6 +40,7 @@ public class AdminCommand extends CompositeCommand {
new AdminTeamAddCommand(this); new AdminTeamAddCommand(this);
new AdminTeamKickCommand(this); new AdminTeamKickCommand(this);
new AdminTeamDisbandCommand(this); new AdminTeamDisbandCommand(this);
new AdminTeamMakeLeaderCommand(this);
} }
@Override @Override

View File

@ -1,4 +1,53 @@
package us.tastybento.bskyblock.commands.admin.teams; package us.tastybento.bskyblock.commands.admin.teams;
public class AdminTeamMakeLeaderCommand { 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;
public class AdminTeamMakeLeaderCommand extends CompositeCommand {
public AdminTeamMakeLeaderCommand(CompositeCommand parent) {
super(parent, "makeleader");
}
@Override
public void setup() {
setPermission(Constants.PERMPREFIX + "admin.team");
setParameters("commands.admin.team.makeleader.parameters");
setDescription("commands.admin.team.makeleader.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.no-island");
return false;
}
if (!getIslands().inTeam(targetUUID)) {
user.sendMessage("general.errors.not-in-team");
return false;
}
if (getIslands().getTeamLeader(targetUUID).equals(targetUUID)) {
user.sendMessage("commands.admin.team.makeleader.already-leader");
return false;
}
// Make new leader
getIslands().makeLeader(user, targetUUID);
user.sendMessage("general.success");
return true;
}
}

View File

@ -1,4 +0,0 @@
package us.tastybento.bskyblock.commands.admin.teams;
public class AdminTeamPromoteCommand {
}

View File

@ -45,8 +45,4 @@ public abstract class AbstractIslandTeamCommand extends CompositeCommand {
protected int getMaxTeamSize(User user) { protected int getMaxTeamSize(User user) {
return Util.getPermValue(user.getPlayer(), "team.maxsize.", getSettings().getMaxTeamSize()); return Util.getPermValue(user.getPlayer(), "team.maxsize.", getSettings().getMaxTeamSize());
} }
protected int getMaxRangeSize(User user) {
return Util.getPermValue(user.getPlayer(), "island.range.", getSettings().getIslandProtectionRange());
}
} }

View File

@ -11,7 +11,6 @@ import us.tastybento.bskyblock.api.commands.CompositeCommand;
import us.tastybento.bskyblock.api.events.IslandBaseEvent; import us.tastybento.bskyblock.api.events.IslandBaseEvent;
import us.tastybento.bskyblock.api.events.team.TeamEvent; import us.tastybento.bskyblock.api.events.team.TeamEvent;
import us.tastybento.bskyblock.api.user.User; import us.tastybento.bskyblock.api.user.User;
import us.tastybento.bskyblock.database.objects.Island;
import us.tastybento.bskyblock.util.Util; import us.tastybento.bskyblock.util.Util;
public class IslandTeamSetownerCommand extends AbstractIslandTeamCommand { public class IslandTeamSetownerCommand extends AbstractIslandTeamCommand {
@ -71,35 +70,11 @@ public class IslandTeamSetownerCommand extends AbstractIslandTeamCommand {
if (event.isCancelled()) { if (event.isCancelled()) {
return false; return false;
} }
makeLeader(user, targetUUID); getIslands().makeLeader(user, targetUUID);
getIslands().save(true); getIslands().save(true);
return true; return true;
} }
private void makeLeader(User user, UUID targetUUID) {
// target is the new leader
Island island = getIslands().getIsland(user.getUniqueId());
island.setOwner(targetUUID);
user.sendMessage("commands.island.team.setowner.name-is-the-owner", "[name]", getPlayers().getName(targetUUID));
// Check if online
User target = User.getInstance(targetUUID);
target.sendMessage("commands.island.team.setowner.you-are-the-owner");
if (target.isOnline()) {
// Check if new leader has a lower range permission than the island size
int range = getMaxRangeSize(user);
// Range can go up or down
if (range != island.getProtectionRange()) {
user.sendMessage("commands.admin.setrange.range-updated", "[number]", String.valueOf(range));
target.sendMessage("commands.admin.setrange.range-updated", "[number]", String.valueOf(range));
getPlugin().log("Makeleader: Island protection range changed from " + island.getProtectionRange() + " to "
+ range + " for " + user.getName() + " due to permission.");
}
island.setProtectionRange(range);
}
}
@Override @Override
public Optional<List<String>> tabComplete(final User user, final String alias, final LinkedList<String> args) { public Optional<List<String>> tabComplete(final User user, final String alias, final LinkedList<String> args) {

View File

@ -66,7 +66,7 @@ public class IslandsManager {
if (space1.isLiquid() && space2.isLiquid()) { if (space1.isLiquid() && space2.isLiquid()) {
return false; return false;
} }
// Portals are not "safe" // Portals are not "safe"
if (space1.getType() == Material.PORTAL || ground.getType() == Material.PORTAL || space2.getType() == Material.PORTAL if (space1.getType() == Material.PORTAL || ground.getType() == Material.PORTAL || space2.getType() == Material.PORTAL
|| space1.getType() == Material.ENDER_PORTAL || ground.getType() == Material.ENDER_PORTAL || space2.getType() == Material.ENDER_PORTAL) { || space1.getType() == Material.ENDER_PORTAL || ground.getType() == Material.ENDER_PORTAL || space2.getType() == Material.ENDER_PORTAL) {
@ -817,4 +817,38 @@ public class IslandsManager {
public boolean inTeam(UUID playerUUID) { public boolean inTeam(UUID playerUUID) {
return getMembers(playerUUID).size() > 1; return getMembers(playerUUID).size() > 1;
} }
/**
* Makes a new leader for an island
* @param user - the user who is issuing the command
* @param targetUUID - the current island member who is going to become the leader
*/
public void makeLeader(User user, UUID targetUUID) {
// target is the new leader
Island island = getIsland(user.getUniqueId());
island.setOwner(targetUUID);
user.sendMessage("commands.island.team.setowner.name-is-the-owner", "[name]", plugin.getPlayers().getName(targetUUID));
// Check if online
User target = User.getInstance(targetUUID);
target.sendMessage("commands.island.team.setowner.you-are-the-owner");
if (target.isOnline()) {
// Check if new leader has a different range permission than the island size
int range = getMaxRangeSize(target);
// Range can go up or down
if (range != island.getProtectionRange()) {
user.sendMessage("commands.admin.setrange.range-updated", "[number]", String.valueOf(range));
target.sendMessage("commands.admin.setrange.range-updated", "[number]", String.valueOf(range));
plugin.log("Makeleader: Island protection range changed from " + island.getProtectionRange() + " to "
+ range + " for " + user.getName() + " due to permission.");
}
island.setProtectionRange(range);
}
}
private int getMaxRangeSize(User user) {
return Util.getPermValue(user.getPlayer(), "island.range.", plugin.getSettings().getIslandProtectionRange());
}
} }

View File

@ -0,0 +1,195 @@
package us.tastybento.bskyblock.commands.admin.teams;
import static org.junit.Assert.*;
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.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.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 AdminTeamMakeLeaderCommandTest {
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.AdminTeamMakeLeaderCommand#execute(us.tastybento.bskyblock.api.user.User, java.util.List)}.
*/
@Test
public void testExecuteNoTarget() {
AdminTeamMakeLeaderCommand itl = new AdminTeamMakeLeaderCommand(ac);
assertFalse(itl.execute(user, new ArrayList<>()));
// Show help
}
/**
* Test method for {@link us.tastybento.bskyblock.commands.admin.teams.AdminTeamMakeLeaderCommand#execute(us.tastybento.bskyblock.api.user.User, java.util.List)}.
*/
@Test
public void testExecuteUnknownPlayer() {
AdminTeamMakeLeaderCommand itl = new AdminTeamMakeLeaderCommand(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.AdminTeamMakeLeaderCommand#execute(us.tastybento.bskyblock.api.user.User, java.util.List)}.
*/
@Test
public void testExecutePlayerNotInTeam() {
AdminTeamMakeLeaderCommand itl = new AdminTeamMakeLeaderCommand(ac);
String[] name = {"tastybento"};
when(pm.getUUID(Mockito.any())).thenReturn(notUUID);
when(im.getMembers(Mockito.any())).thenReturn(new HashSet<>());
assertFalse(itl.execute(user, Arrays.asList(name)));
Mockito.verify(user).sendMessage(Mockito.eq("general.errors.not-in-team"));
}
/**
* Test method for {@link us.tastybento.bskyblock.commands.admin.teams.AdminTeamMakeLeaderCommand#execute(us.tastybento.bskyblock.api.user.User, java.util.List)}.
*/
@Test
public void testExecuteMakeLeaderAlreadyLeader() {
when(im.inTeam(Mockito.any())).thenReturn(true);
Island is = mock(Island.class);
when(im.getIsland(Mockito.any())).thenReturn(is);
String[] name = {"tastybento"};
when(pm.getUUID(Mockito.any())).thenReturn(notUUID);
when(im.getTeamLeader(notUUID)).thenReturn(notUUID);
AdminTeamMakeLeaderCommand itl = new AdminTeamMakeLeaderCommand(ac);
assertFalse(itl.execute(user, Arrays.asList(name)));
Mockito.verify(user).sendMessage("commands.admin.team.makeleader.already-leader");
}
/**
* Test method for {@link us.us.tastybento.bskyblock.commands.admin.teams.AdminTeamMakeLeaderCommand#execute(us.tastybento.bskyblock.api.user.User, java.util.List)}.
*/
@Test
public void testExecuteSuccess() {
when(im.inTeam(Mockito.any())).thenReturn(true);
Island is = mock(Island.class);
when(im.getIsland(Mockito.any())).thenReturn(is);
String[] name = {"tastybento"};
when(pm.getUUID(Mockito.any())).thenReturn(notUUID);
// Leader
when(im.getTeamLeader(notUUID)).thenReturn(uuid);
when(pm.getName(Mockito.eq(uuid))).thenReturn("leader");
// Members
Set<UUID> members = new HashSet<>();
members.add(uuid);
members.add(notUUID);
when(im.getMembers(Mockito.any())).thenReturn(members);
AdminTeamMakeLeaderCommand itl = new AdminTeamMakeLeaderCommand(ac);
assertTrue(itl.execute(user, Arrays.asList(name)));
// Add other verifications
Mockito.verify(im).makeLeader(user, notUUID);
Mockito.verify(user).sendMessage(Mockito.eq("general.success"));
}
}