Getting the entity im looking at?

Discussion in 'Plugin Development' started by Erestor1999, May 4, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    Erestor1999

    Ok, so im a HUGE noob at bukkit, so please bear with me :p
    Code:
          // PHOENIX PASSIVE 1
          
            @EventHandler
            public void interact1223(PlayerInteractEvent e){
                Action eventAction = e.getAction();
                final Player player = e.getPlayer();
               
    
                if (Phoenix.contains(player)) {
                if (eventAction == Action.RIGHT_CLICK_AIR || eventAction == Action.RIGHT_CLICK_BLOCK){
                    if (player.getItemInHand().getType().equals(Material.BLAZE_POWDER)){
                        if (player.getLevel() >= 10){  
                           
                           
                           
                        // I need to damage the entity that the player is looking at here.
                            // plz help!
                       
                       
                      
                         player.giveExpLevels(-10);
    
                         player.sendMessage(ChatColor.GRAY + "You have used your " + ChatColor.BOLD + ChatColor.AQUA + "Burning Breath " + ChatColor.GRAY + "ability!");
                         if (player.getLevel() > 100){
                             player.setLevel(100);
                         }
                         }
                    }
                       
                        }
                }   
                 }
    I cant figure out how to do it.

    Since im a MASSIVE noob at bukkit and java, it would be REALLY helpful if someone would give me an example of the code I would need to get the entity the player is looking at.

    Thanks!

    Can no one help me?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  2. Offline

    ResultStatic

    @Erestor1999 get the player's line of sight (list of blocks in a straight line) , get the nearby players, check if any of those nearby players cross locations with the blocks
     
  3. Try with this class (is not main)

    Code:
    public class Targeter {
    
        public static Player getTargetPlayer(final Player player) {
            return getTarget(player, player.getWorld().getPlayers());
        }
    
        public static Entity getTargetEntity(final Entity entity) {
            return getTarget(entity, entity.getWorld().getEntities());
        }
    
        public static <T extends Entity> T getTarget(final Entity entity,
                final Iterable<T> entities) {
            if (entity == null)
                return null;
            T target = null;
            final double threshold = 1;
            for (final T other : entities) {
                final Vector n = other.getLocation().toVector()
                        .subtract(entity.getLocation().toVector());
                if (entity.getLocation().getDirection().normalize().crossProduct(n)
                        .lengthSquared() < threshold
                        && n.normalize().dot(
                                entity.getLocation().getDirection().normalize()) >= 0) {
                    if (target == null
                            || target.getLocation().distanceSquared(
                                    entity.getLocation()) > other.getLocation()
                                    .distanceSquared(entity.getLocation()))
                        target = other;
                }
            }
            return target;
        }
    
    }
    
     
Thread Status:
Not open for further replies.

Share This Page