Search in sources :

Example 6 with QueryParameters

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

the class RollbackWand method rollback.

/**
     * 
     * @param player
     * @param block
     */
protected void rollback(Player player, Location loc) {
    final Block block = loc.getBlock();
    plugin.eventTimer.recordTimedEvent("rollback wand used");
    // Build params
    QueryParameters params;
    try {
        params = parameters.clone();
    } catch (final CloneNotSupportedException ex) {
        params = new QueryParameters();
        player.sendMessage(Prism.messenger.playerError("Error retrieving parameters. Checking with default parameters."));
    }
    params.setWorld(player.getWorld().getName());
    params.setSpecificBlockLocation(block.getLocation());
    params.setLimit(1);
    params.setProcessType(PrismProcessType.ROLLBACK);
    boolean timeDefault = false;
    for (final String _default : params.getDefaultsUsed()) {
        if (_default.startsWith("t:")) {
            timeDefault = true;
        }
    }
    if (timeDefault) {
        params.setIgnoreTime(true);
    }
    final ActionsQuery aq = new ActionsQuery(plugin);
    final QueryResult results = aq.lookup(params, player);
    if (!results.getActionResults().isEmpty()) {
        final Rollback rb = new Rollback(plugin, player, results.getActionResults(), params, new PrismApplierCallback());
        rb.apply();
    } else {
        final String space_name = (block.getType().equals(Material.AIR) ? "space" : block.getType().toString().replaceAll("_", " ").toLowerCase() + (block.getType().toString().endsWith("BLOCK") ? "" : " block"));
        player.sendMessage(Prism.messenger.playerError("Nothing to rollback for this " + space_name + " found."));
    }
}
Also used : QueryResult(me.botsko.prism.actionlibs.QueryResult) PrismApplierCallback(me.botsko.prism.appliers.PrismApplierCallback) ActionsQuery(me.botsko.prism.actionlibs.ActionsQuery) Block(org.bukkit.block.Block) QueryParameters(me.botsko.prism.actionlibs.QueryParameters) Rollback(me.botsko.prism.appliers.Rollback)

Example 7 with QueryParameters

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

the class PreprocessArgs method process.

/**
     *
     */
public static QueryParameters process(Prism plugin, CommandSender sender, String[] args, PrismProcessType processType, int startAt, boolean useDefaults, boolean optional) {
    // Check for player or sender
    Player player = null;
    if (sender != null && sender instanceof Player) {
        player = (Player) sender;
    }
    // Start query
    final QueryParameters parameters = new QueryParameters();
    parameters.setProcessType(processType);
    // Define pagination/process type
    if (parameters.getProcessType().equals(PrismProcessType.LOOKUP)) {
        parameters.setLimit(plugin.getConfig().getInt("prism.queries.lookup-max-results"));
        parameters.setPerPage(plugin.getConfig().getInt("prism.queries.default-results-per-page"));
    }
    // Load registered parameters
    final HashMap<String, PrismParameterHandler> registeredParams = Prism.getParameters();
    // Store names of matched params/handlers
    final Set<String> foundArgsNames = new HashSet<String>();
    final List<MatchedParam> foundArgsList = new ArrayList<MatchedParam>();
    // Iterate all command arguments
    if (args == null) {
        return parameters;
    }
    for (int i = startAt; i < args.length; i++) {
        final String arg = args[i];
        if (arg.isEmpty())
            continue;
        if (parseParam(plugin, sender, parameters, registeredParams, foundArgsNames, foundArgsList, arg) == ParseResult.NotFound)
            return null;
    }
    parameters.setFoundArgs(foundArgsNames);
    // Reject no matches
    if (foundArgsList.isEmpty() && !optional) {
        if (sender != null)
            sender.sendMessage(Prism.messenger.playerError("You're missing valid parameters. Use /prism ? for assistance."));
        return null;
    }
    /**
         * Call default method for handlers *not* used
         */
    if (useDefaults) {
        for (final Entry<String, PrismParameterHandler> entry : registeredParams.entrySet()) {
            if (!foundArgsNames.contains(entry.getKey().toLowerCase())) {
                entry.getValue().defaultTo(parameters, sender);
            }
        }
    }
    /**
         * Send arguments to parameter handlers
         */
    for (final MatchedParam matchedParam : foundArgsList) {
        try {
            final PrismParameterHandler handler = matchedParam.getHandler();
            handler.process(parameters, matchedParam.getArg(), sender);
        } catch (final IllegalArgumentException e) {
            if (sender != null)
                sender.sendMessage(Prism.messenger.playerError(e.getMessage()));
            return null;
        }
    }
    // Enforce specifying an action
    if (sender != null && sender.hasPermission("prism.parameters.action.required") && parameters.getActionTypes().isEmpty()) {
        sender.sendMessage(Prism.messenger.playerError("You're missing valid actions. Use /prism ? for assistance."));
        return null;
    }
    // Player location
    if (player != null && !plugin.getConfig().getBoolean("prism.queries.never-use-defaults") && parameters.getPlayerLocation() == null && (parameters.getMaxLocation() == null || parameters.getMinLocation() == null)) {
        parameters.setMinMaxVectorsFromPlayerLocation(player.getLocation());
    }
    return parameters;
}
Also used : Player(org.bukkit.entity.Player) QueryParameters(me.botsko.prism.actionlibs.QueryParameters) PrismParameterHandler(me.botsko.prism.parameters.PrismParameterHandler)

