Skip to content

Commit

Permalink
added entity group limit settings
Browse files Browse the repository at this point in the history
  • Loading branch information
weaondara committed Apr 12, 2020
1 parent 086d251 commit e0c208e
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 3 deletions.
66 changes: 66 additions & 0 deletions src/main/java/world/bentobox/limits/Settings.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package world.bentobox.limits;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.EnumMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.EntityType;

public class Settings {

private final Map<EntityType, Integer> limits = new EnumMap<>(EntityType.class);
private final Map<EntityType, List<EntityGroup>> groupLimits = new EnumMap<>(EntityType.class);
private final List<String> gameModes;
private static final List<EntityType> DISALLOWED = Arrays.asList(
EntityType.PRIMED_TNT,
Expand Down Expand Up @@ -60,6 +65,33 @@ public Settings(Limits addon) {
}
addon.log("Entity limits:");
limits.entrySet().stream().map(e -> "Limit " + e.getKey().toString() + " to " + e.getValue()).forEach(addon::log);

//group limits
el = addon.getConfig().getConfigurationSection("entitygrouplimits");
if (el != null) {
for (String name : el.getKeys(false)) {
int limit = el.getInt(name + ".limit");
Set<EntityType> entities = el.getStringList(name + ".entities").stream().map(s -> {
EntityType type = getType(s);
if (type != null) {
if (DISALLOWED.contains(type)) {
addon.logError("Entity type: " + s + " is not supported - skipping...");
} else {
return type;
}
} else {
addon.logError("Unknown entity type: " + s + " - skipping...");
}
return null;
}).filter(e -> e != null).collect(Collectors.toSet());
EntityGroup group = new EntityGroup(name, entities, limit);
entities.forEach(e -> {
List<EntityGroup> groups = groupLimits.getOrDefault(e, new ArrayList());
groups.add(group);
groupLimits.put(e, groups);
});
}
}
}

private EntityType getType(String key) {
Expand All @@ -73,11 +105,45 @@ public Map<EntityType, Integer> getLimits() {
return limits;
}

/**
* @return the group limits
*/
public Map<EntityType, List<EntityGroup>> getGroupLimits() {
return groupLimits;
}

/**
* @return the gameModes
*/
public List<String> getGameModes() {
return gameModes;
}

public static class EntityGroup {
private final String name;
private final Set<EntityType> types;
private final int limit;

public EntityGroup(String name, Set<EntityType> types, int limit) {
this.name = name;
this.types = types;
this.limit = limit;
}

public boolean contains(EntityType type) {
return types.contains(type);
}

public String getName() {
return name;
}

public Set<EntityType> getTypes() {
return types;
}

public int getLimit() {
return limit;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package world.bentobox.limits.listeners;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import org.bukkit.Location;
Expand All @@ -19,6 +21,7 @@
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.util.Util;
import world.bentobox.limits.Limits;
import world.bentobox.limits.Settings;

public class EntityLimitListener implements Listener {
private static final String MOD_BYPASS = "mod.bypass";
Expand Down Expand Up @@ -166,18 +169,36 @@ private void checkLimit(CreatureSpawnEvent e, boolean bypass) {
private boolean atLimit(Island island, Entity ent) {
// Check island settings first
int limitAmount = -1;
List<Settings.EntityGroup> groups = new ArrayList();
if (addon.getBlockLimitListener().getIsland(island.getUniqueId()) != null) {
limitAmount = addon.getBlockLimitListener().getIsland(island.getUniqueId()).getEntityLimit(ent.getType());
}
// If no island settings then try global settings
if (limitAmount < 0 && addon.getSettings().getLimits().containsKey(ent.getType())) {
limitAmount = addon.getSettings().getLimits().get(ent.getType());
}
if (limitAmount < 0) return false;
if (groups.isEmpty() && addon.getSettings().getGroupLimits().containsKey(ent.getType())) {
groups = addon.getSettings().getGroupLimits().get(ent.getType());
}
if (limitAmount < 0 && groups.isEmpty()) return false;

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

// Now do the group limits
for (Settings.EntityGroup group : groups) //do not use lambda
{
count = (int) ent.getWorld().getEntities().stream()
.filter(e -> group.contains(e.getType()))
.filter(e -> island.inIslandSpace(e.getLocation())).count();
if (count >= group.getLimit())
return true;
}
return false;
}
}

Expand Down

0 comments on commit e0c208e

Please sign in to comment.