Search in sources :

Example 16 with Handler

use of me.botsko.prism.api.actions.Handler in project Prism-Bukkit by prism.

the class TeleportCommand method handle.

/**
 * Handle the command.
 */
@Override
public void handle(CallInfo call) {
    // Is there anything even stored to paginate?
    String keyName = "console";
    if (call.getSender() instanceof Player) {
        keyName = call.getSender().getName();
    }
    if (!plugin.cachedQueries.containsKey(keyName) && !call.getArg(1).contains("id:")) {
        Prism.messenger.sendMessage(call.getSender(), Prism.messenger.playerError("There's no saved query to use results from. Maybe they expired? Try your lookup again."));
        return;
    }
    // Parse the incoming ident
    String ident = call.getArg(1);
    if (ident.contains("id:")) {
        ident = ident.replace("id:", "");
    }
    // Determine result index to tp to - either an id, or the next/previous
    // id
    long recordId;
    if (ident.equals("next") || ident.equals("prev")) {
        // Get stored results
        final QueryResult results = plugin.cachedQueries.get(keyName);
        recordId = results.getLastTeleportIndex();
        recordId = (recordId == 0 ? 1 : recordId);
        if (recordId > 0) {
            if (ident.equals("next")) {
                recordId++;
            } else {
                if (recordId > 1) {
                    recordId--;
                }
            }
        }
    } else {
        if (!TypeUtils.isNumeric(ident)) {
            Prism.messenger.sendMessage(call.getPlayer(), Prism.messenger.playerError("You must provide a numeric result number or record ID to teleport to."));
            return;
        }
        recordId = Integer.parseInt(ident);
        if (recordId <= 0) {
            Prism.messenger.sendMessage(call.getPlayer(), Prism.messenger.playerError("Result number or record ID must be greater than zero."));
            return;
        }
    }
    // If a record id provided, re-query the database
    final Handler destinationAction;
    if (call.getArg(1).contains("id:")) {
        // Build params
        final QueryParameters params = new QueryParameters();
        params.setWorld(call.getPlayer().getWorld().getName());
        params.setId(recordId);
        // Query
        final ActionsQuery aq = new ActionsQuery(plugin);
        final QueryResult results = aq.lookup(params, call.getPlayer());
        if (results.getActionResults().isEmpty()) {
            Prism.messenger.sendMessage(call.getPlayer(), Prism.messenger.playerError("No records exists with this ID."));
            return;
        }
        // Get the first result
        destinationAction = results.getActionResults().get(0);
    } else {
        // Get stored results
        final QueryResult results = plugin.cachedQueries.get(keyName);
        if (recordId > results.getActionResults().size()) {
            Prism.messenger.sendMessage(call.getPlayer(), Prism.messenger.playerError("No records exists at this index. Did you mean /pr tp id:" + recordId + " instead?"));
            return;
        }
        final int key = (int) (recordId - 1);
        // Get the result index specified
        destinationAction = results.getActionResults().get(key);
        // Refresh the query time and replace
        results.setQueryTime();
        results.setLastTeleportIndex(recordId);
        plugin.cachedQueries.replace(keyName, results);
    }
    if (destinationAction != null) {
        final World world = destinationAction.getLoc().getWorld();
        if (world == null) {
            Prism.messenger.sendMessage(call.getPlayer(), Prism.messenger.playerError("Action record occurred in world we can't find anymore."));
            return;
        }
        if (PaperLib.isPaper()) {
            PaperLib.teleportAsync(call.getPlayer(), destinationAction.getLoc()).thenAccept(success -> sendTeleportCompleteMessage(success, call.getPlayer(), destinationAction));
        } else {
            sendTeleportCompleteMessage(call.getPlayer().teleport(destinationAction.getLoc()), call.getPlayer(), destinationAction);
        }
    }
}
Also used : Player(org.bukkit.entity.Player) QueryResult(me.botsko.prism.actionlibs.QueryResult) ActionsQuery(me.botsko.prism.actionlibs.ActionsQuery) Handler(me.botsko.prism.api.actions.Handler) SubHandler(me.botsko.prism.commandlibs.SubHandler) QueryParameters(me.botsko.prism.actionlibs.QueryParameters) World(org.bukkit.World)

Example 17 with Handler

use of me.botsko.prism.api.actions.Handler in project Prism-Bukkit by prism.

the class PrismMiscEvents method onPrismBlocksExtinguishEvent.

