Search in sources :

Example 41 with LocationTag

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

the class EntityHelper method faceLocation.

/**
 * Changes an entity's yaw and pitch to make it face a location.
 *
 * @param from The Entity whose yaw and pitch you want to change.
 * @param at   The Location it should be looking at.
 */
public void faceLocation(Entity from, Location at) {
    if (from.getWorld() != at.getWorld()) {
        return;
    }
    Location origin = from instanceof LivingEntity ? ((LivingEntity) from).getEyeLocation() : new LocationTag(from.getLocation()).getBlockLocation().add(0.5, 0.5, 0.5);
    Location rotated = faceLocation(origin, at);
    rotate(from, rotated.getYaw(), rotated.getPitch());
}
Also used : LocationTag(com.denizenscript.denizen.objects.LocationTag) Location(org.bukkit.Location)

Example 42 with LocationTag

use of com.denizenscript.denizen.objects.LocationTag 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 43 with LocationTag

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

the class FlyCommand method execute.

@Override
public void execute(final ScriptEntry scriptEntry) {
    LocationTag origin = scriptEntry.getObjectTag("origin");
    List<EntityTag> entities = (List<EntityTag>) scriptEntry.getObject("entities");
    final List<LocationTag> destinations = scriptEntry.hasObject("destinations") ? (List<LocationTag>) scriptEntry.getObject("destinations") : new ArrayList<>();
    // Set freeflight to true only if there are no destinations
    final boolean freeflight = destinations.size() < 1;
    EntityTag controller = scriptEntry.getObjectTag("controller");
    // If freeflight is on, we need to do some checks
    if (freeflight) {
        // flying entities, so try to find a player in the entity list
        if (controller == null) {
            for (EntityTag entity : entities) {
                if (entity.isPlayer()) {
                    // the controller
                    if (entities.get(entities.size() - 1) != entity) {
                        controller = entity;
                        if (scriptEntry.dbCallShouldDebug()) {
                            Debug.echoDebug(scriptEntry, "Flight control defaulting to " + controller);
                        }
                        break;
                    }
                }
            }
            // If the controller is still null, we cannot continue
            if (controller == null) {
                if (scriptEntry.dbCallShouldDebug()) {
                    Debug.echoDebug(scriptEntry, "There is no one to control the flight's path!");
                }
                return;
            }
        } else // Else, if the controller was set, we need to make sure
        // it is among the flying entities, and add it if it is not
        {
            boolean found = false;
            for (EntityTag entity : entities) {
                if (entity.identify().equals(controller.identify())) {
                    found = true;
                    break;
                }
            }
            // Add the controller to the entity list
            if (!found) {
                if (scriptEntry.dbCallShouldDebug()) {
                    Debug.echoDebug(scriptEntry, "Adding controller " + controller + " to flying entities.");
                }
                entities.add(0, controller);
            }
        }
    }
    final double speed = ((ElementTag) scriptEntry.getObject("speed")).asDouble();
    final float rotationThreshold = ((ElementTag) scriptEntry.getObject("rotation_threshold")).asFloat();
    boolean cancel = scriptEntry.hasObject("cancel");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), (cancel ? db("cancel", true) : ""), origin, db("entities", entities), db("speed", speed), db("rotation threshold degrees", rotationThreshold), (freeflight ? controller : db("destinations", destinations)));
    }
    if (!cancel) {
        for (EntityTag entity : entities) {
            entity.spawnAt(origin);
        }
        Position.mount(Conversion.convertEntities(entities));
    } else {
        Position.dismount(Conversion.convertEntities(entities));
        return;
    }
    final Entity entity = entities.get(entities.size() - 1).getBukkitEntity();
    final LivingEntity finalController = controller != null ? controller.getLivingEntity() : null;
    BukkitRunnable task = new BukkitRunnable() {

        Location location = null;

        Boolean flying = true;

        public void run() {
            if (freeflight) {
                // flying where the controller is looking
                if (!entity.isEmpty() && finalController.isInsideVehicle()) {
                    location = finalController.getEyeLocation().add(finalController.getEyeLocation().getDirection().multiply(30));
                } else {
                    flying = false;
                }
            } else {
                // as there are destinations left
                if (destinations.size() > 0) {
                    location = destinations.get(0);
                } else {
                    flying = false;
                }
            }
            if (flying && entity.isValid()) {
                // when it really needs to
                if (!NMSHandler.getEntityHelper().isFacingLocation(entity, location, rotationThreshold)) {
                    NMSHandler.getEntityHelper().faceLocation(entity, location);
                }
                Vector v1 = entity.getLocation().toVector();
                Vector v2 = location.toVector();
                Vector v3 = v2.clone().subtract(v1).normalize().multiply(speed);
                entity.setVelocity(v3);
                // to be the case
                if (!freeflight) {
                    if (Math.abs(v2.getX() - v1.getX()) < 2 && Math.abs(v2.getY() - v1.getY()) < 2 && Math.abs(v2.getZ() - v1.getZ()) < 2) {
                        destinations.remove(0);
                    }
                }
            } else {
                flying = false;
                this.cancel();
            }
        }
    };
    task.runTaskTimer(Denizen.getInstance(), 0, 3);
}
Also used : Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) LocationTag(com.denizenscript.denizen.objects.LocationTag) LivingEntity(org.bukkit.entity.LivingEntity) EntityTag(com.denizenscript.denizen.objects.EntityTag) ArrayList(java.util.ArrayList) List(java.util.List) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) Vector(org.bukkit.util.Vector) Location(org.bukkit.Location)

