From 76c582ba1d5693d86be629062b05a5857a8dddb8 Mon Sep 17 00:00:00 2001 From: tastybento Date: Mon, 6 Sep 2021 17:11:02 -0700 Subject: [PATCH] Added API to enable offset for limits. --- .../java/world/bentobox/limits/Settings.java | 2 +- .../bentobox/limits/commands/LimitsCalc.java | 4 +- .../limits/listeners/EntityLimitListener.java | 7 +- .../limits/objects/IslandBlockCount.java | 86 ++++++++++++++++--- 4 files changed, 79 insertions(+), 20 deletions(-) diff --git a/src/main/java/world/bentobox/limits/Settings.java b/src/main/java/world/bentobox/limits/Settings.java index c97bf99..cd48181 100644 --- a/src/main/java/world/bentobox/limits/Settings.java +++ b/src/main/java/world/bentobox/limits/Settings.java @@ -110,7 +110,7 @@ public class Settings { } /** - * @return the limits + * @return the entity limits */ public Map getLimits() { return Collections.unmodifiableMap(limits); diff --git a/src/main/java/world/bentobox/limits/commands/LimitsCalc.java b/src/main/java/world/bentobox/limits/commands/LimitsCalc.java index 24f6c65..d6329b8 100644 --- a/src/main/java/world/bentobox/limits/commands/LimitsCalc.java +++ b/src/main/java/world/bentobox/limits/commands/LimitsCalc.java @@ -76,7 +76,7 @@ public class LimitsCalc { }); } - + private void asyncScan(World world2, Pair c) { @@ -141,7 +141,7 @@ public class LimitsCalc { 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( diff --git a/src/main/java/world/bentobox/limits/listeners/EntityLimitListener.java b/src/main/java/world/bentobox/limits/listeners/EntityLimitListener.java index 85f1b4b..00f2c79 100644 --- a/src/main/java/world/bentobox/limits/listeners/EntityLimitListener.java +++ b/src/main/java/world/bentobox/limits/listeners/EntityLimitListener.java @@ -220,7 +220,6 @@ public class EntityLimitListener implements Listener { 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) { @@ -433,6 +432,7 @@ public class EntityLimitListener implements Listener { .forEach(group -> groupsLimits.put(group, group.getLimit())); } if (limitAmount < 0 && groupsLimits.isEmpty()) return new AtLimitResult(); + // We have to count the entities if (limitAmount >= 0) { @@ -440,10 +440,10 @@ public class EntityLimitListener implements Listener { .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 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())) .forEach(e -> groupsLimits.put(groupbyname.get(e.getKey()), e.getValue())); } - // Now do the group limits for (Map.Entry group : groupsLimits.entrySet()) { //do not use lambda if (group.getValue() < 0) diff --git a/src/main/java/world/bentobox/limits/objects/IslandBlockCount.java b/src/main/java/world/bentobox/limits/objects/IslandBlockCount.java index 3a357ea..7c6fd4f 100644 --- a/src/main/java/world/bentobox/limits/objects/IslandBlockCount.java +++ b/src/main/java/world/bentobox/limits/objects/IslandBlockCount.java @@ -29,7 +29,7 @@ public class IslandBlockCount implements DataObject { private Map blockCounts = new EnumMap<>(Material.class); private boolean changed; - + /** * Permission based limits */ @@ -39,9 +39,12 @@ public class IslandBlockCount implements DataObject { private Map entityLimits = new EnumMap<>(EntityType.class); @Expose private Map entityGroupLimits = new HashMap<>(); - - // Required for YAML database - public IslandBlockCount() {} + @Expose + private Map blockLimitsOffset = new EnumMap<>(Material.class); + @Expose + private Map entityLimitsOffset = new EnumMap<>(EntityType.class); + @Expose + private Map entityGroupLimitsOffset = new HashMap<>(); /** * Create an island block count object @@ -131,7 +134,7 @@ public class IslandBlockCount implements DataObject { */ 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) { @@ -159,7 +162,7 @@ public class IslandBlockCount implements DataObject { * @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); } /** @@ -222,7 +225,7 @@ public class IslandBlockCount implements DataObject { * @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); } /** @@ -232,7 +235,7 @@ public class IslandBlockCount implements DataObject { entityLimits.clear(); setChanged(); } - + /** * @return the entityGroupLimits */ @@ -247,7 +250,7 @@ public class IslandBlockCount implements DataObject { this.entityGroupLimits = entityGroupLimits; setChanged(); } - + /** * Set an island-specific entity group limit * @param name - entity group @@ -257,16 +260,16 @@ public class IslandBlockCount implements DataObject { 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 */ @@ -288,11 +291,68 @@ public class IslandBlockCount implements DataObject { public void setChanged(boolean changed) { this.changed = changed; } - + /** * Mark changed */ public void setChanged() { this.changed = true; } + + /** + * @return the blockLimitsOffset + */ + public Map 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 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 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); + } }