/**
 * PrismExtinguishEvent.
 * @param event PrismExtinguishEvent.
 */
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPrismBlocksExtinguishEvent(final PrismExtinguishEvent event) {
    // Get all block changes for this event
    final ArrayList<BlockStateChange> blockStateChanges = event.getBlockStateChanges();
    if (!blockStateChanges.isEmpty()) {
        // Create an entry for the rollback as a whole
        final Handler primaryAction = ActionFactory.createPrismProcess("prism-process", PrismProcessType.EXTINGUISH, event.onBehalfOf(), "" + event.getRadius());
        final long id = RecordingTask.insertActionIntoDatabase(primaryAction);
        if (id == 0) {
            return;
        }
        for (final BlockStateChange stateChange : blockStateChanges) {
            final BlockState orig = stateChange.getOriginalBlock();
            final BlockState newBlock = stateChange.getNewBlock();
            // Build the action
            RecordingQueue.addToQueue(ActionFactory.createPrismRollback("prism-extinguish", orig, newBlock, event.onBehalfOf(), id));
        }
    // ActionQueue.save();
    }
}
Also used : BlockStateChange(me.botsko.prism.api.BlockStateChange) BlockState(org.bukkit.block.BlockState) Handler(me.botsko.prism.api.actions.Handler) EventHandler(org.bukkit.event.EventHandler) EventHandler(org.bukkit.event.EventHandler)

Example 18 with Handler

use of me.botsko.prism.api.actions.Handler in project Prism-Bukkit by prism.

the class SqlInsertBuilder method processExtraData.

/**
 * Process any extra data associated with the ResultSet.
 * @param keys ResultSet
 * @throws SQLException SQLException.
 */