Example 44 with LocationTag

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

the class LeashCommand method execute.

@Override
public void execute(final ScriptEntry scriptEntry) {
    List<EntityTag> entities = (List<EntityTag>) scriptEntry.getObject("entities");
    EntityTag holder = null;
    LocationTag holderLoc = null;
    Entity Holder = null;
    Object holderObject = scriptEntry.getObject("holder");
    if (holderObject instanceof EntityTag) {
        holder = scriptEntry.getObjectTag("holder");
        Holder = holder.getBukkitEntity();
    } else if (holderObject instanceof LocationTag) {
        holderLoc = scriptEntry.getObjectTag("holder");
        Material material = holderLoc.getBlock().getType();
        if (material.name().endsWith("_FENCE")) {
            Holder = holderLoc.getWorld().spawn(holderLoc, LeashHitch.class);
        } else {
            Debug.echoError(scriptEntry, "Bad holder location specified - only fences are permitted!");
            return;
        }
    }
    boolean cancel = scriptEntry.hasObject("cancel");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), (cancel ? db("cancel", "true") : ""), db("entities", entities), db("holder", holder), db("holder", holderLoc));
    }
    for (EntityTag entity : entities) {
        if (entity.isSpawned() && entity.isLivingEntity()) {
            if (cancel) {
                entity.getLivingEntity().setLeashHolder(null);
            } else {
                entity.getLivingEntity().setLeashHolder(Holder);
            }
        }
    }
}
Also used : LocationTag(com.denizenscript.denizen.objects.LocationTag) Entity(org.bukkit.entity.Entity) EntityTag(com.denizenscript.denizen.objects.EntityTag) List(java.util.List) Material(org.bukkit.Material)

Example 45 with LocationTag

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

the class MapCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    ElementTag id = scriptEntry.getElement("map-id");
    WorldTag create = scriptEntry.getObjectTag("new");
    ElementTag reset = scriptEntry.getElement("reset");
    LocationTag resetLoc = scriptEntry.getObjectTag("reset-loc");
    ElementTag image = scriptEntry.getElement("image");
    ScriptTag script = scriptEntry.getObjectTag("script");
    ElementTag resize = scriptEntry.getElement("resize");
    ElementTag width = scriptEntry.getElement("width");
    ElementTag height = scriptEntry.getElement("height");
    ElementTag scale = scriptEntry.getElement("scale");
    ElementTag tracking = scriptEntry.getElement("tracking");
    ElementTag x = scriptEntry.getElement("x-value");
    ElementTag y = scriptEntry.getElement("y-value");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), id, create, reset, resetLoc, image, script, resize, width, height, x, y);
    }
    MapView map;
    if (create != null) {
        map = Bukkit.getServer().createMap(create.getWorld());
        scriptEntry.addObject("created_map", new ElementTag(map.getId()));
        Debug.echoDebug(scriptEntry, "Created map with id " + map.getId() + ".");
    } else if (id != null) {
        map = Bukkit.getServer().getMap((short) id.asInt());
        if (map == null) {
            Debug.echoError("No map found for ID '" + id.asInt() + "'!");
            return;
        }
    } else {
        // not possible
        return;
    }
    if (reset.asBoolean()) {
        if (tracking != null) {
            map.setTrackingPosition(true);
        }
        if (scale != null) {
            map.setScale(MapView.Scale.valueOf(scale.asString().toUpperCase()));
        }
        List<MapRenderer> oldRenderers = DenizenMapManager.removeDenizenRenderers(map);
        for (MapRenderer renderer : oldRenderers) {
            map.addRenderer(renderer);
        }
        if (resetLoc != null) {
            map.setCenterX(resetLoc.getBlockX());
            map.setCenterZ(resetLoc.getBlockZ());
            map.setWorld(resetLoc.getWorld());
        }
    }
    if (script != null) {
        DenizenMapManager.removeDenizenRenderers(map);
        ((MapScriptContainer) script.getContainer()).applyTo(map);
    }
    if (image != null) {
        DenizenMapRenderer dmr = DenizenMapManager.getDenizenRenderer(map);
        int wide = width != null ? width.asInt() : resize.asBoolean() ? 128 : 0;
        int high = height != null ? height.asInt() : resize.asBoolean() ? 128 : 0;
        if (CoreUtilities.toLowerCase(image.asString()).endsWith(".gif")) {
            dmr.autoUpdate = true;
        }
        dmr.addObject(new MapImage(dmr, x.asString(), y.asString(), "true", false, image.asString(), wide, high));
        dmr.hasChanged = true;
    }
}
Also used : LocationTag(com.denizenscript.denizen.objects.LocationTag) MapImage(com.denizenscript.denizen.utilities.maps.MapImage) DenizenMapRenderer(com.denizenscript.denizen.utilities.maps.DenizenMapRenderer) MapRenderer(org.bukkit.map.MapRenderer) ScriptTag(com.denizenscript.denizencore.objects.core.ScriptTag) MapView(org.bukkit.map.MapView) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) WorldTag(com.denizenscript.denizen.objects.WorldTag) DenizenMapRenderer(com.denizenscript.denizen.utilities.maps.DenizenMapRenderer) MapScriptContainer(com.denizenscript.denizen.scripts.containers.core.MapScriptContainer)

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