Search in sources :

Example 1 with QueryParameters

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

the class DeleteCommand method handle.

/**
     * Handle the command
     */
@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();
            call.getSender().sendMessage(Prism.messenger.playerMsg("Current purge tasks have been canceled."));
        } else {
            call.getSender().sendMessage(Prism.messenger.playerError("No purge task is currently 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();
            call.getSender().sendMessage(Prism.messenger.playerSuccess("Unwritten data in queue cleared."));
        } else {
            call.getSender().sendMessage(Prism.messenger.playerError("Event queue is empty, nothing to wipe."));
        }
        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);
    // determine if defaults were used
    final ArrayList<String> defaultsUsed = parameters.getDefaultsUsed();
    String defaultsReminder = "";
    if (!defaultsUsed.isEmpty()) {
        defaultsReminder += " using defaults:";
        for (final String d : defaultsUsed) {
            defaultsReminder += " " + d;
        }
    }
    if (parameters.getFoundArgs().size() > 0) {
        // Identify the minimum for chunking
        final int minId = PurgeChunkingUtil.getMinimumPrimaryKey();
        if (minId == 0) {
            call.getSender().sendMessage(Prism.messenger.playerError("No minimum primary key could be found for purge chunking"));
            return;
        }
        // Identify the max id for chunking
        final int maxId = PurgeChunkingUtil.getMaximumPrimaryKey();
        if (maxId == 0) {
            call.getSender().sendMessage(Prism.messenger.playerError("No maximum primary key could be found for purge chunking"));
            return;
        }
        call.getSender().sendMessage(Prism.messenger.playerSubduedHeaderMsg("Purging data..." + defaultsReminder));
        int purge_tick_delay = plugin.getConfig().getInt("prism.purge.batch-tick-delay");
        if (purge_tick_delay < 1) {
            purge_tick_delay = 20;
        }
        call.getSender().sendMessage(Prism.messenger.playerHeaderMsg("Starting purge cycle." + ChatColor.GRAY + " No one will ever know..."));
        // 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<QueryParameters>();
        paramList.add(parameters);
        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, purge_tick_delay, minId, maxId, callback));
    } else {
        call.getSender().sendMessage(Prism.messenger.playerError("You must supply at least one parameter."));
    }
}
Also used : 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 QueryParameters

use of me.botsko.prism.actionlibs.QueryParameters 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, new Runnable() {

        @Override
        public void run() {
            // determine if defaults were used
            final ArrayList<String> defaultsUsed = parameters.getDefaultsUsed();
            String defaultsReminder = "";
            if (!defaultsUsed.isEmpty()) {
                defaultsReminder += "Using defaults:";
                for (final String d : defaultsUsed) {
                    defaultsReminder += " " + d;
                }
            }
            final ActionsQuery aq = new ActionsQuery(plugin);
            final QueryResult results = aq.lookup(parameters, call.getSender());
            String sharingWithPlayers = "";
            for (final CommandSender shareWith : parameters.getSharedPlayers()) {
                sharingWithPlayers += shareWith.getName() + ", ";
            }
            sharingWithPlayers = sharingWithPlayers.substring(0, sharingWithPlayers.isEmpty() ? 0 : sharingWithPlayers.length() - 2);
            // Add current sender
            parameters.addSharedPlayer(call.getSender());
            for (final CommandSender player : parameters.getSharedPlayers()) {
                final boolean isSender = player.getName().equals(call.getSender().getName());
                if (!isSender) {
                    player.sendMessage(Prism.messenger.playerHeaderMsg(ChatColor.YELLOW + "" + ChatColor.ITALIC + call.getSender().getName() + ChatColor.GOLD + " shared these Prism lookup logs with you:"));
                } else if (!sharingWithPlayers.isEmpty()) {
                    player.sendMessage(Prism.messenger.playerHeaderMsg(ChatColor.GOLD + "Sharing results with players: " + ChatColor.YELLOW + "" + ChatColor.ITALIC + sharingWithPlayers));
                }
                if (!results.getActionResults().isEmpty()) {
                    player.sendMessage(Prism.messenger.playerHeaderMsg("Showing " + results.getTotalResults() + " results. Page 1 of " + results.getTotal_pages()));
                    if (!defaultsReminder.isEmpty() && isSender) {
                        player.sendMessage(Prism.messenger.playerSubduedHeaderMsg(defaultsReminder));
                    }
                    final List<Handler> paginated = results.getPaginatedActionResults();
                    if (paginated != null) {
                        int result_count = results.getIndexOfFirstResult();
                        for (final Handler a : paginated) {
                            final ActionMessage am = new ActionMessage(a);
                            if (parameters.allowsNoRadius() || parameters.hasFlag(Flag.EXTENDED) || plugin.getConfig().getBoolean("prism.messenger.always-show-extended")) {
                                am.showExtended();
                            }
                            am.setResultIndex(result_count);
                            player.sendMessage(Prism.messenger.playerMsg(am.getMessage()));
                            result_count++;
                        }
                    } else {
                        player.sendMessage(Prism.messenger.playerError("Pagination can't find anything. Do you have the right page number?"));
                    }
                    if (parameters.hasFlag(Flag.PASTE)) {
                        String paste = "";
                        for (final Handler a : results.getActionResults()) {
                            paste += new ActionMessage(a).getRawMessage() + "\r\n";
                        }
                        player.sendMessage(MiscUtils.paste_results(plugin, paste));
                    }
                } else {
                    if (!defaultsReminder.isEmpty()) {
                        if (isSender) {
                            player.sendMessage(Prism.messenger.playerSubduedHeaderMsg(defaultsReminder));
                        }
                    }
                    if (isSender) {
                        player.sendMessage(Prism.messenger.playerError("Nothing found." + ChatColor.GRAY + " Either you're missing something, or we are."));
                    }
                }
            }
            // Flush timed data
            plugin.eventTimer.printTimeRecord();
        }
    });
}
Also used : QueryResult(me.botsko.prism.actionlibs.QueryResult) ActionsQuery(me.botsko.prism.actionlibs.ActionsQuery) ArrayList(java.util.ArrayList) ActionMessage(me.botsko.prism.actionlibs.ActionMessage) Handler(me.botsko.prism.actions.Handler) SubHandler(me.botsko.prism.commandlibs.SubHandler) CommandSender(org.bukkit.command.CommandSender) ArrayList(java.util.ArrayList) List(java.util.List) QueryParameters(me.botsko.prism.actionlibs.QueryParameters)

Example 3 with QueryParameters

use of me.botsko.prism.actionlibs.QueryParameters 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);
    boolean cycle_complete = false;
    // 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;
    int newMinId = minId + spread;
    param.setMinPrimaryKey(minId);
    param.setMaxPrimaryKey(newMinId);
    cycle_rows_affected = aq.delete(param);
    plugin.total_records_affected += cycle_rows_affected;
    // need to move on
    if (newMinId > maxId) {
        // Remove rule, reset affected count, mark complete
        paramList.remove(param);
        cycle_complete = true;
    }
    Prism.debug("------------------- " + param.getOriginalCommand());
    Prism.debug("minId: " + minId);
    Prism.debug("maxId: " + maxId);
    Prism.debug("newMinId: " + newMinId);
    Prism.debug("cycle_rows_affected: " + cycle_rows_affected);
    Prism.debug("cycle_complete: " + cycle_complete);
    Prism.debug("plugin.total_records_affected: " + plugin.total_records_affected);
    Prism.debug("-------------------");
    // Send cycle to callback
    callback.cycle(param, cycle_rows_affected, plugin.total_records_affected, cycle_complete);
    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, or reset counts
    if (!cycle_complete) {
        plugin.getPurgeManager().deleteTask = plugin.getServer().getScheduler().runTaskLaterAsynchronously(plugin, new PurgeTask(plugin, paramList, purge_tick_delay, newMinId, maxId, callback), purge_tick_delay);
    } else {
        // reset counts
        plugin.total_records_affected = 0;
        if (paramList.isEmpty())
            return;
        Prism.log("Moving on to next purge rule...");
        // Identify the minimum for chunking
        newMinId = PurgeChunkingUtil.getMinimumPrimaryKey();
        if (newMinId == 0) {
            Prism.log("No minimum primary key could be found for purge chunking.");
            return;
        }
        // schedule a new task with next param
        plugin.getPurgeManager().deleteTask = plugin.getServer().getScheduler().runTaskLaterAsynchronously(plugin, new PurgeTask(plugin, paramList, purge_tick_delay, newMinId, maxId, callback), purge_tick_delay);
    }
}
Also used : ActionsQuery(me.botsko.prism.actionlibs.ActionsQuery) QueryParameters(me.botsko.prism.actionlibs.QueryParameters)

