Search in sources :

Example 1 with QueryResult

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

the class RollbackWand method rollback.

protected void rollback(Player player, Location loc) {
    final Block block = loc.getBlock();
    QueryParameters params = checkQueryParams(block, parameters, player);
    params.setProcessType(PrismProcessType.ROLLBACK);
    final QueryResult results = getResult(params, player);
    if (!results.getActionResults().isEmpty()) {
        final Rollback rb = new Rollback(plugin, player, results.getActionResults(), params, new PrismApplierCallback());
        rb.apply();
    } else {
        final String space_name = (block.getType().equals(Material.AIR) ? "space" : block.getType().toString().replaceAll("_", " ").toLowerCase() + (block.getType().toString().endsWith("BLOCK") ? "" : " block"));
        Prism.messenger.sendMessage(player, Prism.messenger.playerError("Nothing to rollback for this " + space_name + " found."));
    }
}
Also used : QueryResult(me.botsko.prism.actionlibs.QueryResult) PrismApplierCallback(me.botsko.prism.appliers.PrismApplierCallback) Block(org.bukkit.block.Block) QueryParameters(me.botsko.prism.actionlibs.QueryParameters) Rollback(me.botsko.prism.appliers.Rollback)

Example 2 with QueryResult

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

the class LookupCommand method handle.

/**
 * Handle the command.
 */
@Override
public void handle(final CallInfo call) {
    // Process and validate all of the arguments
    final QueryParameters parameters = PreprocessArgs.process(plugin, call.getSender(), call.getArgs(), PrismProcessType.LOOKUP, 1, !plugin.getConfig().getBoolean("prism.queries.never-use-defaults"));
    if (parameters == null) {
        return;
    }
    // Run the lookup itself in an async task so the lookup query isn't done on the main thread
    plugin.getServer().getScheduler().runTaskAsynchronously(plugin, () -> {
        // determine if defaults were used
        final List<String> defaultsUsed = parameters.getDefaultsUsed();
        StringBuilder defaultsReminder = new StringBuilder();
        if (!defaultsUsed.isEmpty()) {
            defaultsReminder.append(Il8nHelper.getRawMessage("queryparameter.defaults.prefix"));
            for (final String d : defaultsUsed) {
                defaultsReminder.append(" ").append(d);
            }
        }
        final ActionsQuery aq = new ActionsQuery(plugin);
        final QueryResult results = aq.lookup(parameters, call.getSender());
        final StringBuilder sharingWithPlayers = new StringBuilder();
        for (final CommandSender shareWith : parameters.getSharedPlayers()) {
            sharingWithPlayers.append(shareWith.getName()).append(", ");
        }
        if (sharingWithPlayers.length() > 0) {
            sharingWithPlayers.delete(sharingWithPlayers.lastIndexOf(","), sharingWithPlayers.length());
        }
        final String playersList = sharingWithPlayers.toString();
        // Add current sender
        parameters.addSharedPlayer(call.getSender());
        for (final CommandSender player : parameters.getSharedPlayers()) {
            final boolean isSender = player.getName().equals(call.getSender().getName());
            if (!isSender) {
                Prism.messenger.sendMessage(player, Prism.messenger.playerHeaderMsg(Il8nHelper.getMessage("lookup-share-message").color(NamedTextColor.GOLD).replaceText(Pattern.compile("<sender>"), builder -> Component.text().content(call.getSender().getName()).color(NamedTextColor.YELLOW).decoration(TextDecoration.ITALIC, TextDecoration.State.TRUE), 1)));
            } else if (sharingWithPlayers.length() > 0) {
                Component component = Il8nHelper.getMessage("lookup-share-to-message").color(NamedTextColor.GOLD).replaceText(Pattern.compile("<players>"), builder -> Component.text().content(playersList).color(NamedTextColor.YELLOW).decoration(TextDecoration.ITALIC, TextDecoration.State.TRUE), 1);
                Prism.messenger.sendMessage(call.getSender(), Prism.messenger.playerHeaderMsg(component));
            }
            if (!results.getActionResults().isEmpty()) {
                Prism.messenger.sendMessage(player, Prism.messenger.playerHeaderMsg(Il8nHelper.formatMessage("lookup-header-message", results.getTotalResults(), 1, results.getTotalPages())));
                if ((defaultsReminder.length() > 0) && isSender) {
                    Prism.messenger.sendMessage(player, Prism.messenger.playerSubduedHeaderMsg(Component.text(defaultsReminder.toString())));
                }
                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, player);
                        resultCount++;
                    }
                    MiscUtils.sendPageButtons(results, player);
                } else {
                    Prism.messenger.sendMessage(player, Prism.messenger.playerError(Il8nHelper.getMessage("no-pagination-found")));
                }
                if (parameters.hasFlag(Flag.PASTE)) {
                    StringBuilder paste = new StringBuilder();
                    for (final Handler a : results.getActionResults()) {
                        paste.append(new ActionMessage(a).getRawMessage()).append("\r\n");
                    }
                    MiscUtils.paste_results(player, paste.toString());
                }
            } else {
                if (defaultsReminder.length() > 0) {
                    if (isSender) {
                        Prism.messenger.sendMessage(player, Prism.messenger.playerSubduedHeaderMsg(Component.text(defaultsReminder.toString())));
                    }
                }
                if (isSender) {
                    Prism.messenger.sendMessage(player, Prism.messenger.playerError("Nothing found." + ChatColor.GRAY + " Either you're missing something, or we are."));
                }
            }
        }
        // Flush timed data
        plugin.eventTimer.printTimeRecord();
    });
}
Also used : Prism(me.botsko.prism.Prism) CommandSender(org.bukkit.command.CommandSender) CallInfo(me.botsko.prism.commandlibs.CallInfo) PreprocessArgs(me.botsko.prism.commandlibs.PreprocessArgs) TextDecoration(net.kyori.adventure.text.format.TextDecoration) Handler(me.botsko.prism.api.actions.Handler) QueryResult(me.botsko.prism.actionlibs.QueryResult) NamedTextColor(net.kyori.adventure.text.format.NamedTextColor) ActionsQuery(me.botsko.prism.actionlibs.ActionsQuery) List(java.util.List) SubHandler(me.botsko.prism.commandlibs.SubHandler) Flag(me.botsko.prism.api.commands.Flag) MiscUtils(me.botsko.prism.utils.MiscUtils) Component(net.kyori.adventure.text.Component) Il8nHelper(me.botsko.prism.Il8nHelper) PrismProcessType(me.botsko.prism.api.actions.PrismProcessType) ChatColor(org.bukkit.ChatColor) Pattern(java.util.regex.Pattern) QueryParameters(me.botsko.prism.actionlibs.QueryParameters) 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) QueryResult(me.botsko.prism.actionlibs.QueryResult) ActionsQuery(me.botsko.prism.actionlibs.ActionsQuery) ActionMessage(me.botsko.prism.actionlibs.ActionMessage) CommandSender(org.bukkit.command.CommandSender) Component(net.kyori.adventure.text.Component)

