Solved How does the hasLineOfSight() method work?

Discussion in 'Spigot Plugin Development' started by CodingCyClone, Jan 14, 2022.

  1. Like the title says how does that method work like where is the source code for it? I searched for it in Minecraft's code and in bukkit's code but I cant find the code all I can find is the boolean value which resides in the LivingEntity class.
    Code (Text):
    public boolean hasLineOfSight(@NotNull Entity other);
     
  2. Just read docs!
    https://hub.spigotmc.org/javadocs/s...html#hasLineOfSight(org.bukkit.entity.Entity)
     
  3. if (yourEntity.hasLineOfSight(YourOtherEntity) ---> then do whatever you want to do
     
  4. Spigot seems to delegate it directly to nms code.
    I'd guess that you iterate along a ray in the direction the first entity is looking at. You stop when you hit another entity or reach a set distance.
    Since you're on a very outdated version you probably have to find a solution yourself. Here are some links to the interesting other methods which may help you.
    rayTrace in World
    rayTrace in BoundingBox
     
    • Useful Useful x 1
  5. That also does not show any of the source code

    Code (Text):
    @Override
        public boolean hasLineOfSight(Entity other) {
            Preconditions.checkState(!getHandle().generation, "Cannot check line of sight during world generation");

            return getHandle().hasLineOfSight(((CraftEntity) other).getHandle());
        }
     
  6. Alright thank you I was just wondering how it was done because I made a ray trace method that uses spherical coordinates (I could of used vectors instead) to iterate out from where the player is looking. I just wanted to know if there was a better solution out there.