Search in sources :

Example 6 with Handler

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

the class PrismMiscEvents method onPrismBlocksExtinguishEvent.

/**
     * 
     * @param event
     */
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPrismBlocksExtinguishEvent(final PrismBlocksExtinguishEvent 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 int 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().getName(), id));
        }
    // ActionQueue.save();
    }
}
Also used : BlockStateChange(me.botsko.prism.events.BlockStateChange) BlockState(org.bukkit.block.BlockState) Handler(me.botsko.prism.actions.Handler) EventHandler(org.bukkit.event.EventHandler) EventHandler(org.bukkit.event.EventHandler)

Example 7 with Handler

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

the class Preview method processWorldChanges.

/**
	 * 
	 */
public void processWorldChanges() {
    blockChangesRead = 0;
    worldChangeQueueTaskId = plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {

        @Override
        public void run() {
            if (plugin.getConfig().getBoolean("prism.debug")) {
                Prism.debug("World change queue size: " + worldChangeQueue.size());
            }
            if (worldChangeQueue.isEmpty()) {
                sender.sendMessage(Prism.messenger.playerError(ChatColor.GRAY + "No actions found that match the criteria."));
                return;
            }
            int iterationCount = 0;
            final int currentQueueOffset = blockChangesRead;
            if (currentQueueOffset < worldChangeQueue.size()) {
                for (final Iterator<Handler> iterator = worldChangeQueue.listIterator(currentQueueOffset); iterator.hasNext(); ) {
                    final Handler a = iterator.next();
                    // again
                    if (is_preview)
                        blockChangesRead++;
                    // We only want to process a set number of block changes
                    // per
                    // schedule. Breaking here will leave the rest in the
                    // queue.
                    iterationCount++;
                    if (iterationCount >= 1000) {
                        break;
                    }
                    // when the type doesn't support it.
                    if (processType.equals(PrismProcessType.ROLLBACK) && !a.getType().canRollback()) {
                        iterator.remove();
                        continue;
                    }
                    // when the type doesn't support it.
                    if (processType.equals(PrismProcessType.RESTORE) && !a.getType().canRestore()) {
                        iterator.remove();
                        continue;
                    }
                    /**
                         * Reverse or restore block changes by allowing the
                         * handler to decide what needs to happen.
                         */
                    ChangeResult result = null;
                    try {
                        if (processType.equals(PrismProcessType.ROLLBACK)) {
                            result = a.applyRollback(player, parameters, is_preview);
                        }
                        if (processType.equals(PrismProcessType.RESTORE)) {
                            result = a.applyRestore(player, parameters, is_preview);
                        }
                        if (processType.equals(PrismProcessType.UNDO)) {
                            result = a.applyUndo(player, parameters, is_preview);
                        }
                        // No action, continue
                        if (result == null) {
                            iterator.remove();
                            continue;
                        }
                        // Skip actions that have not returned any results
                        if (result.getType() == null) {
                            skipped_block_count++;
                            iterator.remove();
                            continue;
                        } else // Skipping
                        if (result.getType().equals(ChangeResultType.SKIPPED)) {
                            skipped_block_count++;
                            iterator.remove();
                            continue;
                        } else // Skipping, but change planned
                        if (result.getType().equals(ChangeResultType.PLANNED)) {
                            changes_planned_count++;
                            continue;
                        } else // Change applied
                        {
                            blockStateChanges.add(result.getBlockStateChange());
                            changes_applied_count++;
                        }
                        // Unless a preview, remove from queue
                        if (!is_preview) {
                            iterator.remove();
                        }
                    } catch (final Exception e) {
                        // Something caused an exception. We *have* to catch
                        // this
                        // so we can remove the item from the queue,
                        // otherwise
                        // the cycle will just spin in eternity and all
                        // damnation, normally killing their server through
                        // log files in the GB.
                        // Log the error so they have something to report
                        Prism.log("Applier error: " + e.getMessage());
                        e.printStackTrace();
                        // Count as skipped, remove from queue
                        skipped_block_count++;
                        iterator.remove();
                    }
                }
            }
            // The task for this action is done being used
            if (worldChangeQueue.isEmpty() || blockChangesRead >= worldChangeQueue.size()) {
                plugin.getServer().getScheduler().cancelTask(worldChangeQueueTaskId);
                if (is_preview) {
                    postProcessPreview();
                } else {
                    postProcess();
                }
            }
        }
    }, 2L, 2L);
}
Also used : Handler(me.botsko.prism.actions.Handler)