Example 8 with QueryParameters

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

the class NearCommand method handle.

/**
     * Handle the command
     */
@Override
public void handle(final CallInfo call) {
    // Build params
    final QueryParameters parameters = new QueryParameters();
    parameters.setPerPage(plugin.getConfig().getInt("prism.queries.default-results-per-page"));
    parameters.setWorld(call.getPlayer().getWorld().getName());
    // allow a custom near radius
    int radius = plugin.getConfig().getInt("prism.near.default-radius");
    if (call.getArgs().length == 2) {
        if (TypeUtils.isNumeric(call.getArg(1))) {
            final int _tmp_radius = Integer.parseInt(call.getArg(1));
            if (_tmp_radius > 0) {
                radius = _tmp_radius;
            } else {
                call.getPlayer().sendMessage(Prism.messenger.playerError("Radius must be greater than zero. Or leave it off to use the default. Use /prism ? for help."));
                return;
            }
        } else {
            call.getPlayer().sendMessage(Prism.messenger.playerError("Radius must be a number. Or leave it off to use the default. Use /prism ? for help."));
            return;
        }
    }
    parameters.setRadius(radius);
    parameters.setMinMaxVectorsFromPlayerLocation(call.getPlayer().getLocation());
    parameters.setLimit(plugin.getConfig().getInt("prism.near.max-results"));
    /**
         * 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() {
            final ActionsQuery aq = new ActionsQuery(plugin);
            final QueryResult results = aq.lookup(parameters, call.getPlayer());
            if (!results.getActionResults().isEmpty()) {
                call.getPlayer().sendMessage(Prism.messenger.playerSubduedHeaderMsg("All changes within " + parameters.getRadius() + " blocks of you..."));
                call.getPlayer().sendMessage(Prism.messenger.playerHeaderMsg("Showing " + results.getTotalResults() + " results. Page 1 of " + results.getTotal_pages()));
                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);
                        call.getPlayer().sendMessage(Prism.messenger.playerMsg(am.getMessage()));
                        result_count++;
                    }
                    // Flush timed data
                    plugin.eventTimer.printTimeRecord();
                } else {
                    call.getPlayer().sendMessage(Prism.messenger.playerError("Pagination can't find anything. Do you have the right page number?"));
                }
            } else {
                call.getPlayer().sendMessage(Prism.messenger.playerError("Couldn't find anything."));
            }
        }
    });
}
Also used : QueryResult(me.botsko.prism.actionlibs.QueryResult) ActionsQuery(me.botsko.prism.actionlibs.ActionsQuery) ActionMessage(me.botsko.prism.actionlibs.ActionMessage) SubHandler(me.botsko.prism.commandlibs.SubHandler) Handler(me.botsko.prism.actions.Handler) List(java.util.List) QueryParameters(me.botsko.prism.actionlibs.QueryParameters)

Example 9 with QueryParameters

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

the class ReportCommand method blockSumReports.

/**
     * 
     * @param sender
     */
protected void blockSumReports(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) {
        call.getSender().sendMessage(Prism.messenger.playerError("You must specify parameters, at least one player."));
        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 BlockReportQueryBuilder reportQuery = new BlockReportQueryBuilder(plugin);
    final String sql = reportQuery.getQuery(parameters, false);
    final int colTextLen = 20;
    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 block change 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.playerHeaderMsg("Total block changes for " + ChatColor.DARK_AQUA + playerName));
                call.getSender().sendMessage(Prism.messenger.playerMsg(ChatColor.GRAY + TypeUtils.padStringRight("Block", colTextLen) + TypeUtils.padStringRight("Placed", colIntLen) + TypeUtils.padStringRight("Broken", colIntLen)));
                while (rs.next()) {
                    final String alias = Prism.getItems().getAlias(rs.getInt(1), 0);
                    final int placed = rs.getInt(2);
                    final int broken = rs.getInt(3);
                    final String colAlias = TypeUtils.padStringRight(alias, colTextLen);
                    final String colPlaced = TypeUtils.padStringRight("" + placed, colIntLen);
                    final String colBroken = TypeUtils.padStringRight("" + broken, colIntLen);
                    call.getSender().sendMessage(Prism.messenger.playerMsg(ChatColor.DARK_AQUA + colAlias + ChatColor.GREEN + colPlaced + " " + ChatColor.RED + colBroken));
                }
            } 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 : SQLException(java.sql.SQLException) BlockReportQueryBuilder(me.botsko.prism.database.mysql.BlockReportQueryBuilder) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) QueryParameters(me.botsko.prism.actionlibs.QueryParameters) MatchRule(me.botsko.prism.actionlibs.MatchRule)