Example 4 with QueryParameters

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

the class PreviewCommand method handle.

/**
     * Handle the command
     */
@Override
public void handle(final CallInfo call) {
    if (call.getArgs().length >= 2) {
        /**
             * Apply previous preview changes
             */
        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 {
                call.getPlayer().sendMessage(Prism.messenger.playerError("You have no preview pending."));
            }
            return;
        }
        /**
             * Cancel preview
             */
        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 {
                call.getPlayer().sendMessage(Prism.messenger.playerError("You have no preview pending."));
            }
            return;
        }
        // Ensure no current preview is waiting
        if (plugin.playerActivePreviews.containsKey(call.getPlayer().getName())) {
            call.getPlayer().sendMessage(Prism.messenger.playerError("You have an existing preview pending. Please apply or cancel before moving on."));
            return;
        }
        /**
             * Begin a rollback or restore preview
             */
        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")) {
                call.getPlayer().sendMessage(Prism.messenger.playerError("Prism does not support previews for WorldEdit rollbacks/restores yet."));
                return;
            }
            // determine if defaults were used
            final ArrayList<String> defaultsUsed = parameters.getDefaultsUsed();
            String defaultsReminder = "";
            if (!defaultsUsed.isEmpty()) {
                defaultsReminder += " using defaults:";
                for (final String d : defaultsUsed) {
                    defaultsReminder += " " + d;
                }
            }
            call.getPlayer().sendMessage(Prism.messenger.playerSubduedHeaderMsg("Preparing results..." + defaultsReminder));
            /**
                 * Run the query itself in an async task so the lookup query isn't done
                 * on the main thread
                 */
            plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() {

                @Override
                public void run() {
                    // 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")) {
                        parameters.setProcessType(PrismProcessType.ROLLBACK);
                        if (!results.getActionResults().isEmpty()) {
                            call.getPlayer().sendMessage(Prism.messenger.playerHeaderMsg("Beginning preview..."));
                            // Perform preview on the main thread
                            plugin.getServer().getScheduler().runTask(plugin, new Runnable() {

                                @Override
                                public void run() {
                                    final Previewable rs = new Rollback(plugin, call.getPlayer(), results.getActionResults(), parameters, new PrismApplierCallback());
                                    rs.preview();
                                }
                            });
                        } else {
                            call.getPlayer().sendMessage(Prism.messenger.playerError("Nothing found to preview."));
                        }
                    }
                    // Restore
                    if (call.getArg(1).equalsIgnoreCase("restore") || call.getArg(1).equalsIgnoreCase("rs")) {
                        parameters.setProcessType(PrismProcessType.RESTORE);
                        if (!results.getActionResults().isEmpty()) {
                            call.getPlayer().sendMessage(Prism.messenger.playerHeaderMsg("Beginning preview..."));
                            // Perform preview on the main thread
                            plugin.getServer().getScheduler().runTask(plugin, new Runnable() {

                                @Override
                                public void run() {
                                    final Previewable rs = new Restore(plugin, call.getPlayer(), results.getActionResults(), parameters, new PrismApplierCallback());
                                    rs.preview();
                                }
                            });
                        } else {
                            call.getPlayer().sendMessage(Prism.messenger.playerError("Nothing found to preview."));
                        }
                    }
                }
            });
            return;
        }
        call.getPlayer().sendMessage(Prism.messenger.playerError("Invalid command. Check /prism ? for help."));
    }
}
Also used : QueryResult(me.botsko.prism.actionlibs.QueryResult) ActionsQuery(me.botsko.prism.actionlibs.ActionsQuery) QueryParameters(me.botsko.prism.actionlibs.QueryParameters)

