Search in sources :

Example 1 with ScriptQueue

use of com.denizenscript.denizencore.scripts.queues.ScriptQueue in project Denizen-For-Bukkit by DenizenScript.

the class ActionHandler method doAction.

public ListTag doAction(String actionName, NPCTag npc, PlayerTag player, AssignmentScriptContainer assignment, Map<String, ObjectTag> context) {
    if (context == null) {
        context = new HashMap<>();
    }
    if (assignment == null) {
        return null;
    }
    if (!assignment.containsScriptSection("actions.on " + actionName)) {
        return null;
    }
    Debug.report(assignment, "Action", ArgumentHelper.debugObj("Type", "On " + actionName), npc, assignment.getAsScriptArg(), player);
    // Fetch script from Actions
    List<ScriptEntry> script = assignment.getEntries(new BukkitScriptEntryData(player, npc), "actions.on " + actionName);
    if (script.isEmpty()) {
        return null;
    }
    Debug.echoDebug(assignment, DebugElement.Header, "Building action 'On " + actionName.toUpperCase() + "' for " + npc.toString());
    // Add entries and context to the queue
    ScriptQueue queue = new InstantQueue(assignment.getName());
    queue.addEntries(script);
    ContextSource.SimpleMap src = new ContextSource.SimpleMap();
    src.contexts = context;
    src.contexts.put("event_header", new ElementTag(actionName));
    queue.setContextSource(src);
    // Start the queue!
    queue.start();
    return queue.determinations;
}
Also used : BukkitScriptEntryData(com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData) ContextSource(com.denizenscript.denizencore.scripts.queues.ContextSource) ScriptEntry(com.denizenscript.denizencore.scripts.ScriptEntry) InstantQueue(com.denizenscript.denizencore.scripts.queues.core.InstantQueue) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) ScriptQueue(com.denizenscript.denizencore.scripts.queues.ScriptQueue)

Example 2 with ScriptQueue

use of com.denizenscript.denizencore.scripts.queues.ScriptQueue in project Denizen-For-Bukkit by DenizenScript.

the class DenizenChat method talk.

