Search in sources :

Example 1 with ActionsQuery

use of me.botsko.prism.actionlibs.ActionsQuery in project Prism-Bukkit by prism.

the class DeleteCommand method handle.

@Override
public void handle(final CallInfo call) {
    // Allow for canceling tasks
    if (call.getArgs().length > 1 && call.getArg(1).equals("cancel")) {
        if (plugin.getPurgeManager().deleteTask != null) {
            plugin.getPurgeManager().deleteTask.cancel();
            Prism.messenger.sendMessage(call.getSender(), Prism.messenger.playerMsg(Il8nHelper.getMessage("cancel-purge")));
        } else {
            Prism.messenger.sendMessage(call.getSender(), Prism.messenger.playerError(Il8nHelper.getMessage("no-purge-running")));
        }
        return;
    }
    // Allow for wiping live queue
    if (call.getArgs().length > 1 && call.getArg(1).equals("queue")) {
        if (RecordingQueue.getQueue().size() > 0) {
            Prism.log("User " + call.getSender().getName() + " wiped the live queue before it could be written to the database. " + RecordingQueue.getQueue().size() + " events lost.");
            RecordingQueue.getQueue().clear();
            Prism.messenger.sendMessage(call.getSender(), Prism.messenger.playerSuccess(Il8nHelper.getMessage("clear-queue")));
        } else {
            Prism.messenger.sendMessage(call.getSender(), Prism.messenger.playerError(Il8nHelper.getMessage("event-queue-clear")));
        }
        return;
    }
    // Process and validate all of the arguments
    final QueryParameters parameters = PreprocessArgs.process(plugin, call.getSender(), call.getArgs(), PrismProcessType.DELETE, 1, !plugin.getConfig().getBoolean("prism.queries.never-use-defaults"));
    if (parameters == null) {
        return;
    }
    parameters.setStringFromRawArgs(call.getArgs(), 1);
    StringBuilder defaultsReminder = checkIfDefaultUsed(parameters);
    if (parameters.getFoundArgs().size() > 0) {
        Prism.messenger.sendMessage(call.getSender(), Prism.messenger.playerSubduedHeaderMsg(Il8nHelper.getMessage("purge-data").replaceFirstText(Pattern.compile("<defaults>"), builder -> Component.text().content(defaultsReminder.toString()))));
        Prism.messenger.sendMessage(call.getSender(), Prism.messenger.playerHeaderMsg(Il8nHelper.getMessage("start-purge")));
        plugin.getServer().getScheduler().runTaskAsynchronously(plugin, () -> {
            int purgeTickDelay = plugin.getConfig().getInt("prism.purge.batch-tick-delay");
            if (purgeTickDelay < 1) {
                purgeTickDelay = 20;
            }
            // build callback
            final SenderPurgeCallback callback = new SenderPurgeCallback();
            callback.setSender(call.getSender());
            // add to an arraylist so we're consistent
            final CopyOnWriteArrayList<QueryParameters> paramList = new CopyOnWriteArrayList<>();
            paramList.add(parameters);
            final ActionsQuery aq = new ActionsQuery(plugin);
            final long[] extents = aq.getQueryExtents(parameters);
            final long minId = extents[0];
            final long maxId = extents[1];
            Prism.log("Beginning prism database purge cycle. Will be performed in batches so " + "we don't tie up the db...");
            deleteTask = plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new PurgeTask(plugin, paramList, purgeTickDelay, minId, maxId, callback));
        });
    } else {
        Prism.messenger.sendMessage(call.getSender(), Prism.messenger.playerError(Il8nHelper.getMessage("no-parameter")));
    }
}
Also used : ActionsQuery(me.botsko.prism.actionlibs.ActionsQuery) QueryParameters(me.botsko.prism.actionlibs.QueryParameters) PurgeTask(me.botsko.prism.purge.PurgeTask) SenderPurgeCallback(me.botsko.prism.purge.SenderPurgeCallback) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 2 with ActionsQuery

use of me.botsko.prism.actionlibs.ActionsQuery in project Prism-Bukkit by prism.

the class PurgeTask method run.

