Compare commits

..

No commits in common. "0dbd451c33beeb7f00f9c4374974d56b81acc29e" and "3e60b7b2eb87da42bcd0dfae5f5656c36c67bd63" have entirely different histories.

7 changed files with 684 additions and 752 deletions

View File

@ -11,22 +11,21 @@ jobs:
name: Build name: Build
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/checkout@v3 - uses: actions/checkout@v2
with: with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
- name: Set up JDK 17 - name: Set up JDK 17
uses: actions/setup-java@v3 uses: actions/setup-java@v1
with: with:
distribution: 'adopt'
java-version: 17 java-version: 17
- name: Cache SonarCloud packages - name: Cache SonarCloud packages
uses: actions/cache@v3 uses: actions/cache@v1
with: with:
path: ~/.sonar/cache path: ~/.sonar/cache
key: ${{ runner.os }}-sonar key: ${{ runner.os }}-sonar
restore-keys: ${{ runner.os }}-sonar restore-keys: ${{ runner.os }}-sonar
- name: Cache Maven packages - name: Cache Maven packages
uses: actions/cache@v3 uses: actions/cache@v1
with: with:
path: ~/.m2 path: ~/.m2
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }} key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}

10
pom.xml
View File

@ -57,14 +57,14 @@
<!-- Non-minecraft related dependencies --> <!-- Non-minecraft related dependencies -->
<powermock.version>2.0.9</powermock.version> <powermock.version>2.0.9</powermock.version>
<!-- More visible way how to change dependency versions --> <!-- More visible way how to change dependency versions -->
<spigot.version>1.20.4-R0.1-SNAPSHOT</spigot.version> <spigot.version>1.19.4-R0.1-SNAPSHOT</spigot.version>
<bentobox.version>2.0.0-SNAPSHOT</bentobox.version> <bentobox.version>1.23.0</bentobox.version>
<!-- Revision variable removes warning about dynamic version --> <!-- Revision variable removes warning about dynamic version -->
<revision>${build.version}-SNAPSHOT</revision> <revision>${build.version}-SNAPSHOT</revision>
<!-- Do not change unless you want different name for local builds. --> <!-- Do not change unless you want different name for local builds. -->
<build.number>-LOCAL</build.number> <build.number>-LOCAL</build.number>
<!-- This allows to change between versions. --> <!-- This allows to change between versions. -->
<build.version>1.20.1</build.version> <build.version>1.19.1</build.version>
<sonar.projectKey>BentoBoxWorld_Limits</sonar.projectKey> <sonar.projectKey>BentoBoxWorld_Limits</sonar.projectKey>
<sonar.organization>bentobox-world</sonar.organization> <sonar.organization>bentobox-world</sonar.organization>
<sonar.host.url>https://sonarcloud.io</sonar.host.url> <sonar.host.url>https://sonarcloud.io</sonar.host.url>
@ -251,15 +251,13 @@
<plugin> <plugin>
<groupId>org.jacoco</groupId> <groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId> <artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.10</version> <version>0.8.7</version>
<configuration> <configuration>
<append>true</append> <append>true</append>
<excludes> <excludes>
<!-- This is required to prevent Jacoco from adding <!-- This is required to prevent Jacoco from adding
synthetic fields to a JavaBean class (causes errors in testing) --> synthetic fields to a JavaBean class (causes errors in testing) -->
<exclude>**/*Names*</exclude> <exclude>**/*Names*</exclude>
<!-- Prevents the Material is too large to mock error -->
<exclude>org/bukkit/Material*</exclude>
</excludes> </excludes>
</configuration> </configuration>
<executions> <executions>

View File

