Skip to content

Commit

Permalink
Added API to enable offset for limits.
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Sep 7, 2021
1 parent ad1f060 commit 76c582b
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/main/java/world/bentobox/limits/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ private EntityType getType(String key) {
}

/**
* @return the limits
* @return the entity limits
*/
public Map<EntityType, Integer> getLimits() {
return Collections.unmodifiableMap(limits);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/world/bentobox/limits/commands/LimitsCalc.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public class LimitsCalc {
});

}



private void asyncScan(World world2, Pair<Integer, Integer> c) {
Expand Down Expand Up @@ -141,7 +141,7 @@ private Set<Pair<Integer, Integer>> getChunksToScan(Island island) {

private void tidyUp() {
if (ibc == null) {
ibc = new IslandBlockCount();
ibc = new IslandBlockCount(island.getUniqueId(), plugin.getIWM().getAddon(world).map(a -> a.getDescription().getName()).orElse("default"));
}
ibc.setBlockCounts(blockCount.entrySet().stream()
.collect(Collectors.toMap(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,6 @@ private void processIsland(Cancellable c, LivingEntity e, Location l, SpawnReaso
addon.getIslands().getIslandAt(e.getLocation()).ifPresent(island -> {
// Check if creature is allowed to spawn or not
AtLimitResult res = atLimit(island, e);

if (bypass || island.isSpawn() || !res.hit()) {
// Allowed
if (async) {
Expand Down Expand Up @@ -433,25 +432,25 @@ AtLimitResult atLimit(Island island, Entity ent) {
.forEach(group -> groupsLimits.put(group, group.getLimit()));
}
if (limitAmount < 0 && groupsLimits.isEmpty()) return new AtLimitResult();

// We have to count the entities
if (limitAmount >= 0)
{
int count = (int) ent.getWorld().getEntities().stream()
.filter(e -> e.getType().equals(ent.getType()))
.filter(e -> island.inIslandSpace(e.getLocation()))
.count();
if (count >= limitAmount)
if (count >= limitAmount) {
return new AtLimitResult(ent.getType(), limitAmount);
}
}

// Merge in any permission-based limits
if (addon.getBlockLimitListener().getIsland(island.getUniqueId()) != null) {
Map<String, EntityGroup> groupbyname = groupsLimits.keySet().stream().collect(Collectors.toMap(e -> e.getName(), e -> e));
addon.getBlockLimitListener().getIsland(island.getUniqueId()).getEntityGroupLimits().entrySet().stream()
.filter(e -> groupbyname.containsKey(e.getKey()))
.forEach(e -> groupsLimits.put(groupbyname.get(e.getKey()), e.getValue()));
}

// Now do the group limits
for (Map.Entry<Settings.EntityGroup, Integer> group : groupsLimits.entrySet()) { //do not use lambda
if (group.getValue() < 0)
Expand Down
86 changes: 73 additions & 13 deletions src/main/java/world/bentobox/limits/objects/IslandBlockCount.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class IslandBlockCount implements DataObject {
private Map<Material, Integer> blockCounts = new EnumMap<>(Material.class);

private boolean changed;

/**
* Permission based limits
*/
Expand All @@ -39,9 +39,12 @@ public class IslandBlockCount implements DataObject {
private Map<EntityType, Integer> entityLimits = new EnumMap<>(EntityType.class);
@Expose
private Map<String, Integer> entityGroupLimits = new HashMap<>();

// Required for YAML database
public IslandBlockCount() {}
@Expose
private Map<Material, Integer> blockLimitsOffset = new EnumMap<>(Material.class);
@Expose
private Map<EntityType, Integer> entityLimitsOffset = new EnumMap<>(EntityType.class);
@Expose
private Map<String, Integer> entityGroupLimitsOffset = new HashMap<>();

/**
* Create an island block count object
Expand Down Expand Up @@ -131,7 +134,7 @@ public boolean isAtLimit(Material material, int limit) {
*/
public boolean isAtLimit(Material m) {
// Check island limits first
return blockLimits.containsKey(m) && blockCounts.getOrDefault(m, 0) >= blockLimits.get(m);
return blockLimits.containsKey(m) && blockCounts.getOrDefault(m, 0) >= getBlockLimit(m);
}

public boolean isBlockLimited(Material m) {
Expand Down Expand Up @@ -159,7 +162,7 @@ public void setBlockLimits(Map<Material, Integer> blockLimits) {
* @return limit or -1 for unlimited
*/
public Integer getBlockLimit(Material m) {
return blockLimits.getOrDefault(m, -1);
return blockLimits.getOrDefault(m, -1) + getBlockLimitsOffset().getOrDefault(m, 0);
}

/**
Expand Down Expand Up @@ -222,7 +225,7 @@ public void setEntityLimit(EntityType t, int limit) {
* @return limit or -1 for unlimited
*/
public int getEntityLimit(EntityType t) {
return entityLimits.getOrDefault(t, -1);
return entityLimits.getOrDefault(t, -1) + getEntityLimitsOffset().getOrDefault(t, 0);
}

/**
Expand All @@ -232,7 +235,7 @@ public void clearEntityLimits() {
entityLimits.clear();
setChanged();
}

/**
* @return the entityGroupLimits
*/
Expand All @@ -247,7 +250,7 @@ public void setEntityGroupLimits(Map<String, Integer> entityGroupLimits) {
this.entityGroupLimits = entityGroupLimits;
setChanged();
}

/**
* Set an island-specific entity group limit
* @param name - entity group
Expand All @@ -257,16 +260,16 @@ 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);
return entityGroupLimits.getOrDefault(name, -1) + getEntityLimitsOffset().getOrDefault(name, 0);
}

/**
* Clear all island-specific entity group limits
*/
Expand All @@ -288,11 +291,68 @@ public boolean isChanged() {
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);
}
}

0 comments on commit 76c582b

Please sign in to comment.