| 1 | package net.minecraft.world.level;
|
| 2 |
|
| 3 | import com.google.common.collect.Iterables;
|
| 4 | import java.util.List;
|
| 5 | import java.util.Optional;
|
| 6 | import java.util.stream.StreamSupport;
|
| 7 | import net.minecraft.core.BlockPos;
|
| 8 | import net.minecraft.core.Direction;
|
| 9 | import net.minecraft.world.entity.Entity;
|
| 10 | import net.minecraft.world.level.block.state.BlockState;
|
| 11 | import net.minecraft.world.level.border.WorldBorder;
|
| 12 | import net.minecraft.world.phys.AABB;
|
| 13 | import net.minecraft.world.phys.BlockHitResult;
|
| 14 | import net.minecraft.world.phys.Vec3;
|
| 15 | import net.minecraft.world.phys.shapes.BooleanOp;
|
| 16 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 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 interface CollisionGetter extends BlockGetter {
|
| 22 | WorldBorder getWorldBorder();
|
| 23 |
|
| 24 | @Nullable BlockGetter getChunkForCollisions(int chunkX, int chunkZ);
|
| 25 |
|
| 26 | default boolean isUnobstructed(final @Nullable Entity source, final VoxelShape shape) {
|
| 27 | return true;
|
| 28 | }
|
| 29 |
|
| 30 | default boolean isUnobstructed(final BlockState state, final BlockPos pos, final CollisionContext context) {
|
| 31 | VoxelShape shape = state.getCollisionShape(this, pos, context);
|
| 32 | return shape.isEmpty() || this.isUnobstructed(null, shape.move(pos));
|
| 33 | }
|
| 34 |
|
| 35 | default boolean isUnobstructed(final Entity ignore) {
|
| 36 | return this.isUnobstructed(ignore, Shapes.create(ignore.getBoundingBox()));
|
| 37 | }
|
| 38 |
|
| 39 | default boolean noCollision(final AABB aabb) {
|
| 40 | return this.noCollision(null, aabb);
|
| 41 | }
|
| 42 |
|
| 43 | default boolean noCollision(final Entity source) {
|
| 44 | return this.noCollision(source, source.getBoundingBox());
|
| 45 | }
|
| 46 |
|
| 47 | default boolean noCollision(final @Nullable Entity entity, final AABB aabb) {
|
| 48 | return this.noCollision(entity, aabb, false);
|
| 49 | }
|
| 50 |
|
| 51 | default boolean noCollision(final @Nullable Entity entity, final AABB aabb, final boolean alwaysCollideWithFluids) {
|
| 52 | return this.noBlockCollision(entity, aabb, alwaysCollideWithFluids) && this.noEntityCollision(entity, aabb) && this.noBorderCollision(entity, aabb);
|
| 53 | }
|
| 54 |
|
| 55 | default boolean noBlockCollision(final @Nullable Entity entity, final AABB aabb) {
|
| 56 | return this.noBlockCollision(entity, aabb, false);
|
| 57 | }
|
| 58 |
|
| 59 | default boolean noBlockCollision(final @Nullable Entity entity, final AABB aabb, final boolean alwaysCollideWithFluids) {
|
| 60 | for (VoxelShape blockCollision : alwaysCollideWithFluids ? this.getBlockAndLiquidCollisions(entity, aabb) : this.getBlockCollisions(entity, aabb)) {
|
| 61 | if (!blockCollision.isEmpty()) {
|
| 62 | return false;
|
| 63 | }
|
| 64 | }
|
| 65 |
|
| 66 | return true;
|
| 67 | }
|
| 68 |
|
| 69 | default boolean noEntityCollision(final @Nullable Entity entity, final AABB aabb) {
|
| 70 | return this.getEntityCollisions(entity, aabb).isEmpty();
|
| 71 | }
|
| 72 |
|
| 73 | default boolean noBorderCollision(final @Nullable Entity entity, final AABB aabb) {
|
| 74 | if (entity == null) {
|
| 75 | return true;
|
| 76 | }
|
| 77 |
|
| 78 | VoxelShape borderShape = this.borderCollision(entity, aabb);
|
| 79 | return borderShape == null || !Shapes.joinIsNotEmpty(borderShape, Shapes.create(aabb), BooleanOp.AND);
|
| 80 | }
|
| 81 |
|
| 82 | List<VoxelShape> getEntityCollisions(final @Nullable Entity source, final AABB testArea);
|
| 83 |
|
| 84 | default Iterable<VoxelShape> getCollisions(final @Nullable Entity source, final AABB box) {
|
| 85 | List<VoxelShape> entityCollisions = this.getEntityCollisions(source, box);
|
| 86 | Iterable<VoxelShape> blockCollisions = this.getBlockCollisions(source, box);
|
| 87 | return entityCollisions.isEmpty() ? blockCollisions : Iterables.concat(entityCollisions, blockCollisions);
|
| 88 | }
|
| 89 |
|
| 90 | default Iterable<VoxelShape> getPreMoveCollisions(final @Nullable Entity source, final AABB box, final Vec3 oldPos) {
|
| 91 | List<VoxelShape> entityCollisions = this.getEntityCollisions(source, box);
|
| 92 | Iterable<VoxelShape> blockCollisions = this.getBlockCollisionsFromContext(CollisionContext.withPosition(source, oldPos.y), box);
|
| 93 | return entityCollisions.isEmpty() ? blockCollisions : Iterables.concat(entityCollisions, blockCollisions);
|
| 94 | }
|
| 95 |
|
| 96 | default Iterable<VoxelShape> getBlockCollisions(final @Nullable Entity source, final AABB box) {
|
| 97 | return this.getBlockCollisionsFromContext(source == null ? CollisionContext.empty() : CollisionContext.of(source), box);
|
| 98 | }
|
| 99 |
|
| 100 | default Iterable<VoxelShape> getBlockAndLiquidCollisions(final @Nullable Entity source, final AABB box) {
|
| 101 | return this.getBlockCollisionsFromContext(source == null ? CollisionContext.emptyWithFluidCollisions() : CollisionContext.of(source, true), box);
|
| 102 | }
|
| 103 |
|
| 104 | default Iterable<VoxelShape> getBlockCollisionsFromContext(final CollisionContext source, final AABB box) {
|
| 105 | return () -> new BlockCollisions<>(this, source, box, false, (p, shape) -> shape);
|
| 106 | }
|
| 107 |
|
| 108 | private @Nullable VoxelShape borderCollision(final Entity source, final AABB box) {
|
| 109 | WorldBorder worldBorder = this.getWorldBorder();
|
| 110 | return worldBorder.isInsideCloseToBorder(source, box) ? worldBorder.getCollisionShape() : null;
|
| 111 | }
|
| 112 |
|
| 113 | default BlockHitResult clipIncludingBorder(final ClipContext c) {
|
| 114 | BlockHitResult hitResult = this.clip(c);
|
| 115 | WorldBorder worldBorder = this.getWorldBorder();
|
| 116 | if (worldBorder.isWithinBounds(c.getFrom()) && !worldBorder.isWithinBounds(hitResult.getLocation())) {
|
| 117 | Vec3 delta = hitResult.getLocation().subtract(c.getFrom());
|
| 118 | Direction deltaDirection = Direction.getApproximateNearest(delta.x, delta.y, delta.z);
|
| 119 | Vec3 hit = worldBorder.clampVec3ToBound(hitResult.getLocation());
|
| 120 | return new BlockHitResult(hit, deltaDirection, BlockPos.containing(hit), false, true);
|
| 121 | } else {
|
| 122 | return hitResult;
|
| 123 | }
|
| 124 | }
|
| 125 |
|
| 126 | default boolean collidesWithSuffocatingBlock(final @Nullable Entity source, final AABB box) {
|
| 127 | BlockCollisions<VoxelShape> blockCollisions = new BlockCollisions<>(this, source, box, true, (p, shape) -> shape);
|
| 128 |
|
| 129 | while (blockCollisions.hasNext()) {
|
| 130 | if (!blockCollisions.next().isEmpty()) {
|
| 131 | return true;
|
| 132 | }
|
| 133 | }
|
| 134 |
|
| 135 | return false;
|
| 136 | }
|
| 137 |
|
| 138 | default Optional<BlockPos> findSupportingBlock(final Entity source, final AABB box) {
|
| 139 | BlockPos mainSupport = null;
|
| 140 | double mainSupportDistance = Double.MAX_VALUE;
|
| 141 | BlockCollisions<BlockPos> blockCollisions = new BlockCollisions<>(this, source, box, false, (posx, shape) -> posx);
|
| 142 |
|
| 143 | while (blockCollisions.hasNext()) {
|
| 144 | BlockPos pos = blockCollisions.next();
|
| 145 | double distance = pos.distToCenterSqr(source.position());
|
| 146 | if (distance < mainSupportDistance || distance == mainSupportDistance && (mainSupport == null || mainSupport.compareTo(pos) < 0)) {
|
| 147 | mainSupport = pos.immutable();
|
| 148 | mainSupportDistance = distance;
|
| 149 | }
|
| 150 | }
|
| 151 |
|
| 152 | return Optional.ofNullable(mainSupport);
|
| 153 | }
|
| 154 |
|
| 155 | default Optional<Vec3> findFreePosition(
|
| 156 | final @Nullable Entity source, final VoxelShape allowedCenters, final Vec3 preferredCenter, final double sizeX, final double sizeY, final double sizeZ
|
| 157 | ) {
|
| 158 | if (allowedCenters.isEmpty()) {
|
| 159 | return Optional.empty();
|
| 160 | }
|
| 161 |
|
| 162 | AABB searchArea = allowedCenters.bounds().inflate(sizeX, sizeY, sizeZ);
|
| 163 | VoxelShape expandedCollisions = StreamSupport.stream(this.getBlockCollisions(source, searchArea).spliterator(), false)
|
| 164 | .filter(shape -> this.getWorldBorder() == null || this.getWorldBorder().isWithinBounds(shape.bounds()))
|
| 165 | .flatMap(shape -> shape.toAabbs().stream())
|
| 166 | .map(aabb -> aabb.inflate(sizeX / 2.0, sizeY / 2.0, sizeZ / 2.0))
|
| 167 | .map(Shapes::create)
|
| 168 | .reduce(Shapes.empty(), Shapes::or);
|
| 169 | VoxelShape freeSpots = Shapes.join(allowedCenters, expandedCollisions, BooleanOp.ONLY_FIRST);
|
| 170 | return freeSpots.closestPointTo(preferredCenter);
|
| 171 | }
|
| 172 | }
|
| 173 |
|