Search in sources :

Example 11 with CommandExecutionException

use of net.aufdemrand.denizencore.exceptions.CommandExecutionException 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 12 with CommandExecutionException

use of net.aufdemrand.denizencore.exceptions.CommandExecutionException in project Denizen-For-Bukkit by DenizenScript.

the class PushableCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
    dNPC denizenNPC = ((BukkitScriptEntryData) scriptEntry.entryData).getNPC();
    if (denizenNPC == null) {
        throw new CommandExecutionException("No valid NPC attached to this queue!");
    }
    PushableTrait trait = denizenNPC.getPushableTrait();
    Element state = scriptEntry.getElement("state");
    Duration delay = scriptEntry.getdObject("delay");
    Element returnable = scriptEntry.getElement("return");
    if (state == null && delay == null && returnable == null) {
        state = new Element("TOGGLE");
    }
    dB.report(scriptEntry, getName(), (state != null ? state.debug() : "") + (delay != null ? delay.debug() : "") + (returnable != null ? returnable.debug() : ""));
    if (delay != null) {
        trait.setDelay(delay.getSecondsAsInt());
    }
    if (returnable != null) {
        trait.setReturnable(returnable.asBoolean());
    }
    if (state != null) {
        switch(Toggle.valueOf(state.asString().toUpperCase())) {
            case TRUE:
            case ON:
                trait.setPushable(true);
                break;
            case FALSE:
            case OFF:
                trait.setPushable(false);
                break;
            case TOGGLE:
                trait.setPushable(!trait.isPushable());
                break;
        }
    }
}
Also used : net.aufdemrand.denizen.objects.dNPC(net.aufdemrand.denizen.objects.dNPC) BukkitScriptEntryData(net.aufdemrand.denizen.BukkitScriptEntryData) Element(net.aufdemrand.denizencore.objects.Element) CommandExecutionException(net.aufdemrand.denizencore.exceptions.CommandExecutionException) Duration(net.aufdemrand.denizencore.objects.Duration) PushableTrait(net.aufdemrand.denizen.npc.traits.PushableTrait)

Example 13 with CommandExecutionException

use of net.aufdemrand.denizencore.exceptions.CommandExecutionException in project Denizen-For-Bukkit by DenizenScript.

the class AnimateCommand method execute.

@SuppressWarnings("unchecked")
@Override
public void execute(final ScriptEntry scriptEntry) throws CommandExecutionException {
    // Get objects
    List<dEntity> entities = (List<dEntity>) scriptEntry.getObject("entities");
    PlayerAnimation animation = scriptEntry.hasObject("animation") ? (PlayerAnimation) scriptEntry.getObject("animation") : null;
    EntityEffect effect = scriptEntry.hasObject("effect") ? (EntityEffect) scriptEntry.getObject("effect") : null;
    String nmsAnimation = scriptEntry.hasObject("nms_animation") ? (String) scriptEntry.getObject("nms_animation") : null;
    // Report to dB
    dB.report(scriptEntry, getName(), (animation != null ? aH.debugObj("animation", animation.name()) : effect != null ? aH.debugObj("effect", effect.name()) : aH.debugObj("animation", nmsAnimation)) + aH.debugObj("entities", entities.toString()));
    // Go through all the entities and animate them
    for (dEntity entity : entities) {
        if (entity.isSpawned()) {
            try {
                if (animation != null && entity.getBukkitEntity() instanceof Player) {
                    Player player = (Player) entity.getBukkitEntity();
                    animation.play(player);
                } else if (effect != null) {
                    entity.getBukkitEntity().playEffect(effect);
                } else {
                    EntityAnimation entityAnimation = NMSHandler.getInstance().getAnimationHelper().getEntityAnimation(nmsAnimation);
                    entityAnimation.play(entity.getBukkitEntity());
                }
            } catch (Exception e) {
                dB.echoError(scriptEntry.getResidingQueue(), "Error playing that animation!");
            }
        // We tried!
        }
    }
}
Also used : EntityEffect(org.bukkit.EntityEffect) EntityAnimation(net.aufdemrand.denizen.nms.interfaces.EntityAnimation) Player(org.bukkit.entity.Player) net.aufdemrand.denizen.objects.dEntity(net.aufdemrand.denizen.objects.dEntity) PlayerAnimation(net.citizensnpcs.util.PlayerAnimation) List(java.util.List) net.aufdemrand.denizencore.objects.dList(net.aufdemrand.denizencore.objects.dList) InvalidArgumentsException(net.aufdemrand.denizencore.exceptions.InvalidArgumentsException) CommandExecutionException(net.aufdemrand.denizencore.exceptions.CommandExecutionException)

