Added team admin commands kick and disband.

This commit is contained in:
tastybento 2018-05-07 15:55:35 -07:00
parent 6a195db956
commit 354aff14f4
16 changed files with 532 additions and 22 deletions

View File

@ -49,6 +49,12 @@ commands:
admin:
help:
description: "admin command"
team:
kick:
parameters: "[team player]"
description: "kick a player from a team"
cannot-kick-leader: "&cYou cannot kick the team leader. Kick members first"
admin-kicked: "&cThe admin kicked you from the team."
info:
parameters: "<player>"
description: "get info on where you are or player's island"

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -0,0 +1,59 @@
package us.tastybento.bskyblock.commands.admin.teams;
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 AdminTeamDisbandCommand extends CompositeCommand {
public AdminTeamDisbandCommand(CompositeCommand parent) {
super(parent, "disband");
}
@Override
public void setup() {
setPermission(Constants.PERMPREFIX + "admin.team");
setParameters("commands.admin.team.disband.parameters");
setDescription("commands.admin.team.disband.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.disband.use-disband-leader", "[leader]", getPlayers().getName(getIslands().getTeamLeader(targetUUID)));
return false;
}
// Disband team
getIslands().getMembers(targetUUID).forEach(m -> {
User.getInstance(m).sendMessage("commands.admin.team.disbanded");
// The leader gets to keep the island
if (!m.equals(targetUUID)) {
getIslands().setLeaveTeam(m);
}
});
user.sendMessage("general.success");
return true;
}
}

View File

@ -0,0 +1,58 @@
package us.tastybento.bskyblock.commands.admin.teams;
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 AdminTeamKickCommand extends CompositeCommand {
public AdminTeamKickCommand(CompositeCommand parent) {
super(parent, "kick");
}
@Override
public void setup() {
setPermission(Constants.PERMPREFIX + "admin.team");
setParameters("commands.admin.team.kick.parameters");
setDescription("commands.admin.team.kick.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.kick.cannot-kick-leader");
getIslands().getIsland(targetUUID).showMembers(getPlugin(), user);
return false;
}
User.getInstance(targetUUID).sendMessage("commands.admin.team.kick.admin-kicked");
getIslands().removePlayer(targetUUID);
user.sendMessage("general.success");
return true;
}
}

View File

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

View File