Example 3 with QueryResult

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

the class PreviewCommand method handle.

@Override
public void handle(final CallInfo call) {
    final Audience audience = Prism.getAudiences().sender(call.getPlayer());
    if (call.getArgs().length >= 2) {
        if (call.getArg(1).equalsIgnoreCase("apply")) {
            if (plugin.playerActivePreviews.containsKey(call.getPlayer().getName())) {
                final PreviewSession previewSession = plugin.playerActivePreviews.get(call.getPlayer().getName());
                previewSession.getPreviewer().apply_preview();
                plugin.playerActivePreviews.remove(call.getPlayer().getName());
            } else {
                Prism.messenger.sendMessage(call.getPlayer(), Prism.messenger.playerError("You have no preview pending."));
            }
            return;
        }
        if (call.getArg(1).equalsIgnoreCase("cancel")) {
            if (plugin.playerActivePreviews.containsKey(call.getPlayer().getName())) {
                final PreviewSession previewSession = plugin.playerActivePreviews.get(call.getPlayer().getName());
                previewSession.getPreviewer().cancel_preview();
                plugin.playerActivePreviews.remove(call.getPlayer().getName());
            } else {
                Prism.messenger.sendMessage(call.getPlayer(), Prism.messenger.playerError(Il8nHelper.getMessage("preview-none-pending")));
            }
            return;
        }
        // Ensure no current preview is waiting
        if (plugin.playerActivePreviews.containsKey(call.getPlayer().getName())) {
            Prism.messenger.sendMessage(call.getPlayer(), Prism.messenger.playerError(Il8nHelper.getMessage("preview-pending")));
            return;
        }
        if (call.getArg(1).equalsIgnoreCase("rollback") || call.getArg(1).equalsIgnoreCase("restore") || call.getArg(1).equalsIgnoreCase("rb") || call.getArg(1).equalsIgnoreCase("rs")) {
            final QueryParameters parameters = PreprocessArgs.process(plugin, call.getPlayer(), call.getArgs(), PrismProcessType.ROLLBACK, 2, !plugin.getConfig().getBoolean("prism.queries.never-use-defaults"));
            if (parameters == null) {
                return;
            }
            parameters.setStringFromRawArgs(call.getArgs(), 1);
            if (parameters.getActionTypes().containsKey("world-edit")) {
                Prism.messenger.sendMessage(call.getPlayer(), Prism.messenger.playerError(Il8nHelper.getMessage("preview-worldedit-unsupported")));
                return;
            }
            StringBuilder defaultsReminder = checkIfDefaultUsed(parameters);
            Prism.messenger.sendMessage(call.getPlayer(), Prism.messenger.playerSubduedHeaderMsg(Il8nHelper.getMessage("queryparameter.defaults.prefix", defaultsReminder.toString())));
            plugin.getServer().getScheduler().runTaskAsynchronously(plugin, () -> {
                // Perform preview
                final ActionsQuery aq = new ActionsQuery(plugin);
                final QueryResult results = aq.lookup(parameters, call.getPlayer());
                // Rollback
                if (call.getArg(1).equalsIgnoreCase("rollback") || call.getArg(1).equalsIgnoreCase("rb")) {
                    handleRollBack(call, parameters, results, audience);
                    // todo remove debug
                    assert (parameters.getProcessType() == PrismProcessType.ROLLBACK);
                }
                // Restore
                if (call.getArg(1).equalsIgnoreCase("restore") || call.getArg(1).equalsIgnoreCase("rs")) {
                    handleRestore(call, parameters, results, audience);
                    // todo remove debug
                    assert (parameters.getProcessType() == PrismProcessType.RESTORE);
                }
            });
            return;
        }
        Prism.messenger.sendMessage(call.getPlayer(), Prism.messenger.playerError(Il8nHelper.getMessage("invalid-command")));
    }
}
Also used : PreviewSession(me.botsko.prism.appliers.PreviewSession) QueryResult(me.botsko.prism.actionlibs.QueryResult) Audience(net.kyori.adventure.audience.Audience) ActionsQuery(me.botsko.prism.actionlibs.ActionsQuery) QueryParameters(me.botsko.prism.actionlibs.QueryParameters)