Example 8 with Handler

use of me.botsko.prism.actions.Handler 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 Handler

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

the class PageCommand 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.getSender().sendMessage(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) {
        call.getSender().sendMessage(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 {
        // Next page
        if (call.getArg(1).equals("next") || call.getArg(1).equals("n")) {
            page = results.getPage() + 1;
        } else // Previous page
        if (call.getArg(1).equals("prev") || call.getArg(1).equals("p")) {
            if (results.getPage() <= 1) {
                call.getSender().sendMessage(Prism.messenger.playerError("There is no previous page."));
                return;
            }
            page = results.getPage() - 1;
        } else {
            call.getSender().sendMessage(Prism.messenger.playerError("Page numbers need to actually be numbers, or next/prev. Like /prism page 2"));
            return;
        }
    }
    // No negatives
    if (page <= 0) {
        call.getSender().sendMessage(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()) {
        call.getSender().sendMessage(Prism.messenger.playerError("Nothing found." + ChatColor.GRAY + " Either you're missing something, or we are."));
        return;
    }
    call.getSender().sendMessage(Prism.messenger.playerHeaderMsg("Showing " + results.getTotalResults() + " results. Page " + page + " of " + results.getTotal_pages()));
    final List<Handler> paginated = results.getPaginatedActionResults();
    if (paginated == null || paginated.size() == 0) {
        call.getSender().sendMessage(Prism.messenger.playerError("Pagination can't find anything. Do you have the right page number?"));
        return;
    }
    // Show it!
    int result_count = results.getIndexOfFirstResult();
    for (final Handler a : paginated) {
        final ActionMessage am = new ActionMessage(a);
        if (results.getParameters().allowsNoRadius() || results.getParameters().hasFlag(Flag.EXTENDED) || plugin.getConfig().getBoolean("prism.messenger.always-show-extended")) {
            am.showExtended();
        }
        am.setResultIndex(result_count);
        call.getSender().sendMessage(Prism.messenger.playerMsg(am.getMessage()));
        result_count++;
    }
}
Also used : Player(org.bukkit.entity.Player) QueryResult(me.botsko.prism.actionlibs.QueryResult) ActionMessage(me.botsko.prism.actionlibs.ActionMessage) SubHandler(me.botsko.prism.commandlibs.SubHandler) Handler(me.botsko.prism.actions.Handler)

Example 10 with Handler

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

the class PrismMiscEvents method onPrismBlocksDrainEvent.

/**
     * 
     * @param event
     */
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPrismBlocksDrainEvent(final PrismBlocksDrainEvent 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.DRAIN, event.onBehalfOf(), "" + event.getRadius());
        final int 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-drain", orig, newBlock, event.onBehalfOf().getName(), id));
        }
    // ActionQueue.save();
    }
}
Also used : BlockStateChange(me.botsko.prism.events.BlockStateChange) BlockState(org.bukkit.block.BlockState) Handler(me.botsko.prism.actions.Handler) EventHandler(org.bukkit.event.EventHandler) EventHandler(org.bukkit.event.EventHandler)

Aggregations

Handler (me.botsko.prism.actions.Handler)12 QueryResult (me.botsko.prism.actionlibs.QueryResult)5 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 Player (org.bukkit.entity.Player)4 Connection (java.sql.Connection)3 PreparedStatement (java.sql.PreparedStatement)3 SQLException (java.sql.SQLException)3 ArrayList (java.util.ArrayList)3 EventHandler (org.bukkit.event.EventHandler)3 List (java.util.List)2 BlockStateChange (me.botsko.prism.events.BlockStateChange)2 BlockState (org.bukkit.block.BlockState)2 CommandSender (org.bukkit.command.CommandSender)2 ResultSet (java.sql.ResultSet)1 PrismProcessAction (me.botsko.prism.actions.PrismProcessAction)1 PrismApplierCallback (me.botsko.prism.appliers.PrismApplierCallback)1 Undo (me.botsko.prism.appliers.Undo)1