Search in sources :

Example 6 with Storage

use of dev.frankheijden.insights.api.concurrent.storage.Storage in project Insights by InsightsPlugin.

the class InsightsListener method handleChunkAddition.

private Optional<Storage> handleChunkAddition(Player player, Chunk chunk, Consumer<Storage> storageConsumer) {
    UUID worldUid = chunk.getWorld().getUID();
    long chunkKey = ChunkUtils.getKey(chunk);
    WorldStorage worldStorage = plugin.getWorldStorage();
    ChunkStorage chunkStorage = worldStorage.getWorld(worldUid);
    Optional<Storage> storageOptional = chunkStorage.get(chunkKey);
    // If the chunk is not known
    if (storageOptional.isEmpty()) {
        // Notify the user scan started
        if (plugin.getSettings().canReceiveAreaScanNotifications(player)) {
            plugin.getMessages().getMessage(Messages.Key.AREA_SCAN_STARTED).addTemplates(Messages.tagOf("area", "chunk")).sendTo(player);
        }
        // Submit the chunk for scanning
        plugin.getChunkContainerExecutor().submit(chunk).thenAccept(storageConsumer).exceptionally(th -> {
            plugin.getLogger().log(Level.SEVERE, th, th::getMessage);
            return null;
        });
    }
    return storageOptional;
}
Also used : WorldStorage(dev.frankheijden.insights.api.concurrent.storage.WorldStorage) Storage(dev.frankheijden.insights.api.concurrent.storage.Storage) ChunkStorage(dev.frankheijden.insights.api.concurrent.storage.ChunkStorage) WorldStorage(dev.frankheijden.insights.api.concurrent.storage.WorldStorage) AddonStorage(dev.frankheijden.insights.api.concurrent.storage.AddonStorage) DistributionStorage(dev.frankheijden.insights.api.concurrent.storage.DistributionStorage) ChunkStorage(dev.frankheijden.insights.api.concurrent.storage.ChunkStorage) UUID(java.util.UUID)

Example 7 with Storage

use of dev.frankheijden.insights.api.concurrent.storage.Storage 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 Storage

use of dev.frankheijden.insights.api.concurrent.storage.Storage 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)

Example 9 with Storage

use of dev.frankheijden.insights.api.concurrent.storage.Storage in project Insights by InsightsPlugin.

the class InsightsListener method handleModification.

protected void handleModification(Location location, Consumer<Storage> storageConsumer) {
    UUID worldUid = location.getWorld().getUID();
    long chunkKey = ChunkUtils.getKey(location);
    plugin.getWorldStorage().getWorld(worldUid).get(chunkKey).ifPresent(storageConsumer);
    plugin.getAddonManager().getRegion(location).flatMap(region -> plugin.getAddonStorage().get(region.getKey())).ifPresent(storageConsumer);
}
Also used : LimitInfo(dev.frankheijden.insights.api.config.limits.LimitInfo) StringUtils(dev.frankheijden.insights.api.utils.StringUtils) ScanTask(dev.frankheijden.insights.api.tasks.ScanTask) Storage(dev.frankheijden.insights.api.concurrent.storage.Storage) ScanObject(dev.frankheijden.insights.api.objects.wrappers.ScanObject) Player(org.bukkit.entity.Player) Level(java.util.logging.Level) ChunkStorage(dev.frankheijden.insights.api.concurrent.storage.ChunkStorage) Messages(dev.frankheijden.insights.api.config.Messages) ScanOptions(dev.frankheijden.insights.api.concurrent.ScanOptions) ChunkPart(dev.frankheijden.insights.api.objects.chunk.ChunkPart) Block(org.bukkit.block.Block) Location(org.bukkit.Location) World(org.bukkit.World) Chunk(org.bukkit.Chunk) InsightsPlugin(dev.frankheijden.insights.api.InsightsPlugin) TagResolver(net.kyori.adventure.text.minimessage.tag.resolver.TagResolver) Material(org.bukkit.Material) Listener(org.bukkit.event.Listener) Limit(dev.frankheijden.insights.api.config.limits.Limit) WorldStorage(dev.frankheijden.insights.api.concurrent.storage.WorldStorage) BlockState(org.bukkit.block.BlockState) InsightsBase(dev.frankheijden.insights.api.objects.InsightsBase) ChunkUtils(dev.frankheijden.insights.api.utils.ChunkUtils) UUID(java.util.UUID) EntityType(org.bukkit.entity.EntityType) Consumer(java.util.function.Consumer) List(java.util.List) Region(dev.frankheijden.insights.api.addons.Region) LimitEnvironment(dev.frankheijden.insights.api.config.LimitEnvironment) Optional(java.util.Optional) AddonStorage(dev.frankheijden.insights.api.concurrent.storage.AddonStorage) DistributionStorage(dev.frankheijden.insights.api.concurrent.storage.DistributionStorage) UUID(java.util.UUID)

Aggregations

Storage (dev.frankheijden.insights.api.concurrent.storage.Storage)9 Region (dev.frankheijden.insights.api.addons.Region)7 Limit (dev.frankheijden.insights.api.config.limits.Limit)7 UUID (java.util.UUID)7 AddonStorage (dev.frankheijden.insights.api.concurrent.storage.AddonStorage)6 ChunkStorage (dev.frankheijden.insights.api.concurrent.storage.ChunkStorage)6 DistributionStorage (dev.frankheijden.insights.api.concurrent.storage.DistributionStorage)6 WorldStorage (dev.frankheijden.insights.api.concurrent.storage.WorldStorage)6 InsightsPlugin (dev.frankheijden.insights.api.InsightsPlugin)5 LimitEnvironment (dev.frankheijden.insights.api.config.LimitEnvironment)5 LimitInfo (dev.frankheijden.insights.api.config.limits.LimitInfo)5 ScanObject (dev.frankheijden.insights.api.objects.wrappers.ScanObject)5 ChunkUtils (dev.frankheijden.insights.api.utils.ChunkUtils)5 Optional (java.util.Optional)5 Location (org.bukkit.Location)5 World (org.bukkit.World)5 Player (org.bukkit.entity.Player)5 Messages (dev.frankheijden.insights.api.config.Messages)4 ChunkPart (dev.frankheijden.insights.api.objects.chunk.ChunkPart)4 StringUtils (dev.frankheijden.insights.api.utils.StringUtils)4