public void processExtraData(ResultSet keys) throws SQLException {
    if (extraDataQueue.isEmpty()) {
        return;
    }
    try (Connection conn = dataSource.getConnection();
        PreparedStatement s = conn.prepareStatement("INSERT INTO `" + prefix + "data_extra` (data_id,data) VALUES (?,?)", Statement.RETURN_GENERATED_KEYS)) {
        conn.setAutoCommit(false);
        int i = 0;
        while (keys.next()) {
            // @todo should not happen
            if (i >= extraDataQueue.size()) {
                Prism.log("Skipping extra data for " + prefix + "data.id " + keys.getLong(1) + " because the queue doesn't have data for it.");
                continue;
            }
            final Handler a = extraDataQueue.get(i);
            if (a.hasExtraData()) {
                String serialData = a.serialize();
                if (serialData != null && !serialData.isEmpty()) {
                    s.setLong(1, keys.getLong(1));
                    s.setString(2, serialData);
                    s.addBatch();
                }
            } else {
                Prism.debug("Skipping extra data for " + prefix + "data.id " + keys.getLong(1) + " because the queue doesn't have data for it.");
            }
            i++;
        }
        // The main delay is here
        s.executeBatch();
        if (conn.isClosed()) {
            Prism.log("Prism database error. We have to bail in the middle of building extra " + "data bulk insert query.");
        } else {
            conn.commit();
        }
    } catch (final SQLException e) {
        e.printStackTrace();
        Prism.getPrismDataSource().handleDataSourceException(e);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) Handler(me.botsko.prism.api.actions.Handler) PreparedStatement(java.sql.PreparedStatement)

Example 19 with Handler

use of me.botsko.prism.api.actions.Handler in project Prism-Bukkit by prism.

the class NearCommand method handle.

@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 {
                Prism.messenger.sendMessage(call.getPlayer(), Prism.messenger.playerError("Radius must be greater than zero. Or leave it off to use the default. " + "Use /prism ? for help."));
                return;
            }
        } else {
            Prism.messenger.sendMessage(call.getPlayer(), 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"));
    plugin.getServer().getScheduler().runTaskAsynchronously(plugin, () -> {
        final ActionsQuery aq = new ActionsQuery(plugin);
        final QueryResult results = aq.lookup(parameters, call.getPlayer());
        if (!results.getActionResults().isEmpty()) {
            Prism.messenger.sendMessage(call.getPlayer(), Prism.messenger.playerSubduedHeaderMsg(Il8nHelper.formatMessage("near-result-report", parameters.getRadius())));
            Prism.messenger.sendMessage(call.getPlayer(), Prism.messenger.playerHeaderMsg(Il8nHelper.formatMessage("lookup-header-message", results.getTotalResults(), 1, results.getTotalPages())));
            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, call.getPlayer());
                    resultCount++;
                }
                MiscUtils.sendPageButtons(results, call.getPlayer());
                // Flush timed data
                plugin.eventTimer.printTimeRecord();
            } else {
                Prism.messenger.sendMessage(call.getPlayer(), Prism.messenger.playerError("Pagination can't find anything. Do you have the right page number?"));
            }
        } else {
            Prism.messenger.sendMessage(call.getPlayer(), 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) Handler(me.botsko.prism.api.actions.Handler) SubHandler(me.botsko.prism.commandlibs.SubHandler) QueryParameters(me.botsko.prism.actionlibs.QueryParameters)

Example 20 with Handler

use of me.botsko.prism.api.actions.Handler in project Prism-Bukkit by prism.

the class PageCommand method handle.

/**
 * {@inheritDoc}
 */
@Override
public void handle(CallInfo call) {
    // Is there anything even stored to paginate?
    String keyName = "console";
    if (call.getSender() instanceof Player) {
        keyName = call.getSender().getName();
    }
    if (!plugin.cachedQueries.containsKey(keyName)) {
        Prism.messenger.sendMessage(call.getSender(), Prism.messenger.playerError("There's no saved query to paginate. Maybe they expired? Try your lookup again."));
        return;
    }
    // Get stored results
    final QueryResult results = plugin.cachedQueries.get(keyName);
    if (call.getArgs().length != 2) {
        Prism.getAudiences().sender(call.getSender()).sendMessage(Identity.nil(), Prism.messenger.playerError("Please specify a page number. Like /prism page 2"));
        return;
    }
    // Determine page number
    int page;
    if (TypeUtils.isNumeric(call.getArg(1))) {
        page = Integer.parseInt(call.getArg(1));
    } else {
        if (call.getArg(1).equals("next") || call.getArg(1).equals("n")) {
            page = results.getPage() + 1;
        } else if (call.getArg(1).equals("prev") || call.getArg(1).equals("p")) {
            if (results.getPage() <= 1) {
                Prism.messenger.sendMessage(call.getSender(), Prism.messenger.playerError("There is no previous page."));
                return;
            }
            page = results.getPage() - 1;
        } else {
            Prism.messenger.sendMessage(call.getSender(), Prism.messenger.playerError("Page numbers need to actually be numbers, or next/prev." + " Like /prism page 2"));
            return;
        }
    }
    // No negatives
    if (page <= 0) {
        Prism.messenger.sendMessage(call.getSender(), Prism.messenger.playerError("Page must be greater than zero."));
        return;
    }
    results.setPage(page);
    // Refresh the query time and replace
    results.setQueryTime();
    plugin.cachedQueries.replace(keyName, results);
    // Results?
    if (results.getActionResults().isEmpty()) {
        Prism.messenger.sendMessage(call.getSender(), Prism.messenger.playerError("Nothing found." + ChatColor.GRAY + " Either you're missing something, or we are."));
        return;
    }
    Prism.messenger.sendMessage(call.getSender(), Prism.messenger.playerHeaderMsg(Il8nHelper.formatMessage("lookup-header-message", results.getTotalResults(), results.getPage(), results.getTotalPages())));
    final List<Handler> paginated = results.getPaginatedActionResults();
    if (paginated == null || paginated.size() == 0) {
        Prism.messenger.sendMessage(call.getSender(), Prism.messenger.playerError("Pagination can't find anything. " + "Do you have the right page number?"));
        return;
    }
    // Show it!
    int resultCount = results.getIndexOfFirstResult();
    for (final Handler a : paginated) {
        final ActionMessage am = new ActionMessage(a);
        if (results.getParameters().hasFlag(Flag.EXTENDED) || plugin.getConfig().getBoolean("prism.messenger.always-show-extended")) {
            am.showExtended();
        }
        am.setResultIndex(resultCount);
        MiscUtils.sendClickableTpRecord(am, call.getSender());
        resultCount++;
    }
    MiscUtils.sendPageButtons(results, call.getSender());
}
Also used : Player(org.bukkit.entity.Player) QueryResult(me.botsko.prism.actionlibs.QueryResult) ActionMessage(me.botsko.prism.actionlibs.ActionMessage) Handler(me.botsko.prism.api.actions.Handler) SubHandler(me.botsko.prism.commandlibs.SubHandler)

Aggregations

Handler (me.botsko.prism.api.actions.Handler)20 QueryResult (me.botsko.prism.actionlibs.QueryResult)6 SubHandler (me.botsko.prism.commandlibs.SubHandler)5 ActionMessage (me.botsko.prism.actionlibs.ActionMessage)4 ActionsQuery (me.botsko.prism.actionlibs.ActionsQuery)4 QueryParameters (me.botsko.prism.actionlibs.QueryParameters)4 EventHandler (org.bukkit.event.EventHandler)4 Connection (java.sql.Connection)3 SQLException (java.sql.SQLException)3 Player (org.bukkit.entity.Player)3 PreparedStatement (java.sql.PreparedStatement)2 BlockStateChange (me.botsko.prism.api.BlockStateChange)2 BlockState (org.bukkit.block.BlockState)2 ItemStack (org.bukkit.inventory.ItemStack)2 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 ResultSet (java.sql.ResultSet)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Pattern (java.util.regex.Pattern)1