Search in sources :

Example 96 with LocationTag

use of com.denizenscript.denizen.objects.LocationTag in project Denizen-For-Bukkit by DenizenScript.

the class MountCommand method execute.

@Override
public void execute(final ScriptEntry scriptEntry) {
    LocationTag location = scriptEntry.getObjectTag("location");
    boolean hasCustomLocation = scriptEntry.hasObject("custom_location");
    List<EntityTag> entities = (List<EntityTag>) scriptEntry.getObject("entities");
    boolean cancel = scriptEntry.hasObject("cancel");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), (cancel ? db("cancel", true) : ""), location, db("entities", entities));
    }
    if (!cancel) {
        for (EntityTag entity : entities) {
            if (!entity.isSpawned() || hasCustomLocation) {
                entity.spawnAt(location);
            }
        }
        Position.mount(Conversion.convertEntities(entities));
    } else {
        Position.dismount(Conversion.convertEntities(entities));
    }
    ListTag entityList = new ListTag();
    entityList.addObjects((List) entities);
    scriptEntry.addObject("mounted_entities", entityList);
}
Also used : LocationTag(com.denizenscript.denizen.objects.LocationTag) EntityTag(com.denizenscript.denizen.objects.EntityTag) List(java.util.List) ListTag(com.denizenscript.denizencore.objects.core.ListTag)

Example 97 with LocationTag

use of com.denizenscript.denizen.objects.LocationTag in project Denizen-For-Bukkit by DenizenScript.

