Right here is a technique to get the entity {that a} participant is taking a look at, if any in any respect:
public Entity getTargetEntity(Participant participant) {
int r = 100;
for (Entity entity : participant.getNearbyEntities(r, r, r)) {
if (entity instanceof LivingEntity) {
LivingEntity livingEntity = (LivingEntity) entity;
Location eye = participant.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 tactic getTargetEntity(participant) will return an Entity object if any is detected within the line-of-sight of the Participant enter parameter, or returns null if no Entity is detected.
The worth of the int r close to the beginning of the tactic determines how far (in blocks) to seek for mobs from the participant, in all instructions (the x-, y-, and z-coordinates).
On this instance, it’s set to 100, nevertheless the tactic can be extra correct with increased numbers. Simply keep in mind that means extra entities can be checked each time the tactic is run, which can eat up vital server assets.
To make use of within the code above, for example, to detect if it’s a Participant that’s being checked out, or “focused”:
public void entityLookingAt(Participant p){
if(getTargetEntity(p) instanceof Participant){
p.sendMessage("You are taking a look at a participant!");
}
}
This could work as much as at the least v1.21.1. Hopefully, this may assist anybody who might have an analogous query sooner or later.
