001/*
002 * This file is part of Baritone.
003 *
004 * Baritone is free software: you can redistribute it and/or modify
005 * it under the terms of the GNU Lesser General Public License as published by
006 * the Free Software Foundation, either version 3 of the License, or
007 * (at your option) any later version.
008 *
009 * Baritone is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012 * GNU Lesser General Public License for more details.
013 *
014 * You should have received a copy of the GNU Lesser General Public License
015 * along with Baritone.  If not, see <https://www.gnu.org/licenses/>.
016 */
017
018package baritone.api.utils;
019
020import net.minecraft.entity.Entity;
021import net.minecraft.util.math.RayTraceResult;
022import net.minecraft.util.math.Vec3d;
023
024/**
025 * @author Brady
026 * @since 8/25/2018
027 */
028public final class RayTraceUtils {
029
030    private RayTraceUtils() {}
031
032    /**
033     * Performs a block raytrace with the specified rotations. This should only be used when
034     * any entity collisions can be ignored, because this method will not recognize if an
035     * entity is in the way or not. The local player's block reach distance will be used.
036     *
037     * @param entity             The entity representing the raytrace source
038     * @param rotation           The rotation to raytrace towards
039     * @param blockReachDistance The block reach distance of the entity
040     * @return The calculated raytrace result
041     */
042    public static RayTraceResult rayTraceTowards(Entity entity, Rotation rotation, double blockReachDistance) {
043        return rayTraceTowards(entity, rotation, blockReachDistance, false);
044    }
045
046    public static RayTraceResult rayTraceTowards(Entity entity, Rotation rotation, double blockReachDistance, boolean wouldSneak) {
047        Vec3d start;
048        if (wouldSneak) {
049            start = inferSneakingEyePosition(entity);
050        } else {
051            start = entity.getPositionEyes(1.0F); // do whatever is correct
052        }
053        Vec3d direction = RotationUtils.calcVec3dFromRotation(rotation);
054        Vec3d end = start.add(
055                direction.x * blockReachDistance,
056                direction.y * blockReachDistance,
057                direction.z * blockReachDistance
058        );
059        return entity.world.rayTraceBlocks(start, end, false, false, true);
060    }
061
062    public static Vec3d inferSneakingEyePosition(Entity entity) {
063        return new Vec3d(entity.posX, entity.posY + IPlayerContext.eyeHeight(true), entity.posZ);
064    }
065}