the class ShootCommand 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()));
    boolean no_rotate = scriptEntry.hasObject("no_rotate") && scriptEntry.getElement("no_rotate").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().clone().add(originEntity.getEyeLocation().clone().getDirection().multiply(30))) : (originLocation != null ? new LocationTag(originLocation.clone().add(originLocation.getDirection().multiply(30))) : null));
    // TODO: Same as PUSH -- is this the place to do this?
    if (destination == null) {
        Debug.echoError("No destination specified!");
        return;
    }
    final List<EntityTag> entities = (List<EntityTag>) scriptEntry.getObject("entities");
    final ScriptTag script = scriptEntry.getObjectTag("script");
    final ElementTag subPath = scriptEntry.getElement("path");
    final ListTag definitions = scriptEntry.getObjectTag("definitions");
    EntityTag shooter = scriptEntry.getObjectTag("shooter");
    ElementTag height = scriptEntry.getElement("height");
    ElementTag gravity = scriptEntry.getElement("gravity");
    ElementTag speed = scriptEntry.getElement("speed");
    ElementTag spread = scriptEntry.getElement("spread");
    LocationTag lead = scriptEntry.getObjectTag("lead");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), originEntity, originLocation, db("entities", entities), destination, height, gravity, speed, script, subPath, shooter, spread, lead, (no_rotate ? db("no_rotate", "true") : ""), definitions);
    }
    final ListTag entityList = new ListTag();
    if (!no_rotate) {
        originLocation = new LocationTag(NMSHandler.getEntityHelper().faceLocation(originLocation, destination));
    }
    for (EntityTag entity : entities) {
        if (!entity.isSpawned() || !no_rotate) {
            entity.spawnAt(originLocation);
        }
        entityList.addObject(entity);
        if (entity.isProjectile()) {
            if (shooter != null || originEntity != null) {
                entity.setShooter(shooter != null ? shooter : originEntity);
            }
            if (script != null || scriptEntry.shouldWaitFor()) {
                arrows.put(entity.getUUID(), null);
            }
        }
    }
    scriptEntry.addObject("shot_entities", entityList);
    if (entityList.size() == 1) {
        scriptEntry.addObject("shot_entity", entityList.getObject(0));
    }
    if (spread == null) {
        Position.mount(Conversion.convertEntities(entities));
    }
    final EntityTag lastEntity = entities.get(entities.size() - 1);
    if (speed == null) {
        if (gravity == null) {
            gravity = new ElementTag(lastEntity.getEntityType().getGravity());
        }
        Vector v1 = lastEntity.getLocation().toVector();
        Vector v2 = destination.toVector();
        Vector v3 = Velocity.calculate(v1, v2, gravity.asDouble(), height.asDouble());
        lastEntity.setVelocity(v3);
    } else if (lead == null) {
        Vector relative = destination.clone().subtract(originLocation).toVector();
        lastEntity.setVelocity(relative.normalize().multiply(speed.asDouble()));
    } else {
        double g = 20;
        double v = speed.asDouble();
        Vector relative = destination.clone().subtract(originLocation).toVector();
        double testAng = Velocity.launchAngle(originLocation, destination.toVector(), v, relative.getY(), g);
        double hangTime = Velocity.hangtime(testAng, v, relative.getY(), g);
        Vector to = destination.clone().add(lead.clone().multiply(hangTime)).toVector();
        relative = to.clone().subtract(originLocation.toVector());
        double dist = Math.sqrt(relative.getX() * relative.getX() + relative.getZ() * relative.getZ());
        if (dist == 0) {
            dist = 0.1d;
        }
        testAng = Velocity.launchAngle(originLocation, to, v, relative.getY(), g);
        relative.setY(Math.tan(testAng) * dist);
        relative = relative.normalize();
        v = v + (1.188 * Math.pow(hangTime, 2));
        relative = relative.multiply(v / 20.0d);
        lastEntity.setVelocity(relative);
    }
    if (spread != null) {
        Vector base = lastEntity.getVelocity().clone();
        float sf = spread.asFloat();
        for (EntityTag entity : entities) {
            Vector newvel = Velocity.spread(base, (CoreUtilities.getRandom().nextDouble() > 0.5f ? 1 : -1) * Math.toRadians(CoreUtilities.getRandom().nextDouble() * sf), (CoreUtilities.getRandom().nextDouble() > 0.5f ? 1 : -1) * Math.toRadians(CoreUtilities.getRandom().nextDouble() * sf));
            entity.setVelocity(newvel);
        }
    }
    final LocationTag start = new LocationTag(lastEntity.getLocation());
    // A task used to trigger a script if the entity is no longer
    // being shot, when the script argument is used
    BukkitRunnable task = new BukkitRunnable() {

        boolean flying = true;

        LocationTag lastLocation = null;

        Vector lastVelocity = null;

        public void run() {
            // If the entity is no longer spawned, stop the task
            if (!lastEntity.isSpawned()) {
                if (Debug.verbose) {
                    Debug.log("Shoot ended because entity not spawned");
                }
                flying = false;
            } else // the air, stop the task
            if (lastLocation != null && lastVelocity != null && !(lastEntity.getBukkitEntity() instanceof Projectile)) {
                if (lastLocation.getWorld() != lastEntity.getBukkitEntity().getWorld() || (lastLocation.distanceSquared(lastEntity.getBukkitEntity().getLocation()) < 0.1 && lastVelocity.distanceSquared(lastEntity.getBukkitEntity().getVelocity()) < 0.1)) {
                    if (Debug.verbose) {
                        Debug.log("Shoot ended because distances short - locations: " + (lastLocation.distanceSquared(lastEntity.getBukkitEntity().getLocation())) + ", velocity: " + (lastVelocity.distanceSquared(lastEntity.getBukkitEntity().getVelocity()) < 0.1));
                    }
                    flying = false;
                }
            }
            if (!arrows.containsKey(lastEntity.getUUID()) || arrows.get(lastEntity.getUUID()) != null) {
                if (Debug.verbose) {
                    Debug.log("Shoot ended because uuid was updated (hit entity?)");
                }
                flying = false;
            }
            // are met
            if (!flying) {
                this.cancel();
                ListTag hitEntities = new ListTag();
                for (EntityTag entity : entities) {
                    if (arrows.containsKey(entity.getUUID())) {
                        EntityTag hit = arrows.get(entity.getUUID());
                        arrows.remove(entity.getUUID());
                        if (hit != null) {
                            hitEntities.addObject(hit);
                        }
                    }
                }
                if (lastLocation == null) {
                    lastLocation = start;
                }
                scriptEntry.addObject("location", new LocationTag(lastLocation));
                scriptEntry.addObject("hit_entities", hitEntities);
                if (script != null) {
                    Consumer<ScriptQueue> configure = (queue) -> {
                        queue.addDefinition("location", new LocationTag(lastLocation));
                        queue.addDefinition("shot_entities", entityList);
                        queue.addDefinition("last_entity", lastEntity);
                        queue.addDefinition("hit_entities", hitEntities);
                    };
                    ScriptUtilities.createAndStartQueue(script.getContainer(), subPath == null ? null : subPath.asString(), scriptEntry.entryData, null, configure, null, null, definitions, scriptEntry);
                }
                scriptEntry.setFinished(true);
            } else {
                // Record its position in case the entity dies
                lastLocation = lastEntity.getLocation();
                lastVelocity = lastEntity.getVelocity();
            }
        }
    };
    if (script != null || scriptEntry.shouldWaitFor()) {
        task.runTaskTimer(Denizen.getInstance(), 1, 2);
    }
}
Also used : Utilities(com.denizenscript.denizen.utilities.Utilities) LocationTag(com.denizenscript.denizen.objects.LocationTag) NMSHandler(com.denizenscript.denizen.nms.NMSHandler) Projectile(org.bukkit.entity.Projectile) HashMap(java.util.HashMap) Debug(com.denizenscript.denizencore.utilities.debugging.Debug) InvalidArgumentsException(com.denizenscript.denizencore.exceptions.InvalidArgumentsException) Holdable(com.denizenscript.denizencore.scripts.commands.Holdable) EventHandler(org.bukkit.event.EventHandler) ScriptQueue(com.denizenscript.denizencore.scripts.queues.ScriptQueue) Map(java.util.Map) ScriptEntry(com.denizenscript.denizencore.scripts.ScriptEntry) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) EntityDamageByEntityEvent(org.bukkit.event.entity.EntityDamageByEntityEvent) Bukkit(org.bukkit.Bukkit) Listener(org.bukkit.event.Listener) ListTag(com.denizenscript.denizencore.objects.core.ListTag) Velocity(com.denizenscript.denizen.utilities.entity.Velocity) Entity(org.bukkit.entity.Entity) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) UUID(java.util.UUID) 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) EventPriority(org.bukkit.event.EventPriority) EntityTag(com.denizenscript.denizen.objects.EntityTag) com.denizenscript.denizencore.objects(com.denizenscript.denizencore.objects) ScriptTag(com.denizenscript.denizencore.objects.core.ScriptTag) AbstractCommand(com.denizenscript.denizencore.scripts.commands.AbstractCommand) CoreUtilities(com.denizenscript.denizencore.utilities.CoreUtilities) Conversion(com.denizenscript.denizen.utilities.Conversion) TaskScriptContainer(com.denizenscript.denizencore.scripts.containers.core.TaskScriptContainer) ProjectileHitEvent(org.bukkit.event.entity.ProjectileHitEvent) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) ListTag(com.denizenscript.denizencore.objects.core.ListTag) Projectile(org.bukkit.entity.Projectile) LocationTag(com.denizenscript.denizen.objects.LocationTag) 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) ScriptQueue(com.denizenscript.denizencore.scripts.queues.ScriptQueue)

