Skip to content

Instantly share code, notes, and snippets.

@Dalethium
Last active May 30, 2017 21:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dalethium/bc650696960c53c86e1137beec7280b1 to your computer and use it in GitHub Desktop.
Save Dalethium/bc650696960c53c86e1137beec7280b1 to your computer and use it in GitHub Desktop.
[Forge][1.11.2] Get the entity that another entity is looking at Server side
/**
* Gets the entity that a entity is looking at
* @param blockReachDistance the distance to check
* @param entity the entity who is looking
* @return the entity found, or null if one is not found
* @since May 30, 2017
* @author Dalethium
*/
@Nullable
public Entity getLookedAtEntity(double blockReachDistance, Entity entity) {
Vec3d eyeVec = entity.getPositionEyes(0f);
Vec3d lookVec = entity.getLook(1.0F);
Vec3d diffVec = eyeVec.addVector(lookVec.xCoord * blockReachDistance, lookVec.yCoord * blockReachDistance, lookVec.zCoord * blockReachDistance);
Vec3d resultVec = null;
RayTraceResult traceA = entity.world.rayTraceBlocks(eyeVec, diffVec, false, false, true);
double distA = blockReachDistance;
Entity pointedEntity = null;
if (traceA != null) {
distA = traceA.hitVec.distanceTo(eyeVec);
}
List<Entity> list = entity.world.getEntitiesInAABBexcluding(entity, entity.getEntityBoundingBox().addCoord(lookVec.xCoord * blockReachDistance, lookVec.yCoord * blockReachDistance, lookVec.zCoord * blockReachDistance).expand(1.0D, 1.0D, 1.0D), Predicates.and(EntitySelectors.NOT_SPECTATING, new Predicate<Entity>() {
public boolean apply(@Nullable Entity entity) {
return entity != null && entity.canBeCollidedWith();
}
}));
for (int j = 0; j < list.size(); ++j) {
Entity currEntity = (Entity)list.get(j);
AxisAlignedBB axisalignedbb = currEntity.getEntityBoundingBox().expandXyz((double)currEntity.getCollisionBorderSize());
RayTraceResult raytraceresult = axisalignedbb.calculateIntercept(eyeVec, diffVec);
if (axisalignedbb.isVecInside(eyeVec)) {
if (distA >= 0.0D) {
pointedEntity = currEntity;
resultVec = raytraceresult == null ? eyeVec : raytraceresult.hitVec;
distA = 0.0D;
}
} else if (raytraceresult != null) {
double distB = eyeVec.distanceTo(raytraceresult.hitVec);
if (distB < distA || distA == 0.0D) {
if (currEntity.getLowestRidingEntity() == entity.getLowestRidingEntity() && !entity.canRiderInteract()) {
if (distA == 0.0D) {
pointedEntity = currEntity;
resultVec = raytraceresult.hitVec;
}
} else {
pointedEntity = currEntity;
resultVec = raytraceresult.hitVec;
distA = distB;
}
}
}
}
return pointedEntity;
}
@Dalethium
Copy link
Author

I spent a lot of time looking for a way to do this and found nothing, so I figured I'd share it, it's mostly code ripped from sections of minecraft/forge code.

It's not very clean, most of it is not even refractored, sorry.

I just finished the method, and did basic functionality testing, obviously this is AS IS and WITHOUT WARRANTY.

If you find a bug or whatever feel free to correct it, I just wanted to give people a starting point.

Have a great day!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment