Fixed teams.

Added test cases for RanksManager.
This commit is contained in:
Tastybento 2018-02-11 20:26:09 -08:00
parent 54dd9e87d4
commit 5ac66edb6a
15 changed files with 307 additions and 48 deletions

View File

@ -126,9 +126,16 @@ commands:
parameters: "<player>"
type-again: "&cEnter the kick command again to confirm"
leader-kicked: "&cThe leader kicked you from the island!"
promote:
description: "promote a player on your island to another rank"
demote:
description: "demote a player on your island down a rank"
parameters: "<player>"
failure: "&cPlayer cannot be demoted any further!"
success: "Demoted [name] to [rank]"
promote:
description: "promote a player on your island up a rank"
parameters: "<player>"
failure: "&cPlayer cannot be promoted any further!"
success: "Promoted [name] to [rank]"
setowner:
description: "transfer your island ownership to a member"
errors:

View File

@ -160,7 +160,7 @@ public class Settings implements ISettings<Settings> {
private boolean resetConfirmation = true;
@ConfigEntry(path = "island.reset-wait")
private long resetWait = 300;
private long resetWait = 10L;
@ConfigEntry(path = "island.reset.leavers-lose-reset")
private boolean leaversLoseReset = false;
@ -266,13 +266,13 @@ public class Settings implements ISettings<Settings> {
private boolean kickConfirmation = true;
@ConfigEntry(path = "island.require-confirmation.kick-wait")
private long kickWait = 300;
private long kickWait = 10L;
@ConfigEntry(path = "island.require-confirmation.leave")
private boolean leaveConfirmation = true;
@ConfigEntry(path = "island.require-confirmation.leave-wait")
private long leaveWait = 300;
private long leaveWait = 10L;
/**

View File

@ -24,19 +24,28 @@ public class IslandTeamCommand extends AbstractIslandTeamCommand {
new IslandTeamInviteCommand(this);
new IslandTeamLeaveCommand(this);
new IslandTeamPromoteCommand(this);
// TODO: These are still in development
//new IslandTeamPromoteCommand(this, "promote");
//new IslandTeamPromoteCommand(this, "demote");
new IslandTeamSetownerCommand(this);
new IslandTeamKickCommand(this);
}
@Override
public boolean execute(User user, List<String> args) {
// Player issuing the command must have an island
UUID teamLeaderUUID = getTeamLeader(user);
if (teamLeaderUUID == null) {
user.sendMessage("general.errors.no-island");
return false;
}
UUID playerUUID = user.getUniqueId();
// Fire event so add-ons can run commands, etc.
if (fireEvent(playerUUID)) {
// Cancelled
return false;
}
UUID teamLeaderUUID = getTeamLeader(user);
Set<UUID> teamMembers = getMembers(user);
if (teamLeaderUUID.equals(playerUUID)) {
int maxSize = getMaxTeamSize(user);

View File

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

View File

@ -106,7 +106,7 @@ public class IslandTeamInviteAcceptCommand extends AbstractIslandTeamCommand {
}
getIslands().save(false);
if (DEBUG) {
getPlugin().getLogger().info("DEBUG: After save " + getIslands().getIsland(prospectiveTeamLeaderUUID).getMemberSet().toString());
getPlugin().getLogger().info(() -> "DEBUG: After save " + getIslands().getIsland(prospectiveTeamLeaderUUID).getMemberSet().toString());
}
return true;
}

View File

@ -37,9 +37,12 @@ public class IslandTeamInviteCommand extends AbstractIslandTeamCommand {
public boolean execute(User user, List<String> args) {
UUID playerUUID = user.getUniqueId();
// Player issuing the command must have an island
boolean inTeam = getPlugin().getPlayers().inTeam(playerUUID);
UUID teamLeaderUUID = getPlugin().getIslands().getTeamLeader(playerUUID);
if (!(inTeam && teamLeaderUUID.equals(playerUUID))) {
UUID teamLeaderUUID = getTeamLeader(user);
if (teamLeaderUUID == null) {
user.sendMessage("general.errors.no-island");
return false;
}
if (!(teamLeaderUUID.equals(playerUUID))) {
user.sendMessage("general.errors.not-leader");
return false;
}

View File

@ -33,7 +33,7 @@ public class IslandTeamKickCommand extends AbstractIslandTeamCommand {
user.sendMessage("general.errors.no-team");
return true;
}
if (!getIslands().getTeamLeader(user.getUniqueId()).equals(user.getUniqueId())) {
if (!getTeamLeader(user).equals(user.getUniqueId())) {
user.sendMessage("general.errors.not-leader");
return true;
}
@ -64,9 +64,11 @@ public class IslandTeamKickCommand extends AbstractIslandTeamCommand {
@Override
public void run() {
kickSet.remove(targetUUID);
user.sendMessage("general.errors.command-cancelled");
}}.runTaskLater(getPlugin(), getSettings().getKickWait());
if (kickSet.contains(targetUUID)) {
kickSet.remove(targetUUID);
user.sendMessage("general.errors.command-cancelled");
}
}}.runTaskLater(getPlugin(), getSettings().getKickWait() * 20);
}
return true;
}

View File

@ -48,9 +48,11 @@ public class IslandTeamLeaveCommand extends AbstractIslandTeamCommand {
@Override
public void run() {
leaveSet.remove(user.getUniqueId());
user.sendMessage("general.errors.command-cancelled");
}}.runTaskLater(getPlugin(), getSettings().getKickWait());
if (leaveSet.contains(user.getUniqueId())) {
leaveSet.remove(user.getUniqueId());
user.sendMessage("general.errors.command-cancelled");
}
}}.runTaskLater(getPlugin(), getSettings().getLeaveWait() * 20);
return false;
}
}

View File

@ -2,26 +2,88 @@ package us.tastybento.bskyblock.commands.island.teams;
import java.util.List;
import org.bukkit.Bukkit;
import us.tastybento.bskyblock.Constants;
import us.tastybento.bskyblock.api.commands.User;
public class IslandTeamPromoteCommand extends AbstractIslandTeamCommand {
public IslandTeamPromoteCommand(IslandTeamCommand islandTeamCommand) {
super(islandTeamCommand, "promote");
public IslandTeamPromoteCommand(IslandTeamCommand islandTeamCommand, String string) {
super(islandTeamCommand, string);
}
@Override
public void setup() {
setPermission(Constants.PERMPREFIX + "island.team");
setOnlyPlayer(true);
setParameters("commands.island.team.promote.parameters");
setDescription("commands.island.team.promote.description");
if (this.getLabel().equals("promote")) {
setParameters("commands.island.team.promote.parameters");
setDescription("commands.island.team.promote.description");
} else {
setParameters("commands.island.team.demote.parameters");
setDescription("commands.island.team.demote.description");
}
}
@Override
public boolean execute(User user, List<String> args) {
return true;
if (!getPlayers().inTeam(user.getUniqueId())) {
user.sendMessage("general.errors.no-team");
return true;
}
if (!getTeamLeader(user).equals(user.getUniqueId())) {
user.sendMessage("general.errors.not-leader");
return true;
}
// If args are not right, show help
if (args.size() != 1) {
showHelp(this, user);
return false;
}
// Get target
User target = getPlayers().getUser(args.get(0));
if (target == null) {
user.sendMessage("general.errors.unknown-player");
return true;
}
if (!inTeam(target) || !getTeamLeader(user).equals(getTeamLeader(target))) {
user.sendMessage("general.errors.not-in-team");
return true;
}
return change(user, target);
}
private boolean change(User user, User target) {
int currentRank = getIslands().getIsland(user.getUniqueId()).getRank(target);
Bukkit.getLogger().info("DEBUG: current rank = " + currentRank);
if (this.getLabel().equals("promote")) {
int nextRank = getPlugin().getRanksManager().getNextRankValue(currentRank);
Bukkit.getLogger().info("DEBUG: next rank = " + nextRank);
if (nextRank > currentRank) {
getIslands().getIsland(user.getUniqueId()).setRank(target, nextRank);
String rankName = user.getTranslation(getPlugin().getRanksManager().getRank(nextRank));
user.sendMessage("commands.island.team.promote.success", "[name]", target.getName(), "[rank]", rankName);
return true;
} else {
user.sendMessage("commands.island.team.promote.failure");
return false;
}
} else {
// Demote
int prevRank = getPlugin().getRanksManager().getPreviousRankValue(currentRank);
Bukkit.getLogger().info("DEBUG: Rev rank = " + prevRank);
if (prevRank < currentRank) {
getIslands().getIsland(user.getUniqueId()).setRank(target, prevRank);
String rankName = user.getTranslation(getPlugin().getRanksManager().getRank(prevRank));
user.sendMessage("commands.island.team.demote.success", "[name]", target.getName(), "[rank]", rankName);
return true;
} else {
user.sendMessage("commands.island.team.demote.failure");
return false;
}
}
}
}

View File

@ -32,7 +32,7 @@ public class IslandTeamSetownerCommand extends AbstractIslandTeamCommand {
UUID playerUUID = user.getUniqueId();
// Can use if in a team
boolean inTeam = getPlugin().getPlayers().inTeam(playerUUID);
UUID teamLeaderUUID = getPlugin().getIslands().getTeamLeader(playerUUID);
UUID teamLeaderUUID = getTeamLeader(user);
if (!(inTeam && teamLeaderUUID.equals(playerUUID))) {
user.sendMessage("general.errors.not-leader");
return false;
@ -51,10 +51,6 @@ public class IslandTeamSetownerCommand extends AbstractIslandTeamCommand {
user.sendMessage("general.errors.no-team");
return false;
}
if (!teamLeaderUUID.equals(playerUUID)) {
user.sendMessage("general.errors.not-leader");
return false;
}
if (targetUUID.equals(playerUUID)) {
user.sendMessage("commands.island.team.setowner.errors.cant-transfer-to-yourself");
return false;

View File

@ -611,4 +611,14 @@ public class PlayersManager{
}
}
/**
* Tries to get the user from this name
* @param string
* @return
*/
public User getUser(String string) {
return User.getInstance(getUUID(string));
}
}

View File

@ -310,7 +310,7 @@ public class IslandCache {
public Set<UUID> getMembers(UUID playerUUID) {
Island island = islandsByUUID.get(playerUUID);
if (island != null) {
return new HashSet<>(island.getMemberSet());
return island.getMemberSet();
}
return new HashSet<>(0);
}
@ -365,10 +365,10 @@ public class IslandCache {
plugin.getLogger().info("DEBUG: player is the owner of this island");
}
// Clear ownership and members
island.getMemberSet().clear();
island.getMembers().clear();
island.setOwner(null);
}
island.getMemberSet().remove(playerUUID);
island.removeMember(playerUUID);
}
if (DEBUG) {
plugin.getLogger().info("DEBUG: removing reference to island by UUID");

View File

@ -14,6 +14,9 @@ import org.bukkit.World;
import org.bukkit.block.BlockState;
import org.bukkit.entity.Entity;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSet.Builder;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.commands.User;
import us.tastybento.bskyblock.api.events.IslandBaseEvent;
@ -200,14 +203,15 @@ public class Island implements DataObject {
/**
* @return the members of the island (owner included)
*/
public Set<UUID> getMemberSet(){
Set<UUID> result = new HashSet<>();
public ImmutableSet<UUID> getMemberSet(){
Builder<UUID> result = new ImmutableSet.Builder<>();
for (Entry<UUID, Integer> member: members.entrySet()) {
if (member.getValue() >= RanksManager.MEMBER_RANK) {
result.add(member.getKey());
}
}
return result;
return result.build();
}
/**

View File

@ -23,13 +23,6 @@ public class RanksManager {
// The store of ranks
private LinkedHashMap<String, Integer> ranks = new LinkedHashMap<>();
{
// Hard coded ranks
addRank(OWNER_RANK_REF, OWNER_RANK);
addRank(MEMBER_RANK_REF, MEMBER_RANK);
addRank(VISITOR_RANK_REF, VISITOR_RANK);
addRank(BANNED_RANK_REF, BANNED_RANK);
}
/**
* @param plugin
@ -37,6 +30,11 @@ public class RanksManager {
public RanksManager(BSkyBlock plugin) {
super();
this.plugin = plugin;
// Hard coded ranks
ranksPut(OWNER_RANK_REF, OWNER_RANK);
ranksPut(MEMBER_RANK_REF, MEMBER_RANK);
ranksPut(VISITOR_RANK_REF, VISITOR_RANK);
ranksPut(BANNED_RANK_REF, BANNED_RANK);
loadCustomRanks();
}
@ -64,13 +62,18 @@ public class RanksManager {
|| reference.equalsIgnoreCase(BANNED_RANK_REF)) {
return false;
}
ranksPut(reference, value);
return true;
}
private void ranksPut(String reference, int value) {
ranks.put(reference, value);
// Sort
ranks = ranks.entrySet().stream()
.sorted(Map.Entry.comparingByValue())
.collect(Collectors.toMap(
Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
return true;
Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
}
/**
@ -102,9 +105,50 @@ public class RanksManager {
* Get the ranks. Ranks are listed in ascending order
* @return immutable map of ranks
*/
public LinkedHashMap<String, Integer> getRanks() {
public Map<String, Integer> getRanks() {
return new LinkedHashMap<>(ranks);
}
/**
* Gets the next rank value above the current rank, excluding the owner rank
* @param currentRank
* @return Optional rank value
*/
public int getNextRankValue(int currentRank) {
return getRanks().values().stream().mapToInt(x -> {
if (x > currentRank) {
return x;
};
return OWNER_RANK;
}).min().orElse(currentRank);
}
/**
* Gets the previous rank value below the current rank
* @param currentRank
* @return Optional rank value
*/
public int getPreviousRankValue(int currentRank) {
return getRanks().values().stream().mapToInt(x -> {
if (x < currentRank) {
return x;
};
return BANNED_RANK;
}).max().orElse(currentRank);
}
/**
* Gets the reference to the rank name for value
* @param rank - value
* @return Reference
*/
public String getRank(int rank) {
for (Entry<String, Integer> en : ranks.entrySet()) {
if (rank == en.getValue()) {
return en.getKey();
}
}
return "";
}
}

View File

@ -0,0 +1,124 @@
/**
*
*/
package us.tastybento.bskyblock.managers;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import org.junit.Before;
import org.junit.Test;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.Settings;
/**
* @author tastybento
*
*/
public class RanksManagerTest {
public static RanksManager ranksManager;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
BSkyBlock plugin = mock(BSkyBlock.class);
Settings settings = mock(Settings.class);
// Blank ranks for now
Map<String, Integer> customRanks = new HashMap<>();
when(plugin.getSettings()).thenReturn(settings);
when(settings.getCustomRanks()).thenReturn(customRanks);
ranksManager = new RanksManager(plugin);
}
/**
* Test method for {@link us.tastybento.bskyblock.managers.RanksManager#addRank(java.lang.String, int)}.
*/
@Test
public void testAddRank() {
assertTrue(ranksManager.addRank("test.rank.reference", 750));
}
/**
* Test method for {@link us.tastybento.bskyblock.managers.RanksManager#removeRank(java.lang.String)}.
*/
@Test
public void testRemoveRank() {
ranksManager.addRank("test.rank.reference2", 650);
assertTrue(ranksManager.removeRank("test.rank.reference2"));
// Second time should fail
assertFalse(ranksManager.removeRank("test.rank.reference2"));
}
/**
* Test method for {@link us.tastybento.bskyblock.managers.RanksManager#getRankValue(java.lang.String)}.
*/
@Test
public void testGetRankValue() {
ranksManager.addRank("test.rank.reference.value", 600);
assertEquals(600, ranksManager.getRankValue("test.rank.reference.value"));
}
/**
* Test method for {@link us.tastybento.bskyblock.managers.RanksManager#getRanks()}.
*/
@Test
public void testGetRanks() {
Map<String, Integer> ranks = ranksManager.getRanks();
assertTrue(ranks.containsKey(RanksManager.BANNED_RANK_REF));
assertTrue(ranks.containsKey(RanksManager.VISITOR_RANK_REF));
assertTrue(ranks.containsKey(RanksManager.MEMBER_RANK_REF));
assertTrue(ranks.containsKey(RanksManager.OWNER_RANK_REF));
}
/**
* Test method for {@link us.tastybento.bskyblock.managers.RanksManager#getNextRankValue(int)}.
*/
@Test
public void testGetNextRankValue() {
assertEquals(RanksManager.BANNED_RANK, ranksManager.getNextRankValue(-20));
assertEquals(RanksManager.VISITOR_RANK, ranksManager.getNextRankValue(RanksManager.BANNED_RANK));
assertEquals(RanksManager.MEMBER_RANK, ranksManager.getNextRankValue(RanksManager.VISITOR_RANK));
assertEquals(RanksManager.OWNER_RANK, ranksManager.getNextRankValue(RanksManager.MEMBER_RANK));
assertEquals(RanksManager.OWNER_RANK, ranksManager.getNextRankValue(RanksManager.OWNER_RANK));
assertEquals(RanksManager.OWNER_RANK, ranksManager.getNextRankValue(2000));
}
/**
* Test method for {@link us.tastybento.bskyblock.managers.RanksManager#getPreviousRankValue(int)}.
*/
@Test
public void testGetPreviousRankValue() {
assertEquals(RanksManager.BANNED_RANK, ranksManager.getPreviousRankValue(-20));
assertEquals(RanksManager.BANNED_RANK, ranksManager.getPreviousRankValue(RanksManager.VISITOR_RANK));
assertEquals(RanksManager.VISITOR_RANK, ranksManager.getPreviousRankValue(RanksManager.MEMBER_RANK));
assertEquals(RanksManager.MEMBER_RANK, ranksManager.getPreviousRankValue(RanksManager.OWNER_RANK));
}
/**
* Test method for {@link us.tastybento.bskyblock.managers.RanksManager#getRank(int)}.
*/
@Test
public void testGetRank() {
assertEquals(RanksManager.BANNED_RANK_REF, ranksManager.getRank(RanksManager.BANNED_RANK));
assertEquals(RanksManager.VISITOR_RANK_REF, ranksManager.getRank(RanksManager.VISITOR_RANK));
assertEquals(RanksManager.MEMBER_RANK_REF, ranksManager.getRank(RanksManager.MEMBER_RANK));
assertEquals(RanksManager.OWNER_RANK_REF, ranksManager.getRank(RanksManager.OWNER_RANK));
assertEquals("", ranksManager.getRank(-999));
}
}