@ -180,7 +180,7 @@ public class EntityLimitListener implements Listener {
if (island.isSpawn() || !res.hit()) { if (island.isSpawn() || !res.hit()) {
// Allowed // Allowed
if (async) { if (async) {
Bukkit.getScheduler().runTask(BentoBox.getInstance(), () -> preSpawn(e.getType(), reason, l)); Bukkit.getScheduler().runTask(BentoBox.getInstance(), () -> l.getWorld().spawn(l, e.getClass(), entity -> preSpawn(entity, reason, l)));
} // else do nothing } // else do nothing
} else { } else {
if (async) { if (async) {
@ -195,23 +195,21 @@ public class EntityLimitListener implements Listener {
return true; return true;
} }
private void preSpawn(EntityType entityType, SpawnReason reason, Location l) { private void preSpawn(Entity entity, SpawnReason reason, Location l) {
justSpawned.add(entity.getUniqueId());
// Check for entities that need cleanup // Check for entities that need cleanup
switch (reason) { switch (reason) {
case BUILD_IRONGOLEM -> detectIronGolem(l); case BUILD_IRONGOLEM -> detectIronGolem(l);
case BUILD_SNOWMAN -> detectSnowman(l); case BUILD_SNOWMAN -> detectSnowman(l);
case BUILD_WITHER -> { case BUILD_WITHER -> {
detectWither(l); 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 // Create explosion
l.getWorld().createExplosion(l, 7F, true, true, entity); l.getWorld().createExplosion(l, 7F, true, true, entity);
} }
default -> {
// Do nothing
}
}
} }
private void detectIronGolem(Location l) { private void detectIronGolem(Location l) {
@ -248,6 +246,7 @@ public class EntityLimitListener implements Listener {
} }
} }
} }
} }
private void detectSnowman(Location l) { private void detectSnowman(Location l) {

View File

@ -30,7 +30,6 @@ import world.bentobox.limits.objects.IslandBlockCount;
/** /**
* Sets block limits based on player permission * Sets block limits based on player permission
*
* @author tastybento * @author tastybento
* *
*/ */
@ -39,139 +38,132 @@ public class JoinListener implements Listener {
private final Limits addon; private final Limits addon;
public JoinListener(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 * Check and set the permissions of the player and how they affect the island limits
* limits * @param player - player
*
* @param player - player
* @param permissionPrefix - permission prefix for this game mode * @param permissionPrefix - permission prefix for this game mode
* @param islandId - island string id * @param islandId - island string id
* @param gameMode - game mode string doing the checking * @param gameMode - game mode string doing the checking
*/ */
public void checkPerms(Player player, String permissionPrefix, String islandId, String gameMode) { public void checkPerms(Player player, String permissionPrefix, String islandId, String gameMode) {
IslandBlockCount ibc = addon.getBlockLimitListener().getIsland(islandId); IslandBlockCount ibc = addon.getBlockLimitListener().getIsland(islandId);
// Check permissions // Check permissions
if (ibc != null) { if (ibc != null) {
// Clear permission limits // Clear permission limits
ibc.getEntityLimits().clear(); ibc.getEntityLimits().clear();
ibc.getEntityGroupLimits().clear(); ibc.getEntityGroupLimits().clear();
ibc.getBlockLimits().clear(); ibc.getBlockLimits().clear();
} }
for (PermissionAttachmentInfo perms : player.getEffectivePermissions()) { for (PermissionAttachmentInfo perms : player.getEffectivePermissions()) {
if (!perms.getValue() || !perms.getPermission().startsWith(permissionPrefix) if (!perms.getValue()
|| badSyntaxCheck(perms, player.getName(), permissionPrefix)) { || !perms.getPermission().startsWith(permissionPrefix)
continue; || badSyntaxCheck(perms, player.getName(), permissionPrefix)) {
} continue;
// Check formatting }
String[] split = perms.getPermission().split("\\."); // Check formatting
// Entities & materials String[] split = perms.getPermission().split("\\.");
EntityType et = Arrays.stream(EntityType.values()).filter(t -> t.name().equalsIgnoreCase(split[3])) // Entities & materials
.findFirst().orElse(null); 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() Material m = Arrays.stream(Material.values()).filter(t -> t.name().equalsIgnoreCase(split[3])).findFirst().orElse(null);
.orElse(null); EntityGroup entgroup = addon.getSettings().getGroupLimitDefinitions().stream()
EntityGroup entgroup = addon.getSettings().getGroupLimitDefinitions().stream() .filter(t -> t.getName().equalsIgnoreCase(split[3])).findFirst().orElse(null);
.filter(t -> t.getName().equalsIgnoreCase(split[3])).findFirst().orElse(null);
if (entgroup == null && et == null && m == null) { if (entgroup == null && et == null && m == null) {
logError(player.getName(), perms.getPermission(), logError(player.getName(), perms.getPermission(), split[3].toUpperCase(Locale.ENGLISH) + " is not a valid material or entity type/group.");
split[3].toUpperCase(Locale.ENGLISH) + " is not a valid material or entity type/group."); break;
break; }
} // Make an ibc if required
// Make an ibc if required if (ibc == null) {
if (ibc == null) { ibc = new IslandBlockCount(islandId, gameMode);
ibc = new IslandBlockCount(islandId, gameMode); }
} // Get the value
// Get the value int value = Integer.parseInt(split[4]);
int value = Integer.parseInt(split[4]); addon.log("Setting login limit via perm for " + player.getName() + "...");
addon.log("Setting login limit via perm for " + player.getName() + "...");
// Fire perm check event // Fire perm check event
LimitsPermCheckEvent l = new LimitsPermCheckEvent(player, islandId, ibc, entgroup, et, m, value); LimitsPermCheckEvent l = new LimitsPermCheckEvent(player, islandId, ibc, entgroup, et, m, value);
Bukkit.getPluginManager().callEvent(l); Bukkit.getPluginManager().callEvent(l);
if (l.isCancelled()) { if (l.isCancelled()) {
addon.log("Permissions not set because another addon/plugin canceled setting."); addon.log("Permissions not set because another addon/plugin canceled setting.");
continue; continue;
} }
// Use event values // Use event values
ibc = l.getIbc(); ibc = l.getIbc();
// Make an ibc if required // Make an ibc if required
if (ibc == null) { if (ibc == null) {
ibc = new IslandBlockCount(islandId, gameMode); ibc = new IslandBlockCount(islandId, gameMode);
} }
// Run null checks and set ibc // Run null checks and set ibc
runNullCheckAndSet(ibc, l); runNullCheckAndSet(ibc, l);
} }
// Check removed permissions // Check removed permissions
// If any changes have been made then store it - don't make files unless they // If any changes have been made then store it - don't make files unless they are needed
// are needed if (ibc != null) addon.getBlockLimitListener().setIsland(islandId, ibc);
if (ibc != null)
addon.getBlockLimitListener().setIsland(islandId, ibc);
} }
private boolean badSyntaxCheck(PermissionAttachmentInfo perms, String name, String permissionPrefix) { private boolean badSyntaxCheck(PermissionAttachmentInfo perms, String name, String permissionPrefix) {
// No wildcards // No wildcards
if (perms.getPermission().contains(permissionPrefix + "*")) { if (perms.getPermission().contains(permissionPrefix + "*")) {
logError(name, perms.getPermission(), "wildcards are not allowed."); logError(name, perms.getPermission(), "wildcards are not allowed.");
return true; return true;
} }
// Check formatting // Check formatting
String[] split = perms.getPermission().split("\\."); String[] split = perms.getPermission().split("\\.");
if (split.length != 5) { if (split.length != 5) {
logError(name, perms.getPermission(), "format must be '" + permissionPrefix + "MATERIAL.NUMBER', '" logError(name, perms.getPermission(), "format must be '" + permissionPrefix + "MATERIAL.NUMBER', '" + permissionPrefix + "ENTITY-TYPE.NUMBER', or '" + permissionPrefix + "ENTITY-GROUP.NUMBER'");
+ permissionPrefix + "ENTITY-TYPE.NUMBER', or '" + permissionPrefix + "ENTITY-GROUP.NUMBER'"); return true;
return true; }
} // Check value
// Check value try {
try { Integer.parseInt(split[4]);
Integer.parseInt(split[4]); } catch(Exception e) {
} catch (Exception e) { logError(name, perms.getPermission(), "the last part MUST be an integer!");
logError(name, perms.getPermission(), "the last part MUST be an integer!"); return true;
return true; }
} return false;
return false;
} }
private void runNullCheckAndSet(@NonNull IslandBlockCount ibc, @NonNull LimitsPermCheckEvent l) { private void runNullCheckAndSet(@NonNull IslandBlockCount ibc, @NonNull LimitsPermCheckEvent l) {
EntityGroup entgroup = l.getEntityGroup(); EntityGroup entgroup = l.getEntityGroup();
EntityType et = l.getEntityType(); EntityType et = l.getEntityType();
Material m = l.getMaterial(); Material m = l.getMaterial();
int value = l.getValue(); int value = l.getValue();
if (entgroup != null) { if (entgroup != null) {
// Entity group limit // Entity group limit
int v = Math.max(ibc.getEntityGroupLimit(entgroup.getName()), value); int v = Math.max(ibc.getEntityGroupLimit(entgroup.getName()), value);
ibc.setEntityGroupLimit(entgroup.getName(), v); ibc.setEntityGroupLimit(entgroup.getName(), v);
addon.log("Setting group limit " + entgroup.getName() + " " + v); addon.log("Setting group limit " + entgroup.getName() + " " + v);
} else if (et != null && m == null) { } else if (et != null && m == null) {
// Entity limit // Entity limit
int v = Math.max(ibc.getEntityLimit(et), value); int v = Math.max(ibc.getEntityLimit(et), value);
ibc.setEntityLimit(et, v); ibc.setEntityLimit(et, v);
addon.log("Setting entity limit " + et + " " + v); addon.log("Setting entity limit " + et + " " + v);
} else if (m != null && et == null) { } else if (m != null && et == null) {
// Block limit // Block limit
int v = Math.max(ibc.getBlockLimit(m), value); int v = Math.max(ibc.getBlockLimit(m), value);
addon.log("Setting block limit " + m + " " + v); addon.log("Setting block limit " + m + " " + v);
ibc.setBlockLimit(m, v); ibc.setBlockLimit(m, v);
} else { } else {
if (m != null && m.isBlock()) { if (m != null && m.isBlock()) {
int v = Math.max(ibc.getBlockLimit(m), value); int v = Math.max(ibc.getBlockLimit(m), value);
addon.log("Setting block limit " + m + " " + v); addon.log("Setting block limit " + m + " " + v);
// Material limit // Material limit
ibc.setBlockLimit(m, v); ibc.setBlockLimit(m, v);
} else if (et != null) { } else if (et != null){
int v = Math.max(ibc.getEntityLimit(et), value); int v = Math.max(ibc.getEntityLimit(et), value);
addon.log("Setting entity limit " + et + " " + v); addon.log("Setting entity limit " + et + " " + v);
// This is an entity setting // This is an entity setting
ibc.setEntityLimit(et, v); ibc.setEntityLimit(et, v);
} }
} }
} }
private void logError(String name, String perm, String error) { 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...");
} }
/* /*
@ -180,66 +172,66 @@ public class JoinListener implements Listener {
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onNewIsland(IslandEvent e) { public void onNewIsland(IslandEvent e) {
if (!e.getReason().equals(Reason.CREATED) && !e.getReason().equals(Reason.RESETTED) if (!e.getReason().equals(Reason.CREATED)
&& !e.getReason().equals(Reason.REGISTERED)) { && !e.getReason().equals(Reason.RESETTED)
return; && !e.getReason().equals(Reason.REGISTERED)) {
} return;
setOwnerPerms(e.getIsland(), e.getOwner()); }
setOwnerPerms(e.getIsland(), e.getOwner());
} }
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onOwnerChange(TeamSetownerEvent e) { public void onOwnerChange(TeamSetownerEvent e) {
removeOwnerPerms(e.getIsland()); removeOwnerPerms(e.getIsland());
setOwnerPerms(e.getIsland(), e.getNewOwner()); setOwnerPerms(e.getIsland(), e.getNewOwner());
} }
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onPlayerJoin(PlayerJoinEvent e) { public void onPlayerJoin(PlayerJoinEvent e) {
// Check if player has any islands in the game modes // Check if player has any islands in the game modes
addon.getGameModes().forEach(gm -> { addon.getGameModes().forEach(gm -> {
addon.getIslands().getIslands(gm.getOverWorld(), e.getPlayer().getUniqueId()).stream() if (addon.getIslands().hasIsland(gm.getOverWorld(), e.getPlayer().getUniqueId())) {
.map(Island::getUniqueId).forEach(islandId -> { String islandId = Objects.requireNonNull(addon.getIslands().getIsland(gm.getOverWorld(), e.getPlayer().getUniqueId())).getUniqueId();
IslandBlockCount ibc = addon.getBlockLimitListener().getIsland(islandId); IslandBlockCount ibc = addon.getBlockLimitListener().getIsland(islandId);
if (!joinEventCheck(e.getPlayer(), islandId, ibc)) { if (joinEventCheck(e.getPlayer(), islandId, ibc)) {
checkPerms(e.getPlayer(), gm.getPermissionPrefix() + "island.limit.", islandId, return;
gm.getDescription().getName()); }
} checkPerms(e.getPlayer(), gm.getPermissionPrefix() + "island.limit.", islandId, gm.getDescription().getName());
}); }
}); });
} }
/** /**
* Fire event so other addons can cancel this permissions change * Fire event so other addons can cancel this permissions change
* * @param player player
* @param player player
* @param islandId island id * @param islandId island id
* @param ibc island block count * @param ibc island block count
* @return true if canceled * @return true if canceled
*/ */
private boolean joinEventCheck(Player player, String islandId, IslandBlockCount ibc) { private boolean joinEventCheck(Player player, String islandId, IslandBlockCount ibc) {
// Fire event, so other addons can cancel this permissions change // Fire event, so other addons can cancel this permissions change
LimitsJoinPermCheckEvent e = new LimitsJoinPermCheckEvent(player, islandId, ibc); LimitsJoinPermCheckEvent e = new LimitsJoinPermCheckEvent(player, islandId, ibc);
Bukkit.getPluginManager().callEvent(e); Bukkit.getPluginManager().callEvent(e);
if (e.isCancelled()) { if (e.isCancelled()) {
return true; return true;
} }
// Get ibc from event if it has changed // Get ibc from event if it has changed
ibc = e.getIbc(); ibc = e.getIbc();
// If perms should be ignored, but the IBC given in the event used, then set it // If perms should be ignored, but the IBC given in the event used, then set it and return
// and return if (e.isIgnorePerms() && ibc != null) {
if (e.isIgnorePerms() && ibc != null) { addon.getBlockLimitListener().setIsland(islandId, ibc);
addon.getBlockLimitListener().setIsland(islandId, ibc); return true;
return true; }
} return false;
return false;
} }
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onUnregisterIsland(IslandEvent e) { public void onUnregisterIsland(IslandEvent e) {
if (!e.getReason().equals(Reason.UNREGISTERED)) { if (!e.getReason().equals(Reason.UNREGISTERED)) {
return; return;
} }
removeOwnerPerms(e.getIsland()); removeOwnerPerms(e.getIsland());
} }
/* /*
@ -247,30 +239,29 @@ public class JoinListener implements Listener {
*/ */
private void removeOwnerPerms(Island island) { private void removeOwnerPerms(Island island) {
World world = island.getWorld(); World world = island.getWorld();
if (addon.inGameModeWorld(world)) { if (addon.inGameModeWorld(world)) {
IslandBlockCount ibc = addon.getBlockLimitListener().getIsland(island.getUniqueId()); IslandBlockCount ibc = addon.getBlockLimitListener().getIsland(island.getUniqueId());
if (ibc != null) { if (ibc != null) {
ibc.getBlockLimits().clear(); ibc.getBlockLimits().clear();
} }
} }
} }
private void setOwnerPerms(Island island, UUID ownerUUID) { private void setOwnerPerms(Island island, UUID ownerUUID) {
World world = island.getWorld(); World world = island.getWorld();
if (addon.inGameModeWorld(world)) { if (addon.inGameModeWorld(world)) {
// Check if owner is online // Check if owner is online
OfflinePlayer owner = Bukkit.getOfflinePlayer(ownerUUID); OfflinePlayer owner = Bukkit.getOfflinePlayer(ownerUUID);
if (owner.isOnline()) { if (owner.isOnline()) {
// Set perm-based limits // Set perm-based limits
String prefix = addon.getGameModePermPrefix(world); String prefix = addon.getGameModePermPrefix(world);
String name = addon.getGameModeName(world); String name = addon.getGameModeName(world);
if (!prefix.isEmpty() && !name.isEmpty() && owner.getPlayer() != null) { if (!prefix.isEmpty() && !name.isEmpty() && owner.getPlayer() != null) {
checkPerms(Objects.requireNonNull(owner.getPlayer()), prefix + "island.limit.", checkPerms(Objects.requireNonNull(owner.getPlayer()), prefix + "island.limit.", island.getUniqueId(), name);
island.getUniqueId(), name); }
} }
} }
}
} }
} }