Example 10 with QueryParameters

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

the class RestoreCommand method handle.

/**
     * Handle the command
     */
@Override
public void handle(final CallInfo call) {
    final QueryParameters parameters = PreprocessArgs.process(plugin, call.getSender(), call.getArgs(), PrismProcessType.RESTORE, 1, !plugin.getConfig().getBoolean("prism.queries.never-use-defaults"));
    if (parameters == null) {
        return;
    }
    parameters.setProcessType(PrismProcessType.RESTORE);
    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;
        }
    }
    call.getSender().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() {
            final ActionsQuery aq = new ActionsQuery(plugin);
            final QueryResult results = aq.lookup(parameters, call.getSender());
            if (!results.getActionResults().isEmpty()) {
                call.getSender().sendMessage(Prism.messenger.playerHeaderMsg("Restoring changes..."));
                // Inform nearby players
                if (call.getSender() instanceof Player) {
                    final Player player = (Player) call.getSender();
                    plugin.notifyNearby(player, parameters.getRadius(), player.getDisplayName() + " is re-applying block changes nearby. Just so you know.");
                }
                // Perform restore on the main thread
                plugin.getServer().getScheduler().runTask(plugin, new Runnable() {

                    @Override
                    public void run() {
                        final Restore rs = new Restore(plugin, call.getSender(), results.getActionResults(), parameters, new PrismApplierCallback());
                        rs.apply();
                    }
                });
            } else {
                call.getSender().sendMessage(Prism.messenger.playerError("Nothing found to restore. Try using /prism l (args) first."));
            }
        }
    });
}
Also used : QueryResult(me.botsko.prism.actionlibs.QueryResult) Player(org.bukkit.entity.Player) PrismApplierCallback(me.botsko.prism.appliers.PrismApplierCallback) ActionsQuery(me.botsko.prism.actionlibs.ActionsQuery) QueryParameters(me.botsko.prism.actionlibs.QueryParameters) Restore(me.botsko.prism.appliers.Restore)

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