@Override
public void run() {
    if (paramList.isEmpty()) {
        return;
    }
    final ActionsQuery aq = new ActionsQuery(plugin);
    // Pull the next-in-line purge param
    final QueryParameters param = paramList.get(0);
    if (minId == 0 && maxId == 0) {
        // First run - Set the min and max IDs
        long[] result = aq.getQueryExtents(param);
        minId = result[0];
        if (minId > 0) {
            maxId = result[1];
        }
    }
    boolean cycleComplete = false;
    cycleRowsAffected = 0;
    // We're chunking by IDs instead of using LIMIT because
    // that should be a lot better as far as required record lock counts
    // http://mysql.rjweb.org/doc.php/deletebig
    int spread = plugin.getConfig().getInt("prism.purge.records-per-batch");
    if (spread <= 1) {
        spread = 10000;
    }
    // Delete includes id < newMinId. This ensures the maxId isn't exceeded on the final chunk
    // and also handles the case where minId == maxId (they need to be different by at least 1).
    long newMinId = Math.min(minId + spread, maxId + 1);
    long startTime = System.nanoTime();
    // Make sure there are rows to potentially delete
    if (maxId > 0) {
        param.setMinPrimaryKey(minId);
        param.setMaxPrimaryKey(newMinId);
        cycleRowsAffected = aq.delete(param);
        plugin.totalRecordsAffected += cycleRowsAffected;
    }
    // If done, remove rule and mark complete
    if (newMinId >= maxId) {
        paramList.remove(param);
        cycleComplete = true;
    }
    // msec
    long cycleTime = (System.nanoTime() - startTime) / 1000000L;
    plugin.maxCycleTime = Math.max(plugin.maxCycleTime, cycleTime);
    Prism.debug("------------------- ");
    Prism.debug("params: " + param.getOriginalCommand());
    Prism.debug("minId: " + minId);
    Prism.debug("maxId: " + maxId);
    Prism.debug("newMinId: " + newMinId);
    Prism.debug("cycleRowsAffected: " + cycleRowsAffected);
    Prism.debug("cycleComplete: " + cycleComplete);
    Prism.debug("plugin.total_records_affected: " + plugin.totalRecordsAffected);
    Prism.debug("-------------------");
    // Send cycle to callback
    callback.cycle(param, cycleRowsAffected, plugin.totalRecordsAffected, cycleComplete, plugin.maxCycleTime);
    if (!plugin.isEnabled()) {
        Prism.log("Can't schedule new purge tasks as plugin is now disabled. " + "If you're shutting down the server, ignore me.");
        return;
    }
    // If cycle is incomplete, reschedule it
    if (!cycleComplete) {
        plugin.getPurgeManager().deleteTask = plugin.getServer().getScheduler().runTaskLaterAsynchronously(plugin, new PurgeTask(plugin, paramList, purgeTickDelay, newMinId, maxId, callback), purgeTickDelay);
    } else {
        // reset counts
        plugin.totalRecordsAffected = 0;
        plugin.maxCycleTime = 0;
        if (paramList.isEmpty()) {
            return;
        }
        Prism.log("Moving on to next purge rule...");
        // schedule a new task with next param
        plugin.getPurgeManager().deleteTask = plugin.getServer().getScheduler().runTaskLaterAsynchronously(plugin, new PurgeTask(plugin, paramList, purgeTickDelay, callback), purgeTickDelay);
    }
}
Also used : ActionsQuery(me.botsko.prism.actionlibs.ActionsQuery) QueryParameters(me.botsko.prism.actionlibs.QueryParameters)

Example 3 with ActionsQuery

use of me.botsko.prism.actionlibs.ActionsQuery in project Prism-Bukkit by prism.

the class LookupCommand method handle.

/**
 * Handle the command.
 */
@Override
public void handle(final CallInfo call) {
    // Process and validate all of the arguments
    final QueryParameters parameters = PreprocessArgs.process(plugin, call.getSender(), call.getArgs(), PrismProcessType.LOOKUP, 1, !plugin.getConfig().getBoolean("prism.queries.never-use-defaults"));
    if (parameters == null) {
        return;
    }
    // Run the lookup itself in an async task so the lookup query isn't done on the main thread
    plugin.getServer().getScheduler().runTaskAsynchronously(plugin, () -> {
        // determine if defaults were used
        final List<String> defaultsUsed = parameters.getDefaultsUsed();
        StringBuilder defaultsReminder = new StringBuilder();
        if (!defaultsUsed.isEmpty()) {
            defaultsReminder.append(Il8nHelper.getRawMessage("queryparameter.defaults.prefix"));
            for (final String d : defaultsUsed) {
                defaultsReminder.append(" ").append(d);
            }
        }
        final ActionsQuery aq = new ActionsQuery(plugin);
        final QueryResult results = aq.lookup(parameters, call.getSender());
        final StringBuilder sharingWithPlayers = new StringBuilder();
        for (final CommandSender shareWith : parameters.getSharedPlayers()) {
            sharingWithPlayers.append(shareWith.getName()).append(", ");
        }
        if (sharingWithPlayers.length() > 0) {
            sharingWithPlayers.delete(sharingWithPlayers.lastIndexOf(","), sharingWithPlayers.length());
        }
        final String playersList = sharingWithPlayers.toString();
        // Add current sender
        parameters.addSharedPlayer(call.getSender());
        for (final CommandSender player : parameters.getSharedPlayers()) {
            final boolean isSender = player.getName().equals(call.getSender().getName());
            if (!isSender) {
                Prism.messenger.sendMessage(player, Prism.messenger.playerHeaderMsg(Il8nHelper.getMessage("lookup-share-message").color(NamedTextColor.GOLD).replaceText(Pattern.compile("<sender>"), builder -> Component.text().content(call.getSender().getName()).color(NamedTextColor.YELLOW).decoration(TextDecoration.ITALIC, TextDecoration.State.TRUE), 1)));
            } else if (sharingWithPlayers.length() > 0) {
                Component component = Il8nHelper.getMessage("lookup-share-to-message").color(NamedTextColor.GOLD).replaceText(Pattern.compile("<players>"), builder -> Component.text().content(playersList).color(NamedTextColor.YELLOW).decoration(TextDecoration.ITALIC, TextDecoration.State.TRUE), 1);
                Prism.messenger.sendMessage(call.getSender(), Prism.messenger.playerHeaderMsg(component));
            }
            if (!results.getActionResults().isEmpty()) {
                Prism.messenger.sendMessage(player, Prism.messenger.playerHeaderMsg(Il8nHelper.formatMessage("lookup-header-message", results.getTotalResults(), 1, results.getTotalPages())));
                if ((defaultsReminder.length() > 0) && isSender) {
                    Prism.messenger.sendMessage(player, Prism.messenger.playerSubduedHeaderMsg(Component.text(defaultsReminder.toString())));
                }
                final List<Handler> paginated = results.getPaginatedActionResults();
                if (paginated != null) {
                    int resultCount = results.getIndexOfFirstResult();
                    for (final Handler a : paginated) {
                        final ActionMessage am = new ActionMessage(a);
                        if (parameters.hasFlag(Flag.EXTENDED) || plugin.getConfig().getBoolean("prism.messenger.always-show-extended")) {
                            am.showExtended();
                        }
                        am.setResultIndex(resultCount);
                        MiscUtils.sendClickableTpRecord(am, player);
                        resultCount++;
                    }
                    MiscUtils.sendPageButtons(results, player);
                } else {
                    Prism.messenger.sendMessage(player, Prism.messenger.playerError(Il8nHelper.getMessage("no-pagination-found")));
                }
                if (parameters.hasFlag(Flag.PASTE)) {
                    StringBuilder paste = new StringBuilder();
                    for (final Handler a : results.getActionResults()) {
                        paste.append(new ActionMessage(a).getRawMessage()).append("\r\n");
                    }
                    MiscUtils.paste_results(player, paste.toString());
                }
            } else {
                if (defaultsReminder.length() > 0) {
                    if (isSender) {
                        Prism.messenger.sendMessage(player, Prism.messenger.playerSubduedHeaderMsg(Component.text(defaultsReminder.toString())));
                    }
                }
                if (isSender) {
                    Prism.messenger.sendMessage(player, Prism.messenger.playerError("Nothing found." + ChatColor.GRAY + " Either you're missing something, or we are."));
                }
            }
        }
        // Flush timed data
        plugin.eventTimer.printTimeRecord();
    });
}
Also used : Prism(me.botsko.prism.Prism) CommandSender(org.bukkit.command.CommandSender) CallInfo(me.botsko.prism.commandlibs.CallInfo) PreprocessArgs(me.botsko.prism.commandlibs.PreprocessArgs) TextDecoration(net.kyori.adventure.text.format.TextDecoration) Handler(me.botsko.prism.api.actions.Handler) QueryResult(me.botsko.prism.actionlibs.QueryResult) NamedTextColor(net.kyori.adventure.text.format.NamedTextColor) ActionsQuery(me.botsko.prism.actionlibs.ActionsQuery) List(java.util.List) SubHandler(me.botsko.prism.commandlibs.SubHandler) Flag(me.botsko.prism.api.commands.Flag) MiscUtils(me.botsko.prism.utils.MiscUtils) Component(net.kyori.adventure.text.Component) Il8nHelper(me.botsko.prism.Il8nHelper) PrismProcessType(me.botsko.prism.api.actions.PrismProcessType) ChatColor(org.bukkit.ChatColor) Pattern(java.util.regex.Pattern) QueryParameters(me.botsko.prism.actionlibs.QueryParameters) ActionMessage(me.botsko.prism.actionlibs.ActionMessage) Handler(me.botsko.prism.api.actions.Handler) SubHandler(me.botsko.prism.commandlibs.SubHandler) QueryParameters(me.botsko.prism.actionlibs.QueryParameters) QueryResult(me.botsko.prism.actionlibs.QueryResult) ActionsQuery(me.botsko.prism.actionlibs.ActionsQuery) ActionMessage(me.botsko.prism.actionlibs.ActionMessage) CommandSender(org.bukkit.command.CommandSender) Component(net.kyori.adventure.text.Component)

