Added API to enable offset for limits.

This commit is contained in:
tastybento 2021-09-06 17:11:02 -07:00
parent ad1f060d76
commit 76c582ba1d
4 changed files with 79 additions and 20 deletions

View File

@ -110,7 +110,7 @@ public class Settings {
} }
/** /**
* @return the limits * @return the entity limits
*/ */
public Map<EntityType, Integer> getLimits() { public Map<EntityType, Integer> getLimits() {
return Collections.unmodifiableMap(limits); return Collections.unmodifiableMap(limits);

View File

@ -76,7 +76,7 @@ public class LimitsCalc {
}); });
} }
private void asyncScan(World world2, Pair<Integer, Integer> c) { private void asyncScan(World world2, Pair<Integer, Integer> c) {
@ -141,7 +141,7 @@ public class LimitsCalc {
private void tidyUp() { private void tidyUp() {
if (ibc == null) { 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() ibc.setBlockCounts(blockCount.entrySet().stream()
.collect(Collectors.toMap( .collect(Collectors.toMap(

View File

@ -220,7 +220,6 @@ public class EntityLimitListener implements Listener {
addon.getIslands().getIslandAt(e.getLocation()).ifPresent(island -> { addon.getIslands().getIslandAt(e.getLocation()).ifPresent(island -> {
// Check if creature is allowed to spawn or not // Check if creature is allowed to spawn or not
AtLimitResult res = atLimit(island, e); AtLimitResult res = atLimit(island, e);
if (bypass || island.isSpawn() || !res.hit()) { if (bypass || island.isSpawn() || !res.hit()) {
// Allowed // Allowed
if (async) { if (async) {
@ -433,6 +432,7 @@ public class EntityLimitListener implements Listener {
.forEach(group -> groupsLimits.put(group, group.getLimit())); .forEach(group -> groupsLimits.put(group, group.getLimit()));
} }
if (limitAmount < 0 && groupsLimits.isEmpty()) return new AtLimitResult(); if (limitAmount < 0 && groupsLimits.isEmpty()) return new AtLimitResult();
// We have to count the entities // We have to count the entities
if (limitAmount >= 0) if (limitAmount >= 0)
{ {
@ -440,10 +440,10 @@ public class EntityLimitListener implements Listener {
.filter(e -> e.getType().equals(ent.getType())) .filter(e -> e.getType().equals(ent.getType()))
.filter(e -> island.inIslandSpace(e.getLocation())) .filter(e -> island.inIslandSpace(e.getLocation()))
.count(); .count();
if (count >= limitAmount) if (count >= limitAmount) {
return new AtLimitResult(ent.getType(), limitAmount); return new AtLimitResult(ent.getType(), limitAmount);
}
} }
// Merge in any permission-based limits // Merge in any permission-based limits
if (addon.getBlockLimitListener().getIsland(island.getUniqueId()) != null) { if (addon.getBlockLimitListener().getIsland(island.getUniqueId()) != null) {
Map<String, EntityGroup> groupbyname = groupsLimits.keySet().stream().collect(Collectors.toMap(e -> e.getName(), e -> e)); Map<String, EntityGroup> groupbyname = groupsLimits.keySet().stream().collect(Collectors.toMap(e -> e.getName(), e -> e));
@ -451,7 +451,6 @@ public class EntityLimitListener implements Listener {
.filter(e -> groupbyname.containsKey(e.getKey())) .filter(e -> groupbyname.containsKey(e.getKey()))
.forEach(e -> groupsLimits.put(groupbyname.get(e.getKey()), e.getValue())); .forEach(e -> groupsLimits.put(groupbyname.get(e.getKey()), e.getValue()));
} }
// Now do the group limits // Now do the group limits
for (Map.Entry<Settings.EntityGroup, Integer> group : groupsLimits.entrySet()) { //do not use lambda for (Map.Entry<Settings.EntityGroup, Integer> group : groupsLimits.entrySet()) { //do not use lambda
if (group.getValue() < 0) if (group.getValue() < 0)

View File

@ -29,7 +29,7 @@ public class IslandBlockCount implements DataObject {
private Map<Material, Integer> blockCounts = new EnumMap<>(Material.class); private Map<Material, Integer> blockCounts = new EnumMap<>(Material.class);
private boolean changed; private boolean changed;
/** /**
* Permission based limits * Permission based limits
*/ */
@ -39,9 +39,12 @@ public class IslandBlockCount implements DataObject {
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<>(); private Map<String, Integer> entityGroupLimits = new HashMap<>();
@Expose
// Required for YAML database private Map<Material, Integer> blockLimitsOffset = new EnumMap<>(Material.class);
public IslandBlockCount() {} @Expose
private Map<EntityType, Integer> entityLimitsOffset = new EnumMap<>(EntityType.class);
@Expose
private Map<String, Integer> entityGroupLimitsOffset = new HashMap<>();
/** /**
* Create an island block count object * Create an island block count object
@ -131,7 +134,7 @@ public class IslandBlockCount implements DataObject {
*/ */
public boolean isAtLimit(Material m) { public boolean isAtLimit(Material m) {
// Check island limits first // 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) { public boolean isBlockLimited(Material m) {
@ -159,7 +162,7 @@ public class IslandBlockCount implements DataObject {
* @return limit or -1 for unlimited * @return limit or -1 for unlimited
*/ */
public Integer getBlockLimit(Material m) { public Integer getBlockLimit(Material m) {
return blockLimits.getOrDefault(m, -1); return blockLimits.getOrDefault(m, -1) + getBlockLimitsOffset().getOrDefault(m, 0);
} }
/** /**
@ -222,7 +225,7 @@ public class IslandBlockCount implements DataObject {
* @return limit or -1 for unlimited * @return limit or -1 for unlimited
*/ */
public int getEntityLimit(EntityType t) { public int getEntityLimit(EntityType t) {
return entityLimits.getOrDefault(t, -1); return entityLimits.getOrDefault(t, -1) + getEntityLimitsOffset().getOrDefault(t, 0);
} }
/** /**
@ -232,7 +235,7 @@ public class IslandBlockCount implements DataObject {
entityLimits.clear(); entityLimits.clear();
setChanged(); setChanged();
} }
/** /**
* @return the entityGroupLimits * @return the entityGroupLimits
*/ */
@ -247,7 +250,7 @@ public class IslandBlockCount implements DataObject {
this.entityGroupLimits = entityGroupLimits; this.entityGroupLimits = entityGroupLimits;
setChanged(); setChanged();
} }
/** /**
* Set an island-specific entity group limit * Set an island-specific entity group limit
* @param name - entity group * @param name - entity group
@ -257,16 +260,16 @@ public class IslandBlockCount implements DataObject {
entityGroupLimits.put(name, limit); entityGroupLimits.put(name, limit);
setChanged(); setChanged();
} }
/** /**
* Get the limit for an entity group * Get the limit for an entity group
* @param name - entity group * @param name - entity group
* @return limit or -1 for unlimited * @return limit or -1 for unlimited
*/ */
public int getEntityGroupLimit(String name) { 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 * Clear all island-specific entity group limits
*/ */
@ -288,11 +291,68 @@ public class IslandBlockCount implements DataObject {
public void setChanged(boolean changed) { public void setChanged(boolean changed) {
this.changed = changed; this.changed = changed;
} }
/** /**
* Mark changed * Mark changed
*/ */
public void setChanged() { public void setChanged() {
this.changed = true; 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);
}
} }