1
\$\begingroup\$

I'm creating a wizardry plugin for Minecraft, and I was wondering how you would get the entity that a player was looking at, I know that you would have to use a Vector, but I really am not familiar with them. Pretty much what I'm looking for is something like this:

public void entityLookingAt(Player p){
    if(p.EntityLookingAt() instanceof Player){
        p.sendMessage("You're looking at a player!");
    }
}
\$\endgroup\$
6
  • \$\begingroup\$ While not off topic, mod questions don't get a lot of traction here. You might want to ask at the forums for Bukkit or Minecraft. \$\endgroup\$
    – House
    Commented Jan 9, 2014 at 17:31
  • \$\begingroup\$ @Byte56 I do not believe that this is a duplicate, as this is asking how to do it using the bukkit API, the other question is asking how to do it with the Minecraft API \$\endgroup\$
    – Jojodmo
    Commented Jan 9, 2014 at 22:41
  • 2
    \$\begingroup\$ Sounds reasonable. \$\endgroup\$
    – House
    Commented Jan 10, 2014 at 0:11
  • \$\begingroup\$ @Domenico He didn't say it was a duplicate, he was just trying to say that this isn't the best place to ask for modding help because you may not get a lot of answers :) \$\endgroup\$
    – Lysol
    Commented Jan 12, 2014 at 6:25
  • \$\begingroup\$ @AidanMueller He marked it as duplicate a while ago :) \$\endgroup\$
    – Jojodmo
    Commented Jan 12, 2014 at 16:05

1 Answer 1

0
\$\begingroup\$

Here is a method to get the entity that a player is looking at, if any at all:

public Entity getTargetEntity(Player player) {
    int r = 100;
    for (Entity entity : player.getNearbyEntities(r, r, r)) {
        if (entity instanceof LivingEntity) {
            LivingEntity livingEntity = (LivingEntity) entity;
            Location eye = player.getEyeLocation();
            Vector toEntity = livingEntity.getEyeLocation().toVector().subtract(eye.toVector());
            double dot = toEntity.normalize().dot(eye.getDirection());
            if (dot > 0.99D) {
                return entity;
            }
        }
    }
    return null;
}

Calling the method getTargetEntity(player) will return an Entity object if any is detected in the line-of-sight of the Player input parameter, or returns null if no Entity is detected.

The value of the int r near the start of the method determines how far (in blocks) to search for mobs from the player, in all directions (the x-, y-, and z-coordinates).

In this example, it is set to 100, however the method will be more accurate with higher numbers. Just remember that means more entities will be checked every time the method is run, which may eat up significant server resources.

To use in the code above, for instance, to detect if it is a Player that is being looked at, or "targeted":

public void entityLookingAt(Player p){
    if(getTargetEntity(p) instanceof Player){
        p.sendMessage("You're looking at a player!");
    }
}

This should work up to at least v1.21.1. Hopefully, this will help anyone who may have a similar question in the future.

\$\endgroup\$
1
  • \$\begingroup\$ For reference, this method for getting the entity that a player is looking at works for the Bukkit/Spigot APIs for making Minecraft plugins. \$\endgroup\$ Commented Nov 8 at 13:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .