Search in sources :

Example 6 with Limit

use of dev.frankheijden.insights.api.config.limits.Limit in project Insights by InsightsPlugin.

the class Limits method addLimit.

/**
 * Adds the given limit to the data structure.
 */
public void addLimit(Limit limit) {
    this.limits.add(limit);
    this.limitsByFileName.put(limit.getFile().getName(), limit);
    for (Material m : limit.getMaterials()) {
        materialLimits.computeIfAbsent(m, k -> new TreeSet<>(comparingInt(l -> l.getLimit(k).getLimit()))).add(limit);
    }
    for (EntityType e : limit.getEntities()) {
        entityLimits.computeIfAbsent(e, k -> new TreeSet<>(comparingInt(l -> l.getLimit(k).getLimit()))).add(limit);
    }
}
Also used : Comparator.comparingInt(java.util.Comparator.comparingInt) EnumMap(java.util.EnumMap) Predicate(java.util.function.Predicate) Limit(dev.frankheijden.insights.api.config.limits.Limit) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) ScanObject(dev.frankheijden.insights.api.objects.wrappers.ScanObject) EntityType(org.bukkit.entity.EntityType) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) List(java.util.List) SetUtils(dev.frankheijden.insights.api.utils.SetUtils) Map(java.util.Map) Optional(java.util.Optional) InsightsPlugin(dev.frankheijden.insights.api.InsightsPlugin) Material(org.bukkit.Material) EntityType(org.bukkit.entity.EntityType) TreeSet(java.util.TreeSet) Material(org.bukkit.Material)

Example 7 with Limit

use of dev.frankheijden.insights.api.config.limits.Limit in project Insights by InsightsPlugin.

the class PistonListener method handlePistonBlock.

private boolean handlePistonBlock(Block from, Block to) {
    Optional<Region> regionOptional = plugin.getAddonManager().getRegion(to.getLocation());
    // Always allow piston pushes within the same chunk
    if (regionOptional.isEmpty() && BlockUtils.isSameChunk(from, to))
        return false;
    Material material = from.getType();
    Optional<Limit> limitOptional = plugin.getLimits().getFirstLimit(material, limit -> true);
    // If no limit is present, allow the block to be moved.
    if (limitOptional.isEmpty())
        return false;
    Chunk chunk = to.getChunk();
    UUID worldUid = chunk.getWorld().getUID();
    long chunkKey = ChunkUtils.getKey(chunk);
    boolean queued;
    Optional<Storage> storageOptional;
    if (regionOptional.isPresent()) {
        String key = regionOptional.get().getKey();
        queued = plugin.getAddonScanTracker().isQueued(key);
        storageOptional = plugin.getAddonStorage().get(key);
    } else {
        queued = plugin.getWorldChunkScanTracker().isQueued(worldUid, chunkKey);
        storageOptional = plugin.getWorldStorage().getWorld(worldUid).get(chunkKey);
    }
    // If the area is already queued, cancel the event and wait for the area to complete scanning.
    if (queued)
        return true;
    // If the storage is not present, scan it & cancel the event.
    if (storageOptional.isEmpty()) {
        if (regionOptional.isPresent()) {
            Region region = regionOptional.get();
            plugin.getAddonScanTracker().add(region.getAddon());
            List<ChunkPart> chunkParts = region.toChunkParts();
            ScanTask.scan(plugin, chunkParts, chunkParts.size(), ScanOptions.scanOnly(), info -> {
            }, storage -> {
                plugin.getAddonScanTracker().remove(region.getAddon());
                plugin.getAddonStorage().put(region.getKey(), storage);
            });
        } else {
            plugin.getChunkContainerExecutor().submit(chunk);
        }
        return true;
    }
    // Else, the storage is present, and we can apply a limit.
    Storage storage = storageOptional.get();
    Limit limit = limitOptional.get();
    LimitInfo limitInfo = limit.getLimit(material);
    // Cache doesn't need to updated here just yet, needs to be done in MONITOR event phase.
    return storage.count(limit, ScanObject.of(material)) + 1 > limitInfo.getLimit();
}
Also used : Material(org.bukkit.Material) Chunk(org.bukkit.Chunk) LimitInfo(dev.frankheijden.insights.api.config.limits.LimitInfo) Storage(dev.frankheijden.insights.api.concurrent.storage.Storage) Region(dev.frankheijden.insights.api.addons.Region) Limit(dev.frankheijden.insights.api.config.limits.Limit) ChunkPart(dev.frankheijden.insights.api.objects.chunk.ChunkPart) UUID(java.util.UUID)

Example 8 with Limit

use of dev.frankheijden.insights.api.config.limits.Limit in project Insights by InsightsPlugin.

the class InsightsPlaceholderExpansion method onPlaceholderRequest.

@Override
public String onPlaceholderRequest(Player player, String identifier) {
    if (identifier == null)
        return "";
    String[] args = identifier.split("_");
    switch(args[0].toLowerCase(Locale.ENGLISH)) {
        case "limits":
            if (args.length < 3)
                break;
            String itemString = StringUtils.join(args, "_", 2).toUpperCase(Locale.ENGLISH);
            final ScanObject<?> item;
            try {
                item = ScanObject.parse(itemString);
            } catch (IllegalArgumentException ex) {
                return "";
            }
            Location location = player.getLocation();
            World world = location.getWorld();
            UUID worldUid = world.getUID();
            LimitEnvironment env = new LimitEnvironment(player, world.getName());
            Optional<Limit> limitOptional = plugin.getLimits().getFirstLimit(item, env);
            if (!limitOptional.isPresent())
                break;
            Limit limit = limitOptional.get();
            switch(args[1].toLowerCase(Locale.ENGLISH)) {
                case "name":
                    return limit.getLimit(item).getName();
                case "max":
                    return String.valueOf(limit.getLimit(item).getLimit());
                case "count":
                    Optional<Region> regionOptional = plugin.getAddonManager().getRegion(location);
                    Optional<Storage> storageOptional;
                    if (regionOptional.isPresent()) {
                        Region region = regionOptional.get();
                        storageOptional = plugin.getAddonStorage().get(region.getKey());
                    } else {
                        long chunkKey = ChunkUtils.getKey(location);
                        storageOptional = plugin.getWorldStorage().getWorld(worldUid).get(chunkKey);
                    }
                    return storageOptional.map(storage -> String.valueOf(storage.count(limit, item))).orElse("");
                default:
                    break;
            }
            break;
        default:
            break;
    }
    return "";
}
Also used : Limit(dev.frankheijden.insights.api.config.limits.Limit) PlaceholderExpansion(me.clip.placeholderapi.expansion.PlaceholderExpansion) StringUtils(dev.frankheijden.insights.api.utils.StringUtils) ChunkUtils(dev.frankheijden.insights.api.utils.ChunkUtils) Storage(dev.frankheijden.insights.api.concurrent.storage.Storage) ScanObject(dev.frankheijden.insights.api.objects.wrappers.ScanObject) Player(org.bukkit.entity.Player) UUID(java.util.UUID) Region(dev.frankheijden.insights.api.addons.Region) Location(org.bukkit.Location) World(org.bukkit.World) Locale(java.util.Locale) LimitEnvironment(dev.frankheijden.insights.api.config.LimitEnvironment) Optional(java.util.Optional) InsightsPlugin(dev.frankheijden.insights.api.InsightsPlugin) World(org.bukkit.World) Storage(dev.frankheijden.insights.api.concurrent.storage.Storage) Region(dev.frankheijden.insights.api.addons.Region) LimitEnvironment(dev.frankheijden.insights.api.config.LimitEnvironment) Limit(dev.frankheijden.insights.api.config.limits.Limit) UUID(java.util.UUID) Location(org.bukkit.Location)

Aggregations

Limit (dev.frankheijden.insights.api.config.limits.Limit)8 Region (dev.frankheijden.insights.api.addons.Region)5 Storage (dev.frankheijden.insights.api.concurrent.storage.Storage)5 UUID (java.util.UUID)5 World (org.bukkit.World)5 InsightsPlugin (dev.frankheijden.insights.api.InsightsPlugin)4 LimitEnvironment (dev.frankheijden.insights.api.config.LimitEnvironment)4 LimitInfo (dev.frankheijden.insights.api.config.limits.LimitInfo)4 ScanObject (dev.frankheijden.insights.api.objects.wrappers.ScanObject)4 Optional (java.util.Optional)4 Material (org.bukkit.Material)4 AddonStorage (dev.frankheijden.insights.api.concurrent.storage.AddonStorage)3 ChunkStorage (dev.frankheijden.insights.api.concurrent.storage.ChunkStorage)3 DistributionStorage (dev.frankheijden.insights.api.concurrent.storage.DistributionStorage)3 WorldStorage (dev.frankheijden.insights.api.concurrent.storage.WorldStorage)3 ChunkPart (dev.frankheijden.insights.api.objects.chunk.ChunkPart)3 ChunkUtils (dev.frankheijden.insights.api.utils.ChunkUtils)3 StringUtils (dev.frankheijden.insights.api.utils.StringUtils)3 List (java.util.List)3 Chunk (org.bukkit.Chunk)3