diff --git a/src/main/java/us/tastybento/bskyblock/database/objects/Island.java b/src/main/java/us/tastybento/bskyblock/database/objects/Island.java index cd3c7be2e..6a193e546 100755 --- a/src/main/java/us/tastybento/bskyblock/database/objects/Island.java +++ b/src/main/java/us/tastybento/bskyblock/database/objects/Island.java @@ -2,6 +2,7 @@ package us.tastybento.bskyblock.database.objects; import java.util.HashMap; import java.util.HashSet; +import java.util.Map.Entry; import java.util.Set; import java.util.UUID; @@ -14,6 +15,7 @@ import org.bukkit.block.BlockState; import org.bukkit.entity.Entity; import us.tastybento.bskyblock.BSkyBlock; +import us.tastybento.bskyblock.api.commands.User; import us.tastybento.bskyblock.api.events.IslandBaseEvent; import us.tastybento.bskyblock.api.events.island.IslandEvent; import us.tastybento.bskyblock.api.events.island.IslandEvent.IslandLockEvent; @@ -32,6 +34,11 @@ import us.tastybento.bskyblock.util.Util; */ public class Island implements DataObject { + private static final Integer OWNER_RANK = 1000; + private static final Integer MEMBER_RANK = 900; + private static final Integer VISITOR_RANK = 0; + private static final Integer BANNED_RANK = -1; + private String uniqueId = ""; public String getUniqueId() { @@ -77,19 +84,8 @@ public class Island implements DataObject { private long updatedDate; //// Team //// - // Owner (Team Leader) private UUID owner; - - // Members (use set because each value must be unique) - private Set members = new HashSet<>(); - - // Trustees - private Set trustees = new HashSet<>(); - // Coops - private Set coops = new HashSet<>(); - - // Banned players - private Set banned = new HashSet<>(); + private HashMap ranks = new HashMap<>(); //// State //// private boolean locked = false; private boolean spawn = false; @@ -104,13 +100,13 @@ public class Island implements DataObject { public Island() {} public Island(Location location, UUID owner, int protectionRange) { - this.members.add(owner); this.owner = owner; + this.ranks.put(owner, OWNER_RANK); this.createdDate = System.currentTimeMillis(); this.updatedDate = System.currentTimeMillis(); this.world = location.getWorld(); this.center = location; - this.range = BSkyBlock.getInstance().getSettings().getIslandProtectionRange(); + this.range = BSkyBlock.getInstance().getSettings().getIslandDistance(); this.minX = center.getBlockX() - range; this.minZ = center.getBlockZ() - range; this.protectionRange = protectionRange; @@ -123,8 +119,7 @@ public class Island implements DataObject { * @param playerUUID */ public void addMember(UUID playerUUID) { - members.add(playerUUID); - banned.remove(playerUUID); + ranks.put(playerUUID, MEMBER_RANK); } /** @@ -135,16 +130,7 @@ public class Island implements DataObject { */ public boolean addToBanList(UUID targetUUID) { // TODO fire ban event - if (members.contains(targetUUID)) { - members.remove(targetUUID); - } - if (coops.contains(targetUUID)) { - coops.remove(targetUUID); - } - if (trustees.contains(targetUUID)) { - trustees.remove(targetUUID); - } - banned.add(targetUUID); + ranks.put(targetUUID, BANNED_RANK); return true; } @@ -152,7 +138,13 @@ public class Island implements DataObject { * @return the banned */ public Set getBanned() { - return banned; + Set result = new HashSet<>(); + for (Entry member: ranks.entrySet()) { + if (member.getValue() <= BANNED_RANK) { + result.add(member.getKey()); + } + } + return result; } /** @@ -162,13 +154,6 @@ public class Island implements DataObject { return center; } - /** - * @return the coop players of the island - */ - public Set getCoops(){ - return coops; - } - /** * @return the date when the island was created */ @@ -201,10 +186,13 @@ public class Island implements DataObject { * @return the members of the island (owner included) */ public Set getMembers(){ - if (members == null) { - members = new HashSet<>(); + Set result = new HashSet<>(); + for (Entry member: ranks.entrySet()) { + if (member.getValue() >= MEMBER_RANK) { + result.add(member.getKey()); + } } - return members; + return result; } /** @@ -271,13 +259,6 @@ public class Island implements DataObject { return range; } - /** - * @return the trustees players of the island - */ - public Set getTrustees(){ - return trustees; - } - /** * @return the date when the island was updated (team member connection, etc...) */ @@ -330,7 +311,7 @@ public class Island implements DataObject { * @return Returns true if target is banned on this island */ public boolean isBanned(UUID targetUUID) { - return banned.contains(targetUUID); + return ranks.containsKey(targetUUID) && ranks.get(targetUUID) == BANNED_RANK ? true : false; } /** @@ -378,17 +359,10 @@ public class Island implements DataObject { */ public boolean removeFromBanList(UUID targetUUID) { // TODO fire unban event - banned.remove(targetUUID); + ranks.remove(targetUUID); return true; } - /** - * @param banned the banned to set - */ - public void setBanned(Set banned) { - this.banned = banned; - } - /** * @param center the center to set */ @@ -396,13 +370,6 @@ public class Island implements DataObject { this.center = center; } - /** - * @param coops - the coops to set - */ - public void setCoops(Set coops){ - this.coops = coops; - } - /** * @param createdDate - the createdDate to sets */ @@ -456,14 +423,6 @@ public class Island implements DataObject { } } - /** - * @param members - the members to set - */ - public void setMembers(Set members){ - //Bukkit.getLogger().info("DEBUG: members size = " + members.size()); - this.members = members; - } - /** * @param minProtectedX the minProtectedX to set */ @@ -501,12 +460,12 @@ public class Island implements DataObject { } /** - * Sets the owner of the island. If the owner was previous banned, they are unbanned + * Sets the owner of the island. * @param owner - the owner/team leader to set */ public void setOwner(UUID owner){ this.owner = owner; - this.banned.remove(owner); + this.ranks.put(owner, OWNER_RANK); } /** @@ -546,13 +505,6 @@ public class Island implements DataObject { }*/ //TODO default flags } - /** - * @param trustees - the trustees to set - */ - public void setTrustees(Set trustees){ - this.trustees = trustees; - } - /** * @param updatedDate - the updatedDate to sets */ @@ -666,6 +618,29 @@ public class Island implements DataObject { } public void removeMember(UUID playerUUID) { - this.members.remove(playerUUID); + ranks.remove(playerUUID); + } + + /** + * Get the rank of user for this island + * @param user + * @return rank integer + */ + public int getRank(User user) { + return ranks.containsKey(user.getUniqueId()) ? ranks.get(user.getUniqueId()) : VISITOR_RANK; + } + + /** + * @return the ranks + */ + public HashMap getRanks() { + return ranks; + } + + /** + * @param ranks the ranks to set + */ + public void setRanks(HashMap ranks) { + this.ranks = ranks; } } \ No newline at end of file diff --git a/src/test/java/bskyblock/TestBSkyBlock.java b/src/test/java/bskyblock/TestBSkyBlock.java index 907129ff1..2826cf748 100644 --- a/src/test/java/bskyblock/TestBSkyBlock.java +++ b/src/test/java/bskyblock/TestBSkyBlock.java @@ -2,6 +2,7 @@ package bskyblock; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotSame; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; @@ -13,10 +14,12 @@ import java.util.LinkedList; import java.util.List; import java.util.Map.Entry; import java.util.Optional; +import java.util.Set; import java.util.UUID; import java.util.logging.Logger; import org.bukkit.Bukkit; +import org.bukkit.Location; import org.bukkit.Server; import org.bukkit.World; import org.bukkit.command.CommandSender; @@ -31,12 +34,14 @@ import us.tastybento.bskyblock.api.commands.CompositeCommand; import us.tastybento.bskyblock.api.commands.User; import us.tastybento.bskyblock.api.events.IslandBaseEvent; import us.tastybento.bskyblock.api.events.team.TeamEvent; +import us.tastybento.bskyblock.database.objects.Island; import us.tastybento.bskyblock.util.Util; public class TestBSkyBlock { private final UUID playerUUID = UUID.randomUUID(); private static CommandSender sender; private static Player player; + private static Location location; private static BSkyBlock plugin; @BeforeClass @@ -61,6 +66,12 @@ public class TestBSkyBlock { plugin = mock(BSkyBlock.class); //Mockito.when(plugin.getServer()).thenReturn(server); + location = mock(Location.class); + Mockito.when(location.getWorld()).thenReturn(world); + Mockito.when(location.getBlockX()).thenReturn(0); + Mockito.when(location.getBlockY()).thenReturn(0); + Mockito.when(location.getBlockZ()).thenReturn(0); + } @Test @@ -258,4 +269,117 @@ public class TestBSkyBlock { } } + + // Protection tests + @Test + public void TestProtection() { + User owner = User.getInstance(playerUUID); + Island island = new Island(); + island.setOwner(playerUUID); + island.setCenter(location); + island.setProtectionRange(100); + + assertNotNull(island); + + User visitor = User.getInstance(UUID.randomUUID()); + assertEquals(1000, island.getRank(owner)); + assertEquals(0, island.getRank(visitor)); + + // Make members + UUID member1 = UUID.randomUUID(); + UUID member2 = UUID.randomUUID(); + UUID member3 = UUID.randomUUID(); + + // Add members + island.addMember(member1); + island.addMember(member2); + island.addMember(member3); + + Set members = island.getMembers(); + assertTrue(members.contains(playerUUID)); + assertTrue(members.contains(member1)); + assertTrue(members.contains(member2)); + assertTrue(members.contains(member3)); + + // Remove members + island.removeMember(member3); + members = island.getMembers(); + assertTrue(members.contains(playerUUID)); + assertTrue(members.contains(member1)); + assertTrue(members.contains(member2)); + assertFalse(members.contains(member3)); + + // Ban member + island.addToBanList(member1); + members = island.getMembers(); + assertTrue(members.contains(playerUUID)); + assertFalse(members.contains(member1)); + assertTrue(members.contains(member2)); + assertFalse(members.contains(member3)); + + Set banned = island.getBanned(); + assertTrue(banned.contains(member1)); + + // Unban + island.removeFromBanList(member1); + assertFalse(island.getBanned().contains(member1)); + + + //island.isAllowed(visitor, Flags.BREAK_BLOCKS); + /* + * + * Score approach: + * + * Rank definitions are global and apply to all islands + * + * There are 4 hard-coded ranks: + * + * Owner is the highest rank = 1000 + * + * Member ranks are >= 900 + * + * Visitors = 0 + * + * Banned = -1 + * + * Owners have full admin capability over the island. Members are required to give up their own island to be a member. + * Visitors are everyone else. + * + * After those 3, it's possible to have custom ranks, e.g. + * + * Trustees = 750 + * Coops = 500 + * etc. + * + * + * Each flag has a bypass score. + * If the user's rank is higher or equal to the bypass score, they will bypass the protection. + * Owners can disable/enable the flags. + * + * Each island will track the rank score for each player on the island. + * Unknown players have a rank of 0. + * + * + * Admins will be able to define groups and their rank value. + * During the game, the players will never see the rank value. They will only see the ranks. + * + * It will be possible to island owners to promote or demote players up and down the ranks. + * + * This will replace the team system completely. + * + * Pros: + * Very flexible + * + * Cons: + * Too complicated. Are there really ever going to be more than just a few ranks? + * To have generic, unlimited ranks, we lose the concept of hard-coded teams, coops, etc. + * The problem is that team members must lose their islands and so we have special code around that. + * i.e., there's a lot more going on than just ranks. + * + * + * Permissions-based + * + * + */ + } }