View File

@ -3,7 +3,6 @@ package world.bentobox.limits.objects;
import java.util.EnumMap; import java.util.EnumMap;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.entity.EntityType; import org.bukkit.entity.EntityType;
@ -20,398 +19,370 @@ import world.bentobox.bentobox.database.objects.Table;
@Table(name = "IslandBlockCount") @Table(name = "IslandBlockCount")
public class IslandBlockCount implements DataObject { public class IslandBlockCount implements DataObject {
@Expose
private String uniqueId;
@Expose
private String gameMode;
@Expose @Expose
private Map<Material, Integer> blockCounts = new EnumMap<>(Material.class); private Map<Material, Integer> blockCounts = new EnumMap<>(Material.class);
private boolean changed;
/** /**
* Permission based limits * Permission based limits
*/ */
@Expose @Expose
private Map<Material, Integer> blockLimits = new EnumMap<>(Material.class); private Map<Material, Integer> blockLimits = new EnumMap<>(Material.class);
@Expose
private Map<Material, Integer> blockLimitsOffset = new EnumMap<>(Material.class);
private boolean changed;
@Expose
private Map<String, Integer> entityGroupLimits = new HashMap<>();
@Expose
private Map<String, Integer> entityGroupLimitsOffset = new HashMap<>();
@Expose @Expose
private Map<EntityType, Integer> entityLimits = new EnumMap<>(EntityType.class); private Map<EntityType, Integer> entityLimits = new EnumMap<>(EntityType.class);
@Expose @Expose
private Map<String, Integer> entityGroupLimits = new HashMap<>();
@Expose
private Map<Material, Integer> blockLimitsOffset = new EnumMap<>(Material.class);
@Expose
private Map<EntityType, Integer> entityLimitsOffset = new EnumMap<>(EntityType.class); private Map<EntityType, Integer> entityLimitsOffset = new EnumMap<>(EntityType.class);
@Expose @Expose
private String gameMode; private Map<String, Integer> entityGroupLimitsOffset = new HashMap<>();
@Expose
private String uniqueId;
/** /**
* Create an island block count object * Create an island block count object
*
* @param islandId - unique Island ID string * @param islandId - unique Island ID string
* @param gameMode - Game mode name from gm.getDescription().getName() * @param gameMode - Game mode name from gm.getDescription().getName()
*/ */
public IslandBlockCount(String islandId, String gameMode) { public IslandBlockCount(String islandId, String gameMode) {
this.uniqueId = islandId; this.uniqueId = islandId;
this.gameMode = gameMode; this.gameMode = gameMode;
setChanged(); setChanged();
} }
/** /* (non-Javadoc)
* Add a material to the count * @see world.bentobox.bentobox.database.objects.DataObject#getUniqueId()
*
* @param material - material
*/ */
public void add(Material material) { @Override
getBlockCounts().merge(material, 1, Integer::sum); public String getUniqueId() {
setChanged(); return uniqueId;
} }
/** /* (non-Javadoc)
* Clear all island-specific entity group limits * @see world.bentobox.bentobox.database.objects.DataObject#setUniqueId(java.lang.String)
*/ */
public void clearEntityGroupLimits() { @Override
entityGroupLimits.clear(); public void setUniqueId(String uniqueId) {
setChanged(); this.uniqueId = uniqueId;
} setChanged();
/**
* Clear all island-specific entity type limits
*/
public void clearEntityLimits() {
entityLimits.clear();
setChanged();
}
/**
* Get the block count for this material for this island
*
* @param m - material
* @return count
*/
public Integer getBlockCount(Material m) {
return getBlockCounts().getOrDefault(m, 0);
} }
/** /**
* @return the blockCount * @return the blockCount
*/ */
public Map<Material, Integer> getBlockCounts() { public Map<Material, Integer> getBlockCounts() {
if (blockCounts == null) { if (blockCounts == null) {
blockCounts = new EnumMap<>(Material.class); blockCounts = new EnumMap<>(Material.class);
} }
return blockCounts; 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<Material, Integer> getBlockLimits() {
return Objects.requireNonNullElse(blockLimits, new EnumMap<>(Material.class));
}
/**
* @return the blockLimitsOffset
*/
public Map<Material, Integer> getBlockLimitsOffset() {
if (blockLimitsOffset == null) {
blockLimitsOffset = new EnumMap<>(Material.class);
}
return blockLimitsOffset;
}
/**
* Get the limit for an entity group
*
* @param name - entity group
* @return limit or -1 for unlimited
*/
public int getEntityGroupLimit(String name) {
return getEntityGroupLimits().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);
}
/**
* @return the entityGroupLimits
*/
public Map<String, Integer> getEntityGroupLimits() {
return Objects.requireNonNullElse(entityGroupLimits, new HashMap<>());
}
/**
* @return the entityGroupLimitsOffset
*/
public Map<String, Integer> getEntityGroupLimitsOffset() {
if (entityGroupLimitsOffset == null) {
entityGroupLimitsOffset = new HashMap<>();
}
return entityGroupLimitsOffset;
}
/**
* 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<EntityType, Integer> getEntityLimits() {
return Objects.requireNonNullElse(entityLimits, new EnumMap<>(EntityType.class));
}
/**
* @return the entityLimitsOffset
*/
public Map<EntityType, Integer> 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 * @param blockCounts the blockCount to set
*/ */
public void setBlockCounts(Map<Material, Integer> blockCounts) { public void setBlockCounts(Map<Material, Integer> blockCounts) {
this.blockCounts = blockCounts; this.blockCounts = blockCounts;
setChanged(); setChanged();
} }
/** /**
* Set the block limit for this material for this island * Get the block count for this material for this island
* * @param m - material
* @param m - material * @return count
* @param limit - maximum number allowed
*/ */
public void setBlockLimit(Material m, int limit) { public Integer getBlockCount(Material m) {
getBlockLimits().put(m, limit); return blockCounts.getOrDefault(m, 0);
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<Material, Integer> getBlockLimits() {
return blockLimits;
} }
/** /**
* @param blockLimits the blockLimits to set * @param blockLimits the blockLimits to set
*/ */
public void setBlockLimits(Map<Material, Integer> blockLimits) { public void setBlockLimits(Map<Material, Integer> blockLimits) {
this.blockLimits = blockLimits; this.blockLimits = blockLimits;
setChanged(); setChanged();
} }
/** /**
* Set an offset to a block limit. This will increase/decrease the value of the * Get the block limit for this material for this island
* limit. * @param m - material
* * @return limit or -1 for unlimited
* @param m material
* @param blockLimitsOffset the blockLimitsOffset to set
*/ */
public void setBlockLimitsOffset(Material m, Integer blockLimitsOffset) { public int getBlockLimit(Material m) {
getBlockLimitsOffset().put(m, blockLimitsOffset); return blockLimits.getOrDefault(m, -1);
} }
/** /**
* Mark changed * Get the block offset for this material for this island
* @param m - material
* @return offset
*/ */
public void setChanged() { public int getBlockLimitOffset(Material m) {
this.changed = true; return getBlockLimitsOffset().getOrDefault(m, 0);
} }
/** /**
* @param changed the changed to set * Set the block limit for this material for this island
* @param m - material
* @param limit - maximum number allowed
*/ */
public void setChanged(boolean changed) { public void setBlockLimit(Material m, int limit) {
this.changed = changed; blockLimits.put(m, limit);
setChanged();
} }
/** /**
* Set an island-specific entity group limit * @return the gameMode
*
* @param name - entity group
* @param limit - limit
*/ */
public void setEntityGroupLimit(String name, int limit) { public String getGameMode() {
getEntityGroupLimits().put(name, limit); return gameMode;
setChanged();
} }
/** public boolean isGameMode(String gameMode) {
* @param entityGroupLimits the entityGroupLimits to set return this.gameMode.equals(gameMode);
*/
public void setEntityGroupLimits(Map<String, Integer> 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);
}
/**
* 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<EntityType, Integer> 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 * @param gameMode the gameMode to set
*/ */
public void setGameMode(String gameMode) { public void setGameMode(String gameMode) {
this.gameMode = gameMode; this.gameMode = gameMode;
setChanged(); setChanged();
} }
/* /**
* (non-Javadoc) * @return the entityLimits
*
* @see
* world.bentobox.bentobox.database.objects.DataObject#setUniqueId(java.lang.
* String)
*/ */
@Override public Map<EntityType, Integer> getEntityLimits() {
public void setUniqueId(String uniqueId) { return entityLimits;
this.uniqueId = uniqueId; }
setChanged();
/**
* @param entityLimits the entityLimits to set
*/
public void setEntityLimits(Map<EntityType, Integer> 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<String, Integer> getEntityGroupLimits() {
return entityGroupLimits;
}
/**
* @param entityGroupLimits the entityGroupLimits to set
*/
public void setEntityGroupLimits(Map<String, Integer> 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);
}
/**
* Clear all island-specific entity group limits
*/
public void clearEntityGroupLimits() {
entityGroupLimits.clear();
setChanged();
}
/**
* @return the changed
*/
public boolean isChanged() {
return changed;
}
/**
* @param changed the changed to set
*/
public void setChanged(boolean changed) {
this.changed = changed;
}
/**
* Mark changed
*/
public void setChanged() {
this.changed = true;
}
/**
* @return the blockLimitsOffset
*/
public Map<Material, Integer> getBlockLimitsOffset() {
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
*/
public void setBlockLimitsOffset(Material m, Integer blockLimitsOffset) {
getBlockLimitsOffset().put(m, blockLimitsOffset);
}
/**
* @return the entityLimitsOffset
*/
public Map<EntityType, Integer> getEntityLimitsOffset() {
if (entityLimitsOffset == null) {
entityLimitsOffset = new EnumMap<>(EntityType.class);
}
return entityLimitsOffset;
}
/**
* 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
*/
public void setEntityLimitsOffset(EntityType t, Integer entityLimitsOffset) {
this.getEntityLimitsOffset().put(t, entityLimitsOffset);
}
/**
* @return the entityGroupLimitsOffset
*/
public Map<String, Integer> getEntityGroupLimitsOffset() {
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
* @param entityGroupLimitsOffset the entityGroupLimitsOffset to set
*/
public void setEntityGroupLimitsOffset(String name, Integer entityGroupLimitsOffset) {
getEntityGroupLimitsOffset().put(name, entityGroupLimitsOffset);
} }
} }

View File

@ -1,7 +1,7 @@
name: Limits name: Limits
main: world.bentobox.limits.Limits main: world.bentobox.limits.Limits
version: ${version}${build.number} version: ${version}${build.number}
api-version: 2.0.0 api-version: 1.16.5
authors: tastybento authors: tastybento

View File

@ -1,5 +1,7 @@
package bentobox.addon.limits.listeners; package bentobox.addon.limits.listeners;
import java.util.*;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
@ -9,14 +11,6 @@ import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; 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.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
@ -51,7 +45,7 @@ import world.bentobox.limits.objects.IslandBlockCount;
* *
*/ */
@RunWith(PowerMockRunner.class) @RunWith(PowerMockRunner.class)
@PrepareForTest({ Bukkit.class }) @PrepareForTest( {Bukkit.class} )
public class JoinListenerTest { public class JoinListenerTest {
@Mock @Mock
@ -79,78 +73,74 @@ public class JoinListenerTest {
@Before @Before
public void setUp() { public void setUp() {
jl = new JoinListener(addon); jl = new JoinListener(addon);
// Setup addon // Setup addon
when(addon.getGameModes()).thenReturn(Collections.singletonList(bskyblock)); when(addon.getGameModes()).thenReturn(Collections.singletonList(bskyblock));
when(addon.getGameModeName(any())).thenReturn("bskyblock"); when(addon.getGameModeName(any())).thenReturn("bskyblock");
when(addon.getGameModePermPrefix(any())).thenReturn("bskyblock."); when(addon.getGameModePermPrefix(any())).thenReturn("bskyblock.");
when(addon.getSettings()).thenReturn(settings); when(addon.getSettings()).thenReturn(settings);
// Settings // Settings
when(settings.getGroupLimitDefinitions()) when(settings.getGroupLimitDefinitions()).thenReturn(new ArrayList<>(List.of(new Settings.EntityGroup("friendly", new HashSet<>(), -1))));
.thenReturn(new ArrayList<>(List.of(new Settings.EntityGroup("friendly", new HashSet<>(), -1)))); // Island Manager
// Island Manager when(im.hasIsland(any(), any(UUID.class))).thenReturn(true);
when(island.getUniqueId()).thenReturn("unique_id"); when(island.getUniqueId()).thenReturn("unique_id");
when(im.getIsland(any(), any(UUID.class))).thenReturn(island); 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
// Default is that player has island when(addon.getIslands()).thenReturn(im);
when(addon.getIslands()).thenReturn(im); // Player
// Player when(player.getUniqueId()).thenReturn(UUID.randomUUID());
when(player.getUniqueId()).thenReturn(UUID.randomUUID()); when(player.getName()).thenReturn("tastybento");
when(player.getName()).thenReturn("tastybento"); // No permissions by default
// No permissions by default when(player.getEffectivePermissions()).thenReturn(Collections.emptySet());
when(player.getEffectivePermissions()).thenReturn(Collections.emptySet()); // bsKyBlock
// bsKyBlock when(bskyblock.getPermissionPrefix()).thenReturn("bskyblock.");
when(bskyblock.getPermissionPrefix()).thenReturn("bskyblock."); AddonDescription desc = new AddonDescription.Builder("main", "BSkyBlock", "1.0").build();
AddonDescription desc = new AddonDescription.Builder("main", "BSkyBlock", "1.0").build(); when(bskyblock.getDescription()).thenReturn(desc);
when(bskyblock.getDescription()).thenReturn(desc);
// Block limit listener // Block limit listener
when(addon.getBlockLimitListener()).thenReturn(bll); when(addon.getBlockLimitListener()).thenReturn(bll);
when(bll.getIsland(anyString())).thenReturn(ibc); when(bll.getIsland(anyString())).thenReturn(ibc);
// bukkit // bukkit
PowerMockito.mockStatic(Bukkit.class); PowerMockito.mockStatic(Bukkit.class);
// default is that owner is online // default is that owner is online
when(owner.isOnline()).thenReturn(true); when(owner.isOnline()).thenReturn(true);
when(owner.getPlayer()).thenReturn(player); when(owner.getPlayer()).thenReturn(player);
when(Bukkit.getOfflinePlayer(any(UUID.class))).thenReturn(owner); when(Bukkit.getOfflinePlayer(any(UUID.class))).thenReturn(owner);
when(Bukkit.getPluginManager()).thenReturn(pim); when(Bukkit.getPluginManager()).thenReturn(pim);
// Island // Island
when(island.getOwner()).thenReturn(UUID.randomUUID()); when(island.getOwner()).thenReturn(UUID.randomUUID());
} }
/** /**
* Test method for * Test method for {@link world.bentobox.limits.listeners.JoinListener#onNewIsland(world.bentobox.bentobox.api.events.island.IslandEvent)}.
* {@link world.bentobox.limits.listeners.JoinListener#onNewIsland(world.bentobox.bentobox.api.events.island.IslandEvent)}.
*/ */
@Test @Test
public void testOnNewIslandWrongReason() { public void testOnNewIslandWrongReason() {
IslandEvent e = new IslandEvent(island, null, false, null, IslandEvent.Reason.BAN); IslandEvent e = new IslandEvent(island, null, false, null, IslandEvent.Reason.BAN);
jl.onNewIsland(e); jl.onNewIsland(e);
verify(island, never()).getWorld(); verify(island, never()).getWorld();
} }
/** /**
* Test method for * Test method for {@link world.bentobox.limits.listeners.JoinListener#onNewIsland(world.bentobox.bentobox.api.events.island.IslandEvent)}.
* {@link world.bentobox.limits.listeners.JoinListener#onNewIsland(world.bentobox.bentobox.api.events.island.IslandEvent)}.
*/ */
@Test @Test
public void testOnNewIslandRegistered() { public void testOnNewIslandRegistered() {
IslandEvent e = new IslandEvent(island, null, false, null, IslandEvent.Reason.REGISTERED); IslandEvent e = new IslandEvent(island, null, false, null, IslandEvent.Reason.REGISTERED);
jl.onNewIsland(e); jl.onNewIsland(e);
verify(island).getWorld(); verify(island).getWorld();
} }
/** /**
* Test method for * Test method for {@link world.bentobox.limits.listeners.JoinListener#onNewIsland(world.bentobox.bentobox.api.events.island.IslandEvent)}.
* {@link world.bentobox.limits.listeners.JoinListener#onNewIsland(world.bentobox.bentobox.api.events.island.IslandEvent)}.
*/ */
@Test @Test
public void testOnNewIslandResetted() { public void testOnNewIslandResetted() {
IslandEvent e = new IslandEvent(island, null, false, null, IslandEvent.Reason.RESETTED); IslandEvent e = new IslandEvent(island, null, false, null, IslandEvent.Reason.RESETTED);
jl.onNewIsland(e); jl.onNewIsland(e);
verify(island).getWorld(); verify(island).getWorld();
} }
/** /**
@ -195,228 +185,213 @@ public class JoinListenerTest {
} }
/** /**
* Test method for * Test method for {@link world.bentobox.limits.listeners.JoinListener#onOwnerChange(world.bentobox.bentobox.api.events.team.TeamEvent.TeamSetownerEvent)}.
* {@link world.bentobox.limits.listeners.JoinListener#onOwnerChange(world.bentobox.bentobox.api.events.team.TeamEvent.TeamSetownerEvent)}.
*/ */
@Test @Test
public void testOnOwnerChange() { public void testOnOwnerChange() {
TeamSetownerEvent e = mock(TeamSetownerEvent.class); TeamSetownerEvent e = mock(TeamSetownerEvent.class);
when(e.getIsland()).thenReturn(island); when(e.getIsland()).thenReturn(island);
when(e.getNewOwner()).thenReturn(UUID.randomUUID()); when(e.getNewOwner()).thenReturn(UUID.randomUUID());
jl.onOwnerChange(e); jl.onOwnerChange(e);
verify(e, Mockito.times(2)).getIsland(); verify(e, Mockito.times(2)).getIsland();
verify(e).getNewOwner(); verify(e).getNewOwner();
} }
/** /**
* Test method for * Test method for {@link world.bentobox.limits.listeners.JoinListener#onPlayerJoin(org.bukkit.event.player.PlayerJoinEvent)}.
* {@link world.bentobox.limits.listeners.JoinListener#onPlayerJoin(org.bukkit.event.player.PlayerJoinEvent)}.
*/ */
@Test @Test
public void testOnPlayerJoin() { public void testOnPlayerJoin() {
PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome"); PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome");
jl.onPlayerJoin(e); jl.onPlayerJoin(e);
verify(addon).getGameModes(); verify(addon).getGameModes();
verify(bll).setIsland("unique_id", ibc); verify(bll).setIsland("unique_id", ibc);
} }
/** /**
* Test method for * Test method for {@link world.bentobox.limits.listeners.JoinListener#onPlayerJoin(org.bukkit.event.player.PlayerJoinEvent)}.
* {@link world.bentobox.limits.listeners.JoinListener#onPlayerJoin(org.bukkit.event.player.PlayerJoinEvent)}.
*/ */
@Test @Test
public void testOnPlayerJoinIBCNull() { public void testOnPlayerJoinIBCNull() {
ibc = null; ibc = null;
PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome"); PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome");
jl.onPlayerJoin(e); jl.onPlayerJoin(e);
verify(addon).getGameModes(); verify(addon).getGameModes();
verify(bll, never()).setIsland("unique_id", ibc); verify(bll, never()).setIsland("unique_id", ibc);
} }
/** /**
* Test method for * Test method for {@link world.bentobox.limits.listeners.JoinListener#onPlayerJoin(org.bukkit.event.player.PlayerJoinEvent)}.
* {@link world.bentobox.limits.listeners.JoinListener#onPlayerJoin(org.bukkit.event.player.PlayerJoinEvent)}.
*/ */
@Test @Test
public void testOnPlayerJoinWithPermNotLimits() { public void testOnPlayerJoinWithPermNotLimits() {
Set<PermissionAttachmentInfo> perms = new HashSet<>(); Set<PermissionAttachmentInfo> perms = new HashSet<>();
PermissionAttachmentInfo permAtt = mock(PermissionAttachmentInfo.class); PermissionAttachmentInfo permAtt = mock(PermissionAttachmentInfo.class);
when(permAtt.getPermission()).thenReturn("bskyblock.my.perm.for.game"); when(permAtt.getPermission()).thenReturn("bskyblock.my.perm.for.game");
perms.add(permAtt); perms.add(permAtt);
when(player.getEffectivePermissions()).thenReturn(perms); when(player.getEffectivePermissions()).thenReturn(perms);
PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome"); PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome");
jl.onPlayerJoin(e); jl.onPlayerJoin(e);
verify(addon).getGameModes(); verify(addon).getGameModes();
verify(bll).setIsland("unique_id", ibc); verify(bll).setIsland("unique_id", ibc);
} }
/** /**
* Test method for * Test method for {@link world.bentobox.limits.listeners.JoinListener#onPlayerJoin(org.bukkit.event.player.PlayerJoinEvent)}.
* {@link world.bentobox.limits.listeners.JoinListener#onPlayerJoin(org.bukkit.event.player.PlayerJoinEvent)}.
*/ */
@Test @Test
public void testOnPlayerJoinWithPermLimitsWrongSize() { public void testOnPlayerJoinWithPermLimitsWrongSize() {
Set<PermissionAttachmentInfo> perms = new HashSet<>(); Set<PermissionAttachmentInfo> perms = new HashSet<>();
PermissionAttachmentInfo permAtt = mock(PermissionAttachmentInfo.class); PermissionAttachmentInfo permAtt = mock(PermissionAttachmentInfo.class);
when(permAtt.getPermission()).thenReturn("bskyblock.island.limit.my.perm.for.game"); when(permAtt.getPermission()).thenReturn("bskyblock.island.limit.my.perm.for.game");
when(permAtt.getValue()).thenReturn(true); when(permAtt.getValue()).thenReturn(true);
perms.add(permAtt); perms.add(permAtt);
when(player.getEffectivePermissions()).thenReturn(perms); when(player.getEffectivePermissions()).thenReturn(perms);
PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome"); PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome");
jl.onPlayerJoin(e); jl.onPlayerJoin(e);
verify(addon).logError( 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...");
"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 * Test method for {@link world.bentobox.limits.listeners.JoinListener#onPlayerJoin(org.bukkit.event.player.PlayerJoinEvent)}.
* {@link world.bentobox.limits.listeners.JoinListener#onPlayerJoin(org.bukkit.event.player.PlayerJoinEvent)}.
*/ */
@Test @Test
public void testOnPlayerJoinWithPermLimitsInvalidMaterial() { public void testOnPlayerJoinWithPermLimitsInvalidMaterial() {
Set<PermissionAttachmentInfo> perms = new HashSet<>(); Set<PermissionAttachmentInfo> perms = new HashSet<>();
PermissionAttachmentInfo permAtt = mock(PermissionAttachmentInfo.class); PermissionAttachmentInfo permAtt = mock(PermissionAttachmentInfo.class);
when(permAtt.getPermission()).thenReturn("bskyblock.island.limit.mumbo.34"); when(permAtt.getPermission()).thenReturn("bskyblock.island.limit.mumbo.34");
when(permAtt.getValue()).thenReturn(true); when(permAtt.getValue()).thenReturn(true);
perms.add(permAtt); perms.add(permAtt);
when(player.getEffectivePermissions()).thenReturn(perms); when(player.getEffectivePermissions()).thenReturn(perms);
PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome"); PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome");
jl.onPlayerJoin(e); jl.onPlayerJoin(e);
verify(addon).logError( verify(addon).logError("Player tastybento has permission: 'bskyblock.island.limit.mumbo.34' but MUMBO is not a valid material or entity type/group. Ignoring...");
"Player tastybento has permission: 'bskyblock.island.limit.mumbo.34' but MUMBO is not a valid material or entity type/group. Ignoring...");
} }
/** /**
* Test method for * Test method for {@link world.bentobox.limits.listeners.JoinListener#onPlayerJoin(org.bukkit.event.player.PlayerJoinEvent)}.
* {@link world.bentobox.limits.listeners.JoinListener#onPlayerJoin(org.bukkit.event.player.PlayerJoinEvent)}.
*/ */
@Test @Test
public void testOnPlayerJoinWithPermLimitsWildcard() { public void testOnPlayerJoinWithPermLimitsWildcard() {
Set<PermissionAttachmentInfo> perms = new HashSet<>(); Set<PermissionAttachmentInfo> perms = new HashSet<>();
PermissionAttachmentInfo permAtt = mock(PermissionAttachmentInfo.class); PermissionAttachmentInfo permAtt = mock(PermissionAttachmentInfo.class);
when(permAtt.getPermission()).thenReturn("bskyblock.island.limit.*"); when(permAtt.getPermission()).thenReturn("bskyblock.island.limit.*");
when(permAtt.getValue()).thenReturn(true); when(permAtt.getValue()).thenReturn(true);
perms.add(permAtt); perms.add(permAtt);
when(player.getEffectivePermissions()).thenReturn(perms); when(player.getEffectivePermissions()).thenReturn(perms);
PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome"); PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome");
jl.onPlayerJoin(e); jl.onPlayerJoin(e);
verify(addon).logError( verify(addon).logError("Player tastybento has permission: 'bskyblock.island.limit.*' but wildcards are not allowed. Ignoring...");
"Player tastybento has permission: 'bskyblock.island.limit.*' but wildcards are not allowed. Ignoring...");
} }
/** /**
* Test method for * Test method for {@link world.bentobox.limits.listeners.JoinListener#onPlayerJoin(org.bukkit.event.player.PlayerJoinEvent)}.
* {@link world.bentobox.limits.listeners.JoinListener#onPlayerJoin(org.bukkit.event.player.PlayerJoinEvent)}.
*/ */
@Test @Test
public void testOnPlayerJoinWithPermLimitsNotNumber() { public void testOnPlayerJoinWithPermLimitsNotNumber() {
Set<PermissionAttachmentInfo> perms = new HashSet<>(); Set<PermissionAttachmentInfo> perms = new HashSet<>();
PermissionAttachmentInfo permAtt = mock(PermissionAttachmentInfo.class); PermissionAttachmentInfo permAtt = mock(PermissionAttachmentInfo.class);
when(permAtt.getPermission()).thenReturn("bskyblock.island.limit.STONE.abc"); when(permAtt.getPermission()).thenReturn("bskyblock.island.limit.STONE.abc");
when(permAtt.getValue()).thenReturn(true); when(permAtt.getValue()).thenReturn(true);
perms.add(permAtt); perms.add(permAtt);
when(player.getEffectivePermissions()).thenReturn(perms); when(player.getEffectivePermissions()).thenReturn(perms);
PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome"); PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome");
jl.onPlayerJoin(e); jl.onPlayerJoin(e);
verify(addon).logError( verify(addon).logError("Player tastybento has permission: 'bskyblock.island.limit.STONE.abc' but the last part MUST be an integer! Ignoring...");
"Player tastybento has permission: 'bskyblock.island.limit.STONE.abc' but the last part MUST be an integer! Ignoring...");
} }
/** /**
* Test method for * Test method for {@link world.bentobox.limits.listeners.JoinListener#onPlayerJoin(org.bukkit.event.player.PlayerJoinEvent)}.
* {@link world.bentobox.limits.listeners.JoinListener#onPlayerJoin(org.bukkit.event.player.PlayerJoinEvent)}.
*/ */
@Test @Test
public void testOnPlayerJoinWithPermLimitsSuccess() { public void testOnPlayerJoinWithPermLimitsSuccess() {
Set<PermissionAttachmentInfo> perms = new HashSet<>(); Set<PermissionAttachmentInfo> perms = new HashSet<>();
PermissionAttachmentInfo permAtt = mock(PermissionAttachmentInfo.class); PermissionAttachmentInfo permAtt = mock(PermissionAttachmentInfo.class);
when(permAtt.getPermission()).thenReturn("bskyblock.island.limit.STONE.24"); when(permAtt.getPermission()).thenReturn("bskyblock.island.limit.STONE.24");
when(permAtt.getValue()).thenReturn(true); when(permAtt.getValue()).thenReturn(true);
perms.add(permAtt); perms.add(permAtt);
when(player.getEffectivePermissions()).thenReturn(perms); when(player.getEffectivePermissions()).thenReturn(perms);
PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome"); PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome");
jl.onPlayerJoin(e); jl.onPlayerJoin(e);
verify(addon, never()).logError(anyString()); verify(addon, never()).logError(anyString());
verify(ibc).setBlockLimit(eq(Material.STONE), eq(24)); verify(ibc).setBlockLimit(eq(Material.STONE), eq(24));
} }
/** /**
* Test method for * Test method for {@link world.bentobox.limits.listeners.JoinListener#onPlayerJoin(org.bukkit.event.player.PlayerJoinEvent)}.
* {@link world.bentobox.limits.listeners.JoinListener#onPlayerJoin(org.bukkit.event.player.PlayerJoinEvent)}.
*/ */
@Test @Test
public void testOnPlayerJoinWithPermLimitsSuccessEntity() { public void testOnPlayerJoinWithPermLimitsSuccessEntity() {
Set<PermissionAttachmentInfo> perms = new HashSet<>(); Set<PermissionAttachmentInfo> perms = new HashSet<>();
PermissionAttachmentInfo permAtt = mock(PermissionAttachmentInfo.class); PermissionAttachmentInfo permAtt = mock(PermissionAttachmentInfo.class);
when(permAtt.getPermission()).thenReturn("bskyblock.island.limit.BAT.24"); when(permAtt.getPermission()).thenReturn("bskyblock.island.limit.BAT.24");
when(permAtt.getValue()).thenReturn(true); when(permAtt.getValue()).thenReturn(true);
perms.add(permAtt); perms.add(permAtt);
when(player.getEffectivePermissions()).thenReturn(perms); when(player.getEffectivePermissions()).thenReturn(perms);
PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome"); PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome");
jl.onPlayerJoin(e); jl.onPlayerJoin(e);
verify(addon, never()).logError(anyString()); verify(addon, never()).logError(anyString());
verify(ibc).setEntityLimit(eq(EntityType.BAT), eq(24)); verify(ibc).setEntityLimit(eq(EntityType.BAT), eq(24));
} }
/** /**
* Test method for * Test method for {@link world.bentobox.limits.listeners.JoinListener#onPlayerJoin(org.bukkit.event.player.PlayerJoinEvent)}.
* {@link world.bentobox.limits.listeners.JoinListener#onPlayerJoin(org.bukkit.event.player.PlayerJoinEvent)}.
*/ */
@Test @Test
public void testOnPlayerJoinWithPermLimitsSuccessEntityGroup() { public void testOnPlayerJoinWithPermLimitsSuccessEntityGroup() {
Set<PermissionAttachmentInfo> perms = new HashSet<>(); Set<PermissionAttachmentInfo> perms = new HashSet<>();
PermissionAttachmentInfo permAtt = mock(PermissionAttachmentInfo.class); PermissionAttachmentInfo permAtt = mock(PermissionAttachmentInfo.class);
when(permAtt.getPermission()).thenReturn("bskyblock.island.limit.friendly.24"); when(permAtt.getPermission()).thenReturn("bskyblock.island.limit.friendly.24");
when(permAtt.getValue()).thenReturn(true); when(permAtt.getValue()).thenReturn(true);
perms.add(permAtt); perms.add(permAtt);
when(player.getEffectivePermissions()).thenReturn(perms); when(player.getEffectivePermissions()).thenReturn(perms);
PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome"); PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome");
jl.onPlayerJoin(e); jl.onPlayerJoin(e);
verify(addon, never()).logError(anyString()); verify(addon, never()).logError(anyString());
verify(ibc).setEntityGroupLimit(eq("friendly"), eq(24)); verify(ibc).setEntityGroupLimit(eq("friendly"), eq(24));
} }
/** /**
* Test method for * Test method for {@link world.bentobox.limits.listeners.JoinListener#onPlayerJoin(org.bukkit.event.player.PlayerJoinEvent)}.
* {@link world.bentobox.limits.listeners.JoinListener#onPlayerJoin(org.bukkit.event.player.PlayerJoinEvent)}.
*/ */
@Test @Test
public void testOnPlayerJoinWithPermLimitsMultiPerms() { public void testOnPlayerJoinWithPermLimitsMultiPerms() {
Set<PermissionAttachmentInfo> perms = new HashSet<>(); Set<PermissionAttachmentInfo> perms = new HashSet<>();
PermissionAttachmentInfo permAtt = mock(PermissionAttachmentInfo.class); PermissionAttachmentInfo permAtt = mock(PermissionAttachmentInfo.class);
when(permAtt.getPermission()).thenReturn("bskyblock.island.limit.STONE.24"); when(permAtt.getPermission()).thenReturn("bskyblock.island.limit.STONE.24");
when(permAtt.getValue()).thenReturn(true); when(permAtt.getValue()).thenReturn(true);
perms.add(permAtt); perms.add(permAtt);
PermissionAttachmentInfo permAtt2 = mock(PermissionAttachmentInfo.class); PermissionAttachmentInfo permAtt2 = mock(PermissionAttachmentInfo.class);
when(permAtt2.getPermission()).thenReturn("bskyblock.island.limit.short_grass.14"); when(permAtt2.getPermission()).thenReturn("bskyblock.island.limit.grass.14");
when(permAtt2.getValue()).thenReturn(true); when(permAtt2.getValue()).thenReturn(true);
perms.add(permAtt2); perms.add(permAtt2);
PermissionAttachmentInfo permAtt3 = mock(PermissionAttachmentInfo.class); PermissionAttachmentInfo permAtt3 = mock(PermissionAttachmentInfo.class);
when(permAtt3.getPermission()).thenReturn("bskyblock.island.limit.dirt.34"); when(permAtt3.getPermission()).thenReturn("bskyblock.island.limit.dirt.34");
when(permAtt3.getValue()).thenReturn(true); when(permAtt3.getValue()).thenReturn(true);
perms.add(permAtt3); perms.add(permAtt3);
PermissionAttachmentInfo permAtt4 = mock(PermissionAttachmentInfo.class); PermissionAttachmentInfo permAtt4 = mock(PermissionAttachmentInfo.class);
when(permAtt4.getPermission()).thenReturn("bskyblock.island.limit.chicken.34"); when(permAtt4.getPermission()).thenReturn("bskyblock.island.limit.chicken.34");
when(permAtt4.getValue()).thenReturn(true); when(permAtt4.getValue()).thenReturn(true);
perms.add(permAtt4); perms.add(permAtt4);
PermissionAttachmentInfo permAtt5 = mock(PermissionAttachmentInfo.class); PermissionAttachmentInfo permAtt5 = mock(PermissionAttachmentInfo.class);
when(permAtt5.getPermission()).thenReturn("bskyblock.island.limit.cave_spider.4"); when(permAtt5.getPermission()).thenReturn("bskyblock.island.limit.cave_spider.4");
when(permAtt5.getValue()).thenReturn(true); when(permAtt5.getValue()).thenReturn(true);
perms.add(permAtt5); perms.add(permAtt5);
PermissionAttachmentInfo permAtt6 = mock(PermissionAttachmentInfo.class); PermissionAttachmentInfo permAtt6 = mock(PermissionAttachmentInfo.class);
when(permAtt6.getPermission()).thenReturn("bskyblock.island.limit.cave_spider.4"); when(permAtt6.getPermission()).thenReturn("bskyblock.island.limit.cave_spider.4");
when(permAtt6.getValue()).thenReturn(false); // negative perm when(permAtt6.getValue()).thenReturn(false); // negative perm
perms.add(permAtt6); perms.add(permAtt6);
when(player.getEffectivePermissions()).thenReturn(perms); when(player.getEffectivePermissions()).thenReturn(perms);
PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome"); PlayerJoinEvent e = new PlayerJoinEvent(player, "welcome");
jl.onPlayerJoin(e); jl.onPlayerJoin(e);
verify(addon, never()).logError(anyString()); verify(addon, never()).logError(anyString());
verify(ibc).setBlockLimit(eq(Material.STONE), eq(24)); verify(ibc).setBlockLimit(eq(Material.STONE), eq(24));
verify(ibc).setBlockLimit(eq(Material.SHORT_GRASS), eq(14)); verify(ibc).setBlockLimit(eq(Material.GRASS), eq(14));
verify(ibc).setBlockLimit(eq(Material.DIRT), eq(34)); verify(ibc).setBlockLimit(eq(Material.DIRT), eq(34));
verify(ibc).setEntityLimit(eq(EntityType.CHICKEN), eq(34)); verify(ibc).setEntityLimit(eq(EntityType.CHICKEN), eq(34));
verify(ibc).setEntityLimit(eq(EntityType.CAVE_SPIDER), eq(4)); verify(ibc).setEntityLimit(eq(EntityType.CAVE_SPIDER), eq(4));
} }
/** /**
@ -480,43 +455,40 @@ public class JoinListenerTest {
} }
/** /**
* Test method for * Test method for {@link world.bentobox.limits.listeners.JoinListener#onUnregisterIsland(world.bentobox.bentobox.api.events.island.IslandEvent)}.
* {@link world.bentobox.limits.listeners.JoinListener#onUnregisterIsland(world.bentobox.bentobox.api.events.island.IslandEvent)}.
*/ */
@Test @Test
public void testOnUnregisterIslandNotUnregistered() { public void testOnUnregisterIslandNotUnregistered() {
IslandEvent e = new IslandEvent(island, null, false, null, IslandEvent.Reason.BAN); IslandEvent e = new IslandEvent(island, null, false, null, IslandEvent.Reason.BAN);
jl.onUnregisterIsland(e); jl.onUnregisterIsland(e);
verify(island, never()).getWorld(); verify(island, never()).getWorld();
} }
/** /**
* Test method for * Test method for {@link world.bentobox.limits.listeners.JoinListener#onUnregisterIsland(world.bentobox.bentobox.api.events.island.IslandEvent)}.
* {@link world.bentobox.limits.listeners.JoinListener#onUnregisterIsland(world.bentobox.bentobox.api.events.island.IslandEvent)}.
*/ */
@Test @Test
public void testOnUnregisterIslandNotInWorld() { public void testOnUnregisterIslandNotInWorld() {
IslandEvent e = new IslandEvent(island, null, false, null, IslandEvent.Reason.UNREGISTERED); IslandEvent e = new IslandEvent(island, null, false, null, IslandEvent.Reason.UNREGISTERED);
jl.onUnregisterIsland(e); jl.onUnregisterIsland(e);
verify(island).getWorld(); verify(island).getWorld();
verify(addon, never()).getBlockLimitListener(); verify(addon, never()).getBlockLimitListener();
} }
/** /**
* Test method for * Test method for {@link world.bentobox.limits.listeners.JoinListener#onUnregisterIsland(world.bentobox.bentobox.api.events.island.IslandEvent)}.
* {@link world.bentobox.limits.listeners.JoinListener#onUnregisterIsland(world.bentobox.bentobox.api.events.island.IslandEvent)}.
*/ */
@Test @Test
public void testOnUnregisterIslandInWorld() { public void testOnUnregisterIslandInWorld() {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<Material, Integer> map = mock(Map.class); Map<Material, Integer> map = mock(Map.class);
when(ibc.getBlockLimits()).thenReturn(map); when(ibc.getBlockLimits()).thenReturn(map);
when(addon.inGameModeWorld(any())).thenReturn(true); when(addon.inGameModeWorld(any())).thenReturn(true);
IslandEvent e = new IslandEvent(island, null, false, null, IslandEvent.Reason.UNREGISTERED); IslandEvent e = new IslandEvent(island, null, false, null, IslandEvent.Reason.UNREGISTERED);
jl.onUnregisterIsland(e); jl.onUnregisterIsland(e);
verify(island).getWorld(); verify(island).getWorld();
verify(addon).getBlockLimitListener(); verify(addon).getBlockLimitListener();
verify(map).clear(); verify(map).clear();
} }
@ -538,4 +510,6 @@ public class JoinListenerTest {
} }
} }