Search in sources :

Example 1 with Limit

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

the class Insights method reloadLimits.

@Override
public void reloadLimits() {
    limits = new Limits();
    Path limitsPath = getDataFolder().toPath().resolve(LIMITS_FOLDER_NAME);
    if (!Files.exists(limitsPath)) {
        try {
            Files.createDirectory(limitsPath);
            IOUtils.copyResources(limitsPath, getClassLoader(), Arrays.asList("bed-limit.yml", "redstone-limit.yml", "tile-limit.yml"));
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(limitsPath, p -> !Files.isDirectory(p))) {
        for (Path child : stream) {
            String fileName = child.getFileName().toString();
            if (!fileName.toLowerCase(Locale.ENGLISH).endsWith(".yml"))
                continue;
            try {
                Limit limit = Limit.parse(child.toFile());
                getLogger().info("Loaded limit '" + fileName + "'");
                limits.addLimit(limit);
            } catch (YamlParseException ex) {
                getLogger().severe("Limit '" + fileName + "' could not be loaded:");
                getLogger().severe(ex.getMessage());
            }
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}
Also used : Path(java.nio.file.Path) Limits(dev.frankheijden.insights.api.config.Limits) YamlParseException(dev.frankheijden.insights.api.config.parser.YamlParseException) IOException(java.io.IOException) Limit(dev.frankheijden.insights.api.config.limits.Limit)

Example 2 with Limit

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

the class Insights method loadCommands.

private void loadCommands() {
    PaperCommandManager<CommandSender> commandManager;
    try {
        commandManager = new PaperCommandManager<>(this, AsynchronousCommandExecutionCoordinator.<CommandSender>newBuilder().build(), Function.identity(), Function.identity());
    } catch (Exception ex) {
        ex.printStackTrace();
        return;
    }
    // Register parsers
    ParserRegistry<CommandSender> parserRegistry = commandManager.getParserRegistry();
    parserRegistry.registerParserSupplier(TypeToken.get(new TypeToken<Limit>() {
    }.getType()), options -> new LimitArgument.LimitParser());
    parserRegistry.registerParserSupplier(TypeToken.get(new TypeToken<ScanObject<?>[]>() {
    }.getType()), options -> new ScanObjectArrayArgument.ScanObjectArrayParser());
    parserRegistry.registerParserSupplier(TypeToken.get(new TypeToken<CommandScanHistory.Page>() {
    }.getType()), options -> new ScanHistoryPageArgument.ScanHistoryPageParser());
    parserRegistry.registerParserSupplier(TypeToken.get(new TypeToken<World>() {
    }.getType()), options -> new WorldArgument.WorldParser());
    // Register capabilities if allowed
    if (commandManager.queryCapability(CloudBukkitCapabilities.BRIGADIER)) {
        commandManager.registerBrigadier();
        CloudBrigadierManager<CommandSender, ?> brigadierManager = commandManager.brigadierManager();
        var handler = new BrigadierHandler(brigadierManager);
        handler.registerTypes();
    }
    if (commandManager.queryCapability(CloudBukkitCapabilities.ASYNCHRONOUS_COMPLETION)) {
        commandManager.registerAsynchronousCompletions();
    }
    // Create Annotation Parser
    AnnotationParser<CommandSender> annotationParser = new AnnotationParser<>(commandManager, CommandSender.class, parameters -> SimpleCommandMeta.empty());
    // Parse commands
    annotationParser.parse(new CommandInsights(this));
    annotationParser.parse(new CommandScan(this));
    annotationParser.parse(new CommandScanCache(this));
    annotationParser.parse(new CommandScanWorld(this));
    annotationParser.parse(new CommandScanRegion(this));
    annotationParser.parse(new CommandScanHistory(this));
    annotationParser.parse(new CommandTeleportChunk(this));
    annotationParser.parse(new CommandCancelScan(this));
}
Also used : AnnotationParser(cloud.commandframework.annotations.AnnotationParser) CommandTeleportChunk(dev.frankheijden.insights.commands.CommandTeleportChunk) BrigadierHandler(dev.frankheijden.insights.commands.brigadier.BrigadierHandler) World(org.bukkit.World) CommandScanWorld(dev.frankheijden.insights.commands.CommandScanWorld) CommandScanRegion(dev.frankheijden.insights.commands.CommandScanRegion) CommandScan(dev.frankheijden.insights.commands.CommandScan) ScanHistoryPageArgument(dev.frankheijden.insights.commands.parser.ScanHistoryPageArgument) CommandScanHistory(dev.frankheijden.insights.commands.CommandScanHistory) CommandCancelScan(dev.frankheijden.insights.commands.CommandCancelScan) LimitArgument(dev.frankheijden.insights.commands.parser.LimitArgument) CommandInsights(dev.frankheijden.insights.commands.CommandInsights) ScanObjectArrayArgument(dev.frankheijden.insights.commands.parser.ScanObjectArrayArgument) CommandScanWorld(dev.frankheijden.insights.commands.CommandScanWorld) YamlParseException(dev.frankheijden.insights.api.config.parser.YamlParseException) IOException(java.io.IOException) WorldArgument(dev.frankheijden.insights.commands.parser.WorldArgument) CommandSender(org.bukkit.command.CommandSender) Limit(dev.frankheijden.insights.api.config.limits.Limit) CommandScanCache(dev.frankheijden.insights.commands.CommandScanCache)

Example 3 with Limit

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

the class InsightsListener method handleAddition.

protected boolean handleAddition(Player player, Location location, ScanObject<?> item, int delta, boolean included) {
    Optional<Region> regionOptional = plugin.getAddonManager().getRegion(location);
    var chunk = location.getChunk();
    var world = location.getWorld();
    UUID worldUid = world.getUID();
    long chunkKey = ChunkUtils.getKey(chunk);
    boolean queued;
    String area;
    LimitEnvironment env;
    if (regionOptional.isPresent()) {
        var region = regionOptional.get();
        queued = plugin.getAddonScanTracker().isQueued(region.getKey());
        area = plugin.getAddonManager().getAddon(region.getAddon()).getAreaName();
        env = new LimitEnvironment(player, world.getName(), region.getAddon());
    } else {
        queued = plugin.getWorldChunkScanTracker().isQueued(worldUid, chunkKey);
        area = "chunk";
        env = new LimitEnvironment(player, world.getName());
    }
    if (queued) {
        if (plugin.getSettings().canReceiveAreaScanNotifications(player)) {
            plugin.getMessages().getMessage(Messages.Key.AREA_SCAN_QUEUED).addTemplates(Messages.tagOf("area", area)).sendTo(player);
        }
        return true;
    }
    // Get the first (smallest) limit for the specific user (bypass permissions taken into account)
    Optional<Limit> limitOptional = plugin.getLimits().getFirstLimit(item, env);
    if (limitOptional.isEmpty())
        return false;
    var limit = limitOptional.get();
    var limitInfo = limit.getLimit(item);
    if (regionOptional.isEmpty() && limit.getSettings().isDisallowedPlacementOutsideRegion()) {
        plugin.getMessages().getMessage(Messages.Key.LIMIT_DISALLOWED_PLACEMENT).addTemplates(TagResolver.resolver(Messages.tagOf("name", limitInfo.getName()), Messages.tagOf("area", area))).sendTo(player);
        return true;
    }
    Consumer<Storage> storageConsumer = storage -> {
        // Only iff the block was included in the chunk AND its not a cuboid/area scan.
        if (included && regionOptional.isEmpty()) {
            storage.modify(item, -delta);
        }
        // Notify the user scan completed
        if (plugin.getSettings().canReceiveAreaScanNotifications(player)) {
            plugin.getMessages().getMessage(Messages.Key.AREA_SCAN_COMPLETED).sendTo(player);
        }
    };
    Optional<Storage> storageOptional;
    if (regionOptional.isPresent()) {
        storageOptional = handleAddonAddition(player, regionOptional.get(), storageConsumer);
    } else {
        storageOptional = handleChunkAddition(player, chunk, storageConsumer);
    }
    // If the storage is not present, cancel.
    if (storageOptional.isEmpty())
        return true;
    var storage = storageOptional.get();
    long count = storage.count(limit, item);
    // If count is beyond limit, act
    if (count + delta > limitInfo.getLimit()) {
        plugin.getMessages().getMessage(Messages.Key.LIMIT_REACHED).addTemplates(Messages.tagOf("limit", StringUtils.pretty(limitInfo.getLimit())), Messages.tagOf("name", limitInfo.getName()), Messages.tagOf("area", area)).sendTo(player);
        return true;
    }
    return false;
}
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) 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) 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)

Example 4 with Limit

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

the class InsightsListener method handleRemoval.

protected void handleRemoval(Player player, Location location, ScanObject<?> item, int delta, boolean included) {
    Optional<Region> regionOptional = plugin.getAddonManager().getRegion(location);
    Chunk chunk = location.getChunk();
    World world = location.getWorld();
    UUID worldUid = world.getUID();
    long chunkKey = ChunkUtils.getKey(chunk);
    UUID uuid = player.getUniqueId();
    boolean queued;
    LimitEnvironment env;
    Optional<Storage> storageOptional;
    if (regionOptional.isPresent()) {
        Region region = regionOptional.get();
        queued = plugin.getAddonScanTracker().isQueued(region.getKey());
        env = new LimitEnvironment(player, world.getName(), region.getAddon());
        storageOptional = plugin.getAddonStorage().get(region.getKey());
    } else {
        queued = plugin.getWorldChunkScanTracker().isQueued(worldUid, chunkKey);
        env = new LimitEnvironment(player, world.getName());
        storageOptional = plugin.getWorldStorage().getWorld(worldUid).get(chunkKey);
    }
    // Modify the area to account for the broken block.
    storageOptional.ifPresent(storage -> storage.modify(item, -delta));
    // Notify the user (if they have permission)
    if (player.hasPermission("insights.notifications")) {
        // If the area is queued, stop check here (notification will be displayed when it completes).
        if (queued)
            return;
        // Get the first (smallest) limit for the specific user (bypass permissions taken into account)
        Optional<Limit> limitOptional = plugin.getLimits().getFirstLimit(item, env);
        if (limitOptional.isEmpty())
            return;
        Limit limit = limitOptional.get();
        LimitInfo limitInfo = limit.getLimit(item);
        // Create a runnable for the notification.
        Consumer<Storage> notification = storage -> {
            long count = storage.count(limit, item);
            float progress = (float) count / limitInfo.getLimit();
            plugin.getNotifications().getCachedProgress(uuid, Messages.Key.LIMIT_NOTIFICATION).progress(progress).add(player).create().addTemplates(Messages.tagOf("name", limitInfo.getName()), Messages.tagOf("count", StringUtils.pretty(count)), Messages.tagOf("limit", StringUtils.pretty(limitInfo.getLimit()))).send();
        };
        // If the data is already stored, send the notification immediately.
        if (storageOptional.isPresent()) {
            notification.accept(storageOptional.get());
            return;
        }
        // Else, we need to scan the area first.
        Consumer<Storage> storageConsumer = storage -> {
            // Only if we're not scanning a cuboid (iff cuboid, the block is already removed from the chunk)
            if (included && regionOptional.isEmpty())
                storage.modify(item, -delta);
            // Notify the user
            notification.accept(storage);
        };
        if (regionOptional.isPresent()) {
            scanRegion(player, regionOptional.get(), storageConsumer);
        } else {
            plugin.getChunkContainerExecutor().submit(chunk).thenAccept(storageConsumer).exceptionally(th -> {
                plugin.getLogger().log(Level.SEVERE, th, th::getMessage);
                return null;
            });
        }
    }
}
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) Chunk(org.bukkit.Chunk) World(org.bukkit.World) LimitInfo(dev.frankheijden.insights.api.config.limits.LimitInfo) 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) 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)