Example 4 with ActionsQuery

use of me.botsko.prism.actionlibs.ActionsQuery in project Prism-Bukkit by prism.

the class PreviewCommand method handle.

@Override
public void handle(final CallInfo call) {
    final Audience audience = Prism.getAudiences().sender(call.getPlayer());
    if (call.getArgs().length >= 2) {
        if (call.getArg(1).equalsIgnoreCase("apply")) {
            if (plugin.playerActivePreviews.containsKey(call.getPlayer().getName())) {
                final PreviewSession previewSession = plugin.playerActivePreviews.get(call.getPlayer().getName());
                previewSession.getPreviewer().apply_preview();
                plugin.playerActivePreviews.remove(call.getPlayer().getName());
            } else {
                Prism.messenger.sendMessage(call.getPlayer(), Prism.messenger.playerError("You have no preview pending."));
            }
            return;
        }
        if (call.getArg(1).equalsIgnoreCase("cancel")) {
            if (plugin.playerActivePreviews.containsKey(call.getPlayer().getName())) {
                final PreviewSession previewSession = plugin.playerActivePreviews.get(call.getPlayer().getName());
                previewSession.getPreviewer().cancel_preview();
                plugin.playerActivePreviews.remove(call.getPlayer().getName());
            } else {
                Prism.messenger.sendMessage(call.getPlayer(), Prism.messenger.playerError(Il8nHelper.getMessage("preview-none-pending")));
            }
            return;
        }
        // Ensure no current preview is waiting
        if (plugin.playerActivePreviews.containsKey(call.getPlayer().getName())) {
            Prism.messenger.sendMessage(call.getPlayer(), Prism.messenger.playerError(Il8nHelper.getMessage("preview-pending")));
            return;
        }
        if (call.getArg(1).equalsIgnoreCase("rollback") || call.getArg(1).equalsIgnoreCase("restore") || call.getArg(1).equalsIgnoreCase("rb") || call.getArg(1).equalsIgnoreCase("rs")) {
            final QueryParameters parameters = PreprocessArgs.process(plugin, call.getPlayer(), call.getArgs(), PrismProcessType.ROLLBACK, 2, !plugin.getConfig().getBoolean("prism.queries.never-use-defaults"));
            if (parameters == null) {
                return;
            }
            parameters.setStringFromRawArgs(call.getArgs(), 1);
            if (parameters.getActionTypes().containsKey("world-edit")) {
                Prism.messenger.sendMessage(call.getPlayer(), Prism.messenger.playerError(Il8nHelper.getMessage("preview-worldedit-unsupported")));
                return;
            }
            StringBuilder defaultsReminder = checkIfDefaultUsed(parameters);
            Prism.messenger.sendMessage(call.getPlayer(), Prism.messenger.playerSubduedHeaderMsg(Il8nHelper.getMessage("queryparameter.defaults.prefix", defaultsReminder.toString())));
            plugin.getServer().getScheduler().runTaskAsynchronously(plugin, () -> {
                // Perform preview
                final ActionsQuery aq = new ActionsQuery(plugin);
                final QueryResult results = aq.lookup(parameters, call.getPlayer());
                // Rollback
                if (call.getArg(1).equalsIgnoreCase("rollback") || call.getArg(1).equalsIgnoreCase("rb")) {
                    handleRollBack(call, parameters, results, audience);
                    // todo remove debug
                    assert (parameters.getProcessType() == PrismProcessType.ROLLBACK);
                }
                // Restore
                if (call.getArg(1).equalsIgnoreCase("restore") || call.getArg(1).equalsIgnoreCase("rs")) {
                    handleRestore(call, parameters, results, audience);
                    // todo remove debug
                    assert (parameters.getProcessType() == PrismProcessType.RESTORE);
                }
            });
            return;
        }
        Prism.messenger.sendMessage(call.getPlayer(), Prism.messenger.playerError(Il8nHelper.getMessage("invalid-command")));
    }
}
Also used : PreviewSession(me.botsko.prism.appliers.PreviewSession) QueryResult(me.botsko.prism.actionlibs.QueryResult) Audience(net.kyori.adventure.audience.Audience) ActionsQuery(me.botsko.prism.actionlibs.ActionsQuery) QueryParameters(me.botsko.prism.actionlibs.QueryParameters)

