use of org.bukkit.Location in project Denizen-For-Bukkit by DenizenScript.
the class EntityHelper_v1_10_R1 method mapTrace.
@Override
public MapTraceResult mapTrace(LivingEntity from, double range) {
Location start = from.getEyeLocation();
Vector startVec = start.toVector();
double xzLen = Math.cos((start.getPitch() % 360) * (Math.PI / 180));
double nx = xzLen * Math.sin(-start.getYaw() * (Math.PI / 180));
double ny = Math.sin(start.getPitch() * (Math.PI / 180));
double nz = xzLen * Math.cos(start.getYaw() * (Math.PI / 180));
Vector endVec = startVec.clone().add(new Vector(nx, -ny, nz).multiply(range));
MovingObjectPosition l = rayTrace(start.getWorld(), startVec, endVec);
if (l == null || l.pos == null) {
return null;
}
Vector finalVec = new Vector(l.pos.x, l.pos.y, l.pos.z);
MapTraceResult mtr = new MapTraceResult();
switch(l.direction) {
case NORTH:
mtr.angle = BlockFace.NORTH;
break;
case SOUTH:
mtr.angle = BlockFace.SOUTH;
break;
case EAST:
mtr.angle = BlockFace.EAST;
break;
case WEST:
mtr.angle = BlockFace.WEST;
break;
}
// wallPosition - ((end - start).normalize() * 0.072)
Vector hit = finalVec.clone().subtract((endVec.clone().subtract(startVec)).normalize().multiply(0.072));
mtr.hitLocation = new Location(start.getWorld(), hit.getX(), hit.getY(), hit.getZ());
return mtr;
}
use of org.bukkit.Location in project Denizen-For-Bukkit by DenizenScript.
the class EntityHelper_v1_10_R1 method follow.
@Override
public void follow(final Entity target, final Entity follower, final double speed, final double lead, final double maxRange, final boolean allowWander) {
if (target == null || follower == null) {
return;
}
final net.minecraft.server.v1_10_R1.Entity nmsEntityFollower = ((CraftEntity) follower).getHandle();
if (!(nmsEntityFollower instanceof EntityInsentient)) {
return;
}
final EntityInsentient nmsFollower = (EntityInsentient) nmsEntityFollower;
final NavigationAbstract followerNavigation = nmsFollower.getNavigation();
UUID uuid = follower.getUniqueId();
if (followTasks.containsKey(uuid)) {
followTasks.get(uuid).cancel();
}
final int locationNearInt = (int) Math.floor(lead);
final boolean hasMax = maxRange > lead;
followTasks.put(follower.getUniqueId(), new BukkitRunnable() {
private boolean inRadius = false;
public void run() {
if (!target.isValid() || !follower.isValid()) {
this.cancel();
}
followerNavigation.a(2F);
Location targetLocation = target.getLocation();
PathEntity path;
if (hasMax && !Utilities.checkLocation(targetLocation, follower.getLocation(), maxRange) && !target.isDead() && target.isOnGround()) {
if (!inRadius) {
follower.teleport(Utilities.getWalkableLocationNear(targetLocation, locationNearInt));
} else {
inRadius = false;
path = followerNavigation.a(targetLocation.getX(), targetLocation.getY(), targetLocation.getZ());
if (path != null) {
followerNavigation.a(path, 1D);
followerNavigation.a(2D);
}
}
} else if (!inRadius && !Utilities.checkLocation(targetLocation, follower.getLocation(), lead)) {
path = followerNavigation.a(targetLocation.getX(), targetLocation.getY(), targetLocation.getZ());
if (path != null) {
followerNavigation.a(path, 1D);
followerNavigation.a(2D);
}
} else {
inRadius = true;
}
if (inRadius && !allowWander) {
followerNavigation.o();
}
nmsFollower.getAttributeInstance(GenericAttributes.MOVEMENT_SPEED).setValue(speed);
}
}.runTaskTimer(NMSHandler.getJavaPlugin(), 0, 10));
}
use of org.bukkit.Location in project Denizen-For-Bukkit by DenizenScript.
the class EntityHelper_v1_10_R1 method rotate.
@Override
public void rotate(Entity entity, float yaw, float pitch) {
// it will appear to be online
if (entity instanceof Player && ((Player) entity).isOnline()) {
Location location = entity.getLocation();
location.setYaw(yaw);
location.setPitch(pitch);
entity.teleport(location);
} else if (entity instanceof LivingEntity) {
if (entity instanceof EnderDragon) {
yaw = normalizeYaw(yaw - 180);
}
look(entity, yaw, pitch);
} else {
net.minecraft.server.v1_10_R1.Entity handle = ((CraftEntity) entity).getHandle();
handle.yaw = yaw;
handle.pitch = pitch;
}
}
use of org.bukkit.Location in project Denizen-For-Bukkit by DenizenScript.
the class Utilities method getWalkableLocationNear.
/**
* Gets a Location within a range that an entity can walk in.
*
* @param location the Location to check with
* @param range the range around the Location
* @return a random Location within range, or null if no Location within range is safe
*/
public static Location getWalkableLocationNear(Location location, int range) {
List<Location> locations = new ArrayList<Location>();
location = location.getBlock().getLocation();
// Loop through each location within the range
for (double x = -(range); x <= range; x++) {
for (double y = -(range); y <= range; y++) {
for (double z = -(range); z <= range; z++) {
// Add each block location within range
Location loc = location.clone().add(x, y, z);
if (checkLocation(location, loc, range) && isWalkable(loc)) {
locations.add(loc);
}
}
}
}
// No safe Locations found
if (locations.isEmpty()) {
return null;
}
// Return a random Location from the list
return locations.get(random.nextInt(locations.size()));
}
use of org.bukkit.Location in project Denizen-For-Bukkit by DenizenScript.
the class CuboidEnterExitSmartEvent method onWorldChange.
public void onWorldChange(PlayerChangedWorldEvent event) {
if (dEntity.isNPC(event.getPlayer())) {
return;
}
Location to = event.getPlayer().getLocation().clone();
Location from = event.getPlayer().getLocation().clone();
from.setWorld(event.getFrom());
PlayerMoveEvent evt = new PlayerMoveEvent(event.getPlayer(), from, to);
internalRun(evt, "world_change");
}
Aggregations