Example 5 with Limit

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

the class InsightsListener method evaluateAddition.

protected void evaluateAddition(Player player, Location location, ScanObject<?> item, int delta) {
    Optional<Region> regionOptional = plugin.getAddonManager().getRegion(location);
    World world = location.getWorld();
    long chunkKey = ChunkUtils.getKey(location);
    UUID uuid = player.getUniqueId();
    LimitEnvironment env;
    Optional<Storage> storageOptional;
    if (regionOptional.isPresent()) {
        Region region = regionOptional.get();
        env = new LimitEnvironment(player, world.getName(), region.getAddon());
        storageOptional = plugin.getAddonStorage().get(region.getKey());
    } else {
        env = new LimitEnvironment(player, world.getName());
        storageOptional = plugin.getWorldStorage().getWorld(world.getUID()).get(chunkKey);
    }
    if (storageOptional.isEmpty())
        return;
    Storage storage = storageOptional.get();
    // If limit is not present, stop here
    Optional<Limit> limitOptional = plugin.getLimits().getFirstLimit(item, env);
    if (limitOptional.isEmpty())
        return;
    Limit limit = limitOptional.get();
    LimitInfo limitInfo = limit.getLimit(item);
    long count = storage.count(limit, item);
    // Notify the user (if they have permission)
    if (player.hasPermission("insights.notifications")) {
        float progress = (float) (count + delta) / limitInfo.getLimit();
        plugin.getNotifications().getCachedProgress(uuid, Messages.Key.LIMIT_NOTIFICATION).progress(progress).add(player).create().addTemplates(Messages.tagOf("name", limitInfo.getName()), Messages.tagOf("count", StringUtils.pretty(count + delta)), Messages.tagOf("limit", StringUtils.pretty(limitInfo.getLimit()))).send();
    }
}
Also used : 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) Region(dev.frankheijden.insights.api.addons.Region) LimitEnvironment(dev.frankheijden.insights.api.config.LimitEnvironment) Limit(dev.frankheijden.insights.api.config.limits.Limit) World(org.bukkit.World) UUID(java.util.UUID) LimitInfo(dev.frankheijden.insights.api.config.limits.LimitInfo)

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