Example 5 with ActionsQuery

use of me.botsko.prism.actionlibs.ActionsQuery in project Prism-Bukkit by prism.

the class OreMonitor method processAlertsFromBlock.

/**
 * Process alerts.
 *
 * @param player    Player
 * @param block     Block
 * @param alertPerm Players with this permission will receive the alert
 */
public void processAlertsFromBlock(final Player player, final Block block, final String alertPerm) {
    if (!plugin.getConfig().getBoolean("prism.alerts.ores.enabled")) {
        return;
    }
    if (player == null || player.getGameMode().equals(GameMode.CREATIVE)) {
        return;
    }
    if (block != null && isWatched(block) && !plugin.alertedBlocks.containsKey(block.getLocation())) {
        threshold = 1;
        // identify all ore blocks on same Y axis in x/z direction
        final ArrayList<Block> matchingBlocks = new ArrayList<>();
        final ArrayList<Block> foundores = findNeighborBlocks(block.getType(), block, matchingBlocks);
        if (!foundores.isEmpty()) {
            // Create alert message
            final String count = foundores.size() + (foundores.size() >= thresholdMax ? "+" : "");
            final String msg = player.getName() + " found " + count + " " + getOreNiceName(block) + " " + getLightLevel(block) + "% light";
            final TextComponent component = Component.text().content(msg).color(getOreColor(block)).hoverEvent(HoverEvent.hoverEvent(HoverEvent.Action.SHOW_ITEM, HoverEvent.ShowItem.of(Key.key(block.getType().getKey().toString()), 1))).build();
            plugin.getServer().getScheduler().runTaskAsynchronously(plugin, () -> {
                // Check if block placed
                // Build params
                final QueryParameters params = new QueryParameters();
                params.setWorld(player.getWorld().getName());
                params.addSpecificBlockLocation(block.getLocation());
                params.addActionType("block-place");
                final ActionsQuery aq = new ActionsQuery(plugin);
                final QueryResult results = aq.lookup(params, player);
                if (results.getActionResults().isEmpty()) {
                    // Block was not placed - Alert staff
                    plugin.alertPlayers(null, component, alertPerm);
                    // Log to console
                    if (plugin.getConfig().getBoolean("prism.alerts.ores.log-to-console")) {
                        Prism.log(PlainComponentSerializer.plain().serialize(component));
                    }
                    // Log to commands
                    List<String> commands = plugin.getConfig().getStringList("prism.alerts.ores.log-commands");
                    MiscUtils.dispatchAlert(msg, commands);
                }
            });
        }
    }
}
Also used : TextComponent(net.kyori.adventure.text.TextComponent) QueryResult(me.botsko.prism.actionlibs.QueryResult) ActionsQuery(me.botsko.prism.actionlibs.ActionsQuery) ArrayList(java.util.ArrayList) Block(org.bukkit.block.Block) QueryParameters(me.botsko.prism.actionlibs.QueryParameters)

Aggregations

ActionsQuery (me.botsko.prism.actionlibs.ActionsQuery)13 QueryParameters (me.botsko.prism.actionlibs.QueryParameters)11 QueryResult (me.botsko.prism.actionlibs.QueryResult)10 Handler (me.botsko.prism.api.actions.Handler)4 SubHandler (me.botsko.prism.commandlibs.SubHandler)4 ActionMessage (me.botsko.prism.actionlibs.ActionMessage)3 PrismApplierCallback (me.botsko.prism.appliers.PrismApplierCallback)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Previewable (me.botsko.prism.appliers.Previewable)2 Audience (net.kyori.adventure.audience.Audience)2 Block (org.bukkit.block.Block)2 Player (org.bukkit.entity.Player)2 CompletableFuture (java.util.concurrent.CompletableFuture)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 Pattern (java.util.regex.Pattern)1 Il8nHelper (me.botsko.prism.Il8nHelper)1 Prism (me.botsko.prism.Prism)1 PrismProcessAction (me.botsko.prism.actions.PrismProcessAction)1 Result (me.botsko.prism.api.Result)1