Checking if the player has line of sight of a mob

Discussion in 'Plugin Development' started by Vhelky, Apr 6, 2021.

  1. Offline

    Vhelky

    Hello, I am kind of new to creating plugins as I only started today, so this may have an obvious answer, but I am trying to check whether or not the player is looking at a specific mob.

    I have already stumbled across the 'hasLineOfSight' method and I tried to implement it, but it did not work and said this: upload_2021-4-6_18-44-59.png

    Any suggestions?
     
  2. Offline

    timtower Administrator Administrator Moderator

    @Vhelky You are only giving a mobtype, you need to provide an actual mob there.
     
    Vhelky likes this.
  3. Offline

    Vhelky

    How can I do that?
     
  4. Offline

    timtower Administrator Administrator Moderator

    With which mob do you want to check if there is line of sight?
     
    Vhelky likes this.
  5. Offline

    Vhelky

    An Enderman
     
  6. Offline

    timtower Administrator Administrator Moderator

    Then find all endermans in the area / world first and loop over them.
     
  7. Offline

    Vhelky

    Is there not a way to just put in which mob I want it to check into the hasLineOfSight method? I would imagine that would be a faster and easier way of doing it, considering that the method does exactly what is detailed in its documentation, but I'm just really confused as to how I can put anything that works in there.
     
  8. Offline

    Wick

    Player#hasLineOfSight is based off of the player's look direction and a specified mob's location, you need to specify a specific entity with a location attached to it, not a vague entity type. So to achieve what you're trying to do, you would need to loop through all mobs around the player (Player#getNearbyEntities), and if the mob is an enderman, check if the player has line of sight with it.
     

Share This Page