Example 14 with CommandExecutionException

use of net.aufdemrand.denizencore.exceptions.CommandExecutionException in project Denizen-For-Bukkit by DenizenScript.

the class PushCommand method execute.

@SuppressWarnings("unchecked")
@Override
public void execute(final ScriptEntry scriptEntry) throws CommandExecutionException {
    dEntity originEntity = (dEntity) scriptEntry.getObject("originEntity");
    dLocation originLocation = scriptEntry.hasObject("originLocation") ? (dLocation) scriptEntry.getObject("originLocation") : new dLocation(originEntity.getEyeLocation().add(originEntity.getEyeLocation().getDirection()).subtract(0, 0.4, 0));
    boolean no_rotate = scriptEntry.hasObject("no_rotate") && scriptEntry.getElement("no_rotate").asBoolean();
    final boolean no_damage = scriptEntry.hasObject("no_damage") && scriptEntry.getElement("no_damage").asBoolean();
    // If there is no destination set, but there is a shooter, get a point
    // in front of the shooter and set it as the destination
    final dLocation destination = scriptEntry.hasObject("destination") ? (dLocation) scriptEntry.getObject("destination") : (originEntity != null ? new dLocation(originEntity.getEyeLocation().add(originEntity.getEyeLocation().getDirection().multiply(30))) : null);
    // TODO: Should this be checked in argument parsing?
    if (destination == null) {
        dB.report(scriptEntry, getName(), "No destination specified!");
        scriptEntry.setFinished(true);
        return;
    }
    List<dEntity> entities = (List<dEntity>) scriptEntry.getObject("entities");
    final dScript script = (dScript) scriptEntry.getObject("script");
    final dList definitions = (dList) scriptEntry.getObject("definitions");
    final double speed = scriptEntry.getElement("speed").asDouble();
    final int maxTicks = ((Duration) scriptEntry.getObject("duration")).getTicksAsInt();
    Element force_along = scriptEntry.getElement("force_along");
    Element precision = scriptEntry.getElement("precision");
    // Report to dB
    dB.report(scriptEntry, getName(), aH.debugObj("origin", originEntity != null ? originEntity : originLocation) + aH.debugObj("entities", entities.toString()) + aH.debugObj("destination", destination) + aH.debugObj("speed", speed) + aH.debugObj("max ticks", maxTicks) + (script != null ? script.debug() : "") + force_along.debug() + precision.debug() + (no_rotate ? aH.debugObj("no_rotate", "true") : "") + (no_damage ? aH.debugObj("no_damage", "true") : "") + (definitions != null ? definitions.debug() : ""));
    final boolean forceAlong = force_along.asBoolean();
    // Keep a dList of entities that can be called using <entry[name].pushed_entities>
    // later in the script queue
    final dList entityList = new dList();
    // Go through all the entities, spawning/teleporting and rotating them
    for (dEntity entity : entities) {
        entity.spawnAt(originLocation);
        // Only add to entityList after the entities have been
        // spawned, otherwise you'll get something like "e@skeleton"
        // instead of "e@57" on it
        entityList.add(entity.toString());
        if (!no_rotate) {
            NMSHandler.getInstance().getEntityHelper().faceLocation(entity.getBukkitEntity(), destination);
        }
        // when applicable
        if (entity.isProjectile() && originEntity != null) {
            entity.setShooter(originEntity);
        }
    }
    // Add entities to context so that the specific entities created/spawned
    // can be fetched.
    scriptEntry.addObject("pushed_entities", entityList);
    Position.mount(Conversion.convertEntities(entities));
    // Get the entity at the bottom of the entity list, because
    // only its gravity should be affected and tracked considering
    // that the other entities will be mounted on it
    final dEntity lastEntity = entities.get(entities.size() - 1);
    final Vector v2 = destination.toVector();
    final Vector Origin = originLocation.toVector();
    final int prec = precision.asInt();
    BukkitRunnable task = new BukkitRunnable() {

        int runs = 0;

        dLocation lastLocation;

        @Override
        public void run() {
            if (runs < maxTicks && lastEntity.isValid()) {
                Vector v1 = lastEntity.getLocation().toVector();
                Vector v3 = v2.clone().subtract(v1).normalize();
                Vector newVel = v3.multiply(speed);
                lastEntity.setVelocity(newVel);
                if (forceAlong) {
                    Vector newDest = v2.clone().subtract(Origin).normalize().multiply(runs / 20).add(Origin);
                    lastEntity.teleport(new Location(lastEntity.getLocation().getWorld(), newDest.getX(), newDest.getY(), newDest.getZ(), lastEntity.getLocation().getYaw(), lastEntity.getLocation().getPitch()));
                }
                runs += prec;
                // Check if the entity is close to its destination
                if (Math.abs(v2.getX() - v1.getX()) < 1.5f && Math.abs(v2.getY() - v1.getY()) < 1.5f && Math.abs(v2.getZ() - v1.getZ()) < 1.5f) {
                    runs = maxTicks;
                }
                // Check if the entity has collided with something
                // using the most basic possible calculation
                BlockHelper blockHelper = NMSHandler.getInstance().getBlockHelper();
                if (!blockHelper.isSafeBlock(lastEntity.getLocation().add(v3).getBlock().getType()) || !blockHelper.isSafeBlock(lastEntity.getLocation().add(newVel).getBlock().getType())) {
                    runs = maxTicks;
                }
                if (no_damage && lastEntity.isLivingEntity()) {
                    lastEntity.getLivingEntity().setFallDistance(0);
                }
                // Record the location in case the entity gets lost (EG, if a pushed arrow hits a mob)
                lastLocation = lastEntity.getLocation();
            } else {
                this.cancel();
                if (script != null) {
                    List<ScriptEntry> entries = script.getContainer().getBaseEntries(scriptEntry.entryData.clone());
                    ScriptQueue queue = InstantQueue.getQueue(ScriptQueue.getNextId(script.getContainer().getName())).addEntries(entries);
                    if (lastEntity.getLocation() != null) {
                        queue.addDefinition("location", lastEntity.getLocation().identify());
                    } else {
                        queue.addDefinition("location", lastLocation.identify());
                    }
                    queue.addDefinition("pushed_entities", entityList.toString());
                    queue.addDefinition("last_entity", lastEntity.identify());
                    if (definitions != null) {
                        int x = 1;
                        String[] definition_names = null;
                        try {
                            definition_names = script.getContainer().getString("definitions").split("\\|");
                        } catch (Exception e) {
                        // TODO: less lazy handling
                        }
                        for (String definition : definitions) {
                            String name = definition_names != null && definition_names.length >= x ? definition_names[x - 1].trim() : String.valueOf(x);
                            queue.addDefinition(name, definition);
                            dB.echoDebug(scriptEntry, "Adding definition %" + name + "% as " + definition);
                            x++;
                        }
                    }
                    queue.start();
                }
                scriptEntry.setFinished(true);
            }
        }
    };
    task.runTaskTimer(DenizenAPI.getCurrentInstance(), 0, prec);
}
Also used : net.aufdemrand.denizencore.objects.dList(net.aufdemrand.denizencore.objects.dList) Element(net.aufdemrand.denizencore.objects.Element) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) Duration(net.aufdemrand.denizencore.objects.Duration) ScriptEntry(net.aufdemrand.denizencore.scripts.ScriptEntry) InvalidArgumentsException(net.aufdemrand.denizencore.exceptions.InvalidArgumentsException) CommandExecutionException(net.aufdemrand.denizencore.exceptions.CommandExecutionException) net.aufdemrand.denizen.objects.dEntity(net.aufdemrand.denizen.objects.dEntity) net.aufdemrand.denizencore.objects.dScript(net.aufdemrand.denizencore.objects.dScript) BlockHelper(net.aufdemrand.denizen.nms.interfaces.BlockHelper) net.aufdemrand.denizencore.objects.dList(net.aufdemrand.denizencore.objects.dList) List(java.util.List) net.aufdemrand.denizen.objects.dLocation(net.aufdemrand.denizen.objects.dLocation) Vector(org.bukkit.util.Vector) Location(org.bukkit.Location) net.aufdemrand.denizen.objects.dLocation(net.aufdemrand.denizen.objects.dLocation) ScriptQueue(net.aufdemrand.denizencore.scripts.queues.ScriptQueue)

