Getting the player a player is looking at.

Discussion in 'Plugin Development' started by Cuvthe3rd, Jun 9, 2015.

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

    Cuvthe3rd

    Hello. I'm trying to make my server unique by removing player name tags, and for players to know who the player is, the player needs to look at the player and the name will apear as a subtitle on the screen. A bit like in Garrys mod where there are no name tags, and people have to look at you to see your name tag. Any help would be appreciated.
     
  2. Offline

    Adamki11s

    Not the easiest thing to do. You'd have to take the players line of sight, and draw a virtual line as such, and check for proximity of a player at certain points along the line. You could make the checks quicker by considering the nearest player in the immediate vicinity of the line.

    But if you want to do it, it will require a lot of math.

    EDIT: Turns out there is already a block iterator class to help with this, see : https://bukkit.org/threads/get-the-entity-a-player-is-looking-at.108296/
     
  3. Offline

    Cuvthe3rd

    I tried that code, but it still works through walls, and i dont want this.
     
  4. Offline

    SuperOriginal

    I haven't even looked at the link but I can assume you can modify the code to fix that.
     
  5. Offline

    Cuvthe3rd

    I've had a go, but iv never used a block iterator before so I don't know how I would do that.
     
  6. Offline

    SuperOriginal

    Neither have I, but we can use common sense.
    Code:
     Block item = iterator.next();
    By looking at the JavaDocs (although we didn't really need to, it's pretty self-explanatory), we can see that this next() method in the link posted above returns the next block in the traced direction. Now that the code has retrieved the next block in the line, you can just check if it isn't air, and if it isn't, then you've encountered a wall.
     
  7. Offline

    Adamki11s

    Well yes, if you want to stop it seeing through walls you'll have to consider the geometry of objects in the way of line of sight. Like I said it requires a lot more math than just iterating over a line to determine if a player can physically 'see' another entity.
     
Thread Status:
Not open for further replies.

Share This Page