Limits/src/main/java/world/bentobox/limits/Settings.java

157 lines
5.6 KiB
Java
Raw Permalink Normal View History

package world.bentobox.limits;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
2020-04-10 15:01:37 +02:00
import java.util.stream.Collectors;
import org.bukkit.Material;
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;
private final boolean asyncGolums;
private static final List<EntityType> DISALLOWED = Arrays.asList(
2024-07-17 06:52:52 +02:00
EntityType.TNT,
EntityType.EVOKER_FANGS,
EntityType.LLAMA_SPIT,
EntityType.DRAGON_FIREBALL,
EntityType.AREA_EFFECT_CLOUD,
2024-07-17 06:52:52 +02:00
EntityType.END_CRYSTAL,
EntityType.SMALL_FIREBALL,
EntityType.FIREBALL,
2024-07-17 06:52:52 +02:00
EntityType.EXPERIENCE_BOTTLE,
EntityType.EXPERIENCE_ORB,
EntityType.SHULKER_BULLET,
EntityType.WITHER_SKULL,
EntityType.TRIDENT,
EntityType.ARROW,
EntityType.SPECTRAL_ARROW,
EntityType.SNOWBALL,
EntityType.EGG,
2024-07-17 06:52:52 +02:00
EntityType.LEASH_KNOT,
EntityType.GIANT,
EntityType.ENDER_PEARL,
EntityType.ENDER_DRAGON,
EntityType.ITEM_FRAME,
EntityType.PAINTING);
public Settings(Limits addon) {
// GameModes
gameModes = addon.getConfig().getStringList("gamemodes");
ConfigurationSection el = addon.getConfig().getConfigurationSection("entitylimits");
if (el != null) {
for (String key : el.getKeys(false)) {
EntityType type = getType(key);
if (type != null) {
if (DISALLOWED.contains(type)) {
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...");
}
}
}
// Async Golums
asyncGolums = addon.getConfig().getBoolean("async-golums", true);
addon.log("Entity limits:");
limits.entrySet().stream().map(e -> "Limit " + e.getKey().toString() + " to " + e.getValue()).forEach(addon::log);
2020-06-27 01:22:18 +02:00
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");
String iconName = el.getString(name + ".icon", "BARRIER");
Material icon = Material.BARRIER;
try {
icon = Material.valueOf(iconName.toUpperCase(Locale.ENGLISH));
} catch (Exception e) {
addon.logError("Invalid group icon name: " + iconName + ". Use a Bukkit Material.");
icon = Material.BARRIER;
}
2020-04-10 15:01:37 +02:00
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;
2021-12-22 23:37:10 +01:00
}).filter(Objects::nonNull).collect(Collectors.toCollection(LinkedHashSet::new));
2020-04-10 16:57:55 +02:00
if (entities.isEmpty())
continue;
EntityGroup group = new EntityGroup(name, entities, limit, icon);
2020-04-10 15:01:37 +02:00
entities.forEach(e -> {
2020-06-27 01:22:18 +02:00
List<EntityGroup> groups = groupLimits.getOrDefault(e, new ArrayList<>());
2020-04-10 15:01:37 +02:00
groups.add(group);
groupLimits.put(e, groups);
});
}
}
2020-06-27 01:22:18 +02:00
addon.log("Entity group limits:");
2021-12-22 23:37:10 +01:00
getGroupLimitDefinitions().stream().map(e -> "Limit " + e.getName() + " (" + e.getTypes().stream().map(Enum::name).collect(Collectors.joining(", ")) + ") to " + e.getLimit()).forEach(addon::log);
}
private EntityType getType(String key) {
return Arrays.stream(EntityType.values()).filter(v -> v.name().equalsIgnoreCase(key)).findFirst().orElse(null);
}
/**
2021-09-07 02:11:02 +02:00
* @return the entity limits
*/
public Map<EntityType, Integer> getLimits() {
return Collections.unmodifiableMap(limits);
}
2020-04-10 15:01:37 +02:00
/**
* @return the group limits
*/
public Map<EntityType, List<EntityGroup>> getGroupLimits() {
return groupLimits;
}
2020-06-27 01:22:18 +02:00
/**
* @return the group limit definitions
*/
2020-04-10 16:57:55 +02:00
public List<EntityGroup> getGroupLimitDefinitions() {
return groupLimits.values().stream().flatMap(Collection::stream).distinct().toList();
2020-04-10 16:57:55 +02:00
}
2020-04-10 15:01:37 +02:00
/**
* @return the gameModes
*/
public List<String> getGameModes() {
return gameModes;
}
/**
* @return the asyncGolums
*/
public boolean isAsyncGolums() {
return asyncGolums;
}
}