Example 98 with LocationTag

use of com.denizenscript.denizen.objects.LocationTag in project Denizen-For-Bukkit by DenizenScript.

the class SpawnCommand method execute.

@Override
public void execute(final ScriptEntry scriptEntry) {
    List<EntityTag> entities = (List<EntityTag>) scriptEntry.getObject("entities");
    LocationTag location = scriptEntry.getObjectTag("location");
    EntityTag target = scriptEntry.getObjectTag("target");
    ElementTag spread = scriptEntry.getElement("spread");
    boolean persistent = scriptEntry.hasObject("persistent");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), db("entities", entities), location, spread, target, (persistent ? db("persistent", "true") : ""));
    }
    // Keep a ListTag of entities that can be called using <entry[name].spawned_entities> later in the script queue
    ListTag entityList = new ListTag();
    // Go through all the entities and spawn them or teleport them, then set their targets if applicable
    for (EntityTag entity : entities) {
        Location loc = location.clone();
        if (spread != null) {
            loc.add(CoreUtilities.getRandom().nextInt(spread.asInt() * 2) - spread.asInt(), 0, CoreUtilities.getRandom().nextInt(spread.asInt() * 2) - spread.asInt());
        }
        entity.spawnAt(loc);
        entityList.addObject(entity);
        if (!entity.isSpawned()) {
            Debug.echoDebug(scriptEntry, "Failed to spawn " + entity + " (blocked by other plugin, script, or gamerule?).");
        } else {
            if (persistent && entity.isLivingEntity()) {
                entity.getLivingEntity().setRemoveWhenFarAway(false);
            }
            if (target != null) {
                entity.target(target.getLivingEntity());
            }
        }
    }
    // Add entities to context so that the specific entities created/spawned can be fetched.
    scriptEntry.addObject("spawned_entities", entityList);
    if (entities.size() != 0) {
        scriptEntry.addObject("spawned_entity", entities.get(0));
    }
}
Also used : LocationTag(com.denizenscript.denizen.objects.LocationTag) EntityTag(com.denizenscript.denizen.objects.EntityTag) List(java.util.List) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) ListTag(com.denizenscript.denizencore.objects.core.ListTag) Location(org.bukkit.Location)

Example 99 with LocationTag

use of com.denizenscript.denizen.objects.LocationTag in project Denizen-For-Bukkit by DenizenScript.

