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.world.entity.Entity;
021import net.minecraft.world.entity.Pose;
022import net.minecraft.world.level.ClipContext;
023import net.minecraft.world.phys.HitResult;
024import net.minecraft.world.phys.Vec3;
025
026/**
027 * @author Brady
028 * @since 8/25/2018
029 */
030public final class RayTraceUtils {
031
032    private RayTraceUtils() {}
033
034    /**
035     * Performs a block raytrace with the specified rotations. This should only be used when
036     * any entity collisions can be ignored, because this method will not recognize if an
037     * entity is in the way or not. The local player's block reach distance will be used.
038     *
039     * @param entity             The entity representing the raytrace source
040     * @param rotation           The rotation to raytrace towards
041     * @param blockReachDistance The block reach distance of the entity
042     * @return The calculated raytrace result
043     */
044    public static HitResult rayTraceTowards(Entity entity, Rotation rotation, double blockReachDistance) {
045        return rayTraceTowards(entity, rotation, blockReachDistance, false);
046    }
047
048    public static HitResult rayTraceTowards(Entity entity, Rotation rotation, double blockReachDistance, boolean wouldSneak) {
049        Vec3 start;
050        if (wouldSneak) {
051            start = inferSneakingEyePosition(entity);
052        } else {
053            start = entity.getEyePosition(1.0F); // do whatever is correct
054        }
055        
056        Vec3 direction = RotationUtils.calcLookDirectionFromRotation(rotation);
057        Vec3 end = start.add(
058                direction.x * blockReachDistance,
059                direction.y * blockReachDistance,
060                direction.z * blockReachDistance
061        );
062        return entity.level().clip(new ClipContext(start, end, ClipContext.Block.OUTLINE, ClipContext.Fluid.NONE, entity));
063    }
064
065    public static Vec3 inferSneakingEyePosition(Entity entity) {
066        return new Vec3(entity.getX(), entity.getY() + entity.getEyeHeight(Pose.CROUCHING), entity.getZ());
067    }
068}