Skip to content

Commit

Permalink
Rework how offsets are used.
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Jan 3, 2022
1 parent d038269 commit 360ba0b
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,13 @@ public void onBlock(BlockMultiPlaceEvent e) {
notify(e, User.getInstance(e.getPlayer()), process(e.getBlock(), true), e.getBlock().getType());
}

/**
* Cancel the event and notify the user of failure
* @param e event
* @param user user
* @param limit maximum limit allowed
* @param m material
*/
private void notify(Cancellable e, User user, int limit, Material m) {
if (limit > -1) {
user.notify("block-limits.hit-limit",
Expand Down Expand Up @@ -389,18 +396,18 @@ private void updateSaveMap(String id) {
*/
private int checkLimit(World w, Material m, String id) {
// Check island limits
IslandBlockCount island = islandCountMap.get(id);
if (island.isBlockLimited(m)) {
return island.isAtLimit(m) ? island.getBlockLimit(m) : -1;
IslandBlockCount ibc = islandCountMap.get(id);
if (ibc.isBlockLimited(m)) {
return ibc.isAtLimit(m) ? ibc.getBlockLimit(m) + ibc.getBlockLimitOffset(m) : -1;
}
// Check specific world limits
if (worldLimitMap.containsKey(w) && worldLimitMap.get(w).containsKey(m)) {
// Material is overridden in world
return island.isAtLimit(m, worldLimitMap.get(w).get(m)) ? worldLimitMap.get(w).get(m) : -1;
return ibc.isAtLimit(m, worldLimitMap.get(w).get(m)) ? worldLimitMap.get(w).get(m) + ibc.getBlockLimitOffset(m) : -1;
}
// Check default limit map
if (defaultLimitMap.containsKey(m) && island.isAtLimit(m, defaultLimitMap.get(m))) {
return defaultLimitMap.get(m);
if (defaultLimitMap.containsKey(m) && ibc.isAtLimit(m, defaultLimitMap.get(m))) {
return defaultLimitMap.get(m) + ibc.getBlockLimitOffset(m);
}
// No limit
return -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.bukkit.event.entity.EntityBreedEvent;
import org.bukkit.event.hanging.HangingPlaceEvent;
import org.bukkit.event.vehicle.VehicleCreateEvent;
import org.eclipse.jdt.annotation.Nullable;

import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.localization.TextVariables;
Expand All @@ -39,6 +40,7 @@
import world.bentobox.limits.Limits;
import world.bentobox.limits.Settings;
import world.bentobox.limits.Settings.EntityGroup;
import world.bentobox.limits.objects.IslandBlockCount;

public class EntityLimitListener implements Listener {
private static final String MOD_BYPASS = "mod.bypass";
Expand Down Expand Up @@ -369,11 +371,16 @@ AtLimitResult atLimit(Island island, Entity ent) {
// Check island settings first
int limitAmount = -1;
Map<Settings.EntityGroup, Integer> groupsLimits = new HashMap<>();
if (addon.getBlockLimitListener().getIsland(island.getUniqueId()) != null) {
limitAmount = addon.getBlockLimitListener().getIsland(island.getUniqueId()).getEntityLimit(ent.getType());

@Nullable
IslandBlockCount ibc = addon.getBlockLimitListener().getIsland(island.getUniqueId());
if (ibc != null) {
// Get the limit amount for this type
limitAmount = ibc.getEntityLimit(ent.getType());
// Handle entity groups
List<Settings.EntityGroup> groupdefs = addon.getSettings().getGroupLimits().getOrDefault(ent.getType(), new ArrayList<>());
groupdefs.forEach(def -> {
int limit = addon.getBlockLimitListener().getIsland(island.getUniqueId()).getEntityGroupLimit(def.getName());
int limit = ibc.getEntityGroupLimit(def.getName());
if (limit >= 0)
groupsLimits.put(def, limit);
});
Expand All @@ -382,12 +389,15 @@ AtLimitResult atLimit(Island island, Entity ent) {
if (limitAmount < 0 && addon.getSettings().getLimits().containsKey(ent.getType())) {
limitAmount = addon.getSettings().getLimits().get(ent.getType());
}
// Group limits
if (addon.getSettings().getGroupLimits().containsKey(ent.getType())) {
addon.getSettings().getGroupLimits().getOrDefault(ent.getType(), new ArrayList<>()).stream()
.filter(group -> !groupsLimits.containsKey(group) || groupsLimits.get(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
if (limitAmount >= 0)
Expand All @@ -396,14 +406,16 @@ AtLimitResult atLimit(Island island, Entity ent) {
.filter(e -> e.getType().equals(ent.getType()))
.filter(e -> island.inIslandSpace(e.getLocation()))
.count();
if (count >= limitAmount) {
return new AtLimitResult(ent.getType(), limitAmount);
int max = limitAmount + ibc.getEntityLimitOffset(ent.getType());
if (count >= max) {
return new AtLimitResult(ent.getType(), max);
}
}
// Merge in any permission-based limits
if (addon.getBlockLimitListener().getIsland(island.getUniqueId()) != null) {
Map<String, EntityGroup> groupbyname = groupsLimits.keySet().stream().collect(Collectors.toMap(EntityGroup::getName, e -> e));
addon.getBlockLimitListener().getIsland(island.getUniqueId()).getEntityGroupLimits().entrySet().stream()
// Group limits
if (ibc != null) {
Map<String, EntityGroup> groupbyname = groupsLimits.keySet().stream()
.collect(Collectors.toMap(EntityGroup::getName, e -> e));
ibc.getEntityGroupLimits().entrySet().stream()
.filter(e -> groupbyname.containsKey(e.getKey()))
.forEach(e -> groupsLimits.put(groupbyname.get(e.getKey()), e.getValue()));
}
Expand All @@ -414,8 +426,10 @@ AtLimitResult atLimit(Island island, Entity ent) {
int count = (int) ent.getWorld().getEntities().stream()
.filter(e -> group.getKey().contains(e.getType()))
.filter(e -> island.inIslandSpace(e.getLocation())).count();
if (count >= group.getValue())
return new AtLimitResult(group.getKey(), group.getValue());
int max = group.getValue() + ibc.getEntityGroupLimitOffset(group.getKey().getName());
if (count >= max) {
return new AtLimitResult(group.getKey(), max);
}
}
return new AtLimitResult();
}
Expand Down
39 changes: 33 additions & 6 deletions src/main/java/world/bentobox/limits/objects/IslandBlockCount.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public void remove(Material material) {
* @return true if count is >= limit
*/
public boolean isAtLimit(Material material, int limit) {
return blockCounts.getOrDefault(material, 0) >= limit;
return blockCounts.getOrDefault(material, 0) >= limit + this.getBlockLimitOffset(material);
}

/**
Expand All @@ -137,7 +137,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) >= getBlockLimit(m);
return blockLimits.containsKey(m) && blockCounts.getOrDefault(m, 0) >= getBlockLimit(m) + this.getBlockLimitOffset(m);
}

public boolean isBlockLimited(Material m) {
Expand All @@ -164,8 +164,17 @@ public void setBlockLimits(Map<Material, Integer> blockLimits) {
* @param m - material
* @return limit or -1 for unlimited
*/
public Integer getBlockLimit(Material m) {
return blockLimits.getOrDefault(m, -1) + getBlockLimitsOffset().getOrDefault(m, 0);
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);
}

/**
Expand Down Expand Up @@ -228,7 +237,16 @@ public void setEntityLimit(EntityType t, int limit) {
* @return limit or -1 for unlimited
*/
public int getEntityLimit(EntityType t) {
return entityLimits.getOrDefault(t, -1) + getEntityLimitsOffset().getOrDefault(t, 0);
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);
}

/**
Expand Down Expand Up @@ -270,7 +288,16 @@ public void setEntityGroupLimit(String name, int limit) {
* @return limit or -1 for unlimited
*/
public int getEntityGroupLimit(String name) {
return entityGroupLimits.getOrDefault(name, -1) + getEntityGroupLimitsOffset().getOrDefault(name, 0);
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);
}

/**
Expand Down

0 comments on commit 360ba0b

Please sign in to comment.