Search in sources :

Example 6 with ScriptQueue

use of net.aufdemrand.denizencore.scripts.queues.ScriptQueue in project Denizen-For-Bukkit by DenizenScript.

the class ModifyBlockCommand method execute.

@Override
public void execute(final ScriptEntry scriptEntry) throws CommandExecutionException {
    final dList materials = scriptEntry.getdObject("materials");
    final List<dLocation> locations = (List<dLocation>) scriptEntry.getObject("locations");
    final dList location_list = scriptEntry.getdObject("location_list");
    final Element physics = scriptEntry.getElement("physics");
    final Element natural = scriptEntry.getElement("natural");
    final Element delayed = scriptEntry.getElement("delayed");
    final Element radiusElement = scriptEntry.getElement("radius");
    final Element heightElement = scriptEntry.getElement("height");
    final Element depthElement = scriptEntry.getElement("depth");
    final dScript script = scriptEntry.getdObject("script");
    dList percents = scriptEntry.getdObject("percents");
    if (percents != null && percents.size() != materials.size()) {
        dB.echoError(scriptEntry.getResidingQueue(), "Percents length != materials length");
        percents = null;
    }
    final List<dMaterial> materialList = materials.filter(dMaterial.class);
    dB.report(scriptEntry, getName(), (locations == null ? location_list.debug() : aH.debugList("locations", locations)) + materials.debug() + physics.debug() + radiusElement.debug() + heightElement.debug() + depthElement.debug() + natural.debug() + delayed.debug() + (script != null ? script.debug() : "") + (percents != null ? percents.debug() : ""));
    final boolean doPhysics = physics.asBoolean();
    final boolean isNatural = natural.asBoolean();
    final int radius = radiusElement.asInt();
    final int height = heightElement.asInt();
    final int depth = depthElement.asInt();
    List<Float> percentages = null;
    if (percents != null) {
        percentages = new ArrayList<Float>();
        for (String str : percents) {
            percentages.add(new Element(str).asFloat());
        }
    }
    final List<Float> percs = percentages;
    if ((locations == null || locations.size() == 0) && (location_list == null || location_list.size() == 0)) {
        dB.echoError("Must specify a valid location!");
    }
    if (materialList.size() == 0) {
        dB.echoError("Must specify a valid material!");
    }
    no_physics = !doPhysics;
    if (delayed.asBoolean()) {
        new BukkitRunnable() {

            int index = 0;

            @Override
            public void run() {
                long start = System.currentTimeMillis();
                dLocation loc;
                if (locations != null) {
                    loc = locations.get(0);
                } else {
                    loc = dLocation.valueOf(location_list.get(0));
                }
                boolean was_static = preSetup(loc);
                while ((locations != null && locations.size() > index) || (location_list != null && location_list.size() > index)) {
                    dLocation nLoc;
                    if (locations != null) {
                        nLoc = locations.get(index);
                    } else {
                        nLoc = dLocation.valueOf(location_list.get(index));
                    }
                    handleLocation(nLoc, index, materialList, doPhysics, isNatural, radius, height, depth, percs);
                    index++;
                    if (System.currentTimeMillis() - start > 50) {
                        break;
                    }
                }
                postComplete(loc, was_static);
                if ((locations != null && locations.size() == index) || (location_list != null && location_list.size() == index)) {
                    if (script != null) {
                        List<ScriptEntry> entries = script.getContainer().getBaseEntries(scriptEntry.entryData.clone());
                        ScriptQueue queue = InstantQueue.getQueue(ScriptQueue.getNextId(script.getContainer().getName())).addEntries(entries);
                        queue.start();
                    }
                    scriptEntry.setFinished(true);
                    cancel();
                }
            }
        }.runTaskTimer(DenizenAPI.getCurrentInstance(), 1, 1);
    } else {
        dLocation loc;
        if (locations != null) {
            loc = locations.get(0);
        } else {
            loc = dLocation.valueOf(location_list.get(0));
        }
        boolean was_static = preSetup(loc);
        int index = 0;
        if (locations != null) {
            for (dObject obj : locations) {
                handleLocation((dLocation) obj, index, materialList, doPhysics, isNatural, radius, height, depth, percentages);
                index++;
            }
        } else {
            for (String str : location_list) {
                handleLocation(dLocation.valueOf(str), index, materialList, doPhysics, isNatural, radius, height, depth, percentages);
                index++;
            }
        }
        postComplete(loc, was_static);
        scriptEntry.setFinished(true);
    }
}
Also used : net.aufdemrand.denizen.objects.dMaterial(net.aufdemrand.denizen.objects.dMaterial) net.aufdemrand.denizencore.objects.dList(net.aufdemrand.denizencore.objects.dList) Element(net.aufdemrand.denizencore.objects.Element) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) net.aufdemrand.denizencore.objects.dScript(net.aufdemrand.denizencore.objects.dScript) net.aufdemrand.denizencore.objects.dObject(net.aufdemrand.denizencore.objects.dObject) ArrayList(java.util.ArrayList) net.aufdemrand.denizencore.objects.dList(net.aufdemrand.denizencore.objects.dList) List(java.util.List) net.aufdemrand.denizen.objects.dLocation(net.aufdemrand.denizen.objects.dLocation) ScriptQueue(net.aufdemrand.denizencore.scripts.queues.ScriptQueue)

