/** * Get the distance between this location and another. The value of this * method is not cached and uses a costly square-root function, so do not * repeatedly call this method to get the location's magnitude. NaN will * be returned if the inner result of the sqrt() function overflows, which * will be caused if the distance is too long. * * @see Vector * @param o The other location * @return the distance * @throws IllegalArgumentException for differing worlds */ public double distance(Location o) { return Math.sqrt(distanceSquared(o)); }
@Override public int compare(Entity e1, Entity e2) { int r = ((Double) (e1.getLocation().distanceSquared(source))) .compareTo(e2.getLocation().distanceSquared(source)); if (reversed) { r = -r; } return r; } }
private Collection<GlowPlayer> collectPlayersInRadius(int radius) { int radiusSquared = radius * radius; return world.getRawPlayers().stream() .filter(player -> player.getLocation().distanceSquared(location) <= radiusSquared) .collect(Collectors.toList()); } }
@Override public void playSound(Location location, Sound sound, SoundCategory category, float volume, float pitch) { checkNotNull(location); checkNotNull(sound); double radiusSquared = Math.pow(volume * 16, 2); getRawPlayers().stream() .filter(player -> player.getLocation().distanceSquared(location) <= radiusSquared) .forEach(player -> player.playSound(location, sound, category, volume, pitch)); }
@Override public void playEffect(Location location, Effect effect, int data, int radius) { checkNotNull(location); checkNotNull(effect); int radiusSquared = radius * radius; getRawPlayers().stream() .filter(player -> player.getLocation().distanceSquared(location) <= radiusSquared) .forEach(player -> player.playEffect(location, effect, data)); }
/** * Plays an effect to all but one player within a given radius around a location. * * @param location the {@link Location} around which players must be to * hear the effect * @param effect the {@link Effect} * @param data a data bit needed for some effects * @param radius the radius around the location * @param exclude the player who won't see the effect */ public void playEffectExceptTo(Location location, Effect effect, int data, int radius, Player exclude) { checkNotNull(location); checkNotNull(effect); checkNotNull(exclude); int radiusSquared = radius * radius; getRawPlayers().stream().filter(player -> !player.equals(exclude) && player.getLocation().distanceSquared(location) <= radiusSquared) .forEach(player -> player.playEffect(location, effect, data)); }
/** * Plays a sound with a random pitch, but excludes specified players from hearing it. * * @param location the sound location * @param sound the sound to play * @param volume the volume multiplier * @param pitch the pitch modifier * @param exclude the players not to play the sound for * @throws NullPointerException if any of the location, sound, or exclude parameters is null. */ public static void playSoundAtLocationExcept(Location location, Sound sound, float volume, float pitch, GlowPlayer... exclude) { checkNotNull(location); checkNotNull(sound); checkNotNull(exclude); Set<GlowPlayer> excludeSet = Sets.newHashSet(exclude); GlowWorld world = (GlowWorld) location.getWorld(); double radiusSquared = volume * volume * 256; world.getRawPlayers() .stream() .filter(player -> player.getLocation().distanceSquared(location) <= radiusSquared && !excludeSet.contains(player)) .forEach(player -> player.playSound(location, sound, volume, pitch)); }
/** * Add the exhaustion for sprinting from the given location to the current location, if this * player <em>is</em> sprinting. * * @param move the previous location */ public void addMoveExhaustion(Location move) { if (shouldCalculateExhaustion() && !teleported && isSprinting()) { double distanceSquared = location.distanceSquared(move); if (distanceSquared > 0) { // update packet and rotation double distance = Math.sqrt(distanceSquared); addExhaustion((float) (0.1f * distance)); } } }
/** * Displays the given particle to all players. * * @param loc the location * @param particle the particle type * @param id the block or item type ID * @param data the block or item data * @param offsetX TODO: document this parameter * @param offsetY TODO: document this parameter * @param offsetZ TODO: document this parameter * @param speed TODO: document this parameter * @param amount the number of particles * @param radius TODO: document this parameter */ //@Override public void showParticle(Location loc, Effect particle, int id, int data, float offsetX, float offsetY, float offsetZ, float speed, int amount, int radius) { checkNotNull(loc); checkNotNull(particle); double radiusSquared = radius * radius; getRawPlayers().stream() .filter(player -> player.getLocation().distanceSquared(loc) <= radiusSquared) .forEach(player -> player.spigot() .playEffect(loc, particle, id, data, offsetX, offsetY, offsetZ, speed, amount, radius)); }
private double spread(World world, List<Player> list, Location[] locations, boolean teams) { double distance = 0.0D; int i = 0; Map<Team, Location> hashmap = Maps.newHashMap(); for (int j = 0; j < list.size(); ++j) { Player player = list.get(j); Location location; if (teams) { Team team = player.getScoreboard().getPlayerTeam(player); if (!hashmap.containsKey(team)) { hashmap.put(team, locations[i++]); } location = hashmap.get(team); } else { location = locations[i++]; } player.teleport(new Location(world, Math.floor(location.getX()) + 0.5D, world.getHighestBlockYAt((int) location.getX(), (int) location.getZ()), Math.floor(location.getZ()) + 0.5D)); double value = Double.MAX_VALUE; for (int k = 0; k < locations.length; ++k) { if (location != locations[k]) { double d = location.distanceSquared(locations[k]); value = Math.min(d, value); } } distance += value; } distance /= list.size(); return distance; }
@Override public void pulse() { super.pulse(); radius += radiusPerTick; waitTime--; duration--; if (duration <= 0 || radius <= 0) { remove(); } if (waitTime <= 0) { long currentTick = world.getFullTime(); for (Entity entity : world.getNearbyEntities(location, radius, radius, radius)) { if (entity instanceof LivingEntity && temporaryImmunities.getOrDefault(entity, Long.MIN_VALUE) <= currentTick && location.distanceSquared(entity.getLocation()) < radius * radius) { customEffects.values().forEach( effect -> EntityUtils.applyPotionEffectWithIntensity( effect, (LivingEntity) entity, 0.5, 0.25)); temporaryImmunities.put((LivingEntity) entity, currentTick + reapplicationDelay); } } } }
/** * Whether the hostile mob can despawn immediately. * * @return true if the mob can despawn immediately, false otherwise */ public boolean canDespawnImmediately() { if (isRemoved()) { return false; } if (world.getPlayerCount() == 0) { return false; } if (isCustomNameVisible()) { return false; } boolean hasNearbyPlayer = world.getPlayers().stream() .anyMatch(player -> location.distanceSquared(player.getLocation()) <= DESPAWN_RANGE_IMMEDIATE * DESPAWN_RANGE_IMMEDIATE); if (hasNearbyPlayer) { return false; } return true; }
@Override public void pulse() { super.pulse(); if (tickSkipped) { // find player to give experience // todo: drag self towards player Optional<Player> player = getWorld().getPlayers().stream() .filter(p -> p.getLocation().distanceSquared(location) <= 1) .findAny(); if (player.isPresent()) { player.get().giveExp(experience); world.playSound(location, Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 1.0F, 1.0F); remove(); return; } } if (getTicksLived() > LIFETIME) { remove(); return; } if (!tickSkipped) { tickSkipped = true; } }
@Override public void start(GlowLivingEntity entity) { target = null; List<Entity> nearbyEntities = entity.getNearbyEntities(RANGE, RANGE / 2, RANGE); double nearestSquared = Double.MAX_VALUE; for (Entity nearbyEntity : nearbyEntities) { if (nearbyEntity.getType() != EntityType.PLAYER) { continue; } double dist = nearbyEntity.getLocation().distanceSquared(entity.getLocation()); if (dist < nearestSquared) { target = (GlowPlayer) nearbyEntity; nearestSquared = dist; } } }
@Override public void start(GlowLivingEntity entity) { target = null; List<Entity> nearbyEntities = entity.getNearbyEntities(RANGE, RANGE / 2, RANGE); double nearestSquared = Double.MAX_VALUE; for (Entity nearbyEntity : nearbyEntities) { if (nearbyEntity.getType() != EntityType.PLAYER) { continue; } double dist = nearbyEntity.getLocation().distanceSquared(entity.getLocation()); if (dist < nearestSquared) { target = (GlowPlayer) nearbyEntity; nearestSquared = dist; } } }
@Override public <T> void spawnParticle(Particle particle, Location location, int count, double offsetX, double offsetY, double offsetZ, double extra, T data) { double distance = getLocation().distanceSquared(location); boolean isLongDistance = GlowParticle.isLongDistance(particle); int particleId = GlowParticle.getId(particle); int[] particleData = GlowParticle.getExtData(particle, data); if (distance <= 1024.0D || isLongDistance && distance <= 262144.0D) { getSession().send(new PlayParticleMessage(particleId, isLongDistance, (float) location.getX(), (float) location.getY(), (float) location.getZ(), (float) offsetX, (float) offsetY, (float) offsetZ, (float) extra, count, particleData)); } }
@Override public void execute(GlowLivingEntity entity) { if (delay == 1) { delay = 0; return; } if (target == null || !target.isOnline() || entity.getLocation().distanceSquared(target.getLocation()) > (RANGE * RANGE)) { reset(entity); return; } Location other = target.getEyeLocation(); Location location = entity.getLocation(); double x = other.getX() - location.getX(); double z = other.getZ() - location.getZ(); float yaw = (float) (Math.atan2(z, x) * (180 / Math.PI)) - 90; entity.setHeadYaw(yaw); // todo: smooth head rotation (delta) // todo: pitch rotation (head up/down) delay = 1; TransportHelper.moveTowards(entity, target.getLocation()); } }
@Override public void execute(GlowLivingEntity entity) { if (delay == 1) { delay = 0; return; } if (target == null || !target.isOnline() || entity.getLocation().distanceSquared(target.getLocation()) > (RANGE * RANGE)) { reset(entity); return; } Location other = target.getEyeLocation(); Location location = entity.getLocation(); double x = other.getX() - location.getX(); double z = other.getZ() - location.getZ(); float yaw = (float) (Math.atan2(z, x) * (180 / Math.PI)) - 90; entity.setHeadYaw(yaw); // todo: smooth head rotation (delta) // todo: pitch rotation (head up/down) delay = 1; if (entity.getType() == EntityType.ZOMBIE) { entity.setState(HostileMobState.TARGETING); } } }
double distanceSquared = location.distanceSquared(leashHolder.getLocation());
private void explode() { if (!EventFactory.getInstance().callEvent(new FireworkExplodeEvent(this)).isCancelled()) { this.playEffect(EntityEffect.FIREWORK_EXPLODE); int effectsSize = getFireworkMeta().getEffectsSize(); if (effectsSize > 0) { if (boostedEntity != null) { boostedEntity.damage((5 + effectsSize * 2), DamageCause.ENTITY_EXPLOSION); } List<Entity> nearbyEntities = this.getNearbyEntities(2.5, 2.5, 2.5); for (Entity nearbyEntity : nearbyEntities) { if (!(nearbyEntity instanceof LivingEntity)) { continue; } if (this.getLocation().distanceSquared(nearbyEntity.getLocation()) > 25) { continue; } // "The explosion of firework rockets deals 2.5 hearts of damage, per firework // star." ((LivingEntity) nearbyEntity) .damage((effectsSize * 5), DamageCause.ENTITY_EXPLOSION); } } } remove(); }