Example 15 with CommandExecutionException

use of net.aufdemrand.denizencore.exceptions.CommandExecutionException in project Denizen-For-Bukkit by DenizenScript.

the class RunCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
    dB.report(scriptEntry, getName(), (scriptEntry.hasObject("script") ? scriptEntry.getdObject("script").debug() : scriptEntry.getScript().debug()) + (scriptEntry.hasObject("instant") ? scriptEntry.getdObject("instant").debug() : "") + (scriptEntry.hasObject("path") ? scriptEntry.getElement("path").debug() : "") + (scriptEntry.hasObject("local") ? scriptEntry.getElement("local").debug() : "") + (scriptEntry.hasObject("delay") ? scriptEntry.getdObject("delay").debug() : "") + (scriptEntry.hasObject("id") ? scriptEntry.getdObject("id").debug() : "") + (scriptEntry.hasObject("definitions") ? scriptEntry.getdObject("definitions").debug() : "") + (scriptEntry.hasObject("speed") ? scriptEntry.getdObject("speed").debug() : ""));
    // Get the script
    dScript script = scriptEntry.getdObject("script");
    // Get the entries
    List<ScriptEntry> entries;
    // If it's local
    if (scriptEntry.hasObject("local")) {
        entries = scriptEntry.getScript().getContainer().getEntries(scriptEntry.entryData.clone(), scriptEntry.getElement("path").asString());
        script = scriptEntry.getScript();
    } else // If it has a path
    if (scriptEntry.hasObject("path") && scriptEntry.getObject("path") != null) {
        entries = script.getContainer().getEntries(scriptEntry.entryData.clone(), scriptEntry.getElement("path").asString());
    } else // Else, assume standard path
    {
        entries = script.getContainer().getBaseEntries(scriptEntry.entryData.clone());
    }
    // Get the 'id' if specified
    String id = (scriptEntry.hasObject("id") ? (scriptEntry.getElement("id")).asString() : ScriptQueue.getNextId(script.getContainer().getName()));
    // Build the queue
    ScriptQueue queue;
    if (scriptEntry.hasObject("instant")) {
        queue = InstantQueue.getQueue(id).addEntries(entries);
    } else {
        if (scriptEntry.hasObject("speed")) {
            Duration speed = scriptEntry.getdObject("speed");
            queue = ((TimedQueue) TimedQueue.getQueue(id).addEntries(entries)).setSpeed(speed.getTicks());
        } else {
            // Check speed of the script if a TimedQueue -- if identified, use the speed from the script.
            if (script != null && script.getContainer().contains("SPEED")) {
                long ticks = Duration.valueOf(script.getContainer().getString("SPEED", "0")).getTicks();
                if (ticks > 0) {
                    queue = ((TimedQueue) TimedQueue.getQueue(id).addEntries(entries)).setSpeed(ticks);
                } else {
                    queue = InstantQueue.getQueue(id).addEntries(entries);
                }
            } else {
                queue = TimedQueue.getQueue(id).addEntries(entries);
            }
        }
    }
    // Set any delay
    if (scriptEntry.hasObject("delay")) {
        queue.delayUntil(System.currentTimeMillis() + ((Duration) scriptEntry.getObject("delay")).getMillis());
    }
    // Set any definitions
    if (scriptEntry.hasObject("definitions")) {
        int x = 1;
        Element raw_defintions = scriptEntry.getElement("definitions");
        dList definitions = dList.valueOf(raw_defintions.asString());
        String[] definition_names = null;
        try {
            definition_names = script.getContainer().getString("definitions").split("\\|");
        } catch (Exception e) {
        // TODO: less lazy handling
        }
        for (String definition : definitions) {
            String name = definition_names != null && definition_names.length >= x ? definition_names[x - 1].trim() : String.valueOf(x);
            queue.addDefinition(name, definition);
            dB.echoDebug(scriptEntry, "Adding definition %" + name + "% as " + definition);
            x++;
        }
        queue.addDefinition("raw_context", raw_defintions.asString());
    }
    // Setup a callback if the queue is being waited on
    if (scriptEntry.shouldWaitFor()) {
        // Record the ScriptEntry
        final ScriptEntry se = scriptEntry;
        queue.callBack(new Runnable() {

            @Override
            public void run() {
                se.setFinished(true);
            }
        });
    }
    // Allow determinations because why not
    long reqId = DetermineCommand.getNewId();
    queue.setReqId(reqId);
    // Also add the reqId to each of the entries for reasons
    ScriptBuilder.addObjectToEntries(entries, "ReqId", reqId);
    // Save the queue for script referencing
    scriptEntry.addObject("created_queue", queue);
    // OK, GO!
    queue.start();
}
Also used : ScriptEntry(net.aufdemrand.denizencore.scripts.ScriptEntry) InvalidArgumentsException(net.aufdemrand.denizencore.exceptions.InvalidArgumentsException) CommandExecutionException(net.aufdemrand.denizencore.exceptions.CommandExecutionException) ScriptQueue(net.aufdemrand.denizencore.scripts.queues.ScriptQueue)