the class WalkCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    LocationTag loc = scriptEntry.getObjectTag("location");
    ElementTag speed = scriptEntry.getElement("speed");
    ElementTag auto_range = scriptEntry.getElement("auto_range");
    ElementTag radius = scriptEntry.getElement("radius");
    ElementTag stop = scriptEntry.getElement("stop");
    List<EntityTag> entities = (List<EntityTag>) scriptEntry.getObject("entities");
    final LocationTag lookat = scriptEntry.getObjectTag("lookat");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), loc, speed, auto_range, radius, lookat, stop, (db("entities", entities)));
    }
    boolean shouldStop = stop.asBoolean();
    List<NPCTag> npcs = new ArrayList<>();
    final List<EntityTag> waitForEntities = new ArrayList<>();
    for (final EntityTag entity : entities) {
        if (entity.isCitizensNPC()) {
            NPCTag npc = entity.getDenizenNPC();
            npcs.add(npc);
            if (!npc.isSpawned()) {
                Debug.echoError(scriptEntry, "NPC " + npc.identify() + " is not spawned!");
                continue;
            }
            if (shouldStop) {
                npc.getNavigator().cancelNavigation();
                continue;
            }
            if (auto_range != null && auto_range.asBoolean()) {
                double distance = npc.getLocation().distance(loc);
                if (npc.getNavigator().getLocalParameters().range() < distance + 10) {
                    npc.getNavigator().getLocalParameters().range((float) distance + 10);
                }
            }
            npc.getNavigator().setTarget(loc);
            if (lookat != null) {
                npc.getNavigator().getLocalParameters().lookAtFunction(nav -> lookat);
            }
            if (speed != null) {
                npc.getNavigator().getLocalParameters().speedModifier(speed.asFloat());
            }
            if (radius != null) {
                npc.getNavigator().getLocalParameters().addRunCallback(WalkCommandCitizensEvents.generateNewFlocker(npc.getCitizen(), radius.asDouble()));
            }
        } else if (shouldStop) {
            NMSHandler.getEntityHelper().stopWalking(entity.getBukkitEntity());
        } else {
            waitForEntities.add(entity);
            NMSHandler.getEntityHelper().walkTo(entity.getLivingEntity(), loc, speed != null ? speed.asDouble() : null, () -> checkHeld(entity));
        }
    }
    if (scriptEntry.shouldWaitFor()) {
        held.add(scriptEntry);
        if (!npcs.isEmpty()) {
            scriptEntry.addObject("tally", npcs);
        }
        if (!waitForEntities.isEmpty()) {
            scriptEntry.addObject("entities", waitForEntities);
        }
    }
}
Also used : LocationTag(com.denizenscript.denizen.objects.LocationTag) NPCTag(com.denizenscript.denizen.objects.NPCTag) ArrayList(java.util.ArrayList) EntityTag(com.denizenscript.denizen.objects.EntityTag) ArrayList(java.util.ArrayList) List(java.util.List) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag)

Example 100 with LocationTag

use of com.denizenscript.denizen.objects.LocationTag in project Denizen-For-Bukkit by DenizenScript.

the class CompassCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    LocationTag location = scriptEntry.getObjectTag("location");
    ElementTag reset = scriptEntry.getElement("reset");
    Player player = Utilities.getEntryPlayer(scriptEntry).getPlayerEntity();
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), location, reset);
    }
    if (reset.asBoolean()) {
        Location bed = player.getBedSpawnLocation();
        player.setCompassTarget(bed != null ? bed : player.getWorld().getSpawnLocation());
    } else {
        player.setCompassTarget(location);
    }
}
Also used : LocationTag(com.denizenscript.denizen.objects.LocationTag) Player(org.bukkit.entity.Player) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) Location(org.bukkit.Location)

Aggregations

LocationTag (com.denizenscript.denizen.objects.LocationTag)133 EventHandler (org.bukkit.event.EventHandler)69 EntityTag (com.denizenscript.denizen.objects.EntityTag)45 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)40 MaterialTag (com.denizenscript.denizen.objects.MaterialTag)33 List (java.util.List)21 ItemTag (com.denizenscript.denizen.objects.ItemTag)18 DurationTag (com.denizenscript.denizencore.objects.core.DurationTag)15 PlayerTag (com.denizenscript.denizen.objects.PlayerTag)14 ListTag (com.denizenscript.denizencore.objects.core.ListTag)13 NPCTag (com.denizenscript.denizen.objects.NPCTag)12 Location (org.bukkit.Location)11 ArrayList (java.util.ArrayList)8 Entity (org.bukkit.entity.Entity)8 FakeBlock (com.denizenscript.denizen.utilities.blocks.FakeBlock)6 InvalidArgumentsException (com.denizenscript.denizencore.exceptions.InvalidArgumentsException)6 Player (org.bukkit.entity.Player)6 Vector (org.bukkit.util.Vector)6 UUID (java.util.UUID)5 BukkitRunnable (org.bukkit.scheduler.BukkitRunnable)5