Search in sources :

Example 1 with GlowProjectile

use of net.glowstone.entity.projectile.GlowProjectile in project Glowstone by GlowstoneMC.

the class GlowLivingEntity method launchProjectile.

/**
 * Throws and returns a projectile, initializing its velocity.
 *
 * @param type     a projectile class that can be passed to
 *                 {@link org.bukkit.World#spawn(Location, Class)}
 * @param location initial location
 * @param x        x component of direction (doesn't need to be normalized)
 * @param y        y component of direction (doesn't need to be normalized)
 * @param z        z component of direction (doesn't need to be normalized)
 * @param speed    speed
 * @param <T>      the projectile class
 * @return the newly launched projectile
 */
private <T extends Projectile> T launchProjectile(Class<? extends T> type, Location location, double x, double y, double z, float speed) {
    double magnitude = Math.sqrt(x * x + y * y + z * z);
    if (magnitude > 0) {
        x += (x * (speed - magnitude)) / magnitude;
        y += (y * (speed - magnitude)) / magnitude;
        z += (z * (speed - magnitude)) / magnitude;
    }
    location.add(location.getDirection());
    location.setPitch(0);
    location.setYaw(0);
    T projectile = location.getWorld().spawn(location, type);
    ProjectileLaunchEvent launchEvent = EventFactory.getInstance().callEvent(new ProjectileLaunchEvent(projectile));
    if (launchEvent.isCancelled()) {
        projectile.remove();
    }
    projectile.setVelocity(new Vector(x, y, z));
    ((GlowProjectile) projectile).setRawLocation(location);
    return projectile;
}
Also used : GlowProjectile(net.glowstone.entity.projectile.GlowProjectile) ProjectileLaunchEvent(org.bukkit.event.entity.ProjectileLaunchEvent) Vector(org.bukkit.util.Vector)

Aggregations

GlowProjectile (net.glowstone.entity.projectile.GlowProjectile)1 ProjectileLaunchEvent (org.bukkit.event.entity.ProjectileLaunchEvent)1 Vector (org.bukkit.util.Vector)1