@ -195,6 +195,7 @@ public class Island implements DataObject {
}
/**
* Get the team members of the island. If this is empty or cleared, there is no team.
* @return the members - key is the UUID, value is the RanksManager enum, e.g. RanksManager.MEMBER_RANK
*/
public Map<UUID, Integer> getMembers() {
@ -458,6 +459,10 @@ public class Island implements DataObject {
return true;
}
/**
* Removes a player from the team member map. Do not call this directly. Use {@link us.tastybento.bskyblock.managers.IslandsManager#removePlayer(UUID)}
* @param playerUUID
*/
public void removeMember(UUID playerUUID) {
members.remove(playerUUID);
}

View File

@ -704,7 +704,7 @@ public class IslandsManager {
}
/**
* Removes this player from any and all islands
* Removes this player from any and all islands.
* @param playerUUID - the player's UUID
*/
public void removePlayer(UUID playerUUID) {

View File

@ -254,8 +254,10 @@ public class IslandCache {
// Clear ownership and members
island.getMembers().clear();
island.setOwner(null);
} else {
// Remove player from the island membership
island.removeMember(playerUUID);
}
island.removeMember(playerUUID);
}
islandsByUUID.remove(playerUUID);
}

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 AdminTeamDisbandCommandTest {
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.AdminTeamDisbandCommand#execute(us.tastybento.bskyblock.api.user.User, java.util.List)}.
*/
@Test
public void testExecuteNoTarget() {
AdminTeamDisbandCommand itl = new AdminTeamDisbandCommand(ac);
assertFalse(itl.execute(user, new ArrayList<>()));
// Show help
}
/**
* Test method for {@link us.tastybento.bskyblock.commands.admin.teams.AdminTeamDisbandCommand#execute(us.tastybento.bskyblock.api.user.User, java.util.List)}.
*/
@Test
public void testExecuteUnknownPlayer() {
AdminTeamDisbandCommand itl = new AdminTeamDisbandCommand(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.AdminTeamDisbandCommand#execute(us.tastybento.bskyblock.api.user.User, java.util.List)}.
*/
@Test
public void testExecutePlayerNotInTeam() {
AdminTeamDisbandCommand itl = new AdminTeamDisbandCommand(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.AdminTeamDisbandCommand#execute(us.tastybento.bskyblock.api.user.User, java.util.List)}.
*/
@Test
public void testExecuteDisbandNotLeader() {
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(uuid);
when(pm.getName(Mockito.any())).thenReturn("leader");
AdminTeamDisbandCommand itl = new AdminTeamDisbandCommand(ac);
assertFalse(itl.execute(user, Arrays.asList(name)));
Mockito.verify(user).sendMessage("commands.admin.team.disband.use-disband-leader", "[leader]", "leader");
}
/**
* Test method for {@link us.us.tastybento.bskyblock.commands.admin.teams.AdminTeamDisbandCommand#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(notUUID);
// Members
Set<UUID> members = new HashSet<>();
members.add(uuid);
members.add(notUUID);
when(im.getMembers(Mockito.any())).thenReturn(members);
AdminTeamDisbandCommand itl = new AdminTeamDisbandCommand(ac);
assertTrue(itl.execute(user, Arrays.asList(name)));
Mockito.verify(im, Mockito.never()).setLeaveTeam(notUUID);
Mockito.verify(im).setLeaveTeam(uuid);
Mockito.verify(user).sendMessage(Mockito.eq("general.success"));
}
}

View File

@ -0,0 +1,193 @@
/**
*
*/
package us.tastybento.bskyblock.commands.admin.teams;
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.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.commands.admin.teams.AdminTeamKickCommand;
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 AdminTeamKickCommandTest {
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.AdminTeamKickCommand#execute(us.tastybento.bskyblock.api.user.User, java.util.List)}.
*/
@Test
public void testExecuteNoTarget() {
AdminTeamKickCommand itl = new AdminTeamKickCommand(ac);
assertFalse(itl.execute(user, new ArrayList<>()));
// Show help
}
/**
* Test method for {@link us.tastybento.bskyblock.commands.admin.teams.AdminTeamKickCommand#execute(us.tastybento.bskyblock.api.user.User, java.util.List)}.
*/
@Test
public void testExecuteUnknownPlayer() {
AdminTeamKickCommand itl = new AdminTeamKickCommand(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.AdminTeamKickCommand#execute(us.tastybento.bskyblock.api.user.User, java.util.List)}.
*/
@Test
public void testExecutePlayerNotInTeam() {
AdminTeamKickCommand itl = new AdminTeamKickCommand(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.AdminTeamKickCommand#execute(us.tastybento.bskyblock.api.user.User, java.util.List)}.
*/
@Test
public void testExecuteKickLeader() {
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);
AdminTeamKickCommand itl = new AdminTeamKickCommand(ac);
assertFalse(itl.execute(user, Arrays.asList(name)));
Mockito.verify(user).sendMessage(Mockito.eq("commands.admin.team.kick.cannot-kick-leader"));
Mockito.verify(is).showMembers(plugin, user);
}
/**
* Test method for {@link us.us.tastybento.bskyblock.commands.admin.teams.AdminTeamKickCommand#execute(us.tastybento.bskyblock.api.user.User, java.util.List)}.
*/
@Test
public void testExecute() {
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(uuid);
AdminTeamKickCommand itl = new AdminTeamKickCommand(ac);
assertTrue(itl.execute(user, Arrays.asList(name)));
Mockito.verify(im).removePlayer(notUUID);
Mockito.verify(user).sendMessage(Mockito.eq("general.success"));
}
}