@Override
public void talk(SpeechContext speechContext) {
    if (!(speechContext instanceof DenizenSpeechContext)) {
        return;
    }
    DenizenSpeechContext context = (DenizenSpeechContext) speechContext;
    Talkable talker = context.getTalker();
    if (talker == null) {
        return;
    }
    ScriptEntry entry = context.getScriptEntry();
    ScriptQueue queue = entry.getResidingQueue();
    String defTalker = null;
    if (queue.hasDefinition("talker")) {
        defTalker = queue.getDefinition("talker");
    }
    queue.addDefinition("talker", new EntityTag(talker.getEntity()).identify());
    String defMessage = null;
    if (queue.hasDefinition("message")) {
        defMessage = queue.getDefinition("message");
    }
    queue.addDefinition("message", context.getMessage());
    // Chat to the world using Denizen chat settings
    if (!context.hasRecipients()) {
        String text = TagManager.tag(Settings.chatNoTargetFormat(), new BukkitTagContext(entry));
        talkToBystanders(talker, text, context);
    } else // Single recipient
    if (context.size() <= 1) {
        // Send chat to target
        String text = TagManager.tag(Settings.chatToTargetFormat(), new BukkitTagContext(entry));
        for (Talkable entity : context) {
            entity.talkTo(context, text, this);
        }
        // Check if bystanders hear targeted chat
        if (context.isBystandersEnabled()) {
            String defTarget = null;
            if (queue.hasDefinition("target")) {
                defTarget = queue.getDefinition("target");
            }
            queue.addDefinition("target", new EntityTag(context.iterator().next().getEntity()).identify());
            String bystanderText = TagManager.tag(Settings.chatWithTargetToBystandersFormat(), new BukkitTagContext(entry));
            talkToBystanders(talker, bystanderText, context);
            if (defTarget != null) {
                queue.addDefinition("target", defTarget);
            }
        }
    } else // Multiple recipients
    {
        // Send chat to targets
        String text = TagManager.tag(Settings.chatToTargetFormat(), new BukkitTagContext(entry));
        for (Talkable entity : context) {
            entity.talkTo(context, text, this);
        }
        if (context.isBystandersEnabled()) {
            String[] format = Settings.chatMultipleTargetsFormat().split("%target%");
            if (format.length <= 1) {
                Debug.echoError("Invalid 'Commands.Chat.Options.Multiple targets format' in config.yml! Must have at least 1 %target%");
            }
            StringBuilder parsed = new StringBuilder();
            Iterator<Talkable> iter = context.iterator();
            int i = 0;
            while (iter.hasNext()) {
                if (i == format.length - 1) {
                    parsed.append(format[i]);
                    break;
                }
                parsed.append(format[i]).append(new EntityTag(iter.next().getEntity()).getName());
                i++;
            }
            String targets = TagManager.tag(parsed.toString(), new BukkitTagContext(entry));
            String defTargets = null;
            if (queue.hasDefinition("targets")) {
                defTargets = queue.getDefinition("targets");
            }
            queue.addDefinition("targets", targets);
            String bystanderText = TagManager.tag(Settings.chatWithTargetsToBystandersFormat(), new BukkitTagContext(entry));
            talkToBystanders(talker, bystanderText, context);
            if (defTargets != null) {
                queue.addDefinition("targets", defTargets);
            }
        }
    }
    if (defMessage != null) {
        queue.addDefinition("message", defMessage);
    }
    if (defTalker != null) {
        queue.addDefinition("talker", defTalker);
    }
}
Also used : BukkitTagContext(com.denizenscript.denizen.tags.BukkitTagContext) Iterator(java.util.Iterator) ScriptEntry(com.denizenscript.denizencore.scripts.ScriptEntry) EntityTag(com.denizenscript.denizen.objects.EntityTag) Talkable(net.citizensnpcs.api.ai.speech.Talkable) ScriptQueue(com.denizenscript.denizencore.scripts.queues.ScriptQueue)

Example 3 with ScriptQueue

use of com.denizenscript.denizencore.scripts.queues.ScriptQueue in project Denizen-For-Bukkit by DenizenScript.

the class PushCommand method execute.

@Override
public void execute(final ScriptEntry scriptEntry) {
    EntityTag originEntity = scriptEntry.getObjectTag("origin_entity");
    LocationTag originLocation = scriptEntry.hasObject("origin_location") ? (LocationTag) scriptEntry.getObject("origin_location") : new LocationTag(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 LocationTag destination = scriptEntry.hasObject("destination") ? (LocationTag) scriptEntry.getObject("destination") : (originEntity != null ? new LocationTag(originEntity.getEyeLocation().add(originEntity.getEyeLocation().getDirection().multiply(30))) : null);
    // TODO: Should this be checked in argument parsing?
    if (destination == null) {
        Debug.echoError("No destination specified!");
        scriptEntry.setFinished(true);
        return;
    }
    List<EntityTag> entities = (List<EntityTag>) scriptEntry.getObject("entities");
    final ScriptTag script = scriptEntry.getObjectTag("script");
    final ListTag definitions = scriptEntry.getObjectTag("definitions");
    ElementTag speedElement = scriptEntry.getElement("speed");
    DurationTag duration = (DurationTag) scriptEntry.getObject("duration");
    ElementTag force_along = scriptEntry.getElement("force_along");
    ElementTag precision = scriptEntry.getElement("precision");
    ElementTag ignore_collision = scriptEntry.getElement("ignore_collision");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), db("origin", originEntity != null ? originEntity : originLocation), db("entities", entities), destination, speedElement, duration, script, force_along, precision, (no_rotate ? db("no_rotate", "true") : ""), (no_damage ? db("no_damage", "true") : ""), ignore_collision, definitions);
    }
    final boolean ignoreCollision = ignore_collision != null && ignore_collision.asBoolean();
    final double speed = speedElement.asDouble();
    final int maxTicks = duration.getTicksAsInt();
    final boolean forceAlong = force_along.asBoolean();
    // Keep a ListTag of entities that can be called using <entry[name].pushed_entities> later in the script queue
    final ListTag entityList = new ListTag();
    for (EntityTag entity : entities) {
        entity.spawnAt(originLocation);
        entityList.addObject(entity);
        if (!no_rotate) {
            NMSHandler.getEntityHelper().faceLocation(entity.getBukkitEntity(), destination);
        }
        if (entity.isProjectile() && originEntity != null) {
            entity.setShooter(originEntity);
        }
    }
    scriptEntry.addObject("pushed_entities", entityList);
    Position.mount(Conversion.convertEntities(entities));
    final EntityTag 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;

        LocationTag lastLocation;

        @Override
        public void run() {
            if (runs < maxTicks && lastEntity.isValid()) {
                Vector v1 = lastEntity.getLocation().toVector();
                Vector v3 = v2.clone().subtract(v1).normalize();
                if (forceAlong) {
                    Vector newDest = v2.clone().subtract(Origin).normalize().multiply(runs * speed).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;
                    return;
                }
                Vector newVel = v3.multiply(speed);
                lastEntity.setVelocity(newVel);
                if (!ignoreCollision && lastEntity.isValid()) {
                    BoundingBox box = lastEntity.getBukkitEntity().getBoundingBox().expand(newVel);
                    Location ref = lastEntity.getLocation().clone();
                    for (int x = (int) Math.floor(box.getMinX()); x < Math.ceil(box.getMaxX()); x++) {
                        ref.setX(x);
                        for (int y = (int) Math.floor(box.getMinY()); y < Math.ceil(box.getMaxY()); y++) {
                            ref.setY(y);
                            for (int z = (int) Math.floor(box.getMinZ()); z < Math.ceil(box.getMaxZ()); z++) {
                                ref.setZ(z);
                                if (!isSafeBlock(ref)) {
                                    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) {
                    Consumer<ScriptQueue> configure = (queue) -> {
                        if (lastEntity.getLocation() != null) {
                            queue.addDefinition("location", lastEntity.getLocation());
                        } else {
                            queue.addDefinition("location", lastLocation);
                        }
                        queue.addDefinition("pushed_entities", entityList);
                        queue.addDefinition("last_entity", lastEntity);
                    };
                    ScriptUtilities.createAndStartQueue(script.getContainer(), null, scriptEntry.entryData, null, configure, null, null, definitions, scriptEntry);
                }
                scriptEntry.setFinished(true);
            }
        }
    };
    task.runTaskTimer(Denizen.getInstance(), 0, prec);
}
Also used : Utilities(com.denizenscript.denizen.utilities.Utilities) BoundingBox(org.bukkit.util.BoundingBox) LocationTag(com.denizenscript.denizen.objects.LocationTag) NMSHandler(com.denizenscript.denizen.nms.NMSHandler) InvalidArgumentsException(com.denizenscript.denizencore.exceptions.InvalidArgumentsException) Holdable(com.denizenscript.denizencore.scripts.commands.Holdable) ScriptQueue(com.denizenscript.denizencore.scripts.queues.ScriptQueue) Location(org.bukkit.Location) ScriptEntry(com.denizenscript.denizencore.scripts.ScriptEntry) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) ListTag(com.denizenscript.denizencore.objects.core.ListTag) DurationTag(com.denizenscript.denizencore.objects.core.DurationTag) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) Position(com.denizenscript.denizen.utilities.entity.Position) Denizen(com.denizenscript.denizen.Denizen) Vector(org.bukkit.util.Vector) Consumer(java.util.function.Consumer) List(java.util.List) ScriptUtilities(com.denizenscript.denizencore.utilities.ScriptUtilities) EntityTag(com.denizenscript.denizen.objects.EntityTag) com.denizenscript.denizencore.objects(com.denizenscript.denizencore.objects) Debug(com.denizenscript.denizen.utilities.debugging.Debug) ScriptTag(com.denizenscript.denizencore.objects.core.ScriptTag) AbstractCommand(com.denizenscript.denizencore.scripts.commands.AbstractCommand) Conversion(com.denizenscript.denizen.utilities.Conversion) TaskScriptContainer(com.denizenscript.denizencore.scripts.containers.core.TaskScriptContainer) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) DurationTag(com.denizenscript.denizencore.objects.core.DurationTag) ListTag(com.denizenscript.denizencore.objects.core.ListTag) LocationTag(com.denizenscript.denizen.objects.LocationTag) BoundingBox(org.bukkit.util.BoundingBox) ScriptTag(com.denizenscript.denizencore.objects.core.ScriptTag) EntityTag(com.denizenscript.denizen.objects.EntityTag) List(java.util.List) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) Vector(org.bukkit.util.Vector) Location(org.bukkit.Location) ScriptQueue(com.denizenscript.denizencore.scripts.queues.ScriptQueue)