Example 7 with ScriptQueue

use of net.aufdemrand.denizencore.scripts.queues.ScriptQueue in project Denizen-For-Bukkit by DenizenScript.

the class CommandScriptContainer method runCommandScript.

public ScriptQueue runCommandScript(dPlayer player, dNPC npc, Map<String, dObject> context) {
    ScriptQueue queue = InstantQueue.getQueue(ScriptQueue.getNextId(getName())).addEntries(getBaseEntries(new BukkitScriptEntryData(player, npc)));
    if (context != null) {
        OldEventManager.OldEventContextSource oecs = new OldEventManager.OldEventContextSource();
        oecs.contexts = context;
        queue.setContextSource(oecs);
    }
    queue.start();
    return queue;
}
Also used : BukkitScriptEntryData(net.aufdemrand.denizen.BukkitScriptEntryData) OldEventManager(net.aufdemrand.denizencore.events.OldEventManager) ScriptQueue(net.aufdemrand.denizencore.scripts.queues.ScriptQueue)

Example 8 with ScriptQueue

use of net.aufdemrand.denizencore.scripts.queues.ScriptQueue 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 9 with ScriptQueue

use of net.aufdemrand.denizencore.scripts.queues.ScriptQueue 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)

Example 10 with ScriptQueue

use of net.aufdemrand.denizencore.scripts.queues.ScriptQueue in project Denizen-For-Bukkit by DenizenScript.

the class WaitCommand method parseArgs.

@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
    // Initialize required fields
    ScriptQueue queue = scriptEntry.getResidingQueue();
    Duration delay = new Duration(3);
    // Iterate through arguments
    for (String arg : scriptEntry.getArguments()) {
        // Set duration
        if (aH.matchesDuration(arg)) {
            delay = Duration.valueOf(arg);
        }
        // Specify queue
        if (aH.matchesQueue(arg)) {
            queue = ScriptQueue._getExistingQueue(arg);
        }
    }
    scriptEntry.addObject("queue", queue);
    scriptEntry.addObject("delay", delay);
}
Also used : Duration(net.aufdemrand.denizencore.objects.Duration) ScriptQueue(net.aufdemrand.denizencore.scripts.queues.ScriptQueue)

Aggregations

ScriptQueue (net.aufdemrand.denizencore.scripts.queues.ScriptQueue)13 ScriptEntry (net.aufdemrand.denizencore.scripts.ScriptEntry)8 Element (net.aufdemrand.denizencore.objects.Element)5 List (java.util.List)4 BukkitScriptEntryData (net.aufdemrand.denizen.BukkitScriptEntryData)4 OldEventManager (net.aufdemrand.denizencore.events.OldEventManager)4 Duration (net.aufdemrand.denizencore.objects.Duration)4 net.aufdemrand.denizen.objects.dEntity (net.aufdemrand.denizen.objects.dEntity)3 net.aufdemrand.denizen.objects.dLocation (net.aufdemrand.denizen.objects.dLocation)3 CommandExecutionException (net.aufdemrand.denizencore.exceptions.CommandExecutionException)3 InvalidArgumentsException (net.aufdemrand.denizencore.exceptions.InvalidArgumentsException)3 net.aufdemrand.denizencore.objects.dList (net.aufdemrand.denizencore.objects.dList)3 net.aufdemrand.denizencore.objects.dScript (net.aufdemrand.denizencore.objects.dScript)3 BukkitRunnable (org.bukkit.scheduler.BukkitRunnable)3 ArrayList (java.util.ArrayList)2 net.aufdemrand.denizencore.objects.dObject (net.aufdemrand.denizencore.objects.dObject)2 Delayable (net.aufdemrand.denizencore.scripts.queues.core.Delayable)2 Vector (org.bukkit.util.Vector)2 Iterator (java.util.Iterator)1 BlockHelper (net.aufdemrand.denizen.nms.interfaces.BlockHelper)1