diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index c771fd5..825b18d 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -11,21 +11,22 @@ jobs: name: Build runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 with: fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis - name: Set up JDK 17 - uses: actions/setup-java@v1 + uses: actions/setup-java@v3 with: + distribution: 'adopt' java-version: 17 - name: Cache SonarCloud packages - uses: actions/cache@v1 + uses: actions/cache@v3 with: path: ~/.sonar/cache key: ${{ runner.os }}-sonar restore-keys: ${{ runner.os }}-sonar - name: Cache Maven packages - uses: actions/cache@v1 + uses: actions/cache@v3 with: path: ~/.m2 key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }} diff --git a/pom.xml b/pom.xml index 4ebfc30..5e6ee5d 100644 --- a/pom.xml +++ b/pom.xml @@ -57,14 +57,14 @@ 2.0.9 - 1.19.4-R0.1-SNAPSHOT - 1.23.0 + 1.20.4-R0.1-SNAPSHOT + 2.0.0-SNAPSHOT ${build.version}-SNAPSHOT -LOCAL - 1.19.1 + 1.20.1 BentoBoxWorld_Limits bentobox-world https://sonarcloud.io @@ -251,13 +251,15 @@ org.jacoco jacoco-maven-plugin - 0.8.7 + 0.8.10 true **/*Names* + + org/bukkit/Material* diff --git a/src/main/java/world/bentobox/limits/listeners/EntityLimitListener.java b/src/main/java/world/bentobox/limits/listeners/EntityLimitListener.java index 3f3cf3e..f5da3e6 100644 --- a/src/main/java/world/bentobox/limits/listeners/EntityLimitListener.java +++ b/src/main/java/world/bentobox/limits/listeners/EntityLimitListener.java @@ -180,7 +180,7 @@ public class EntityLimitListener implements Listener { if (island.isSpawn() || !res.hit()) { // Allowed if (async) { - Bukkit.getScheduler().runTask(BentoBox.getInstance(), () -> l.getWorld().spawn(l, e.getClass(), entity -> preSpawn(entity, reason, l))); + Bukkit.getScheduler().runTask(BentoBox.getInstance(), () -> preSpawn(e.getType(), reason, l)); } // else do nothing } else { if (async) { @@ -195,21 +195,23 @@ public class EntityLimitListener implements Listener { return true; } - private void preSpawn(Entity entity, SpawnReason reason, Location l) { - justSpawned.add(entity.getUniqueId()); + private void preSpawn(EntityType entityType, SpawnReason reason, Location l) { + // Check for entities that need cleanup switch (reason) { case BUILD_IRONGOLEM -> detectIronGolem(l); case BUILD_SNOWMAN -> detectSnowman(l); case BUILD_WITHER -> { detectWither(l); + } + default -> throw new IllegalArgumentException("Unexpected value: " + reason); + } + Entity entity = l.getWorld().spawnEntity(l, entityType); + justSpawned.add(entity.getUniqueId()); + if (reason == SpawnReason.BUILD_WITHER) { // Create explosion l.getWorld().createExplosion(l, 7F, true, true, entity); } - default -> { - // Do nothing - } - } } private void detectIronGolem(Location l) { @@ -246,7 +248,6 @@ public class EntityLimitListener implements Listener { } } } - } private void detectSnowman(Location l) { diff --git a/src/main/java/world/bentobox/limits/listeners/JoinListener.java b/src/main/java/world/bentobox/limits/listeners/JoinListener.java index 388e52e..e78d72f 100644 --- a/src/main/java/world/bentobox/limits/listeners/JoinListener.java +++ b/src/main/java/world/bentobox/limits/listeners/JoinListener.java @@ -30,6 +30,7 @@ import world.bentobox.limits.objects.IslandBlockCount; /** * Sets block limits based on player permission + * * @author tastybento * */ @@ -38,132 +39,139 @@ public class JoinListener implements Listener { private final Limits addon; public JoinListener(Limits addon) { - this.addon = addon; + this.addon = addon; } /** - * Check and set the permissions of the player and how they affect the island limits - * @param player - player + * Check and set the permissions of the player and how they affect the island + * limits + * + * @param player - player * @param permissionPrefix - permission prefix for this game mode - * @param islandId - island string id - * @param gameMode - game mode string doing the checking + * @param islandId - island string id + * @param gameMode - game mode string doing the checking */ public void checkPerms(Player player, String permissionPrefix, String islandId, String gameMode) { - IslandBlockCount ibc = addon.getBlockLimitListener().getIsland(islandId); - // Check permissions - if (ibc != null) { - // Clear permission limits - ibc.getEntityLimits().clear(); - ibc.getEntityGroupLimits().clear(); - ibc.getBlockLimits().clear(); - } - for (PermissionAttachmentInfo perms : player.getEffectivePermissions()) { - if (!perms.getValue() - || !perms.getPermission().startsWith(permissionPrefix) - || badSyntaxCheck(perms, player.getName(), permissionPrefix)) { - continue; - } - // Check formatting - String[] split = perms.getPermission().split("\\."); - // Entities & materials - EntityType et = Arrays.stream(EntityType.values()).filter(t -> t.name().equalsIgnoreCase(split[3])).findFirst().orElse(null); - Material m = Arrays.stream(Material.values()).filter(t -> t.name().equalsIgnoreCase(split[3])).findFirst().orElse(null); - EntityGroup entgroup = addon.getSettings().getGroupLimitDefinitions().stream() - .filter(t -> t.getName().equalsIgnoreCase(split[3])).findFirst().orElse(null); + IslandBlockCount ibc = addon.getBlockLimitListener().getIsland(islandId); + // Check permissions + if (ibc != null) { + // Clear permission limits + ibc.getEntityLimits().clear(); + ibc.getEntityGroupLimits().clear(); + ibc.getBlockLimits().clear(); + } + for (PermissionAttachmentInfo perms : player.getEffectivePermissions()) { + if (!perms.getValue() || !perms.getPermission().startsWith(permissionPrefix) + || badSyntaxCheck(perms, player.getName(), permissionPrefix)) { + continue; + } + // Check formatting + String[] split = perms.getPermission().split("\\."); + // Entities & materials + EntityType et = Arrays.stream(EntityType.values()).filter(t -> t.name().equalsIgnoreCase(split[3])) + .findFirst().orElse(null); + Material m = Arrays.stream(Material.values()).filter(t -> t.name().equalsIgnoreCase(split[3])).findFirst() + .orElse(null); + EntityGroup entgroup = addon.getSettings().getGroupLimitDefinitions().stream() + .filter(t -> t.getName().equalsIgnoreCase(split[3])).findFirst().orElse(null); - if (entgroup == null && et == null && m == null) { - logError(player.getName(), perms.getPermission(), split[3].toUpperCase(Locale.ENGLISH) + " is not a valid material or entity type/group."); - break; - } - // Make an ibc if required - if (ibc == null) { - ibc = new IslandBlockCount(islandId, gameMode); - } - // Get the value - int value = Integer.parseInt(split[4]); - addon.log("Setting login limit via perm for " + player.getName() + "..."); + if (entgroup == null && et == null && m == null) { + logError(player.getName(), perms.getPermission(), + split[3].toUpperCase(Locale.ENGLISH) + " is not a valid material or entity type/group."); + break; + } + // Make an ibc if required + if (ibc == null) { + ibc = new IslandBlockCount(islandId, gameMode); + } + // Get the value + int value = Integer.parseInt(split[4]); + addon.log("Setting login limit via perm for " + player.getName() + "..."); - // Fire perm check event - LimitsPermCheckEvent l = new LimitsPermCheckEvent(player, islandId, ibc, entgroup, et, m, value); - Bukkit.getPluginManager().callEvent(l); - if (l.isCancelled()) { - addon.log("Permissions not set because another addon/plugin canceled setting."); - continue; - } - // Use event values - ibc = l.getIbc(); - // Make an ibc if required - if (ibc == null) { - ibc = new IslandBlockCount(islandId, gameMode); - } - // Run null checks and set ibc - runNullCheckAndSet(ibc, l); - } - // Check removed permissions - // If any changes have been made then store it - don't make files unless they are needed - if (ibc != null) addon.getBlockLimitListener().setIsland(islandId, ibc); + // Fire perm check event + LimitsPermCheckEvent l = new LimitsPermCheckEvent(player, islandId, ibc, entgroup, et, m, value); + Bukkit.getPluginManager().callEvent(l); + if (l.isCancelled()) { + addon.log("Permissions not set because another addon/plugin canceled setting."); + continue; + } + // Use event values + ibc = l.getIbc(); + // Make an ibc if required + if (ibc == null) { + ibc = new IslandBlockCount(islandId, gameMode); + } + // Run null checks and set ibc + runNullCheckAndSet(ibc, l); + } + // Check removed permissions + // If any changes have been made then store it - don't make files unless they + // are needed + if (ibc != null) + addon.getBlockLimitListener().setIsland(islandId, ibc); } private boolean badSyntaxCheck(PermissionAttachmentInfo perms, String name, String permissionPrefix) { - // No wildcards - if (perms.getPermission().contains(permissionPrefix + "*")) { - logError(name, perms.getPermission(), "wildcards are not allowed."); - return true; - } - // Check formatting - String[] split = perms.getPermission().split("\\."); - if (split.length != 5) { - logError(name, perms.getPermission(), "format must be '" + permissionPrefix + "MATERIAL.NUMBER', '" + permissionPrefix + "ENTITY-TYPE.NUMBER', or '" + permissionPrefix + "ENTITY-GROUP.NUMBER'"); - return true; - } - // Check value - try { - Integer.parseInt(split[4]); - } catch(Exception e) { - logError(name, perms.getPermission(), "the last part MUST be an integer!"); - return true; - } - return false; + // No wildcards + if (perms.getPermission().contains(permissionPrefix + "*")) { + logError(name, perms.getPermission(), "wildcards are not allowed."); + return true; + } + // Check formatting + String[] split = perms.getPermission().split("\\."); + if (split.length != 5) { + logError(name, perms.getPermission(), "format must be '" + permissionPrefix + "MATERIAL.NUMBER', '" + + permissionPrefix + "ENTITY-TYPE.NUMBER', or '" + permissionPrefix + "ENTITY-GROUP.NUMBER'"); + return true; + } + // Check value + try { + Integer.parseInt(split[4]); + } catch (Exception e) { + logError(name, perms.getPermission(), "the last part MUST be an integer!"); + return true; + } + return false; } - private void runNullCheckAndSet(@NonNull IslandBlockCount ibc, @NonNull LimitsPermCheckEvent l) { - EntityGroup entgroup = l.getEntityGroup(); - EntityType et = l.getEntityType(); - Material m = l.getMaterial(); - int value = l.getValue(); - if (entgroup != null) { - // Entity group limit - int v = Math.max(ibc.getEntityGroupLimit(entgroup.getName()), value); - ibc.setEntityGroupLimit(entgroup.getName(), v); - addon.log("Setting group limit " + entgroup.getName() + " " + v); - } else if (et != null && m == null) { - // Entity limit - int v = Math.max(ibc.getEntityLimit(et), value); - ibc.setEntityLimit(et, v); - addon.log("Setting entity limit " + et + " " + v); - } else if (m != null && et == null) { - // Block limit - int v = Math.max(ibc.getBlockLimit(m), value); - addon.log("Setting block limit " + m + " " + v); - ibc.setBlockLimit(m, v); - } else { - if (m != null && m.isBlock()) { - int v = Math.max(ibc.getBlockLimit(m), value); - addon.log("Setting block limit " + m + " " + v); - // Material limit - ibc.setBlockLimit(m, v); - } else if (et != null){ - int v = Math.max(ibc.getEntityLimit(et), value); - addon.log("Setting entity limit " + et + " " + v); - // This is an entity setting - ibc.setEntityLimit(et, v); - } - } + private void runNullCheckAndSet(@NonNull IslandBlockCount ibc, @NonNull LimitsPermCheckEvent l) { + EntityGroup entgroup = l.getEntityGroup(); + EntityType et = l.getEntityType(); + Material m = l.getMaterial(); + int value = l.getValue(); + if (entgroup != null) { + // Entity group limit + int v = Math.max(ibc.getEntityGroupLimit(entgroup.getName()), value); + ibc.setEntityGroupLimit(entgroup.getName(), v); + addon.log("Setting group limit " + entgroup.getName() + " " + v); + } else if (et != null && m == null) { + // Entity limit + int v = Math.max(ibc.getEntityLimit(et), value); + ibc.setEntityLimit(et, v); + addon.log("Setting entity limit " + et + " " + v); + } else if (m != null && et == null) { + // Block limit + int v = Math.max(ibc.getBlockLimit(m), value); + addon.log("Setting block limit " + m + " " + v); + ibc.setBlockLimit(m, v); + } else { + if (m != null && m.isBlock()) { + int v = Math.max(ibc.getBlockLimit(m), value); + addon.log("Setting block limit " + m + " " + v); + // Material limit + ibc.setBlockLimit(m, v); + } else if (et != null) { + int v = Math.max(ibc.getEntityLimit(et), value); + addon.log("Setting entity limit " + et + " " + v); + // This is an entity setting + ibc.setEntityLimit(et, v); + } + } } private void logError(String name, String perm, String error) { - addon.logError("Player " + name + " has permission: '" + perm + "' but " + error + " Ignoring..."); + addon.logError("Player " + name + " has permission: '" + perm + "' but " + error + " Ignoring..."); } /* @@ -172,66 +180,66 @@ public class JoinListener implements Listener { @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onNewIsland(IslandEvent e) { - if (!e.getReason().equals(Reason.CREATED) - && !e.getReason().equals(Reason.RESETTED) - && !e.getReason().equals(Reason.REGISTERED)) { - return; - } - setOwnerPerms(e.getIsland(), e.getOwner()); + if (!e.getReason().equals(Reason.CREATED) && !e.getReason().equals(Reason.RESETTED) + && !e.getReason().equals(Reason.REGISTERED)) { + return; + } + setOwnerPerms(e.getIsland(), e.getOwner()); } @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onOwnerChange(TeamSetownerEvent e) { - removeOwnerPerms(e.getIsland()); - setOwnerPerms(e.getIsland(), e.getNewOwner()); + removeOwnerPerms(e.getIsland()); + setOwnerPerms(e.getIsland(), e.getNewOwner()); } - @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) public void onPlayerJoin(PlayerJoinEvent e) { - // Check if player has any islands in the game modes - addon.getGameModes().forEach(gm -> { - if (addon.getIslands().hasIsland(gm.getOverWorld(), e.getPlayer().getUniqueId())) { - String islandId = Objects.requireNonNull(addon.getIslands().getIsland(gm.getOverWorld(), e.getPlayer().getUniqueId())).getUniqueId(); - IslandBlockCount ibc = addon.getBlockLimitListener().getIsland(islandId); - if (joinEventCheck(e.getPlayer(), islandId, ibc)) { - return; - } - checkPerms(e.getPlayer(), gm.getPermissionPrefix() + "island.limit.", islandId, gm.getDescription().getName()); - } - }); + // Check if player has any islands in the game modes + addon.getGameModes().forEach(gm -> { + addon.getIslands().getIslands(gm.getOverWorld(), e.getPlayer().getUniqueId()).stream() + .map(Island::getUniqueId).forEach(islandId -> { + IslandBlockCount ibc = addon.getBlockLimitListener().getIsland(islandId); + if (!joinEventCheck(e.getPlayer(), islandId, ibc)) { + checkPerms(e.getPlayer(), gm.getPermissionPrefix() + "island.limit.", islandId, + gm.getDescription().getName()); + } + }); + }); } /** * Fire event so other addons can cancel this permissions change - * @param player player + * + * @param player player * @param islandId island id - * @param ibc island block count + * @param ibc island block count * @return true if canceled */ private boolean joinEventCheck(Player player, String islandId, IslandBlockCount ibc) { - // Fire event, so other addons can cancel this permissions change - LimitsJoinPermCheckEvent e = new LimitsJoinPermCheckEvent(player, islandId, ibc); - Bukkit.getPluginManager().callEvent(e); - if (e.isCancelled()) { - return true; - } - // Get ibc from event if it has changed - ibc = e.getIbc(); - // If perms should be ignored, but the IBC given in the event used, then set it and return - if (e.isIgnorePerms() && ibc != null) { - addon.getBlockLimitListener().setIsland(islandId, ibc); - return true; - } - return false; + // Fire event, so other addons can cancel this permissions change + LimitsJoinPermCheckEvent e = new LimitsJoinPermCheckEvent(player, islandId, ibc); + Bukkit.getPluginManager().callEvent(e); + if (e.isCancelled()) { + return true; + } + // Get ibc from event if it has changed + ibc = e.getIbc(); + // If perms should be ignored, but the IBC given in the event used, then set it + // and return + if (e.isIgnorePerms() && ibc != null) { + addon.getBlockLimitListener().setIsland(islandId, ibc); + return true; + } + return false; } @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onUnregisterIsland(IslandEvent e) { - if (!e.getReason().equals(Reason.UNREGISTERED)) { - return; - } - removeOwnerPerms(e.getIsland()); + if (!e.getReason().equals(Reason.UNREGISTERED)) { + return; + } + removeOwnerPerms(e.getIsland()); } /* @@ -239,29 +247,30 @@ public class JoinListener implements Listener { */ private void removeOwnerPerms(Island island) { - World world = island.getWorld(); - if (addon.inGameModeWorld(world)) { - IslandBlockCount ibc = addon.getBlockLimitListener().getIsland(island.getUniqueId()); - if (ibc != null) { - ibc.getBlockLimits().clear(); - } - } + World world = island.getWorld(); + if (addon.inGameModeWorld(world)) { + IslandBlockCount ibc = addon.getBlockLimitListener().getIsland(island.getUniqueId()); + if (ibc != null) { + ibc.getBlockLimits().clear(); + } + } } private void setOwnerPerms(Island island, UUID ownerUUID) { - World world = island.getWorld(); - if (addon.inGameModeWorld(world)) { - // Check if owner is online - OfflinePlayer owner = Bukkit.getOfflinePlayer(ownerUUID); - if (owner.isOnline()) { - // Set perm-based limits - String prefix = addon.getGameModePermPrefix(world); - String name = addon.getGameModeName(world); - if (!prefix.isEmpty() && !name.isEmpty() && owner.getPlayer() != null) { - checkPerms(Objects.requireNonNull(owner.getPlayer()), prefix + "island.limit.", island.getUniqueId(), name); - } - } - } + World world = island.getWorld(); + if (addon.inGameModeWorld(world)) { + // Check if owner is online + OfflinePlayer owner = Bukkit.getOfflinePlayer(ownerUUID); + if (owner.isOnline()) { + // Set perm-based limits + String prefix = addon.getGameModePermPrefix(world); + String name = addon.getGameModeName(world); + if (!prefix.isEmpty() && !name.isEmpty() && owner.getPlayer() != null) { + checkPerms(Objects.requireNonNull(owner.getPlayer()), prefix + "island.limit.", + island.getUniqueId(), name); + } + } + } } } diff --git a/src/main/java/world/bentobox/limits/objects/IslandBlockCount.java b/src/main/java/world/bentobox/limits/objects/IslandBlockCount.java index 8cc137e..1ff6cc8 100644 --- a/src/main/java/world/bentobox/limits/objects/IslandBlockCount.java +++ b/src/main/java/world/bentobox/limits/objects/IslandBlockCount.java @@ -3,6 +3,7 @@ package world.bentobox.limits.objects; import java.util.EnumMap; import java.util.HashMap; import java.util.Map; +import java.util.Objects; import org.bukkit.Material; import org.bukkit.entity.EntityType; @@ -19,370 +20,398 @@ import world.bentobox.bentobox.database.objects.Table; @Table(name = "IslandBlockCount") public class IslandBlockCount implements DataObject { - @Expose - private String uniqueId; - - @Expose - private String gameMode; - @Expose private Map blockCounts = new EnumMap<>(Material.class); - private boolean changed; - /** * Permission based limits */ @Expose private Map blockLimits = new EnumMap<>(Material.class); + @Expose - private Map entityLimits = new EnumMap<>(EntityType.class); + private Map blockLimitsOffset = new EnumMap<>(Material.class); + + private boolean changed; + @Expose private Map entityGroupLimits = new HashMap<>(); @Expose - private Map blockLimitsOffset = new EnumMap<>(Material.class); + private Map entityGroupLimitsOffset = new HashMap<>(); + @Expose + private Map entityLimits = new EnumMap<>(EntityType.class); @Expose private Map entityLimitsOffset = new EnumMap<>(EntityType.class); @Expose - private Map entityGroupLimitsOffset = new HashMap<>(); + private String gameMode; + @Expose + private String uniqueId; /** * Create an island block count object + * * @param islandId - unique Island ID string * @param gameMode - Game mode name from gm.getDescription().getName() */ public IslandBlockCount(String islandId, String gameMode) { - this.uniqueId = islandId; - this.gameMode = gameMode; - setChanged(); - } - - /* (non-Javadoc) - * @see world.bentobox.bentobox.database.objects.DataObject#getUniqueId() - */ - @Override - public String getUniqueId() { - return uniqueId; - } - - /* (non-Javadoc) - * @see world.bentobox.bentobox.database.objects.DataObject#setUniqueId(java.lang.String) - */ - @Override - public void setUniqueId(String uniqueId) { - this.uniqueId = uniqueId; - setChanged(); - } - - /** - * @return the blockCount - */ - public Map getBlockCounts() { - if (blockCounts == null) { - blockCounts = new EnumMap<>(Material.class); - } - return blockCounts; - } - - /** - * @param blockCounts the blockCount to set - */ - public void setBlockCounts(Map blockCounts) { - this.blockCounts = blockCounts; - setChanged(); - } - - /** - * Get the block count for this material for this island - * @param m - material - * @return count - */ - public Integer getBlockCount(Material m) { - return blockCounts.getOrDefault(m, 0); + this.uniqueId = islandId; + this.gameMode = gameMode; + setChanged(); } /** * Add a material to the count + * * @param material - material */ public void add(Material material) { - blockCounts.merge(material, 1, Integer::sum); - setChanged(); - } - - /** - * Remove a material from the count - * @param material - material - */ - public void remove(Material material) { - blockCounts.put(material, blockCounts.getOrDefault(material, 0) - 1); - blockCounts.values().removeIf(v -> v <= 0); - setChanged(); - } - - /** - * Check if this material is at or over a limit - * @param material - block material - * @param limit - limit to check - * @return true if count is >= limit - */ - public boolean isAtLimit(Material material, int limit) { - return blockCounts.getOrDefault(material, 0) >= limit + this.getBlockLimitOffset(material); - } - - /** - * Check if no more of this material can be added to this island - * @param m - material - * @return true if no more material can be added - */ - public boolean isAtLimit(Material m) { - // Check island limits first - return blockLimits.containsKey(m) && blockCounts.getOrDefault(m, 0) >= getBlockLimit(m) + this.getBlockLimitOffset(m); - } - - public boolean isBlockLimited(Material m) { - return blockLimits.containsKey(m); - } - - /** - * @return the blockLimits - */ - public Map getBlockLimits() { - return blockLimits; - } - - /** - * @param blockLimits the blockLimits to set - */ - public void setBlockLimits(Map blockLimits) { - this.blockLimits = blockLimits; - setChanged(); - } - - /** - * Get the block limit for this material for this island - * @param m - material - * @return limit or -1 for unlimited - */ - public int getBlockLimit(Material m) { - return blockLimits.getOrDefault(m, -1); - } - - /** - * Get the block offset for this material for this island - * @param m - material - * @return offset - */ - public int getBlockLimitOffset(Material m) { - return getBlockLimitsOffset().getOrDefault(m, 0); - } - - /** - * Set the block limit for this material for this island - * @param m - material - * @param limit - maximum number allowed - */ - public void setBlockLimit(Material m, int limit) { - blockLimits.put(m, limit); - setChanged(); - } - - /** - * @return the gameMode - */ - public String getGameMode() { - return gameMode; - } - - public boolean isGameMode(String gameMode) { - return this.gameMode.equals(gameMode); - } - - /** - * @param gameMode the gameMode to set - */ - public void setGameMode(String gameMode) { - this.gameMode = gameMode; - setChanged(); - } - - /** - * @return the entityLimits - */ - public Map getEntityLimits() { - return entityLimits; - } - - /** - * @param entityLimits the entityLimits to set - */ - public void setEntityLimits(Map entityLimits) { - this.entityLimits = entityLimits; - setChanged(); - } - - /** - * Set an island-specific entity type limit - * @param t - entity type - * @param limit - limit - */ - public void setEntityLimit(EntityType t, int limit) { - entityLimits.put(t, limit); - setChanged(); - } - - /** - * Get the limit for an entity type - * @param t - entity type - * @return limit or -1 for unlimited - */ - public int getEntityLimit(EntityType t) { - return entityLimits.getOrDefault(t, -1); - } - - /** - * Get the limit offset for an entity type - * @param t - entity type - * @return offset - */ - public int getEntityLimitOffset(EntityType t) { - return getEntityLimitsOffset().getOrDefault(t, 0); - } - - /** - * Clear all island-specific entity type limits - */ - public void clearEntityLimits() { - entityLimits.clear(); - setChanged(); - } - - /** - * @return the entityGroupLimits - */ - public Map getEntityGroupLimits() { - return entityGroupLimits; - } - - /** - * @param entityGroupLimits the entityGroupLimits to set - */ - public void setEntityGroupLimits(Map entityGroupLimits) { - this.entityGroupLimits = entityGroupLimits; - setChanged(); - } - - /** - * Set an island-specific entity group limit - * @param name - entity group - * @param limit - limit - */ - public void setEntityGroupLimit(String name, int limit) { - entityGroupLimits.put(name, limit); - setChanged(); - } - - /** - * Get the limit for an entity group - * @param name - entity group - * @return limit or -1 for unlimited - */ - public int getEntityGroupLimit(String name) { - return entityGroupLimits.getOrDefault(name, -1); - } - - /** - * Get the offset for an entity group - * @param name - entity group - * @return offset - */ - public int getEntityGroupLimitOffset(String name) { - return getEntityGroupLimitsOffset().getOrDefault(name, 0); + getBlockCounts().merge(material, 1, Integer::sum); + setChanged(); } /** * Clear all island-specific entity group limits */ public void clearEntityGroupLimits() { - entityGroupLimits.clear(); - setChanged(); + entityGroupLimits.clear(); + setChanged(); } /** - * @return the changed + * Clear all island-specific entity type limits */ - public boolean isChanged() { - return changed; + public void clearEntityLimits() { + entityLimits.clear(); + setChanged(); } /** - * @param changed the changed to set + * Get the block count for this material for this island + * + * @param m - material + * @return count */ - public void setChanged(boolean changed) { - this.changed = changed; + public Integer getBlockCount(Material m) { + return getBlockCounts().getOrDefault(m, 0); } /** - * Mark changed + * @return the blockCount */ - public void setChanged() { - this.changed = true; + public Map getBlockCounts() { + if (blockCounts == null) { + blockCounts = new EnumMap<>(Material.class); + } + return blockCounts; + } + + /** + * Get the block limit for this material for this island + * + * @param m - material + * @return limit or -1 for unlimited + */ + public int getBlockLimit(Material m) { + return getBlockLimits().getOrDefault(m, -1); + } + + /** + * Get the block offset for this material for this island + * + * @param m - material + * @return offset + */ + public int getBlockLimitOffset(Material m) { + return getBlockLimitsOffset().getOrDefault(m, 0); + } + + /** + * @return the blockLimits + */ + public Map getBlockLimits() { + return Objects.requireNonNullElse(blockLimits, new EnumMap<>(Material.class)); } /** * @return the blockLimitsOffset */ public Map getBlockLimitsOffset() { - if (blockLimitsOffset == null) { - blockLimitsOffset = new EnumMap<>(Material.class); - } - return blockLimitsOffset; + if (blockLimitsOffset == null) { + blockLimitsOffset = new EnumMap<>(Material.class); + } + return blockLimitsOffset; } /** - * Set an offset to a block limit. This will increase/decrease the value of the limit. - * @param m material - * @param blockLimitsOffset the blockLimitsOffset to set + * Get the limit for an entity group + * + * @param name - entity group + * @return limit or -1 for unlimited */ - public void setBlockLimitsOffset(Material m, Integer blockLimitsOffset) { - getBlockLimitsOffset().put(m, blockLimitsOffset); + public int getEntityGroupLimit(String name) { + return getEntityGroupLimits().getOrDefault(name, -1); } /** - * @return the entityLimitsOffset + * Get the offset for an entity group + * + * @param name - entity group + * @return offset */ - public Map getEntityLimitsOffset() { - if (entityLimitsOffset == null) { - entityLimitsOffset = new EnumMap<>(EntityType.class); - } - return entityLimitsOffset; + public int getEntityGroupLimitOffset(String name) { + return getEntityGroupLimitsOffset().getOrDefault(name, 0); } /** - * Set an offset to an entity limit. This will increase/decrease the value of the limit. - * @param t Entity Type - * @param entityLimitsOffset the entityLimitsOffset to set + * @return the entityGroupLimits */ - public void setEntityLimitsOffset(EntityType t, Integer entityLimitsOffset) { - this.getEntityLimitsOffset().put(t, entityLimitsOffset); + public Map getEntityGroupLimits() { + return Objects.requireNonNullElse(entityGroupLimits, new HashMap<>()); } /** * @return the entityGroupLimitsOffset */ public Map getEntityGroupLimitsOffset() { - if (entityGroupLimitsOffset == null) { - entityGroupLimitsOffset = new HashMap<>(); - } - return entityGroupLimitsOffset; + if (entityGroupLimitsOffset == null) { + entityGroupLimitsOffset = new HashMap<>(); + } + return entityGroupLimitsOffset; } /** - * Set an offset to an entity group limit. This will increase/decrease the value of the limit. - * @param name group name + * Get the limit for an entity type + * + * @param t - entity type + * @return limit or -1 for unlimited + */ + public int getEntityLimit(EntityType t) { + return getEntityLimits().getOrDefault(t, -1); + } + + /** + * Get the limit offset for an entity type + * + * @param t - entity type + * @return offset + */ + public int getEntityLimitOffset(EntityType t) { + return getEntityLimitsOffset().getOrDefault(t, 0); + } + + /** + * @return the entityLimits + */ + public Map getEntityLimits() { + return Objects.requireNonNullElse(entityLimits, new EnumMap<>(EntityType.class)); + } + + /** + * @return the entityLimitsOffset + */ + public Map getEntityLimitsOffset() { + if (entityLimitsOffset == null) { + entityLimitsOffset = new EnumMap<>(EntityType.class); + } + return entityLimitsOffset; + } + + /** + * @return the gameMode + */ + public String getGameMode() { + return gameMode; + } + + /* + * (non-Javadoc) + * + * @see world.bentobox.bentobox.database.objects.DataObject#getUniqueId() + */ + @Override + public String getUniqueId() { + return uniqueId; + } + + /** + * Check if no more of this material can be added to this island + * + * @param m - material + * @return true if no more material can be added + */ + public boolean isAtLimit(Material m) { + // Check island limits first + return getBlockLimits().containsKey(m) + && getBlockCounts().getOrDefault(m, 0) >= getBlockLimit(m) + this.getBlockLimitOffset(m); + } + + /** + * Check if this material is at or over a limit + * + * @param material - block material + * @param limit - limit to check + * @return true if count is >= limit + */ + public boolean isAtLimit(Material material, int limit) { + return getBlockCounts().getOrDefault(material, 0) >= limit + this.getBlockLimitOffset(material); + } + + public boolean isBlockLimited(Material m) { + return getBlockLimits().containsKey(m); + } + + /** + * @return the changed + */ + public boolean isChanged() { + return changed; + } + + public boolean isGameMode(String gameMode) { + return getGameMode().equals(gameMode); + } + + /** + * Remove a material from the count + * + * @param material - material + */ + public void remove(Material material) { + getBlockCounts().put(material, getBlockCounts().getOrDefault(material, 0) - 1); + getBlockCounts().values().removeIf(v -> v <= 0); + setChanged(); + } + + /** + * @param blockCounts the blockCount to set + */ + public void setBlockCounts(Map blockCounts) { + this.blockCounts = blockCounts; + setChanged(); + } + + /** + * Set the block limit for this material for this island + * + * @param m - material + * @param limit - maximum number allowed + */ + public void setBlockLimit(Material m, int limit) { + getBlockLimits().put(m, limit); + setChanged(); + } + + /** + * @param blockLimits the blockLimits to set + */ + public void setBlockLimits(Map blockLimits) { + this.blockLimits = blockLimits; + setChanged(); + } + + /** + * Set an offset to a block limit. This will increase/decrease the value of the + * limit. + * + * @param m material + * @param blockLimitsOffset the blockLimitsOffset to set + */ + public void setBlockLimitsOffset(Material m, Integer blockLimitsOffset) { + getBlockLimitsOffset().put(m, blockLimitsOffset); + } + + /** + * Mark changed + */ + public void setChanged() { + this.changed = true; + } + + /** + * @param changed the changed to set + */ + public void setChanged(boolean changed) { + this.changed = changed; + } + + /** + * Set an island-specific entity group limit + * + * @param name - entity group + * @param limit - limit + */ + public void setEntityGroupLimit(String name, int limit) { + getEntityGroupLimits().put(name, limit); + setChanged(); + } + + /** + * @param entityGroupLimits the entityGroupLimits to set + */ + public void setEntityGroupLimits(Map entityGroupLimits) { + this.entityGroupLimits = entityGroupLimits; + setChanged(); + } + + /** + * Set an offset to an entity group limit. This will increase/decrease the value + * of the limit. + * + * @param name group name * @param entityGroupLimitsOffset the entityGroupLimitsOffset to set */ public void setEntityGroupLimitsOffset(String name, Integer entityGroupLimitsOffset) { - getEntityGroupLimitsOffset().put(name, entityGroupLimitsOffset); + getEntityGroupLimitsOffset().put(name, entityGroupLimitsOffset); + } + + /** + * Set an island-specific entity type limit + * + * @param t - entity type + * @param limit - limit + */ + public void setEntityLimit(EntityType t, int limit) { + getEntityLimits().put(t, limit); + setChanged(); + } + + /** + * @param entityLimits the entityLimits to set + */ + public void setEntityLimits(Map entityLimits) { + this.entityLimits = entityLimits; + setChanged(); + } + + /** + * Set an offset to an entity limit. This will increase/decrease the value of + * the limit. + * + * @param type Entity Type + * @param entityLimitsOffset the entityLimitsOffset to set + */ + public void setEntityLimitsOffset(EntityType type, Integer entityLimitsOffset) { + this.getEntityLimitsOffset().put(type, entityLimitsOffset); + } + + /** + * @param gameMode the gameMode to set + */ + public void setGameMode(String gameMode) { + this.gameMode = gameMode; + setChanged(); + } + + /* + * (non-Javadoc) + * + * @see + * world.bentobox.bentobox.database.objects.DataObject#setUniqueId(java.lang. + * String) + */ + @Override + public void setUniqueId(String uniqueId) { + this.uniqueId = uniqueId; + setChanged(); } } diff --git a/src/main/resources/addon.yml b/src/main/resources/addon.yml index e5d8fcb..69b51b2 100755 --- a/src/main/resources/addon.yml +++ b/src/main/resources/addon.yml @@ -1,7 +1,7 @@ name: Limits main: world.bentobox.limits.Limits version: ${version}${build.number} -api-version: 1.16.5 +api-version: 2.0.0 authors: tastybento diff --git a/src/test/java/bentobox/addon/limits/listeners/JoinListenerTest.java b/src/test/java/bentobox/addon/limits/listeners/JoinListenerTest.java index eccda2f..4359089 100644 --- a/src/test/java/bentobox/addon/limits/listeners/JoinListenerTest.java +++ b/src/test/java/bentobox/addon/limits/listeners/JoinListenerTest.java @@ -1,7 +1,5 @@ package bentobox.addon.limits.listeners; -import java.util.*; - import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; @@ -11,6 +9,14 @@ import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.UUID; + import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.OfflinePlayer; @@ -45,7 +51,7 @@ import world.bentobox.limits.objects.IslandBlockCount; * */ @RunWith(PowerMockRunner.class) -@PrepareForTest( {Bukkit.class} ) +@PrepareForTest({ Bukkit.class }) public class JoinListenerTest { @Mock @@ -73,74 +79,78 @@ public class JoinListenerTest { @Before public void setUp() { - jl = new JoinListener(addon); - // Setup addon - when(addon.getGameModes()).thenReturn(Collections.singletonList(bskyblock)); - when(addon.getGameModeName(any())).thenReturn("bskyblock"); - when(addon.getGameModePermPrefix(any())).thenReturn("bskyblock."); - when(addon.getSettings()).thenReturn(settings); - // Settings - when(settings.getGroupLimitDefinitions()).thenReturn(new ArrayList<>(List.of(new Settings.EntityGroup("friendly", new HashSet<>(), -1)))); - // Island Manager - when(im.hasIsland(any(), any(UUID.class))).thenReturn(true); - when(island.getUniqueId()).thenReturn("unique_id"); - when(im.getIsland(any(), any(UUID.class))).thenReturn(island); - // Default is that player has island - when(addon.getIslands()).thenReturn(im); - // Player - when(player.getUniqueId()).thenReturn(UUID.randomUUID()); - when(player.getName()).thenReturn("tastybento"); - // No permissions by default - when(player.getEffectivePermissions()).thenReturn(Collections.emptySet()); - // bsKyBlock - when(bskyblock.getPermissionPrefix()).thenReturn("bskyblock."); - AddonDescription desc = new AddonDescription.Builder("main", "BSkyBlock", "1.0").build(); - when(bskyblock.getDescription()).thenReturn(desc); + jl = new JoinListener(addon); + // Setup addon + when(addon.getGameModes()).thenReturn(Collections.singletonList(bskyblock)); + when(addon.getGameModeName(any())).thenReturn("bskyblock"); + when(addon.getGameModePermPrefix(any())).thenReturn("bskyblock."); + when(addon.getSettings()).thenReturn(settings); + // Settings + when(settings.getGroupLimitDefinitions()) + .thenReturn(new ArrayList<>(List.of(new Settings.EntityGroup("friendly", new HashSet<>(), -1)))); + // Island Manager + when(island.getUniqueId()).thenReturn("unique_id"); + when(im.getIsland(any(), any(UUID.class))).thenReturn(island); + when(im.getIslands(any(), any(UUID.class))).thenReturn(Set.of(island)); + // Default is that player has island + when(addon.getIslands()).thenReturn(im); + // Player + when(player.getUniqueId()).thenReturn(UUID.randomUUID()); + when(player.getName()).thenReturn("tastybento"); + // No permissions by default + when(player.getEffectivePermissions()).thenReturn(Collections.emptySet()); + // bsKyBlock + when(bskyblock.getPermissionPrefix()).thenReturn("bskyblock."); + AddonDescription desc = new AddonDescription.Builder("main", "BSkyBlock", "1.0").build(); + when(bskyblock.getDescription()).thenReturn(desc); - // Block limit listener - when(addon.getBlockLimitListener()).thenReturn(bll); - when(bll.getIsland(anyString())).thenReturn(ibc); + // Block limit listener + when(addon.getBlockLimitListener()).thenReturn(bll); + when(bll.getIsland(anyString())).thenReturn(ibc); - // bukkit - PowerMockito.mockStatic(Bukkit.class); - // default is that owner is online - when(owner.isOnline()).thenReturn(true); - when(owner.getPlayer()).thenReturn(player); - when(Bukkit.getOfflinePlayer(any(UUID.class))).thenReturn(owner); - when(Bukkit.getPluginManager()).thenReturn(pim); + // bukkit + PowerMockito.mockStatic(Bukkit.class); + // default is that owner is online + when(owner.isOnline()).thenReturn(true); + when(owner.getPlayer()).thenReturn(player); + when(Bukkit.getOfflinePlayer(any(UUID.class))).thenReturn(owner); + when(Bukkit.getPluginManager()).thenReturn(pim); - // Island - when(island.getOwner()).thenReturn(UUID.randomUUID()); + // Island + when(island.getOwner()).thenReturn(UUID.randomUUID()); } /** - * Test method for {@link world.bentobox.limits.listeners.JoinListener#onNewIsland(world.bentobox.bentobox.api.events.island.IslandEvent)}. + * Test method for + * {@link world.bentobox.limits.listeners.JoinListener#onNewIsland(world.bentobox.bentobox.api.events.island.IslandEvent)}. */ @Test public void testOnNewIslandWrongReason() { - IslandEvent e = new IslandEvent(island, null, false, null, IslandEvent.Reason.BAN); - jl.onNewIsland(e); - verify(island, never()).getWorld(); + IslandEvent e = new IslandEvent(island, null, false, null, IslandEvent.Reason.BAN); + jl.onNewIsland(e); + verify(island, never()).getWorld(); } /** - * Test method for {@link world.bentobox.limits.listeners.JoinListener#onNewIsland(world.bentobox.bentobox.api.events.island.IslandEvent)}. + * Test method for + * {@link world.bentobox.limits.listeners.JoinListener#onNewIsland(world.bentobox.bentobox.api.events.island.IslandEvent)}. */ @Test public void testOnNewIslandRegistered() { - IslandEvent e = new IslandEvent(island, null, false, null, IslandEvent.Reason.REGISTERED); - jl.onNewIsland(e); - verify(island).getWorld(); + IslandEvent e = new IslandEvent(island, null, false, null, IslandEvent.Reason.REGISTERED); + jl.onNewIsland(e); + verify(island).getWorld(); } /** - * Test method for {@link world.bentobox.limits.listeners.JoinListener#onNewIsland(world.bentobox.bentobox.api.events.island.IslandEvent)}. + * Test method for + * {@link world.bentobox.limits.listeners.JoinListener#onNewIsland(world.bentobox.bentobox.api.events.island.IslandEvent)}. */ @Test public void testOnNewIslandResetted() { - IslandEvent e = new IslandEvent(island, null, false, null, IslandEvent.Reason.RESETTED); - jl.onNewIsland(e); - verify(island).getWorld(); + IslandEvent e = new IslandEvent(island, null, false, null, IslandEvent.Reason.RESETTED); + jl.onNewIsland(e); + verify(island).getWorld(); } /** @@ -185,213 +195,228 @@ public class JoinListenerTest { } /** - * Test method for {@link world.bentobox.limits.listeners.JoinListener#onOwnerChange(world.bentobox.bentobox.api.events.team.TeamEvent.TeamSetownerEvent)}. + * Test method for + * {@link world.bentobox.limits.listeners.JoinListener#onOwnerChange(world.bentobox.bentobox.api.events.team.TeamEvent.TeamSetownerEvent)}. */ @Test public void testOnOwnerChange() { - TeamSetownerEvent e = mock(TeamSetownerEvent.class); - when(e.getIsland()).thenReturn(island); - when(e.getNewOwner()).thenReturn(UUID.randomUUID()); - jl.onOwnerChange(e); - verify(e, Mockito.times(2)).getIsland(); - verify(e).getNewOwner(); + TeamSetownerEvent e = mock(TeamSetownerEvent.class); + when(e.getIsland()).thenReturn(island); + when(e.getNewOwner()).thenReturn(UUID.randomUUID()); + jl.onOwnerChange(e); + verify(e, Mockito.times(2)).getIsland(); + verify(e).getNewOwner(); } /** - * Test method for {@link world.bentobox.limits.listeners.JoinListener#onPlayerJoin(org.bukkit.event.player.PlayerJoinEvent)}. + * Test method for + * {@link world.bentobox.limits.listeners.JoinListener#onPlayerJoin(org.bukkit.event.player.PlayerJoinEvent)}. */ @Test public void testOnPlayerJoin() { - PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome"); - jl.onPlayerJoin(e); - verify(addon).getGameModes(); - verify(bll).setIsland("unique_id", ibc); + PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome"); + jl.onPlayerJoin(e); + verify(addon).getGameModes(); + verify(bll).setIsland("unique_id", ibc); } /** - * Test method for {@link world.bentobox.limits.listeners.JoinListener#onPlayerJoin(org.bukkit.event.player.PlayerJoinEvent)}. + * Test method for + * {@link world.bentobox.limits.listeners.JoinListener#onPlayerJoin(org.bukkit.event.player.PlayerJoinEvent)}. */ @Test public void testOnPlayerJoinIBCNull() { - ibc = null; - PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome"); - jl.onPlayerJoin(e); - verify(addon).getGameModes(); - verify(bll, never()).setIsland("unique_id", ibc); + ibc = null; + PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome"); + jl.onPlayerJoin(e); + verify(addon).getGameModes(); + verify(bll, never()).setIsland("unique_id", ibc); } - /** - * Test method for {@link world.bentobox.limits.listeners.JoinListener#onPlayerJoin(org.bukkit.event.player.PlayerJoinEvent)}. + * Test method for + * {@link world.bentobox.limits.listeners.JoinListener#onPlayerJoin(org.bukkit.event.player.PlayerJoinEvent)}. */ @Test public void testOnPlayerJoinWithPermNotLimits() { - Set perms = new HashSet<>(); - PermissionAttachmentInfo permAtt = mock(PermissionAttachmentInfo.class); - when(permAtt.getPermission()).thenReturn("bskyblock.my.perm.for.game"); - perms.add(permAtt); - when(player.getEffectivePermissions()).thenReturn(perms); - PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome"); - jl.onPlayerJoin(e); - verify(addon).getGameModes(); - verify(bll).setIsland("unique_id", ibc); + Set perms = new HashSet<>(); + PermissionAttachmentInfo permAtt = mock(PermissionAttachmentInfo.class); + when(permAtt.getPermission()).thenReturn("bskyblock.my.perm.for.game"); + perms.add(permAtt); + when(player.getEffectivePermissions()).thenReturn(perms); + PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome"); + jl.onPlayerJoin(e); + verify(addon).getGameModes(); + verify(bll).setIsland("unique_id", ibc); } /** - * Test method for {@link world.bentobox.limits.listeners.JoinListener#onPlayerJoin(org.bukkit.event.player.PlayerJoinEvent)}. + * Test method for + * {@link world.bentobox.limits.listeners.JoinListener#onPlayerJoin(org.bukkit.event.player.PlayerJoinEvent)}. */ @Test public void testOnPlayerJoinWithPermLimitsWrongSize() { - Set perms = new HashSet<>(); - PermissionAttachmentInfo permAtt = mock(PermissionAttachmentInfo.class); - when(permAtt.getPermission()).thenReturn("bskyblock.island.limit.my.perm.for.game"); - when(permAtt.getValue()).thenReturn(true); - perms.add(permAtt); - when(player.getEffectivePermissions()).thenReturn(perms); - PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome"); - jl.onPlayerJoin(e); - verify(addon).logError("Player tastybento has permission: 'bskyblock.island.limit.my.perm.for.game' but format must be 'bskyblock.island.limit.MATERIAL.NUMBER', 'bskyblock.island.limit.ENTITY-TYPE.NUMBER', or 'bskyblock.island.limit.ENTITY-GROUP.NUMBER' Ignoring..."); + Set perms = new HashSet<>(); + PermissionAttachmentInfo permAtt = mock(PermissionAttachmentInfo.class); + when(permAtt.getPermission()).thenReturn("bskyblock.island.limit.my.perm.for.game"); + when(permAtt.getValue()).thenReturn(true); + perms.add(permAtt); + when(player.getEffectivePermissions()).thenReturn(perms); + PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome"); + jl.onPlayerJoin(e); + verify(addon).logError( + "Player tastybento has permission: 'bskyblock.island.limit.my.perm.for.game' but format must be 'bskyblock.island.limit.MATERIAL.NUMBER', 'bskyblock.island.limit.ENTITY-TYPE.NUMBER', or 'bskyblock.island.limit.ENTITY-GROUP.NUMBER' Ignoring..."); } /** - * Test method for {@link world.bentobox.limits.listeners.JoinListener#onPlayerJoin(org.bukkit.event.player.PlayerJoinEvent)}. + * Test method for + * {@link world.bentobox.limits.listeners.JoinListener#onPlayerJoin(org.bukkit.event.player.PlayerJoinEvent)}. */ @Test public void testOnPlayerJoinWithPermLimitsInvalidMaterial() { - Set perms = new HashSet<>(); - PermissionAttachmentInfo permAtt = mock(PermissionAttachmentInfo.class); - when(permAtt.getPermission()).thenReturn("bskyblock.island.limit.mumbo.34"); - when(permAtt.getValue()).thenReturn(true); - perms.add(permAtt); - when(player.getEffectivePermissions()).thenReturn(perms); - PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome"); - jl.onPlayerJoin(e); - verify(addon).logError("Player tastybento has permission: 'bskyblock.island.limit.mumbo.34' but MUMBO is not a valid material or entity type/group. Ignoring..."); + Set perms = new HashSet<>(); + PermissionAttachmentInfo permAtt = mock(PermissionAttachmentInfo.class); + when(permAtt.getPermission()).thenReturn("bskyblock.island.limit.mumbo.34"); + when(permAtt.getValue()).thenReturn(true); + perms.add(permAtt); + when(player.getEffectivePermissions()).thenReturn(perms); + PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome"); + jl.onPlayerJoin(e); + verify(addon).logError( + "Player tastybento has permission: 'bskyblock.island.limit.mumbo.34' but MUMBO is not a valid material or entity type/group. Ignoring..."); } /** - * Test method for {@link world.bentobox.limits.listeners.JoinListener#onPlayerJoin(org.bukkit.event.player.PlayerJoinEvent)}. + * Test method for + * {@link world.bentobox.limits.listeners.JoinListener#onPlayerJoin(org.bukkit.event.player.PlayerJoinEvent)}. */ @Test public void testOnPlayerJoinWithPermLimitsWildcard() { - Set perms = new HashSet<>(); - PermissionAttachmentInfo permAtt = mock(PermissionAttachmentInfo.class); - when(permAtt.getPermission()).thenReturn("bskyblock.island.limit.*"); - when(permAtt.getValue()).thenReturn(true); - perms.add(permAtt); - when(player.getEffectivePermissions()).thenReturn(perms); - PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome"); - jl.onPlayerJoin(e); - verify(addon).logError("Player tastybento has permission: 'bskyblock.island.limit.*' but wildcards are not allowed. Ignoring..."); + Set perms = new HashSet<>(); + PermissionAttachmentInfo permAtt = mock(PermissionAttachmentInfo.class); + when(permAtt.getPermission()).thenReturn("bskyblock.island.limit.*"); + when(permAtt.getValue()).thenReturn(true); + perms.add(permAtt); + when(player.getEffectivePermissions()).thenReturn(perms); + PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome"); + jl.onPlayerJoin(e); + verify(addon).logError( + "Player tastybento has permission: 'bskyblock.island.limit.*' but wildcards are not allowed. Ignoring..."); } /** - * Test method for {@link world.bentobox.limits.listeners.JoinListener#onPlayerJoin(org.bukkit.event.player.PlayerJoinEvent)}. + * Test method for + * {@link world.bentobox.limits.listeners.JoinListener#onPlayerJoin(org.bukkit.event.player.PlayerJoinEvent)}. */ @Test public void testOnPlayerJoinWithPermLimitsNotNumber() { - Set perms = new HashSet<>(); - PermissionAttachmentInfo permAtt = mock(PermissionAttachmentInfo.class); - when(permAtt.getPermission()).thenReturn("bskyblock.island.limit.STONE.abc"); - when(permAtt.getValue()).thenReturn(true); - perms.add(permAtt); - when(player.getEffectivePermissions()).thenReturn(perms); - PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome"); - jl.onPlayerJoin(e); - verify(addon).logError("Player tastybento has permission: 'bskyblock.island.limit.STONE.abc' but the last part MUST be an integer! Ignoring..."); + Set perms = new HashSet<>(); + PermissionAttachmentInfo permAtt = mock(PermissionAttachmentInfo.class); + when(permAtt.getPermission()).thenReturn("bskyblock.island.limit.STONE.abc"); + when(permAtt.getValue()).thenReturn(true); + perms.add(permAtt); + when(player.getEffectivePermissions()).thenReturn(perms); + PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome"); + jl.onPlayerJoin(e); + verify(addon).logError( + "Player tastybento has permission: 'bskyblock.island.limit.STONE.abc' but the last part MUST be an integer! Ignoring..."); } /** - * Test method for {@link world.bentobox.limits.listeners.JoinListener#onPlayerJoin(org.bukkit.event.player.PlayerJoinEvent)}. + * Test method for + * {@link world.bentobox.limits.listeners.JoinListener#onPlayerJoin(org.bukkit.event.player.PlayerJoinEvent)}. */ @Test public void testOnPlayerJoinWithPermLimitsSuccess() { - Set perms = new HashSet<>(); - PermissionAttachmentInfo permAtt = mock(PermissionAttachmentInfo.class); - when(permAtt.getPermission()).thenReturn("bskyblock.island.limit.STONE.24"); - when(permAtt.getValue()).thenReturn(true); - perms.add(permAtt); - when(player.getEffectivePermissions()).thenReturn(perms); - PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome"); - jl.onPlayerJoin(e); - verify(addon, never()).logError(anyString()); - verify(ibc).setBlockLimit(eq(Material.STONE), eq(24)); + Set perms = new HashSet<>(); + PermissionAttachmentInfo permAtt = mock(PermissionAttachmentInfo.class); + when(permAtt.getPermission()).thenReturn("bskyblock.island.limit.STONE.24"); + when(permAtt.getValue()).thenReturn(true); + perms.add(permAtt); + when(player.getEffectivePermissions()).thenReturn(perms); + PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome"); + jl.onPlayerJoin(e); + verify(addon, never()).logError(anyString()); + verify(ibc).setBlockLimit(eq(Material.STONE), eq(24)); } /** - * Test method for {@link world.bentobox.limits.listeners.JoinListener#onPlayerJoin(org.bukkit.event.player.PlayerJoinEvent)}. + * Test method for + * {@link world.bentobox.limits.listeners.JoinListener#onPlayerJoin(org.bukkit.event.player.PlayerJoinEvent)}. */ @Test public void testOnPlayerJoinWithPermLimitsSuccessEntity() { - Set perms = new HashSet<>(); - PermissionAttachmentInfo permAtt = mock(PermissionAttachmentInfo.class); - when(permAtt.getPermission()).thenReturn("bskyblock.island.limit.BAT.24"); - when(permAtt.getValue()).thenReturn(true); - perms.add(permAtt); - when(player.getEffectivePermissions()).thenReturn(perms); - PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome"); - jl.onPlayerJoin(e); - verify(addon, never()).logError(anyString()); - verify(ibc).setEntityLimit(eq(EntityType.BAT), eq(24)); + Set perms = new HashSet<>(); + PermissionAttachmentInfo permAtt = mock(PermissionAttachmentInfo.class); + when(permAtt.getPermission()).thenReturn("bskyblock.island.limit.BAT.24"); + when(permAtt.getValue()).thenReturn(true); + perms.add(permAtt); + when(player.getEffectivePermissions()).thenReturn(perms); + PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome"); + jl.onPlayerJoin(e); + verify(addon, never()).logError(anyString()); + verify(ibc).setEntityLimit(eq(EntityType.BAT), eq(24)); } /** - * Test method for {@link world.bentobox.limits.listeners.JoinListener#onPlayerJoin(org.bukkit.event.player.PlayerJoinEvent)}. + * Test method for + * {@link world.bentobox.limits.listeners.JoinListener#onPlayerJoin(org.bukkit.event.player.PlayerJoinEvent)}. */ @Test public void testOnPlayerJoinWithPermLimitsSuccessEntityGroup() { - Set perms = new HashSet<>(); - PermissionAttachmentInfo permAtt = mock(PermissionAttachmentInfo.class); - when(permAtt.getPermission()).thenReturn("bskyblock.island.limit.friendly.24"); - when(permAtt.getValue()).thenReturn(true); - perms.add(permAtt); - when(player.getEffectivePermissions()).thenReturn(perms); - PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome"); - jl.onPlayerJoin(e); - verify(addon, never()).logError(anyString()); - verify(ibc).setEntityGroupLimit(eq("friendly"), eq(24)); + Set perms = new HashSet<>(); + PermissionAttachmentInfo permAtt = mock(PermissionAttachmentInfo.class); + when(permAtt.getPermission()).thenReturn("bskyblock.island.limit.friendly.24"); + when(permAtt.getValue()).thenReturn(true); + perms.add(permAtt); + when(player.getEffectivePermissions()).thenReturn(perms); + PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome"); + jl.onPlayerJoin(e); + verify(addon, never()).logError(anyString()); + verify(ibc).setEntityGroupLimit(eq("friendly"), eq(24)); } /** - * Test method for {@link world.bentobox.limits.listeners.JoinListener#onPlayerJoin(org.bukkit.event.player.PlayerJoinEvent)}. + * Test method for + * {@link world.bentobox.limits.listeners.JoinListener#onPlayerJoin(org.bukkit.event.player.PlayerJoinEvent)}. */ @Test public void testOnPlayerJoinWithPermLimitsMultiPerms() { - Set perms = new HashSet<>(); - PermissionAttachmentInfo permAtt = mock(PermissionAttachmentInfo.class); - when(permAtt.getPermission()).thenReturn("bskyblock.island.limit.STONE.24"); - when(permAtt.getValue()).thenReturn(true); - perms.add(permAtt); - PermissionAttachmentInfo permAtt2 = mock(PermissionAttachmentInfo.class); - when(permAtt2.getPermission()).thenReturn("bskyblock.island.limit.grass.14"); - when(permAtt2.getValue()).thenReturn(true); - perms.add(permAtt2); - PermissionAttachmentInfo permAtt3 = mock(PermissionAttachmentInfo.class); - when(permAtt3.getPermission()).thenReturn("bskyblock.island.limit.dirt.34"); - when(permAtt3.getValue()).thenReturn(true); - perms.add(permAtt3); - PermissionAttachmentInfo permAtt4 = mock(PermissionAttachmentInfo.class); - when(permAtt4.getPermission()).thenReturn("bskyblock.island.limit.chicken.34"); - when(permAtt4.getValue()).thenReturn(true); - perms.add(permAtt4); - PermissionAttachmentInfo permAtt5 = mock(PermissionAttachmentInfo.class); - when(permAtt5.getPermission()).thenReturn("bskyblock.island.limit.cave_spider.4"); - when(permAtt5.getValue()).thenReturn(true); - perms.add(permAtt5); - PermissionAttachmentInfo permAtt6 = mock(PermissionAttachmentInfo.class); - when(permAtt6.getPermission()).thenReturn("bskyblock.island.limit.cave_spider.4"); - when(permAtt6.getValue()).thenReturn(false); // negative perm - perms.add(permAtt6); + Set perms = new HashSet<>(); + PermissionAttachmentInfo permAtt = mock(PermissionAttachmentInfo.class); + when(permAtt.getPermission()).thenReturn("bskyblock.island.limit.STONE.24"); + when(permAtt.getValue()).thenReturn(true); + perms.add(permAtt); + PermissionAttachmentInfo permAtt2 = mock(PermissionAttachmentInfo.class); + when(permAtt2.getPermission()).thenReturn("bskyblock.island.limit.short_grass.14"); + when(permAtt2.getValue()).thenReturn(true); + perms.add(permAtt2); + PermissionAttachmentInfo permAtt3 = mock(PermissionAttachmentInfo.class); + when(permAtt3.getPermission()).thenReturn("bskyblock.island.limit.dirt.34"); + when(permAtt3.getValue()).thenReturn(true); + perms.add(permAtt3); + PermissionAttachmentInfo permAtt4 = mock(PermissionAttachmentInfo.class); + when(permAtt4.getPermission()).thenReturn("bskyblock.island.limit.chicken.34"); + when(permAtt4.getValue()).thenReturn(true); + perms.add(permAtt4); + PermissionAttachmentInfo permAtt5 = mock(PermissionAttachmentInfo.class); + when(permAtt5.getPermission()).thenReturn("bskyblock.island.limit.cave_spider.4"); + when(permAtt5.getValue()).thenReturn(true); + perms.add(permAtt5); + PermissionAttachmentInfo permAtt6 = mock(PermissionAttachmentInfo.class); + when(permAtt6.getPermission()).thenReturn("bskyblock.island.limit.cave_spider.4"); + when(permAtt6.getValue()).thenReturn(false); // negative perm + perms.add(permAtt6); - when(player.getEffectivePermissions()).thenReturn(perms); - PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome"); - jl.onPlayerJoin(e); - verify(addon, never()).logError(anyString()); - verify(ibc).setBlockLimit(eq(Material.STONE), eq(24)); - verify(ibc).setBlockLimit(eq(Material.GRASS), eq(14)); - verify(ibc).setBlockLimit(eq(Material.DIRT), eq(34)); - verify(ibc).setEntityLimit(eq(EntityType.CHICKEN), eq(34)); - verify(ibc).setEntityLimit(eq(EntityType.CAVE_SPIDER), eq(4)); + when(player.getEffectivePermissions()).thenReturn(perms); + PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome"); + jl.onPlayerJoin(e); + verify(addon, never()).logError(anyString()); + verify(ibc).setBlockLimit(eq(Material.STONE), eq(24)); + verify(ibc).setBlockLimit(eq(Material.SHORT_GRASS), eq(14)); + verify(ibc).setBlockLimit(eq(Material.DIRT), eq(34)); + verify(ibc).setEntityLimit(eq(EntityType.CHICKEN), eq(34)); + verify(ibc).setEntityLimit(eq(EntityType.CAVE_SPIDER), eq(4)); } /** @@ -455,40 +480,43 @@ public class JoinListenerTest { } /** - * Test method for {@link world.bentobox.limits.listeners.JoinListener#onUnregisterIsland(world.bentobox.bentobox.api.events.island.IslandEvent)}. + * Test method for + * {@link world.bentobox.limits.listeners.JoinListener#onUnregisterIsland(world.bentobox.bentobox.api.events.island.IslandEvent)}. */ @Test public void testOnUnregisterIslandNotUnregistered() { - IslandEvent e = new IslandEvent(island, null, false, null, IslandEvent.Reason.BAN); - jl.onUnregisterIsland(e); - verify(island, never()).getWorld(); + IslandEvent e = new IslandEvent(island, null, false, null, IslandEvent.Reason.BAN); + jl.onUnregisterIsland(e); + verify(island, never()).getWorld(); } /** - * Test method for {@link world.bentobox.limits.listeners.JoinListener#onUnregisterIsland(world.bentobox.bentobox.api.events.island.IslandEvent)}. + * Test method for + * {@link world.bentobox.limits.listeners.JoinListener#onUnregisterIsland(world.bentobox.bentobox.api.events.island.IslandEvent)}. */ @Test public void testOnUnregisterIslandNotInWorld() { - IslandEvent e = new IslandEvent(island, null, false, null, IslandEvent.Reason.UNREGISTERED); - jl.onUnregisterIsland(e); - verify(island).getWorld(); - verify(addon, never()).getBlockLimitListener(); + IslandEvent e = new IslandEvent(island, null, false, null, IslandEvent.Reason.UNREGISTERED); + jl.onUnregisterIsland(e); + verify(island).getWorld(); + verify(addon, never()).getBlockLimitListener(); } /** - * Test method for {@link world.bentobox.limits.listeners.JoinListener#onUnregisterIsland(world.bentobox.bentobox.api.events.island.IslandEvent)}. + * Test method for + * {@link world.bentobox.limits.listeners.JoinListener#onUnregisterIsland(world.bentobox.bentobox.api.events.island.IslandEvent)}. */ @Test public void testOnUnregisterIslandInWorld() { - @SuppressWarnings("unchecked") - Map map = mock(Map.class); - when(ibc.getBlockLimits()).thenReturn(map); - when(addon.inGameModeWorld(any())).thenReturn(true); - IslandEvent e = new IslandEvent(island, null, false, null, IslandEvent.Reason.UNREGISTERED); - jl.onUnregisterIsland(e); - verify(island).getWorld(); - verify(addon).getBlockLimitListener(); - verify(map).clear(); + @SuppressWarnings("unchecked") + Map map = mock(Map.class); + when(ibc.getBlockLimits()).thenReturn(map); + when(addon.inGameModeWorld(any())).thenReturn(true); + IslandEvent e = new IslandEvent(island, null, false, null, IslandEvent.Reason.UNREGISTERED); + jl.onUnregisterIsland(e); + verify(island).getWorld(); + verify(addon).getBlockLimitListener(); + verify(map).clear(); } @@ -510,6 +538,4 @@ public class JoinListenerTest { } - - }