Solved Get the entity a player is looking at

Discussion in 'Plugin Development' started by Hoolean, Oct 30, 2012.

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

    Hoolean

    Hello,

    Is there a method I can use to find the entity a player is looking at?

    E.g. Player is looking at creeper. Player does command. Plugin says "You are looking at a CREEPER! HOW YA LIVIN'!?!?!?"

    Thanks in advance :D !
     
    Violence010 likes this.
  2. Offline

    Tzeentchful

    Here is a method i found on the forums already. try search before you ask.

    Code:
     public static Entity getTarget(final Player player) {
     
            BlockIterator iterator = new BlockIterator(player.getWorld(), player
                    .getLocation().toVector(), player.getEyeLocation()
                    .getDirection(), 0, 100);
            Entity target = null;
            while (iterator.hasNext()) {
                Block item = iterator.next();
                for (Entity entity : player.getNearbyEntities(100, 100, 100)) {
                    int acc = 2;
                    for (int x = -acc; x < acc; x++)
                        for (int z = -acc; z < acc; z++)
                            for (int y = -acc; y < acc; y++)
                                if (entity.getLocation().getBlock()
                                        .getRelative(x, y, z).equals(item)) {
                                    return target = entity;
                                }
                }
            }
            return target;
        }
     
    MrBluebear3 likes this.
Thread Status:
Not open for further replies.

Share This Page