Example 4 with ScriptQueue

use of com.denizenscript.denizencore.scripts.queues.ScriptQueue in project Denizen-For-Bukkit by DenizenScript.

the class ClickableCommand method runClickable.

public static void runClickable(UUID id, Player player) {
    Clickable clickable = clickables.get(id);
    if (clickable == null) {
        return;
    }
    if (clickable.until != 0 && System.currentTimeMillis() > clickable.until) {
        clickables.remove(id);
        return;
    }
    if (clickable.forPlayers != null && !clickable.forPlayers.contains(player.getUniqueId())) {
        return;
    }
    if (clickable.remainingUsages > 0) {
        clickable.remainingUsages--;
        if (clickable.remainingUsages <= 0) {
            clickables.remove(id);
        }
    }
    Consumer<ScriptQueue> configure = (queue) -> {
        if (clickable.defMap != null) {
            for (Map.Entry<StringHolder, ObjectTag> val : clickable.defMap.map.entrySet()) {
                queue.addDefinition(val.getKey().str, val.getValue());
            }
        }
    };
    ScriptUtilities.createAndStartQueue(clickable.script.getContainer(), clickable.path, new BukkitScriptEntryData(new PlayerTag(player), clickable.npc), null, configure, null, null, clickable.definitions, clickable.context);
}
Also used : Utilities(com.denizenscript.denizen.utilities.Utilities) InvalidArgumentsRuntimeException(com.denizenscript.denizencore.exceptions.InvalidArgumentsRuntimeException) java.util(java.util) ObjectTag(com.denizenscript.denizencore.objects.ObjectTag) com.denizenscript.denizencore.objects.core(com.denizenscript.denizencore.objects.core) BukkitScriptEntryData(com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData) Player(org.bukkit.entity.Player) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) InvalidArgumentsException(com.denizenscript.denizencore.exceptions.InvalidArgumentsException) Argument(com.denizenscript.denizencore.objects.Argument) Consumer(java.util.function.Consumer) ScriptQueue(com.denizenscript.denizencore.scripts.queues.ScriptQueue) ScriptUtilities(com.denizenscript.denizencore.utilities.ScriptUtilities) NPCTag(com.denizenscript.denizen.objects.NPCTag) TagContext(com.denizenscript.denizencore.tags.TagContext) ScriptEntry(com.denizenscript.denizencore.scripts.ScriptEntry) Debug(com.denizenscript.denizen.utilities.debugging.Debug) AbstractCommand(com.denizenscript.denizencore.scripts.commands.AbstractCommand) TaskScriptContainer(com.denizenscript.denizencore.scripts.containers.core.TaskScriptContainer) StringHolder(com.denizenscript.denizencore.utilities.text.StringHolder) ScriptEntry(com.denizenscript.denizencore.scripts.ScriptEntry) BukkitScriptEntryData(com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) ScriptQueue(com.denizenscript.denizencore.scripts.queues.ScriptQueue)

