Search in sources :

Example 1 with AbstractListener

use of net.aufdemrand.denizen.listeners.AbstractListener in project Denizen-For-Bukkit by DenizenScript.

the class ListenCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
    Element action = scriptEntry.getElement("action");
    Element type = scriptEntry.getElement("type");
    Element id = scriptEntry.getElement("id");
    dScript finish_script = (dScript) scriptEntry.getObject("finish_script");
    dB.report(scriptEntry, getName(), action.debug() + (type != null ? type.debug() : "") + id.debug() + (finish_script != null ? finish_script.debug() : ""));
    dB.echoError(scriptEntry.getResidingQueue(), "Warning: Listen is outdated and may become unsupported in the future.");
    List<aH.Argument> arguments = (ArrayList<aH.Argument>) scriptEntry.getObject("args");
    switch(Action.valueOf(action.asString().toUpperCase())) {
        case NEW:
            // First make sure there isn't already a 'player listener' for this player with the specified ID.
            if (DenizenAPI.getCurrentInstance().getListenerRegistry().getListenersFor(((BukkitScriptEntryData) scriptEntry.entryData).getPlayer()) != null && DenizenAPI.getCurrentInstance().getListenerRegistry().getListenersFor(((BukkitScriptEntryData) scriptEntry.entryData).getPlayer()).containsKey(CoreUtilities.toLowerCase(id.asString()))) {
                dB.echoError(scriptEntry.getResidingQueue(), "Cancelled creation of NEW listener! Listener ID '" + id.asString() + "' already exists!");
                break;
            }
            // Also make sure there is a valid script input
            if (finish_script == null) {
                dB.echoError("Must specify a valid script!");
                break;
            }
            try {
                DenizenAPI.getCurrentInstance().getListenerRegistry().get(type.asString()).createInstance(((BukkitScriptEntryData) scriptEntry.entryData).getPlayer(), id.asString()).build(((BukkitScriptEntryData) scriptEntry.entryData).getPlayer(), id.asString(), type.asString(), arguments, finish_script, ((BukkitScriptEntryData) scriptEntry.entryData).getNPC());
            } catch (Exception e) {
                dB.echoDebug(scriptEntry, "Cancelled creation of NEW listener!");
                // Why? Maybe a wrong listener type...
                if (DenizenAPI.getCurrentInstance().getListenerRegistry().get(type.asString()) == null) {
                    dB.echoError(scriptEntry.getResidingQueue(), "Invalid listener type!");
                } else // Just print the stacktrace if anything else, so we can debug other possible
                // problems.
                {
                    dB.echoError(scriptEntry.getResidingQueue(), e);
                }
                // Deconstruct the listener in case it was partially created while erroring out.
                try {
                    DenizenAPI.getCurrentInstance().getListenerRegistry().getListenerFor(((BukkitScriptEntryData) scriptEntry.entryData).getPlayer(), id.asString()).cancel();
                } catch (Exception ex) {
                }
            }
            break;
        case FINISH:
            if (DenizenAPI.getCurrentInstance().getListenerRegistry().getListenerFor(((BukkitScriptEntryData) scriptEntry.entryData).getPlayer(), id.asString()) != null) {
                DenizenAPI.getCurrentInstance().getListenerRegistry().getListenerFor(((BukkitScriptEntryData) scriptEntry.entryData).getPlayer(), id.asString()).finish();
            }
            break;
        case CANCEL:
            if (((BukkitScriptEntryData) scriptEntry.entryData).getPlayer() != null) {
                if (id != null) {
                    if (DenizenAPI.getCurrentInstance().getListenerRegistry().getListenerFor(((BukkitScriptEntryData) scriptEntry.entryData).getPlayer(), id.asString()) != null) {
                        DenizenAPI.getCurrentInstance().getListenerRegistry().getListenerFor(((BukkitScriptEntryData) scriptEntry.entryData).getPlayer(), id.asString()).cancel();
                    } else {
                        for (AbstractListener listener : DenizenAPI.getCurrentInstance().getListenerRegistry().getListenersFor(((BukkitScriptEntryData) scriptEntry.entryData).getPlayer()).values()) {
                            listener.cancel();
                        }
                    }
                }
            } else {
                DenizenAPI.getCurrentInstance().getSaves().set("Listeners." + ((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getName() + "." + id, null);
            }
            break;
    }
}
Also used : BukkitScriptEntryData(net.aufdemrand.denizen.BukkitScriptEntryData) net.aufdemrand.denizencore.objects.dScript(net.aufdemrand.denizencore.objects.dScript) Element(net.aufdemrand.denizencore.objects.Element) net.aufdemrand.denizencore.objects.aH(net.aufdemrand.denizencore.objects.aH) ArrayList(java.util.ArrayList) AbstractListener(net.aufdemrand.denizen.listeners.AbstractListener) InvalidArgumentsException(net.aufdemrand.denizencore.exceptions.InvalidArgumentsException) CommandExecutionException(net.aufdemrand.denizencore.exceptions.CommandExecutionException)

Example 2 with AbstractListener

use of net.aufdemrand.denizen.listeners.AbstractListener in project Denizen-For-Bukkit by DenizenScript.

the class DenizenCommandHandler method listener.

/*
     * DENIZEN LISTENER
     */
@Command(aliases = { "denizen" }, usage = "listener (--player) --id listener_id --report|cancel|finish", desc = "Checks/cancels/finishes listeners in progress.", modifiers = { "listener" }, min = 1, max = 3, permission = "denizen.basic", flags = "s")
public void listener(CommandContext args, CommandSender sender) throws CommandException {
    dPlayer player = null;
    if (sender instanceof Player) {
        player = dPlayer.mirrorBukkitPlayer((Player) sender);
    }
    if (args.hasValueFlag("player")) {
        player = dPlayer.valueOf(args.getFlag("player"));
    }
    if (player == null) {
        throw new CommandException("Specified player not online or not found!");
    }
    Map<String, AbstractListener> listeners = denizen.getListenerRegistry().getListenersFor(player);
    if (listeners == null || listeners.isEmpty()) {
        Messaging.send(sender, player.getName() + " has no active listeners.");
        return;
    }
    if (args.hasValueFlag("report")) {
        for (AbstractListener quest : denizen.getListenerRegistry().getListenersFor(player).values()) {
            if (quest.getListenerId().equalsIgnoreCase(args.getFlag("report"))) {
                Messaging.send(sender, quest.report());
            }
        }
        return;
    } else if (args.hasValueFlag("cancel")) {
        for (AbstractListener quest : denizen.getListenerRegistry().getListenersFor(player).values()) {
            if (quest.getListenerId().equalsIgnoreCase(args.getFlag("cancel"))) {
                Messaging.send(sender, "Cancelling '" + quest.getListenerId() + "' for " + player.getName() + ".");
                quest.cancel();
            }
        }
        return;
    } else if (args.hasValueFlag("finish")) {
        for (AbstractListener quest : denizen.getListenerRegistry().getListenersFor(player).values()) {
            if (quest.getListenerId().equalsIgnoreCase(args.getFlag("finish"))) {
                Messaging.send(sender, "Force-finishing '" + quest.getListenerId() + "' for " + player.getName() + ".");
                quest.finish();
            }
        }
        return;
    } else if (args.length() > 2 && args.getInteger(1, 0) < 1) {
        Messaging.send(sender, "");
        Messaging.send(sender, "<f>Use '--report|cancel|finish id' to modify/view a specific quest listener.");
        Messaging.send(sender, "<b>Example: /denizen listener --report \"Journey 1\"");
        Messaging.send(sender, "");
        return;
    }
    Paginator paginator = new Paginator();
    paginator.header("Active quest listeners for " + player.getName() + ":");
    paginator.addLine("<e>Key: <a>Type  <b>ID");
    if (listeners == null || listeners.isEmpty()) {
        paginator.addLine("None.");
    } else {
        for (AbstractListener quest : listeners.values()) {
            paginator.addLine("<a>" + quest.getListenerType() + "  <b>" + quest.getListenerId());
        }
    }
    paginator.sendPage(sender, args.getInteger(1, 1));
}
Also used : Player(org.bukkit.entity.Player) net.aufdemrand.denizen.objects.dPlayer(net.aufdemrand.denizen.objects.dPlayer) net.aufdemrand.denizen.objects.dPlayer(net.aufdemrand.denizen.objects.dPlayer) AbstractListener(net.aufdemrand.denizen.listeners.AbstractListener) CommandException(net.aufdemrand.denizen.utilities.command.exceptions.CommandException) Paginator(net.aufdemrand.denizen.utilities.command.Paginator) Command(net.aufdemrand.denizen.utilities.command.Command)

Aggregations

AbstractListener (net.aufdemrand.denizen.listeners.AbstractListener)2 ArrayList (java.util.ArrayList)1 BukkitScriptEntryData (net.aufdemrand.denizen.BukkitScriptEntryData)1 net.aufdemrand.denizen.objects.dPlayer (net.aufdemrand.denizen.objects.dPlayer)1 Command (net.aufdemrand.denizen.utilities.command.Command)1 Paginator (net.aufdemrand.denizen.utilities.command.Paginator)1 CommandException (net.aufdemrand.denizen.utilities.command.exceptions.CommandException)1 CommandExecutionException (net.aufdemrand.denizencore.exceptions.CommandExecutionException)1 InvalidArgumentsException (net.aufdemrand.denizencore.exceptions.InvalidArgumentsException)1 Element (net.aufdemrand.denizencore.objects.Element)1 net.aufdemrand.denizencore.objects.aH (net.aufdemrand.denizencore.objects.aH)1 net.aufdemrand.denizencore.objects.dScript (net.aufdemrand.denizencore.objects.dScript)1 Player (org.bukkit.entity.Player)1