Search in sources :

Example 1 with BlockReportQueryBuilder

use of me.botsko.prism.database.mysql.BlockReportQueryBuilder 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)

Aggregations

Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 MatchRule (me.botsko.prism.actionlibs.MatchRule)1 QueryParameters (me.botsko.prism.actionlibs.QueryParameters)1 BlockReportQueryBuilder (me.botsko.prism.database.mysql.BlockReportQueryBuilder)1