Example 5 with ScriptQueue

use of com.denizenscript.denizencore.scripts.queues.ScriptQueue in project Denizen-For-Bukkit by DenizenScript.

the class AbstractTrigger method parse.

public boolean parse(NPCTag npc, PlayerTag player, InteractScriptContainer script, String id, Map<String, ObjectTag> context) {
    if (npc == null || player == null || script == null) {
        return false;
    }
    List<ScriptEntry> entries = script.getEntriesFor(this.getClass(), player, npc, id, true);
    if (entries.isEmpty()) {
        return false;
    }
    Debug.echoDebug(script, DebugElement.Header, "Parsing " + name + " trigger: n@" + npc.getName() + "/p@" + player.getName());
    // Create Queue
    long speedTicks;
    if (script.contains("SPEED", String.class)) {
        speedTicks = DurationTag.valueOf(script.getString("SPEED", "0"), new BukkitTagContext(script)).getTicks();
    } else {
        speedTicks = DurationTag.valueOf(Settings.interactQueueSpeed(), new BukkitTagContext(script)).getTicks();
    }
    ScriptQueue queue;
    if (speedTicks > 0) {
        queue = new TimedQueue(script.getName()).setSpeed(speedTicks);
    } else {
        queue = new InstantQueue(script.getName());
    }
    // Add all entries to set it up
    queue.addEntries(entries);
    // Add context
    if (context != null) {
        ContextSource.SimpleMap src = new ContextSource.SimpleMap();
        src.contexts = context;
        queue.setContextSource(src);
    }
    if (!npc.getTriggerTrait().properly_set.get(name)) {
        if (missetWarning.testShouldWarn()) {
            Debug.echoError(missetWarning.message.replace("{NAME}", name).replace("{NPC}", npc.getId() + "/" + npc.getName()));
        }
    }
    // Start it
    queue.start();
    return true;
}
Also used : BukkitTagContext(com.denizenscript.denizen.tags.BukkitTagContext) ContextSource(com.denizenscript.denizencore.scripts.queues.ContextSource) ScriptEntry(com.denizenscript.denizencore.scripts.ScriptEntry) TimedQueue(com.denizenscript.denizencore.scripts.queues.core.TimedQueue) InstantQueue(com.denizenscript.denizencore.scripts.queues.core.InstantQueue) ScriptQueue(com.denizenscript.denizencore.scripts.queues.ScriptQueue)

Aggregations

ScriptQueue (com.denizenscript.denizencore.scripts.queues.ScriptQueue)10 ScriptEntry (com.denizenscript.denizencore.scripts.ScriptEntry)8 BukkitScriptEntryData (com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData)6 ContextSource (com.denizenscript.denizencore.scripts.queues.ContextSource)5 InstantQueue (com.denizenscript.denizencore.scripts.queues.core.InstantQueue)5 ScriptTag (com.denizenscript.denizencore.objects.core.ScriptTag)4 EntityTag (com.denizenscript.denizen.objects.EntityTag)3 BukkitTagContext (com.denizenscript.denizen.tags.BukkitTagContext)3 Utilities (com.denizenscript.denizen.utilities.Utilities)3 InvalidArgumentsException (com.denizenscript.denizencore.exceptions.InvalidArgumentsException)3 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)3 AbstractCommand (com.denizenscript.denizencore.scripts.commands.AbstractCommand)3 TaskScriptContainer (com.denizenscript.denizencore.scripts.containers.core.TaskScriptContainer)3 ScriptUtilities (com.denizenscript.denizencore.utilities.ScriptUtilities)3 Consumer (java.util.function.Consumer)3 Denizen (com.denizenscript.denizen.Denizen)2 NMSHandler (com.denizenscript.denizen.nms.NMSHandler)2 LocationTag (com.denizenscript.denizen.objects.LocationTag)2 Conversion (com.denizenscript.denizen.utilities.Conversion)2 Debug (com.denizenscript.denizen.utilities.debugging.Debug)2