Java Code Examples for org.bukkit.Location#getDirection()

The following examples show how to use org.bukkit.Location#getDirection() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Utils.java    From ArmorStandTools with MIT License 6 votes vote down vote up
static Location getLocationFacing(Location loc) {
    Location l = loc.clone();
    Vector v = l.getDirection();
    v.setY(0);
    v.multiply(3);
    l.add(v);
    l.setYaw(l.getYaw() + 180);
    int n;
    boolean ok = false;
    for (n = 0; n < 5; n++) {
        if (l.getBlock().getType().isSolid()) {
            l.add(0, 1, 0);
        } else {
            ok = true;
            break;
        }
    }
    if (!ok) {
        l.subtract(0, 5, 0);
    }
    return l;
}
 
Example 2
Source File: Swapper.java    From AnnihilationPro with MIT License 5 votes vote down vote up
@Override
protected boolean performSpecialAction(Player player, AnniPlayer p)
{
	if(p.getTeam() != null)
	{
		Player e = instance.getPlayerInSightTest(player, 15);
		if(e != null)
		{
			AnniPlayer pl = AnniPlayer.getPlayer(e.getUniqueId());
			if(pl != null && !pl.getTeam().equals(p.getTeam()))
			{
				Location playerLoc = player.getLocation().clone();
				Location entityLoc = e.getLocation().clone();
				
				Vector playerLook = playerLoc.getDirection();
				Vector playerVec = playerLoc.toVector();
				Vector entityVec = entityLoc.toVector();
				Vector toVec = playerVec.subtract(entityVec).normalize();
				
				e.teleport(playerLoc.setDirection(playerLook.normalize()));
				player.teleport(entityLoc.setDirection(toVec));
				e.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 3, 1));
				return true;
			}
		}	
	}
	return false;
}
 
Example 3
Source File: VectorTarget.java    From Civs with GNU General Public License v3.0 4 votes vote down vote up
@Override
public Set<?> getTargets() {
    Set<LivingEntity> returnSet = new HashSet<LivingEntity>();
    int level = getLevel();
    ConfigurationSection config = getConfig();
    Entity origin = getOrigin();
    if (!(origin instanceof LivingEntity)) {
        return returnSet;
    }
    LivingEntity player = (LivingEntity) origin;
    int range = (int) Math.round(Spell.getLevelAdjustedValue(config.getString("range","15"), level, null, null));
    boolean pen = config.getBoolean("penetration", true);
    boolean allowMultiple = config.getBoolean("allow-multiple", false);

    Location observerPos = player.getEyeLocation();
    Vector3D observerDir = new Vector3D(observerPos.getDirection());

    Vector3D observerStart = new Vector3D(observerPos);
    Vector3D observerEnd = observerStart.add(observerDir.multiply(range));

    double closestDistance = 999999999;
    HashSet<Material> materialHashSet = new HashSet<>();
    Location wallLocation = player.getTargetBlock(materialHashSet, range).getLocation();
    double wallDistance = player.getLocation().distanceSquared(wallLocation);
    // Get nearby entities
    for (Entity target : player.getNearbyEntities(range, range, range)) {
        // Bounding box of the given player
        Vector3D targetPos = new Vector3D(target.getLocation());
        Vector3D minimum = targetPos.add(-0.5, 0, -0.5);
        Vector3D maximum = targetPos.add(0.5, 1.67, 0.5);

        if (target != player &&
                Vector3D.hasIntersection(observerStart, observerEnd, minimum, maximum) &&
                target instanceof LivingEntity) {

            if (!pen && player.getLocation().distanceSquared(target.getLocation()) > wallDistance) {
                continue;
            }

            if (!allowMultiple) {
                double currentDistance = observerPos.distanceSquared(target.getLocation());
                if (closestDistance < currentDistance) {
                    continue;
                } else {
                    closestDistance = currentDistance;
                    returnSet.clear();
                }
            }

            returnSet.add((LivingEntity) target);
        }
    }

    return returnSet;
}
 
Example 4
Source File: Utils.java    From Shopkeepers with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Determines the exact intersection point of a players view and a targeted block.
 * 
 * @param player
 *            the player
 * @param targetBlock
 *            the block the player is looking at
 * @return the intersection point of the players view and the target block,
 *         or null if no intersection was found
 */
public static Location getBlockIntersection(Player player, Block targetBlock) {
	if (player == null || targetBlock == null) return null;

	// block bounds:
	double minX = targetBlock.getX();
	double minY = targetBlock.getY();
	double minZ = targetBlock.getZ();

	double maxX = minX + 1.0D;
	double maxY = minY + 1.0D;
	double maxZ = minZ + 1.0D;

	// ray origin:
	Location origin = player.getEyeLocation();
	double originX = origin.getX();
	double originY = origin.getY();
	double originZ = origin.getZ();

	// ray direction
	Vector dir = origin.getDirection();
	double dirX = dir.getX();
	double dirY = dir.getY();
	double dirZ = dir.getZ();

	// tiny improvement to save a few divisions below:
	double divX = 1.0D / dirX;
	double divY = 1.0D / dirY;
	double divZ = 1.0D / dirZ;

	// intersection interval:
	double t0 = 0.0D;
	double t1 = Double.MAX_VALUE;

	double tmin;
	double tmax;

	double tymin;
	double tymax;

	double tzmin;
	double tzmax;

	if (dirX >= 0.0D) {
		tmin = (minX - originX) * divX;
		tmax = (maxX - originX) * divX;
	} else {
		tmin = (maxX - originX) * divX;
		tmax = (minX - originX) * divX;
	}

	if (dirY >= 0.0D) {
		tymin = (minY - originY) * divY;
		tymax = (maxY - originY) * divY;
	} else {
		tymin = (maxY - originY) * divY;
		tymax = (minY - originY) * divY;
	}

	if ((tmin > tymax) || (tymin > tmax)) {
		return null;
	}

	if (tymin > tmin) tmin = tymin;
	if (tymax < tmax) tmax = tymax;

	if (dirZ >= 0.0D) {
		tzmin = (minZ - originZ) * divZ;
		tzmax = (maxZ - originZ) * divZ;
	} else {
		tzmin = (maxZ - originZ) * divZ;
		tzmax = (minZ - originZ) * divZ;
	}

	if ((tmin > tzmax) || (tzmin > tmax)) {
		return null;
	}

	if (tzmin > tmin) tmin = tzmin;
	if (tzmax < tmax) tmax = tzmax;

	if ((tmin >= t1) || (tmax <= t0)) {
		return null;
	}

	// intersection:
	Location intersection = origin.add(dir.multiply(tmin));
	return intersection;
}
 
Example 5
Source File: BlockIterator.java    From Kettle with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Constructs the BlockIterator
 *
 * @param loc         The location for the start of the ray trace
 * @param yOffset     The trace begins vertically offset from the start vector
 *                    by this value
 * @param maxDistance This is the maximum distance in blocks for the
 *                    trace. Setting this value above 140 may lead to problems with
 *                    unloaded chunks. A value of 0 indicates no limit
 */
public BlockIterator(Location loc, double yOffset, int maxDistance) {
    this(loc.getWorld(), loc.toVector(), loc.getDirection(), yOffset, maxDistance);
}
 
Example 6
Source File: BlockIterator.java    From Kettle with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Constructs the BlockIterator.
 *
 * @param loc     The location for the start of the ray trace
 * @param yOffset The trace begins vertically offset from the start vector
 *                by this value
 */

public BlockIterator(Location loc, double yOffset) {
    this(loc.getWorld(), loc.toVector(), loc.getDirection(), yOffset, 0);
}