Skip to content

Commit

Permalink
added entity group limits via permissions to islands
Browse files Browse the repository at this point in the history
  • Loading branch information
weaondara committed Apr 12, 2020
1 parent 625bbe3 commit f77c482
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 11 deletions.
9 changes: 8 additions & 1 deletion src/main/java/world/bentobox/limits/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Arrays;
import java.util.EnumMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -84,7 +85,7 @@ public Settings(Limits addon) {
addon.logError("Unknown entity type: " + s + " - skipping...");
}
return null;
}).filter(e -> e != null).collect(Collectors.toSet());
}).filter(e -> e != null).collect(Collectors.toCollection(LinkedHashSet::new));
if (entities.isEmpty())
continue;
EntityGroup group = new EntityGroup(name, entities, limit);
Expand All @@ -95,6 +96,9 @@ public Settings(Limits addon) {
});
}
}

addon.log("Entity group limits:");
getGroupLimitDefinitions().stream().map(e -> "Limit " + e.getName() + " to " + e.getLimit()).forEach(addon::log);
}

private EntityType getType(String key) {
Expand All @@ -115,6 +119,9 @@ public Map<EntityType, List<EntityGroup>> getGroupLimits() {
return groupLimits;
}

/**
* @return the group limit definitions
*/
public List<EntityGroup> getGroupLimitDefinitions() {
return groupLimits.values().stream().flatMap(e -> e.stream()).distinct().collect(Collectors.toList());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package world.bentobox.limits.listeners;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import org.bukkit.Location;
import org.bukkit.World;
Expand Down Expand Up @@ -169,18 +172,26 @@ 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();
Map<Settings.EntityGroup, Integer> groupsLimits = new HashMap<>();
if (addon.getBlockLimitListener().getIsland(island.getUniqueId()) != null) {
limitAmount = addon.getBlockLimitListener().getIsland(island.getUniqueId()).getEntityLimit(ent.getType());
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());
if (limit >= 0)
groupsLimits.put(def, limit);
});
}
// 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 (groups.isEmpty() && addon.getSettings().getGroupLimits().containsKey(ent.getType())) {
groups = addon.getSettings().getGroupLimits().get(ent.getType());
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 && groups.isEmpty()) return false;
if (limitAmount < 0 && groupsLimits.isEmpty()) return false;

// We have to count the entities
int count = (int) ent.getWorld().getEntities().stream()
Expand All @@ -190,12 +201,11 @@ private boolean atLimit(Island island, Entity ent) {
return true;

// Now do the group limits
for (Settings.EntityGroup group : groups) //do not use lambda
{
for (Map.Entry<Settings.EntityGroup, Integer> group : groupsLimits.entrySet()) { //do not use lambda
count = (int) ent.getWorld().getEntities().stream()
.filter(e -> group.contains(e.getType()))
.filter(e -> group.getKey().contains(e.getType()))
.filter(e -> island.inIslandSpace(e.getLocation())).count();
if (count >= group.getLimit())
if (count >= group.getValue())
return true;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import world.bentobox.bentobox.api.events.team.TeamEvent.TeamSetownerEvent;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.limits.Limits;
import world.bentobox.limits.Settings.EntityGroup;
import world.bentobox.limits.objects.IslandBlockCount;

/**
Expand Down Expand Up @@ -55,7 +56,7 @@ private void checkPerms(Player player, String permissionPrefix, String islandId,
// Check formatting
String[] split = perms.getPermission().split("\\.");
if (split.length != 5) {
logError(player.getName(), perms.getPermission(), "format must be '" + permissionPrefix + "MATERIAL.NUMBER' or '" + permissionPrefix + "ENTITY-TYPE.NUMBER'");
logError(player.getName(), perms.getPermission(), "format must be '" + permissionPrefix + "MATERIAL.NUMBER', '" + permissionPrefix + "ENTITY-TYPE.NUMBER', or '" + permissionPrefix + "ENTITY-GROUP.NUMBER'");
return;
}
// Check value
Expand All @@ -66,6 +67,7 @@ private void checkPerms(Player player, String permissionPrefix, String islandId,
// Entities & materials
EntityType et = Arrays.stream(EntityType.values()).filter(t -> t.name().equalsIgnoreCase(split[3])).findFirst().orElse(null);
Material m = Arrays.stream(Material.values()).filter(t -> t.name().equalsIgnoreCase(split[3])).findFirst().orElse(null);
EntityGroup entgroup = addon.getSettings().getGroupLimitDefinitions().stream().filter(e -> e.getName().equalsIgnoreCase(split[3])).findFirst().orElse(null);

if (et == null && m == null) {
logError(player.getName(), perms.getPermission(), split[3].toUpperCase(Locale.ENGLISH) + " is not a valid material or entity type.");
Expand All @@ -75,7 +77,9 @@ private void checkPerms(Player player, String permissionPrefix, String islandId,
if (ibc == null) {
ibc = new IslandBlockCount(islandId, gameMode);
}
if (et != null && m == null) {
if (entgroup != null) {
ibc.setEntityGroupLimit(entgroup.getName(), Math.max(ibc.getEntityGroupLimit(entgroup.getName()), Integer.valueOf(split[4])));
} else if (et != null && m == null) {
// Entity limit
ibc.setEntityLimit(et, Math.max(ibc.getEntityLimit(et), Integer.valueOf(split[4])));
} else if (m != null && et == null) {
Expand Down
41 changes: 41 additions & 0 deletions src/main/java/world/bentobox/limits/objects/IslandBlockCount.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package world.bentobox.limits.objects;

import java.util.EnumMap;
import java.util.HashMap;
import java.util.Map;

import org.bukkit.Material;
Expand Down Expand Up @@ -32,6 +33,8 @@ public class IslandBlockCount implements DataObject {
private Map<Material, Integer> blockLimits = new EnumMap<>(Material.class);
@Expose
private Map<EntityType, Integer> entityLimits = new EnumMap<>(EntityType.class);
@Expose
private Map<String, Integer> entityGroupLimits = new HashMap<>();

// Required for YAML database
public IslandBlockCount() {}
Expand Down Expand Up @@ -201,4 +204,42 @@ public void clearEntityLimits() {
entityLimits.clear();
}

/**
* @return the entityGroupLimits
*/
public Map<String, Integer> getEntityGroupLimits() {
return entityGroupLimits;
}

/**
* @param entityGroupLimits the entityGroupLimits to set
*/
public void setEntityGroupLimits(Map<String, Integer> entityGroupLimits) {
this.entityGroupLimits = entityGroupLimits;
}

/**
* Set an island-specific entity group limit
* @param name - entity group
* @param limit - limit
*/
public void setEntityGroupLimit(String name, int limit) {
entityGroupLimits.put(name, limit);
}

/**
* 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);
}

/**
* Clear all island-specific entity group limits
*/
public void clearEntityGroupLimits() {
entityGroupLimits.clear();
}
}

0 comments on commit f77c482

Please sign in to comment.