Search in sources :

Example 11 with ActionsQuery

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

the class RestoreCommand method handle.

@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);
    StringBuilder defaultsReminder = checkIfDefaultUsed(parameters);
    Prism.messenger.sendMessage(call.getSender(), Prism.messenger.playerSubduedHeaderMsg(ReplaceableTextComponent.builder("restore-prepare").replace("<defaults>", defaultsReminder).build()));
    plugin.getServer().getScheduler().runTaskAsynchronously(plugin, () -> {
        final ActionsQuery aq = new ActionsQuery(plugin);
        final QueryResult results = aq.lookup(parameters, call.getSender());
        if (!results.getActionResults().isEmpty()) {
            Prism.messenger.sendMessage(call.getSender(), Prism.messenger.playerHeaderMsg(Il8nHelper.getMessage("restore-start")));
            // Inform nearby players
            if (call.getSender() instanceof Player) {
                final Player player = (Player) call.getSender();
                plugin.notifyNearby(player, parameters.getRadius(), ReplaceableTextComponent.builder("block-changes-near").replace("<player>", player.getDisplayName()).build());
            }
            // Perform restore on the main thread
            plugin.getServer().getScheduler().runTask(plugin, () -> {
                final Previewable rs = new Restore(plugin, call.getSender(), results.getActionResults(), parameters, new PrismApplierCallback());
                rs.apply();
            });
        } else {
            Prism.messenger.sendMessage(call.getSender(), Prism.messenger.playerError(Il8nHelper.getMessage("restore-error")));
        }
    });
}
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) Previewable(me.botsko.prism.appliers.Previewable) QueryParameters(me.botsko.prism.actionlibs.QueryParameters) Restore(me.botsko.prism.appliers.Restore)

Example 12 with ActionsQuery

use of me.botsko.prism.actionlibs.ActionsQuery 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 13 with ActionsQuery

use of me.botsko.prism.actionlibs.ActionsQuery 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)

Aggregations

ActionsQuery (me.botsko.prism.actionlibs.ActionsQuery)13 QueryParameters (me.botsko.prism.actionlibs.QueryParameters)11 QueryResult (me.botsko.prism.actionlibs.QueryResult)10 Handler (me.botsko.prism.api.actions.Handler)4 SubHandler (me.botsko.prism.commandlibs.SubHandler)4 ActionMessage (me.botsko.prism.actionlibs.ActionMessage)3 PrismApplierCallback (me.botsko.prism.appliers.PrismApplierCallback)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Previewable (me.botsko.prism.appliers.Previewable)2 Audience (net.kyori.adventure.audience.Audience)2 Block (org.bukkit.block.Block)2 Player (org.bukkit.entity.Player)2 CompletableFuture (java.util.concurrent.CompletableFuture)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 Pattern (java.util.regex.Pattern)1 Il8nHelper (me.botsko.prism.Il8nHelper)1 Prism (me.botsko.prism.Prism)1 PrismProcessAction (me.botsko.prism.actions.PrismProcessAction)1 Result (me.botsko.prism.api.Result)1