Example 5 with QueryParameters

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

the class ReportCommand method actionTypeCountReport.

/**
     * 
     * @param sender
     */
protected void actionTypeCountReport(final CallInfo call) {
    // Process and validate all of the arguments
    final QueryParameters parameters = PreprocessArgs.process(plugin, call.getSender(), call.getArgs(), PrismProcessType.LOOKUP, 3, !plugin.getConfig().getBoolean("prism.queries.never-use-defaults"));
    if (parameters == null) {
        return;
    }
    // No actions
    if (!parameters.getActionTypes().isEmpty()) {
        call.getSender().sendMessage(Prism.messenger.playerError("You may not specify any action types for this report."));
        return;
    }
    // Verify single player name for now
    final HashMap<String, MatchRule> players = parameters.getPlayerNames();
    if (players.size() != 1) {
        call.getSender().sendMessage(Prism.messenger.playerError("You must provide only a single player name."));
        return;
    }
    // Get single playername
    String tempName = "";
    for (final String player : players.keySet()) {
        tempName = player;
        break;
    }
    final String playerName = tempName;
    final ActionReportQueryBuilder reportQuery = new ActionReportQueryBuilder(plugin);
    final String sql = reportQuery.getQuery(parameters, false);
    final int colTextLen = 16;
    final int colIntLen = 12;
    /**
         * Run the lookup itself in an async task so the lookup query isn't done
         * on the main thread
         */
    plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() {

        @Override
        public void run() {
            call.getSender().sendMessage(Prism.messenger.playerSubduedHeaderMsg("Crafting action type report for " + ChatColor.DARK_AQUA + playerName + "..."));
            Connection conn = null;
            PreparedStatement s = null;
            ResultSet rs = null;
            try {
                conn = Prism.dbc();
                s = conn.prepareStatement(sql);
                s.executeQuery();
                rs = s.getResultSet();
                call.getSender().sendMessage(Prism.messenger.playerMsg(ChatColor.GRAY + TypeUtils.padStringRight("Action", colTextLen) + TypeUtils.padStringRight("Count", colIntLen)));
                while (rs.next()) {
                    final String action = rs.getString(2);
                    final int count = rs.getInt(1);
                    final String colAlias = TypeUtils.padStringRight(action, colTextLen);
                    final String colPlaced = TypeUtils.padStringRight("" + count, colIntLen);
                    call.getSender().sendMessage(Prism.messenger.playerMsg(ChatColor.DARK_AQUA + colAlias + ChatColor.GREEN + colPlaced));
                }
            } catch (final SQLException e) {
                e.printStackTrace();
            } finally {
                if (rs != null)
                    try {
                        rs.close();
                    } catch (final SQLException ignored) {
                    }
                if (s != null)
                    try {
                        s.close();
                    } catch (final SQLException ignored) {
                    }
                if (conn != null)
                    try {
                        conn.close();
                    } catch (final SQLException ignored) {
                    }
            }
        }
    });
}
Also used : ActionReportQueryBuilder(me.botsko.prism.database.mysql.ActionReportQueryBuilder) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) QueryParameters(me.botsko.prism.actionlibs.QueryParameters) MatchRule(me.botsko.prism.actionlibs.MatchRule)

Aggregations

QueryParameters (me.botsko.prism.actionlibs.QueryParameters)18 ActionsQuery (me.botsko.prism.actionlibs.ActionsQuery)12 QueryResult (me.botsko.prism.actionlibs.QueryResult)11 PrismApplierCallback (me.botsko.prism.appliers.PrismApplierCallback)5 ActionMessage (me.botsko.prism.actionlibs.ActionMessage)4 Handler (me.botsko.prism.actions.Handler)4 SubHandler (me.botsko.prism.commandlibs.SubHandler)4 Block (org.bukkit.block.Block)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Player (org.bukkit.entity.Player)3 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 MatchRule (me.botsko.prism.actionlibs.MatchRule)2 Restore (me.botsko.prism.appliers.Restore)2 Rollback (me.botsko.prism.appliers.Rollback)2 PrismProcessAction (me.botsko.prism.actions.PrismProcessAction)1