Search in sources :

Example 1 with PreviewSession

use of me.botsko.prism.appliers.PreviewSession 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 2 with PreviewSession

use of me.botsko.prism.appliers.PreviewSession in project Prism-Bukkit by prism.

the class Prism method endExpiredPreviews.

/**
 * Clears expired Previews.
 */
@SuppressWarnings("WeakerAccess")
public void endExpiredPreviews() {
    getServer().getScheduler().scheduleSyncRepeatingTask(this, () -> {
        final java.util.Date date = new java.util.Date();
        for (final Entry<String, PreviewSession> query : playerActivePreviews.entrySet()) {
            final PreviewSession result = query.getValue();
            final long diff = (date.getTime() - result.getQueryTime()) / 1000;
            if (diff >= 60) {
                // inform player
                final Player player = result.getPlayer();
                if (player.isOnline()) {
                    Prism.messenger.sendMessage(player, Prism.messenger.playerHeaderMsg(Il8nHelper.getMessage("cancel-preview-forgotten")));
                }
                playerActivePreviews.remove(query.getKey());
            }
        }
    }, 1200L, 1200L);
}
Also used : PreviewSession(me.botsko.prism.appliers.PreviewSession) Player(org.bukkit.entity.Player) PrismPlayer(me.botsko.prism.players.PrismPlayer) Date(java.util.Date) Date(java.util.Date)

Aggregations

PreviewSession (me.botsko.prism.appliers.PreviewSession)2 Date (java.util.Date)1 ActionsQuery (me.botsko.prism.actionlibs.ActionsQuery)1 QueryParameters (me.botsko.prism.actionlibs.QueryParameters)1 QueryResult (me.botsko.prism.actionlibs.QueryResult)1 PrismPlayer (me.botsko.prism.players.PrismPlayer)1 Audience (net.kyori.adventure.audience.Audience)1 Player (org.bukkit.entity.Player)1