Java Code Examples for net.minecraft.entity.Entity#rayTrace()

The following examples show how to use net.minecraft.entity.Entity#rayTrace() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MixinEntityRenderer.java    From LiquidBounce with GNU General Public License v3.0 4 votes vote down vote up
/**
 * @author
 */
@Overwrite
public void getMouseOver(float p_getMouseOver_1_) {
    Entity entity = this.mc.getRenderViewEntity();
    if(entity != null && this.mc.theWorld != null) {
        this.mc.mcProfiler.startSection("pick");
        this.mc.pointedEntity = null;

        final Reach reach = (Reach) LiquidBounce.moduleManager.getModule(Reach.class);

        double d0 = reach.getState() ? reach.getMaxRange() : (double) this.mc.playerController.getBlockReachDistance();
        this.mc.objectMouseOver = entity.rayTrace(reach.getState() ? reach.getBuildReachValue().get() : d0, p_getMouseOver_1_);
        double d1 = d0;
        Vec3 vec3 = entity.getPositionEyes(p_getMouseOver_1_);
        boolean flag = false;
        if(this.mc.playerController.extendedReach()) {
            d0 = 6.0D;
            d1 = 6.0D;
        }else if(d0 > 3.0D) {
            flag = true;
        }

        if(this.mc.objectMouseOver != null) {
            d1 = this.mc.objectMouseOver.hitVec.distanceTo(vec3);
        }

        if(reach.getState()) {
            d1 = reach.getCombatReachValue().get();

            final MovingObjectPosition movingObjectPosition = entity.rayTrace(d1, p_getMouseOver_1_);

            if(movingObjectPosition != null) d1 = movingObjectPosition.hitVec.distanceTo(vec3);
        }

        Vec3 vec31 = entity.getLook(p_getMouseOver_1_);
        Vec3 vec32 = vec3.addVector(vec31.xCoord * d0, vec31.yCoord * d0, vec31.zCoord * d0);
        this.pointedEntity = null;
        Vec3 vec33 = null;
        float f = 1.0F;
        List<Entity> list = this.mc.theWorld.getEntitiesInAABBexcluding(entity, entity.getEntityBoundingBox().addCoord(vec31.xCoord * d0, vec31.yCoord * d0, vec31.zCoord * d0).expand((double) f, (double) f, (double) f), Predicates.and(EntitySelectors.NOT_SPECTATING, p_apply_1_ -> p_apply_1_.canBeCollidedWith()));
        double d2 = d1;

        for(int j = 0; j < list.size(); ++j) {
            Entity entity1 = (Entity) list.get(j);
            float f1 = entity1.getCollisionBorderSize();
            AxisAlignedBB axisalignedbb = entity1.getEntityBoundingBox().expand((double) f1, (double) f1, (double) f1);
            MovingObjectPosition movingobjectposition = axisalignedbb.calculateIntercept(vec3, vec32);
            if(axisalignedbb.isVecInside(vec3)) {
                if(d2 >= 0.0D) {
                    this.pointedEntity = entity1;
                    vec33 = movingobjectposition == null ? vec3 : movingobjectposition.hitVec;
                    d2 = 0.0D;
                }
            }else if(movingobjectposition != null) {
                double d3 = vec3.distanceTo(movingobjectposition.hitVec);
                if(d3 < d2 || d2 == 0.0D) {
                    if(entity1 == entity.ridingEntity && !entity.canRiderInteract()) {
                        if(d2 == 0.0D) {
                            this.pointedEntity = entity1;
                            vec33 = movingobjectposition.hitVec;
                        }
                    }else{
                        this.pointedEntity = entity1;
                        vec33 = movingobjectposition.hitVec;
                        d2 = d3;
                    }
                }
            }
        }

        if (this.pointedEntity != null && flag && vec3.distanceTo(vec33) > (reach.getState() ? reach.getCombatReachValue().get() : 3.0D)) {
            this.pointedEntity = null;
            this.mc.objectMouseOver = new MovingObjectPosition(MovingObjectPosition.MovingObjectType.MISS, vec33, (EnumFacing) null, new BlockPos(vec33));
        }

        if(this.pointedEntity != null && (d2 < d1 || this.mc.objectMouseOver == null)) {
            this.mc.objectMouseOver = new MovingObjectPosition(this.pointedEntity, vec33);
            if(this.pointedEntity instanceof EntityLivingBase || this.pointedEntity instanceof EntityItemFrame) {
                this.mc.pointedEntity = this.pointedEntity;
            }
        }

        this.mc.mcProfiler.endSection();
    }
}
 
Example 2
Source File: MixinEntityRenderer.java    From Hyperium with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * @author - Sk1er (added forward for distance)
 * @reason - ReachDisplay
 */