Example 4 with QueryResult

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

the class OreMonitor method processAlertsFromBlock.

/**
 * Process alerts.
 *
 * @param player    Player
 * @param block     Block
 * @param alertPerm Players with this permission will receive the alert
 */
public void processAlertsFromBlock(final Player player, final Block block, final String alertPerm) {
    if (!plugin.getConfig().getBoolean("prism.alerts.ores.enabled")) {
        return;
    }
    if (player == null || player.getGameMode().equals(GameMode.CREATIVE)) {
        return;
    }
    if (block != null && isWatched(block) && !plugin.alertedBlocks.containsKey(block.getLocation())) {
        threshold = 1;
        // identify all ore blocks on same Y axis in x/z direction
        final ArrayList<Block> matchingBlocks = new ArrayList<>();
        final ArrayList<Block> foundores = findNeighborBlocks(block.getType(), block, matchingBlocks);
        if (!foundores.isEmpty()) {
            // Create alert message
            final String count = foundores.size() + (foundores.size() >= thresholdMax ? "+" : "");
            final String msg = player.getName() + " found " + count + " " + getOreNiceName(block) + " " + getLightLevel(block) + "% light";
            final TextComponent component = Component.text().content(msg).color(getOreColor(block)).hoverEvent(HoverEvent.hoverEvent(HoverEvent.Action.SHOW_ITEM, HoverEvent.ShowItem.of(Key.key(block.getType().getKey().toString()), 1))).build();
            plugin.getServer().getScheduler().runTaskAsynchronously(plugin, () -> {
                // Check if block placed
                // Build params
                final QueryParameters params = new QueryParameters();
                params.setWorld(player.getWorld().getName());
                params.addSpecificBlockLocation(block.getLocation());
                params.addActionType("block-place");
                final ActionsQuery aq = new ActionsQuery(plugin);
                final QueryResult results = aq.lookup(params, player);
                if (results.getActionResults().isEmpty()) {
                    // Block was not placed - Alert staff
                    plugin.alertPlayers(null, component, alertPerm);
                    // Log to console
                    if (plugin.getConfig().getBoolean("prism.alerts.ores.log-to-console")) {
                        Prism.log(PlainComponentSerializer.plain().serialize(component));
                    }
                    // Log to commands
                    List<String> commands = plugin.getConfig().getStringList("prism.alerts.ores.log-commands");
                    MiscUtils.dispatchAlert(msg, commands);
                }
            });
        }
    }
}
Also used : TextComponent(net.kyori.adventure.text.TextComponent) QueryResult(me.botsko.prism.actionlibs.QueryResult) ActionsQuery(me.botsko.prism.actionlibs.ActionsQuery) ArrayList(java.util.ArrayList) Block(org.bukkit.block.Block) QueryParameters(me.botsko.prism.actionlibs.QueryParameters)