Aggregations

CommandExecutionException (net.aufdemrand.denizencore.exceptions.CommandExecutionException)21 InvalidArgumentsException (net.aufdemrand.denizencore.exceptions.InvalidArgumentsException)17 Element (net.aufdemrand.denizencore.objects.Element)16 net.aufdemrand.denizen.objects.dLocation (net.aufdemrand.denizen.objects.dLocation)8 net.aufdemrand.denizencore.objects.dList (net.aufdemrand.denizencore.objects.dList)8 Duration (net.aufdemrand.denizencore.objects.Duration)6 BukkitScriptEntryData (net.aufdemrand.denizen.BukkitScriptEntryData)5 net.aufdemrand.denizencore.objects.dScript (net.aufdemrand.denizencore.objects.dScript)5 List (java.util.List)4 net.aufdemrand.denizen.objects.dEntity (net.aufdemrand.denizen.objects.dEntity)4 ScriptEntry (net.aufdemrand.denizencore.scripts.ScriptEntry)4 Player (org.bukkit.entity.Player)4 File (java.io.File)3 net.aufdemrand.denizen.objects.dNPC (net.aufdemrand.denizen.objects.dNPC)3 net.aufdemrand.denizen.objects.dPlayer (net.aufdemrand.denizen.objects.dPlayer)3 net.aufdemrand.denizencore.objects.dObject (net.aufdemrand.denizencore.objects.dObject)3 ScriptQueue (net.aufdemrand.denizencore.scripts.queues.ScriptQueue)3 Location (org.bukkit.Location)2 BukkitRunnable (org.bukkit.scheduler.BukkitRunnable)2 Vector (org.bukkit.util.Vector)2