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

The following examples show how to use net.minecraft.entity.Entity#getPositionEyes() . 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: RayTraceUtils.java    From litematica with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Nullable
public static RayTraceResult traceToSchematicWorld(Entity entity, double range, boolean respectRenderRange)
{
    World world = SchematicWorldHandler.getSchematicWorld();
    boolean invert = Hotkeys.INVERT_GHOST_BLOCK_RENDER_STATE.getKeybind().isKeybindHeld();

    if (world == null ||
        (respectRenderRange &&
            (Configs.Visuals.ENABLE_RENDERING.getBooleanValue() == false ||
             Configs.Visuals.ENABLE_SCHEMATIC_RENDERING.getBooleanValue() == invert)))
    {
        return null;
    }

    Vec3d start = entity.getPositionEyes(1f);
    Vec3d look = entity.getLook(1f).scale(range);
    Vec3d end = start.add(look);

    LayerRange layerRange = respectRenderRange ? DataManager.getRenderLayerRange() : null;

    return fi.dy.masa.malilib.util.RayTraceUtils.rayTraceBlocks(world, start, end,
            fi.dy.masa.malilib.util.RayTraceUtils::checkRayCollision,
            fi.dy.masa.malilib.util.RayTraceUtils.RayTraceFluidHandling.SOURCE_ONLY,
            fi.dy.masa.malilib.util.RayTraceUtils.BLOCK_FILTER_NON_AIR,
            false, true, layerRange, 200);
}
 
Example 2
Source File: DiscreteMovementCommandsImplementation.java    From malmo with MIT License 6 votes vote down vote up
private RayTraceResult getObjectMouseOver(DiscreteMovementCommand command)
{
    RayTraceResult mop = null;
    if (command.equals(DiscreteMovementCommand.USE))
        mop = Minecraft.getMinecraft().objectMouseOver;
    else if (command.equals(DiscreteMovementCommand.JUMPUSE))
    {
        long partialTicks = 0;  //Minecraft.timer.renderPartialTicks
        Entity viewer = Minecraft.getMinecraft().player;
        double blockReach = Minecraft.getMinecraft().playerController.getBlockReachDistance();
        Vec3d eyePos = viewer.getPositionEyes(partialTicks);
        Vec3d lookVec = viewer.getLook(partialTicks);
        int yOffset = 1;    // For the jump
        Vec3d searchVec = eyePos.addVector(lookVec.xCoord * blockReach, yOffset + lookVec.yCoord * blockReach, lookVec.zCoord * blockReach);
        mop = Minecraft.getMinecraft().world.rayTraceBlocks(eyePos, searchVec, false, false, false);
    }
    return mop;
}
 
Example 3
Source File: EntityUtils.java    From enderutilities with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Returns the index of the BB in the given list that the given entity is currently looking at.
 * @return the list index of the pointed box, or -1 of no hit was detected
 */
public static int getPointedBox(Entity entity, double reach, List<AxisAlignedBB> boxes, float partialTicks)
{
    Vec3d eyesVec = entity.getPositionEyes(partialTicks);
    Vec3d lookVec = entity.getLook(partialTicks);

    return getPointedBox(eyesVec, lookVec, reach, boxes);
}
 
Example 4
Source File: EntityUtils.java    From enderutilities with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Returns the index of the BB in the given list that the given entity is currently looking at.
 * @return the list index of the pointed box, or null of no hit was detected
 */
public static <T> T getPointedBox(Entity entity, double reach, Map<T, AxisAlignedBB> boxes, float partialTicks)
{
    Vec3d eyesVec = entity.getPositionEyes(partialTicks);
    Vec3d lookVec = entity.getLook(partialTicks);

    return getPointedBox(eyesVec, lookVec, reach, boxes);
}
 
Example 5
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 6
Source File: RaycastUtils.java    From LiquidBounce with GNU General Public License v3.0 4 votes vote down vote up
private static Entity raycastEntity(final double range, final float yaw, final float pitch, final IEntityFilter entityFilter) {
    final Entity renderViewEntity = mc.getRenderViewEntity();

    if(renderViewEntity != null && mc.theWorld != null) {
        double blockReachDistance = range;
        final Vec3 eyePosition = renderViewEntity.getPositionEyes(1F);

        final float yawCos = MathHelper.cos(-yaw * 0.017453292F - (float) Math.PI);
        final float yawSin = MathHelper.sin(-yaw * 0.017453292F - (float) Math.PI);
        final float pitchCos = -MathHelper.cos(-pitch * 0.017453292F);
        final float pitchSin = MathHelper.sin(-pitch * 0.017453292F);

        final Vec3 entityLook = new Vec3(yawSin * pitchCos, pitchSin, yawCos * pitchCos);
        final Vec3 vector = eyePosition.addVector(entityLook.xCoord * blockReachDistance, entityLook.yCoord * blockReachDistance, entityLook.zCoord * blockReachDistance);
        final List<Entity> entityList = mc.theWorld.getEntitiesInAABBexcluding(renderViewEntity, renderViewEntity.getEntityBoundingBox().addCoord(entityLook.xCoord * blockReachDistance, entityLook.yCoord * blockReachDistance, entityLook.zCoord * blockReachDistance).expand(1D, 1D, 1D), Predicates.and(EntitySelectors.NOT_SPECTATING, Entity :: canBeCollidedWith));

        Entity pointedEntity = null;

        for(final Entity entity : entityList) {
            if(!entityFilter.canRaycast(entity))
                continue;

            final float collisionBorderSize = entity.getCollisionBorderSize();
            final AxisAlignedBB axisAlignedBB = entity.getEntityBoundingBox().expand(collisionBorderSize, collisionBorderSize, collisionBorderSize);
            final MovingObjectPosition movingObjectPosition = axisAlignedBB.calculateIntercept(eyePosition, vector);

            if(axisAlignedBB.isVecInside(eyePosition)) {
                if(blockReachDistance >= 0.0D) {
                    pointedEntity = entity;
                    blockReachDistance = 0.0D;
                }
            }else if(movingObjectPosition != null) {
                final double eyeDistance = eyePosition.distanceTo(movingObjectPosition.hitVec);

                if(eyeDistance < blockReachDistance || blockReachDistance == 0.0D) {
                    if(entity == renderViewEntity.ridingEntity && !renderViewEntity.canRiderInteract()) {
                        if(blockReachDistance == 0.0D)
                            pointedEntity = entity;
                    }else{
                        pointedEntity = entity;
                        blockReachDistance = eyeDistance;
                    }
                }
            }
        }

        return pointedEntity;
    }

    return null;
}
 
Example 7
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 8
Source File: RayTraceUtils.java    From litematica with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Nonnull
public static RayTraceWrapper getWrappedRayTraceFromEntity(World world, Entity entity, double range)
{
    Vec3d eyesPos = entity.getPositionEyes(1f);
    Vec3d rangedLookRot = entity.getLook(1f).scale(range);
    Vec3d lookEndPos = eyesPos.add(rangedLookRot);

    RayTraceResult result = fi.dy.masa.malilib.util.RayTraceUtils
            .getRayTraceFromEntity(world, entity, RayTraceFluidHandling.NONE, false, range);
    double closestVanilla = result.typeOfHit != RayTraceResult.Type.MISS ? result.hitVec.distanceTo(eyesPos) : -1D;

    AreaSelection area = DataManager.getSelectionManager().getCurrentSelection();
    RayTraceWrapper wrapper = null;

    clearTraceVars();

    if (DataManager.getToolMode().getUsesSchematic() == false && area != null)
    {
        for (SelectionBox box : area.getAllSubRegionBoxes())
        {
            boolean hitCorner = false;
            hitCorner |= traceToSelectionBoxCorner(box, Corner.CORNER_1, eyesPos, lookEndPos);
            hitCorner |= traceToSelectionBoxCorner(box, Corner.CORNER_2, eyesPos, lookEndPos);

            if (hitCorner == false)
            {
                traceToSelectionBoxBody(box, eyesPos, lookEndPos);
            }
        }

        BlockPos origin = area.getExplicitOrigin();

        if (origin != null)
        {
            traceToOrigin(origin, eyesPos, lookEndPos, HitType.SELECTION_ORIGIN, null);
        }
    }

    if (DataManager.getToolMode().getUsesSchematic())
    {
        for (SchematicPlacement placement : DataManager.getSchematicPlacementManager().getVisibleSchematicPlacements())
        {
            if (placement.isEnabled())
            {
                traceToPlacementBox(placement, eyesPos, lookEndPos);
                traceToOrigin(placement.getOrigin(), eyesPos, lookEndPos, HitType.PLACEMENT_ORIGIN, placement);
            }
        }
    }

    double closestDistance = closestVanilla;

    if (closestBoxDistance >= 0 && (closestVanilla < 0 || closestBoxDistance <= closestVanilla))
    {
        closestDistance = closestBoxDistance;
        wrapper = closestBox;
    }

    // Corners are preferred over box body hits, thus this being after the box check
    if (closestCornerDistance >= 0 && (closestVanilla < 0 || closestCornerDistance <= closestVanilla))
    {
        closestDistance = closestCornerDistance;
        wrapper = closestCorner;
    }

    // Origins are preferred over everything else
    if (closestOriginDistance >= 0 && (closestVanilla < 0 || closestOriginDistance <= closestVanilla))
    {
        closestDistance = closestOriginDistance;

        if (originType == HitType.PLACEMENT_ORIGIN)
        {
            wrapper = closestOrigin;
        }
        else
        {
            wrapper = new RayTraceWrapper(RayTraceWrapper.HitType.SELECTION_ORIGIN);
        }
    }

    clearTraceVars();

    if (wrapper == null || closestDistance < 0)
    {
        wrapper = new RayTraceWrapper();
    }

    return wrapper;
}
 
Example 9
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;
  }
}