Search in sources :

Example 11 with Handler

use of me.botsko.prism.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:")) {
        call.getSender().sendMessage(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
    int record_id;
    if (ident.equals("next") || ident.equals("prev")) {
        // Get stored results
        final QueryResult results = plugin.cachedQueries.get(keyName);
        record_id = results.getLastTeleportIndex();
        record_id = (record_id == 0 ? 1 : record_id);
        if (record_id > 0) {
            if (ident.equals("next")) {
                record_id++;
            } else {
                if (record_id > 1) {
                    record_id--;
                }
            }
        }
    } else {
        if (!TypeUtils.isNumeric(ident)) {
            call.getPlayer().sendMessage(Prism.messenger.playerError("You must provide a numeric result number or record ID to teleport to."));
            return;
        }
        record_id = Integer.parseInt(ident);
        if (record_id <= 0) {
            call.getPlayer().sendMessage(Prism.messenger.playerError("Result number or record ID must be greater than zero."));
            return;
        }
    }
    // If a record id provided, re-query the database
    Handler destinationAction;
    if (call.getArg(1).contains("id:")) {
        // Build params
        final QueryParameters params = new QueryParameters();
        params.setWorld(call.getPlayer().getWorld().getName());
        params.setId(record_id);
        // Query
        final ActionsQuery aq = new ActionsQuery(plugin);
        final QueryResult results = aq.lookup(params, call.getPlayer());
        if (results.getActionResults().isEmpty()) {
            call.getPlayer().sendMessage(Prism.messenger.playerError("No records exists with this ID."));
            return;
        }
        // Get the first result
        destinationAction = results.getActionResults().get(0);
    } else // Otherwise, look for a cached query
    {
        // Get stored results
        final QueryResult results = plugin.cachedQueries.get(keyName);
        if (record_id > results.getActionResults().size()) {
            call.getPlayer().sendMessage(Prism.messenger.playerError("No records exists at this index. Did you mean /pr tp id:" + record_id + " instead?"));
            return;
        }
        final int key = (record_id - 1);
        // Get the result index specified
        destinationAction = results.getActionResults().get(key);
        // Refresh the query time and replace
        results.setQueryTime();
        results.setLastTeleportIndex(record_id);
        plugin.cachedQueries.replace(keyName, results);
    }
    if (destinationAction != null) {
        final World world = plugin.getServer().getWorld(destinationAction.getWorldName());
        if (world == null) {
            call.getPlayer().sendMessage(Prism.messenger.playerError("Action record occurred in world we can't find anymore."));
            return;
        }
        final Location loc = new Location(world, destinationAction.getX(), destinationAction.getY(), destinationAction.getZ());
        call.getPlayer().teleport(loc);
        call.getPlayer().sendMessage(Prism.messenger.playerSubduedHeaderMsg("Teleporting... " + ChatColor.WHITE + destinationAction.getType().getName() + ChatColor.GRAY + " by " + ChatColor.WHITE + destinationAction.getPlayerName() + ChatColor.GRAY + ", " + ChatColor.WHITE + destinationAction.getTimeSince()));
    }
}
Also used : Player(org.bukkit.entity.Player) QueryResult(me.botsko.prism.actionlibs.QueryResult) ActionsQuery(me.botsko.prism.actionlibs.ActionsQuery) Handler(me.botsko.prism.actions.Handler) SubHandler(me.botsko.prism.commandlibs.SubHandler) QueryParameters(me.botsko.prism.actionlibs.QueryParameters) World(org.bukkit.World) Location(org.bukkit.Location)

Example 12 with Handler

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

the class UndoCommand method handle.

/**
     * Handle the command
     */
@Override
public void handle(CallInfo call) {
    if (call.getArgs().length > 1) {
        final ActionsQuery aq = new ActionsQuery(plugin);
        int record_id = 0;
        if (TypeUtils.isNumeric(call.getArg(1))) {
            record_id = Integer.parseInt(call.getArg(1));
            if (record_id <= 0) {
                call.getPlayer().sendMessage(Prism.messenger.playerError("Record ID must be greater than zero."));
                return;
            }
        } else {
            if (call.getArg(1).equals("last")) {
                record_id = aq.getUsersLastPrismProcessId(call.getPlayer().getName());
            }
        }
        // Invalid id
        if (record_id == 0) {
            call.getPlayer().sendMessage(Prism.messenger.playerError("Either you have no last process or an invalid ID."));
            return;
        }
        final PrismProcessAction process = aq.getPrismProcessRecord(record_id);
        if (process == null) {
            call.getPlayer().sendMessage(Prism.messenger.playerError("A process does not exists with that value."));
            return;
        }
        // We only support this for drains
        if (!process.getProcessChildActionType().equals("prism-drain")) {
            call.getPlayer().sendMessage(Prism.messenger.playerError("You can't currently undo anything other than a drain process."));
            return;
        }
        // Pull the actual block change data for this undo event
        final QueryParameters parameters = new QueryParameters();
        parameters.setWorld(call.getPlayer().getWorld().getName());
        parameters.addActionType(process.getProcessChildActionType());
        parameters.addPlayerName(call.getPlayer().getName());
        parameters.setParentId(record_id);
        parameters.setProcessType(PrismProcessType.UNDO);
        // make sure the distance isn't too far away
        final QueryResult results = aq.lookup(parameters, call.getPlayer());
        if (!results.getActionResults().isEmpty()) {
            call.getPlayer().sendMessage(Prism.messenger.playerHeaderMsg("Undoing..." + ChatColor.GRAY + " Abandon ship!"));
            final Undo rb = new Undo(plugin, call.getPlayer(), results.getActionResults(), parameters, new PrismApplierCallback());
            rb.apply();
        } else {
            call.getPlayer().sendMessage(Prism.messenger.playerError("Nothing found to undo. Must be a problem with Prism."));
        }
    } else {
        // Show the list
        // Process and validate all of the arguments
        final QueryParameters parameters = new QueryParameters();
        parameters.setAllowNoRadius(true);
        parameters.addActionType("prism-process");
        parameters.addPlayerName(call.getPlayer().getName());
        // @todo config this, and move the logic
        parameters.setLimit(5);
        // to queryparams
        final ActionsQuery aq = new ActionsQuery(plugin);
        final QueryResult results = aq.lookup(parameters, call.getPlayer());
        if (!results.getActionResults().isEmpty()) {
            call.getPlayer().sendMessage(Prism.messenger.playerHeaderMsg("Showing " + results.getTotalResults() + " results. Page 1 of " + results.getTotal_pages()));
            call.getPlayer().sendMessage(Prism.messenger.playerSubduedHeaderMsg("Use /prism undo [id] to reverse a process"));
            final List<Handler> paginated = results.getPaginatedActionResults();
            if (paginated != null) {
                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();
                    }
                    call.getPlayer().sendMessage(Prism.messenger.playerMsg(am.getMessage()));
                }
            } 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("Nothing found." + ChatColor.GRAY + " Either you're missing something, or we are."));
        }
    }
}
Also used : PrismProcessAction(me.botsko.prism.actions.PrismProcessAction) QueryResult(me.botsko.prism.actionlibs.QueryResult) PrismApplierCallback(me.botsko.prism.appliers.PrismApplierCallback) ActionsQuery(me.botsko.prism.actionlibs.ActionsQuery) ActionMessage(me.botsko.prism.actionlibs.ActionMessage) Handler(me.botsko.prism.actions.Handler) SubHandler(me.botsko.prism.commandlibs.SubHandler) QueryParameters(me.botsko.prism.actionlibs.QueryParameters) Undo(me.botsko.prism.appliers.Undo)

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