2019-12-16 16:47:51 +01:00
|
|
|
package world.bentobox.limits;
|
2018-10-05 06:48:44 +02:00
|
|
|
|
2020-04-10 15:01:37 +02:00
|
|
|
import java.util.ArrayList;
|
2018-10-05 06:48:44 +02:00
|
|
|
import java.util.Arrays;
|
2020-05-01 01:30:34 +02:00
|
|
|
import java.util.Collections;
|
2019-11-08 21:35:47 +01:00
|
|
|
import java.util.EnumMap;
|
2020-04-10 15:01:37 +02:00
|
|
|
import java.util.HashSet;
|
2020-04-11 13:53:28 +02:00
|
|
|
import java.util.LinkedHashSet;
|
2019-02-07 08:20:45 +01:00
|
|
|
import java.util.List;
|
2018-10-05 06:48:44 +02:00
|
|
|
import java.util.Map;
|
2020-04-10 16:57:55 +02:00
|
|
|
import java.util.Objects;
|
2020-04-10 15:01:37 +02:00
|
|
|
import java.util.Set;
|
|
|
|
import java.util.stream.Collectors;
|
2018-10-05 06:48:44 +02:00
|
|
|
|
|
|
|
import org.bukkit.configuration.ConfigurationSection;
|
|
|
|
import org.bukkit.entity.EntityType;
|
|
|
|
|
|
|
|
public class Settings {
|
|
|
|
|
2019-11-08 21:35:47 +01:00
|
|
|
private final Map<EntityType, Integer> limits = new EnumMap<>(EntityType.class);
|
2020-04-10 15:01:37 +02:00
|
|
|
private final Map<EntityType, List<EntityGroup>> groupLimits = new EnumMap<>(EntityType.class);
|
2019-02-09 19:30:02 +01:00
|
|
|
private final List<String> gameModes;
|
2019-11-09 23:44:58 +01:00
|
|
|
private static final List<EntityType> DISALLOWED = Arrays.asList(
|
|
|
|
EntityType.PRIMED_TNT,
|
|
|
|
EntityType.EVOKER_FANGS,
|
|
|
|
EntityType.LLAMA_SPIT,
|
|
|
|
EntityType.DRAGON_FIREBALL,
|
|
|
|
EntityType.AREA_EFFECT_CLOUD,
|
|
|
|
EntityType.ENDER_SIGNAL,
|
|
|
|
EntityType.SMALL_FIREBALL,
|
|
|
|
EntityType.FIREBALL,
|
|
|
|
EntityType.THROWN_EXP_BOTTLE,
|
|
|
|
EntityType.EXPERIENCE_ORB,
|
|
|
|
EntityType.SHULKER_BULLET,
|
|
|
|
EntityType.WITHER_SKULL,
|
|
|
|
EntityType.TRIDENT,
|
|
|
|
EntityType.ARROW,
|
|
|
|
EntityType.SPECTRAL_ARROW,
|
|
|
|
EntityType.SNOWBALL,
|
|
|
|
EntityType.EGG,
|
|
|
|
EntityType.LEASH_HITCH,
|
|
|
|
EntityType.GIANT,
|
|
|
|
EntityType.ENDER_CRYSTAL,
|
|
|
|
EntityType.ENDER_PEARL,
|
|
|
|
EntityType.ENDER_DRAGON,
|
|
|
|
EntityType.ITEM_FRAME,
|
|
|
|
EntityType.PAINTING);
|
2018-10-05 06:48:44 +02:00
|
|
|
|
|
|
|
public Settings(Limits addon) {
|
2019-02-07 08:20:45 +01:00
|
|
|
|
|
|
|
// GameModes
|
2019-02-09 07:35:29 +01:00
|
|
|
gameModes = addon.getConfig().getStringList("gamemodes");
|
2019-02-07 08:20:45 +01:00
|
|
|
|
2018-10-05 06:48:44 +02:00
|
|
|
ConfigurationSection el = addon.getConfig().getConfigurationSection("entitylimits");
|
|
|
|
if (el != null) {
|
|
|
|
for (String key : el.getKeys(false)) {
|
|
|
|
EntityType type = getType(key);
|
|
|
|
if (type != null) {
|
2019-11-09 23:44:58 +01:00
|
|
|
if (DISALLOWED.contains(type)) {
|
2019-04-23 00:50:15 +02:00
|
|
|
addon.logError("Entity type: " + key + " is not supported - skipping...");
|
|
|
|
} else {
|
|
|
|
limits.put(type, el.getInt(key, 0));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
addon.logError("Unknown entity type: " + key + " - skipping...");
|
2018-10-05 06:48:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-23 00:50:15 +02:00
|
|
|
addon.log("Entity limits:");
|
|
|
|
limits.entrySet().stream().map(e -> "Limit " + e.getKey().toString() + " to " + e.getValue()).forEach(addon::log);
|
2020-04-10 15:01:37 +02:00
|
|
|
|
|
|
|
//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;
|
2020-04-11 13:53:28 +02:00
|
|
|
}).filter(e -> e != null).collect(Collectors.toCollection(LinkedHashSet::new));
|
2020-04-10 16:57:55 +02:00
|
|
|
if (entities.isEmpty())
|
|
|
|
continue;
|
2020-04-10 15:01:37 +02:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2020-04-11 13:53:28 +02:00
|
|
|
|
|
|
|
addon.log("Entity group limits:");
|
2020-04-12 14:39:19 +02:00
|
|
|
getGroupLimitDefinitions().stream().map(e -> "Limit " + e.getName() + " (" + e.getTypes().stream().map(x -> x.name()).collect(Collectors.joining(", ")) + ") to " + e.getLimit()).forEach(addon::log);
|
2018-10-05 06:48:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private EntityType getType(String key) {
|
|
|
|
return Arrays.stream(EntityType.values()).filter(v -> v.name().equalsIgnoreCase(key)).findFirst().orElse(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the limits
|
|
|
|
*/
|
|
|
|
public Map<EntityType, Integer> getLimits() {
|
2020-05-01 01:30:34 +02:00
|
|
|
return Collections.unmodifiableMap(limits);
|
2018-10-05 06:48:44 +02:00
|
|
|
}
|
|
|
|
|
2020-04-10 15:01:37 +02:00
|
|
|
/**
|
|
|
|
* @return the group limits
|
|
|
|
*/
|
|
|
|
public Map<EntityType, List<EntityGroup>> getGroupLimits() {
|
|
|
|
return groupLimits;
|
|
|
|
}
|
2020-04-10 16:57:55 +02:00
|
|
|
|
2020-04-11 13:53:28 +02:00
|
|
|
/**
|
|
|
|
* @return the group limit definitions
|
|
|
|
*/
|
2020-04-10 16:57:55 +02:00
|
|
|
public List<EntityGroup> getGroupLimitDefinitions() {
|
|
|
|
return groupLimits.values().stream().flatMap(e -> e.stream()).distinct().collect(Collectors.toList());
|
|
|
|
}
|
2020-04-10 15:01:37 +02:00
|
|
|
|
2019-02-07 08:20:45 +01:00
|
|
|
/**
|
|
|
|
* @return the gameModes
|
|
|
|
*/
|
|
|
|
public List<String> getGameModes() {
|
|
|
|
return gameModes;
|
|
|
|
}
|
|
|
|
|
2020-04-10 15:01:37 +02:00
|
|
|
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;
|
|
|
|
}
|
2020-04-10 16:57:55 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public int hashCode()
|
|
|
|
{
|
|
|
|
int hash = 7;
|
|
|
|
hash = 83 * hash + Objects.hashCode(this.name);
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object obj)
|
|
|
|
{
|
|
|
|
if (this == obj)
|
|
|
|
return true;
|
|
|
|
if (obj == null)
|
|
|
|
return false;
|
|
|
|
if (getClass() != obj.getClass())
|
|
|
|
return false;
|
|
|
|
final EntityGroup other = (EntityGroup) obj;
|
|
|
|
if (!Objects.equals(this.name, other.name))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
2020-04-11 17:42:18 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString()
|
|
|
|
{
|
|
|
|
return "EntityGroup{" + "name=" + name + ", types=" + types + ", limit=" + limit + '}';
|
|
|
|
}
|
2020-04-10 15:01:37 +02:00
|
|
|
}
|
2018-10-05 06:48:44 +02:00
|
|
|
}
|