@Overwrite
public void getMouseOver(float partialTicks) {
    Entity entity = mc.getRenderViewEntity();
    if (entity != null && mc.theWorld != null) {
        mc.mcProfiler.startSection("pick");
        mc.pointedEntity = null;
        double d0 = mc.playerController.getBlockReachDistance();
        mc.objectMouseOver = entity.rayTrace(d0, partialTicks);
        double d1 = d0;
        Vec3 vec3 = entity.getPositionEyes(partialTicks);
        boolean flag = false;

        if (mc.playerController.extendedReach()) {
            d0 = 6.0D;
            d1 = 6.0D;
        } else if (d0 > 3.0D) {
            flag = true;
        }

        if (mc.objectMouseOver != null) d1 = mc.objectMouseOver.hitVec.distanceTo(vec3);

        Vec3 vec31 = entity.getLook(partialTicks);
        Vec3 vec32 = vec3.addVector(vec31.xCoord * d0, vec31.yCoord * d0, vec31.zCoord * d0);
        pointedEntity = null;
        Vec3 vec33 = null;
        float f = 1.0F;
        List<Entity> list = mc.theWorld.getEntitiesInAABBexcluding(entity, entity.getEntityBoundingBox().addCoord(vec31.xCoord * d0,
            vec31.yCoord * d0, vec31.zCoord * d0).expand(f, f, f), Predicates.and(EntitySelectors.NOT_SPECTATING, Entity::canBeCollidedWith));
        double d2 = d1;

        for (Entity entity1 : list) {
            float f1 = entity1.getCollisionBorderSize();
            AxisAlignedBB axisalignedbb = entity1.getEntityBoundingBox().expand(f1, f1, f1);
            MovingObjectPosition movingobjectposition = axisalignedbb.calculateIntercept(vec3, vec32);

            if (axisalignedbb.isVecInside(vec3)) {
                if (d2 >= 0.0D) {
                    pointedEntity = entity1;
                    vec33 = movingobjectposition == null ? vec3 : movingobjectposition.hitVec;
                    d2 = 0.0D;
                }
            } else if (movingobjectposition != null) {
                double d3 = vec3.distanceTo(movingobjectposition.hitVec);

                if (d3 < d2 || d2 == 0.0D) {
                    if (entity1 == entity.ridingEntity) {
                        if (d2 == 0.0D) {
                            pointedEntity = entity1;
                            vec33 = movingobjectposition.hitVec;
                        }
                    } else {
                        pointedEntity = entity1;
                        vec33 = movingobjectposition.hitVec;
                        d2 = d3;
                    }
                }
            }
        }
        double v = 0;

        if (pointedEntity != null && flag && (v = vec3.distanceTo(vec33)) > 3.0D) {
            pointedEntity = null;
            mc.objectMouseOver = new MovingObjectPosition(MovingObjectPosition.MovingObjectType.MISS, vec33, null, new BlockPos(vec33));
        }

        if (v != 0 || pointedEntity != null) ReachDisplay.dis = v;

        if (pointedEntity != null && (d2 < d1 || mc.objectMouseOver == null)) {
            mc.objectMouseOver = new MovingObjectPosition(pointedEntity, vec33);

            if (pointedEntity instanceof EntityLivingBase || pointedEntity instanceof EntityItemFrame) {
                mc.pointedEntity = pointedEntity;
            }
        }

        mc.mcProfiler.endSection();
    }
}
 
Example 3
Source File: LocalPlayerUtils.java    From ForgeHax with MIT License 4 votes vote down vote up
public static RayTraceResult getViewTrace(
    Entity entity, Vec3d direction, float partialTicks, double reach, double reachAttack) {
  if (entity == null) {
    return null;
  }
  
  Vec3d eyes = entity.getPositionEyes(partialTicks);
  RayTraceResult trace = entity.rayTrace(reach, partialTicks);
  
  Vec3d dir = direction.scale(reach);
  Vec3d lookDir = eyes.add(dir);
  
  double hitDistance = trace == null ? reachAttack : trace.hitVec.distanceTo(eyes);
  Entity hitEntity = null;
  Vec3d hitEntityVec = null;
  
  for (Entity ent :
      getWorld()
          .getEntitiesInAABBexcluding(
              entity,
              entity.getEntityBoundingBox().expand(dir.x, dir.y, dir.y).grow(1.D),
              Predicates.and(
                  EntitySelectors.NOT_SPECTATING,
                  ent -> ent != null && ent.canBeCollidedWith()))) {
    AxisAlignedBB bb = ent.getEntityBoundingBox().grow(ent.getCollisionBorderSize());
    RayTraceResult tr = bb.calculateIntercept(eyes, lookDir);
    if (bb.contains(eyes)) {
      if (hitDistance > 0.D) {
        hitEntity = ent;
        hitEntityVec = tr == null ? eyes : tr.hitVec;
        hitDistance = 0.D;
      }
    } else if (tr != null) {
      double dist = eyes.distanceTo(tr.hitVec);
      if (dist < hitDistance || hitDistance == 0.D) {
        if (entity.getLowestRidingEntity() == ent.getLowestRidingEntity()
            && !ent.canRiderInteract()) {
          if (hitDistance == 0.D) {
            hitEntity = ent;
            hitEntityVec = tr.hitVec;
          }
        } else {
          hitEntity = ent;
          hitEntityVec = tr.hitVec;
          hitDistance = dist;
        }
      }
    }
  }
  
  if (hitEntity != null && reach > 3.D && eyes.distanceTo(hitEntityVec) > 3.D) {
    return new RayTraceResult(Type.MISS, hitEntityVec, EnumFacing.UP, new BlockPos(hitEntityVec));
  } else if (hitEntity != null && trace == null && hitDistance < reachAttack) {
    return new RayTraceResult(hitEntity, hitEntityVec);
  } else {
    return trace;
  }
}