| 1 | package net.minecraft.world.entity.vehicle;
|
| 2 |
|
| 3 | import java.util.function.Function;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.core.Direction;
|
| 6 | import net.minecraft.tags.BlockTags;
|
| 7 | import net.minecraft.world.entity.EntityType;
|
| 8 | import net.minecraft.world.entity.EntityTypes;
|
| 9 | import net.minecraft.world.entity.LivingEntity;
|
| 10 | import net.minecraft.world.entity.Pose;
|
| 11 | import net.minecraft.world.level.BlockGetter;
|
| 12 | import net.minecraft.world.level.CollisionGetter;
|
| 13 | import net.minecraft.world.level.block.TrapDoorBlock;
|
| 14 | import net.minecraft.world.level.block.state.BlockState;
|
| 15 | import net.minecraft.world.phys.AABB;
|
| 16 | import net.minecraft.world.phys.Vec3;
|
| 17 | import net.minecraft.world.phys.shapes.Shapes;
|
| 18 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 19 | import org.jspecify.annotations.Nullable;
|
| 20 |
|
| 21 | public class DismountHelper {
|
| 22 | public static int[][] offsetsForDirection(final Direction forward) {
|
| 23 | Direction right = forward.getClockWise();
|
| 24 | Direction left = right.getOpposite();
|
| 25 | Direction back = forward.getOpposite();
|
| 26 | return new int[][]{
|
| 27 | {right.getStepX(), right.getStepZ()},
|
| 28 | {left.getStepX(), left.getStepZ()},
|
| 29 | {back.getStepX() + right.getStepX(), back.getStepZ() + right.getStepZ()},
|
| 30 | {back.getStepX() + left.getStepX(), back.getStepZ() + left.getStepZ()},
|
| 31 | {forward.getStepX() + right.getStepX(), forward.getStepZ() + right.getStepZ()},
|
| 32 | {forward.getStepX() + left.getStepX(), forward.getStepZ() + left.getStepZ()},
|
| 33 | {back.getStepX(), back.getStepZ()},
|
| 34 | {forward.getStepX(), forward.getStepZ()}
|
| 35 | };
|
| 36 | }
|
| 37 |
|
| 38 | public static boolean isBlockFloorValid(final double blockFloorHeight) {
|
| 39 | return !Double.isInfinite(blockFloorHeight) && blockFloorHeight < 1.0;
|
| 40 | }
|
| 41 |
|
| 42 | public static boolean canDismountTo(final CollisionGetter level, final LivingEntity passenger, final AABB box) {
|
| 43 | for (VoxelShape collision : level.getBlockCollisions(passenger, box)) {
|
| 44 | if (!collision.isEmpty()) {
|
| 45 | return false;
|
| 46 | }
|
| 47 | }
|
| 48 |
|
| 49 | return level.getWorldBorder().isWithinBounds(box);
|
| 50 | }
|
| 51 |
|
| 52 | public static boolean canDismountTo(final CollisionGetter level, final Vec3 location, final LivingEntity passenger, final Pose dismountPose) {
|
| 53 | return canDismountTo(level, passenger, passenger.getLocalBoundsForPose(dismountPose).move(location));
|
| 54 | }
|
| 55 |
|
| 56 | public static VoxelShape nonClimbableShape(final BlockGetter level, final BlockPos pos) {
|
| 57 | BlockState blockState = level.getBlockState(pos);
|
| 58 | return !blockState.is(BlockTags.CLIMBABLE) && (!(blockState.getBlock() instanceof TrapDoorBlock) || !blockState.getValue(TrapDoorBlock.OPEN))
|
| 59 | ? blockState.getCollisionShape(level, pos)
|
| 60 | : Shapes.empty();
|
| 61 | }
|
| 62 |
|
| 63 | public static double findCeilingFrom(final BlockPos pos, final int blocks, final Function<BlockPos, VoxelShape> shapeGetter) {
|
| 64 | BlockPos.MutableBlockPos cursor = pos.mutable();
|
| 65 | int y = 0;
|
| 66 |
|
| 67 | while (y < blocks) {
|
| 68 | VoxelShape collisionShape = shapeGetter.apply(cursor);
|
| 69 | if (!collisionShape.isEmpty()) {
|
| 70 | return pos.getY() + y + collisionShape.min(Direction.Axis.Y);
|
| 71 | }
|
| 72 |
|
| 73 | y++;
|
| 74 | cursor.move(Direction.UP);
|
| 75 | }
|
| 76 |
|
| 77 | return Double.POSITIVE_INFINITY;
|
| 78 | }
|
| 79 |
|
| 80 | public static @Nullable Vec3 findSafeDismountLocation(
|
| 81 | final EntityType<?> type, final CollisionGetter level, final BlockPos blockPos, final boolean checkDangerous
|
| 82 | ) {
|
| 83 | if (checkDangerous && type.isBlockDangerous(level.getBlockState(blockPos))) {
|
| 84 | return null;
|
| 85 | }
|
| 86 |
|
| 87 | double floorHeight = level.getBlockFloorHeight(nonClimbableShape(level, blockPos), () -> nonClimbableShape(level, blockPos.below()));
|
| 88 | if (!isBlockFloorValid(floorHeight)) {
|
| 89 | return null;
|
| 90 | }
|
| 91 |
|
| 92 | if (checkDangerous && floorHeight <= 0.0 && type.isBlockDangerous(level.getBlockState(blockPos.below()))) {
|
| 93 | return null;
|
| 94 | }
|
| 95 |
|
| 96 | Vec3 position = Vec3.upFromBottomCenterOf(blockPos, floorHeight);
|
| 97 | AABB aabb = type.getDimensions().makeBoundingBox(position);
|
| 98 |
|
| 99 | for (VoxelShape shape : level.getBlockCollisions(null, aabb)) {
|
| 100 | if (!shape.isEmpty()) {
|
| 101 | return null;
|
| 102 | }
|
| 103 | }
|
| 104 |
|
| 105 | if (type != EntityTypes.PLAYER
|
| 106 | || !level.getBlockState(blockPos).is(BlockTags.INVALID_SPAWN_INSIDE) && !level.getBlockState(blockPos.above()).is(BlockTags.INVALID_SPAWN_INSIDE)) {
|
| 107 | return !level.getWorldBorder().isWithinBounds(aabb) ? null : position;
|
| 108 | } else {
|
| 109 | return null;
|
| 110 | }
|
| 111 | }
|
| 112 | }
|
| 113 |
|