Java Code Examples for org.bukkit.entity.LivingEntity#getNearbyEntities()

The following examples show how to use org.bukkit.entity.LivingEntity#getNearbyEntities() . 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: TargetHelper.java    From ActionHealth with MIT License 5 votes vote down vote up
/**
 * <p>Gets all entities the player is looking at within the range using
 * the given tolerance.</p>
 *
 * @param source    living entity to get the targets of
 * @param range     maximum range to check
 * @param tolerance tolerance of the line calculation
 * @return all entities in the player's vision line
 */
public List<LivingEntity> getLivingTargets(LivingEntity source, double range, double tolerance) {
    if (source == null) {
        return new ArrayList<>();
    }

    List<Entity> list = source.getNearbyEntities(range, range, range);
    List<LivingEntity> targets = new ArrayList<LivingEntity>();

    Vector facing = source.getLocation().getDirection();
    double fLengthSq = facing.lengthSquared();

    for (Entity entity : list) {
        if (!isInFront(source, entity) || !(entity instanceof LivingEntity)) continue;

        Vector relative = entity.getLocation().subtract(source.getLocation()).toVector();
        double dot = relative.dot(facing);
        double rLengthSq = relative.lengthSquared();
        double cosSquared = (dot * dot) / (rLengthSq * fLengthSq);
        double sinSquared = 1 - cosSquared;
        double dSquared = rLengthSq * sinSquared;

        // If close enough to vision line, return the entity
        if (dSquared < tolerance) targets.add((LivingEntity) entity);
    }

    return targets;
}
 
Example 2
Source File: TargetHelper.java    From ActionHealth with MIT License 5 votes vote down vote up
/**
 * Gets the targets in a cone
 *
 * @param source entity to get the targets for
 * @param arc    arc angle of the cone
 * @param range  range of the cone
 * @return list of targets
 */
public List<LivingEntity> getConeTargets(LivingEntity source, double arc, double range) {
    List<LivingEntity> targets = new ArrayList<LivingEntity>();
    List<Entity> list = source.getNearbyEntities(range, range, range);
    if (arc <= 0) return targets;

    // Initialize values
    Vector dir = source.getLocation().getDirection();
    dir.setY(0);
    double cos = Math.cos(arc * Math.PI / 180);
    double cosSq = cos * cos;

    // Get the targets in the cone
    for (Entity entity : list) {
        if (entity instanceof LivingEntity) {

            // Greater than 360 degrees is all targets
            if (arc >= 360) {
                targets.add((LivingEntity) entity);
            }

            // Otherwise, select targets based on dot product
            else {
                Vector relative = entity.getLocation().subtract(source.getLocation()).toVector();
                relative.setY(0);
                double dot = relative.dot(dir);
                double value = dot * dot / relative.lengthSquared();
                if (arc < 180 && dot > 0 && value >= cosSq) targets.add((LivingEntity) entity);
                else if (arc >= 180 && (dot > 0 || dot <= cosSq)) targets.add((LivingEntity) entity);
            }
        }
    }

    return targets;
}
 
Example 3
Source File: AreaTarget.java    From Civs with GNU General Public License v3.0 5 votes vote down vote up
public Set<?> getTargets() {
    int level = getLevel();
    Spell spell = getSpell();
    Set<LivingEntity> returnSet = new HashSet<>();
    if (!(getOrigin() instanceof LivingEntity)) {
        return returnSet;
    }
    LivingEntity player = (LivingEntity) getOrigin();
    ConfigurationSection config = getConfig();
    int range = (int) Math.round(Spell.getLevelAdjustedValue(getConfig().getString("range","15"), level, null, spell));
    int radius = (int) Math.round(Spell.getLevelAdjustedValue(getConfig().getString("radius", "5"), level, null, spell));
    int maxTargets = (int) Math.round(Spell.getLevelAdjustedValue(getConfig().getString("max-targets", "-1"), level, null, spell));
    Collection<Entity> nearbyEntities;
    if (range < 1) {
        nearbyEntities = player.getNearbyEntities(radius, radius, radius);
    } else {
        HashSet<Material> materialHashSet = new HashSet<>();
        Location center = player.getTargetBlock(materialHashSet, range).getLocation();
        center = applyTargetSettings(center);
        ArmorStand removeMe = (ArmorStand) center.getWorld().spawnEntity(center, EntityType.ARMOR_STAND);
        removeMe.setVisible(false);
        nearbyEntities = removeMe.getNearbyEntities(radius, radius, radius);
        removeMe.remove();
    }

    for (Entity target : nearbyEntities) {
        if (maxTargets > 0 && returnSet.size() >= maxTargets) {
            break;
        }

        if (target != player &&
                target instanceof LivingEntity) {

            returnSet.add((LivingEntity) target);
        }
    }
    return returnSet;
}
 