Example 5 with QueryResult

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

the class OreMonitor method processAlertsFromBlock.

/**
     * 
     * @param player
     * @param block
     */
public void processAlertsFromBlock(final Player player, final Block block) {
    if (!plugin.getConfig().getBoolean("prism.alerts.ores.enabled")) {
        return;
    }
    if (player == null || player.getGameMode() == null || player.getGameMode().equals(GameMode.CREATIVE)) {
        return;
    }
    if (block != null && isWatched(block) && !plugin.alertedBlocks.containsKey(block.getLocation())) {
        threshold = 1;
        // identify all ore blocks on same Y axis in x/z direction
        final ArrayList<Block> matchingBlocks = new ArrayList<Block>();
        final ArrayList<Block> foundores = findNeighborBlocks(block.getType(), block, matchingBlocks);
        if (!foundores.isEmpty()) {
            // Save the block
            final BlockState state = block.getState();
            // Set to air to get the light
            block.setType(Material.AIR);
            int light = block.getLightLevel();
            light = (light > 0 ? Math.round(((light) & 0xFF) * 100) / 15 : 0);
            // Restore the block
            block.setType(state.getType());
            final String count = foundores.size() + (foundores.size() >= threshold_max ? "+" : "");
            final String msg = getOreColor(block) + player.getName() + " found " + count + " " + getOreNiceName(block) + " " + light + "% light";
            /**
                 * 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() {
                    // check if block placed
                    boolean wasplaced = false;
                    // Build params
                    final QueryParameters params = new QueryParameters();
                    params.setWorld(player.getWorld().getName());
                    params.addSpecificBlockLocation(block.getLocation());
                    params.addActionType("block-place");
                    final ActionsQuery aq = new ActionsQuery(plugin);
                    final QueryResult results = aq.lookup(params, player);
                    if (!results.getActionResults().isEmpty()) {
                        wasplaced = true;
                    }
                    if (!wasplaced) {
                        // Alert staff
                        plugin.alertPlayers(null, TypeUtils.colorize(msg));
                        // Log to console
                        if (plugin.getConfig().getBoolean("prism.alerts.ores.log-to-console")) {
                            Prism.log(msg);
                        }
                        // Log to commands
                        List<String> commands = plugin.getConfig().getStringList("prism.alerts.ores.log-commands");
                        MiscUtils.dispatchAlert(msg, commands);
                    }
                }
            });
        }
    }
}
Also used : QueryResult(me.botsko.prism.actionlibs.QueryResult) BlockState(org.bukkit.block.BlockState) ActionsQuery(me.botsko.prism.actionlibs.ActionsQuery) ArrayList(java.util.ArrayList) Block(org.bukkit.block.Block) ArrayList(java.util.ArrayList) List(java.util.List) QueryParameters(me.botsko.prism.actionlibs.QueryParameters)

Aggregations

QueryResult (me.botsko.prism.actionlibs.QueryResult)15 QueryParameters (me.botsko.prism.actionlibs.QueryParameters)12 ActionsQuery (me.botsko.prism.actionlibs.ActionsQuery)9 Handler (me.botsko.prism.api.actions.Handler)6 ActionMessage (me.botsko.prism.actionlibs.ActionMessage)5 PrismApplierCallback (me.botsko.prism.appliers.PrismApplierCallback)5 SubHandler (me.botsko.prism.commandlibs.SubHandler)5 Block (org.bukkit.block.Block)5 ArrayList (java.util.ArrayList)4 Previewable (me.botsko.prism.appliers.Previewable)3 Player (org.bukkit.entity.Player)3 List (java.util.List)2 Restore (me.botsko.prism.appliers.Restore)2 Rollback (me.botsko.prism.appliers.Rollback)2 Audience (net.kyori.adventure.audience.Audience)2 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1