Example 4
Source File: AntiExploitMessage.java    From EliteMobs with GNU General Public License v3.0 5 votes vote down vote up
public static void sendWarning(LivingEntity livingEntity) {

        for (Entity entity : livingEntity.getNearbyEntities(15, 15, 15))
            if (entity instanceof Player) {
                Player player = (Player) entity;
                player.spigot().sendMessage(ChatMessageType.ACTION_BAR,
                        TextComponent.fromLegacyText(ChatColorConverter.convert(ConfigValues.mobCombatSettingsConfig.getString(MobCombatSettingsConfig.ANTI_EXPLOIT_MESSAGE))));
            }


    }
 
Example 5
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 6
Source File: EliteMobScanner.java    From EliteMobs with GNU General Public License v3.0 4 votes vote down vote up
public static void scanValidAggressiveLivingEntity(LivingEntity livingEntity) {

                if (!EntityTracker.isNaturalEntity(livingEntity) &&
                        !ConfigValues.mobCombatSettingsConfig.getBoolean(MobCombatSettingsConfig.STACK_AGGRESSIVE_SPAWNER_MOBS))
                    return;
                if (EntityTracker.isNaturalEntity(livingEntity) &&
                        !ConfigValues.mobCombatSettingsConfig.getBoolean(MobCombatSettingsConfig.STACK_AGGRESSIVE_NATURAL_MOBS))
                    return;

                for (Entity secondEntity : livingEntity.getNearbyEntities(aggressiveRange, aggressiveRange, aggressiveRange)) {

                    if (!secondEntity.getType().equals(livingEntity.getType())) continue;
                    if (!livingEntity.isValid() || !secondEntity.isValid()) continue;

                    if (!EntityTracker.isNaturalEntity(secondEntity) &&
                            !ConfigValues.mobCombatSettingsConfig.getBoolean(MobCombatSettingsConfig.STACK_AGGRESSIVE_SPAWNER_MOBS))
                        return;
                    if (EntityTracker.isNaturalEntity(secondEntity) &&
                            !ConfigValues.mobCombatSettingsConfig.getBoolean(MobCombatSettingsConfig.STACK_AGGRESSIVE_NATURAL_MOBS))
                        return;


                    EliteMobEntity eliteMobEntity1 = null;
                    EliteMobEntity eliteMobEntity2 = null;

                    int level1 = 1;
                    int level2 = 1;

                    if (EntityTracker.isEliteMob(livingEntity)) {
                        eliteMobEntity1 = EntityTracker.getEliteMobEntity(livingEntity);
                        if (!eliteMobEntity1.canStack()) continue;
                        level1 = eliteMobEntity1.getLevel();
                    }

                    if (EntityTracker.isEliteMob(secondEntity)) {
                        eliteMobEntity2 = EntityTracker.getEliteMobEntity(secondEntity);
                        if (!eliteMobEntity2.canStack()) continue;
                        level2 = eliteMobEntity2.getLevel();
                    }

                    int newLevel = level1 + level2;

                    if (newLevel > ConfigValues.mobCombatSettingsConfig.getInt(MobCombatSettingsConfig.ELITEMOB_STACKING_CAP))
                        return;

                    double healthPercentage = (livingEntity.getHealth() / livingEntity.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue() +
                            ((LivingEntity) secondEntity).getHealth() / ((LivingEntity) secondEntity).getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue()) / 2;

                    /*
                    This is a bit of a dirty hack
                    It won't register the entity twice, but it will overwrite the existing method
                    */
                    eliteMobEntity1 = new EliteMobEntity(livingEntity, newLevel, healthPercentage, CreatureSpawnEvent.SpawnReason.CUSTOM);
                    secondEntity.remove();

                }

    }