Skip to content

Commit

Permalink
revert change: entity.collisions.movement: handle external changes to…
Browse files Browse the repository at this point in the history
… list of collision shapes correctly
  • Loading branch information
AbdElAziz333 committed Jan 9, 2023
1 parent 079a633 commit fe4ba0a
Showing 1 changed file with 18 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,31 @@
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Mixin(Entity.class)
public class EntityMixin {

@Redirect(
method = "collide(Lnet/minecraft/world/phys/Vec3;)Lnet/minecraft/world/phys/Vec3;",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/world/entity/Entity;collideBoundingBox(Lnet/minecraft/world/entity/Entity;Lnet/minecraft/world/phys/Vec3;Lnet/minecraft/world/phys/AABB;Lnet/minecraft/world/level/Level;Ljava/util/List;)Lnet/minecraft/world/phys/Vec3;"
),
require = 5
)
private static Vec3 adjustMovmentForCollisionsGetEntitiesLater(@Nullable Entity entity, Vec3 movement, AABB entityBoundingBox, Level world, List<VoxelShape> collisions) {
return canaryCollideMultiAxisMovement(entity, movement, entityBoundingBox, world, true, collisions);
}
private static final List<VoxelShape> GET_ENTITIES_LATER = Collections.unmodifiableList(new ArrayList<>());

/**
* @author 2No2Name
* @reason Replace with optimized implementation
*/
@Overwrite
public static Vec3 collideBoundingBox(@Nullable Entity entity, Vec3 movement, AABB entityBoundingBox, Level world, List<VoxelShape> collisions) {
return canaryCollideMultiAxisMovement(entity, movement, entityBoundingBox, world, false, collisions);
return canaryCollideMultiAxisMovement(entity, movement, entityBoundingBox, world, collisions == GET_ENTITIES_LATER);
}

private static Vec3 canaryCollideMultiAxisMovement(@Nullable Entity entity, Vec3 movement, AABB entityBoundingBox, Level world, boolean getEntityCollisions, List<VoxelShape> otherCollisions) {
private static Vec3 canaryCollideMultiAxisMovement(@Nullable Entity entity, Vec3 movement, AABB entityBoundingBox, Level world, boolean getEntityCollisions) {
//vanilla order: entities, worldborder, blocks. It is unknown whether changing this order changes the result regarding the confusing 1e-7 VoxelShape margin behavior. Not yet investigated
double velX = movement.x;
double velY = movement.y;
double velZ = movement.z;
boolean isVerticalOnly = velX == 0 && velZ == 0;
AABB movementSpace;
if (isVerticalOnly) {
//Reduced collision volume optimization for entities that are just standing around
if (velY < 0) {
movementSpace = new AABB(entityBoundingBox.minX, entityBoundingBox.minY + velY, entityBoundingBox.minZ, entityBoundingBox.maxX, entityBoundingBox.minY, entityBoundingBox.maxZ);
} else {
Expand All @@ -64,10 +54,7 @@ private static Vec3 canaryCollideMultiAxisMovement(@Nullable Entity entity, Vec3
if (velY != 0.0) {
velY = Shapes.collide(Direction.Axis.Y, entityBoundingBox, blockCollisions, velY);
if (velY != 0.0) {
if (!otherCollisions.isEmpty()) {
velY = Shapes.collide(Direction.Axis.Y, entityBoundingBox, otherCollisions, velY);
}
if (velY != 0.0 && getEntityCollisions) {
if (getEntityCollisions) {
entityWorldBorderCollisions = CanaryEntityCollisions.getEntityWorldBorderCollisions(world, entity, movementSpace, entity != null);
velY = Shapes.collide(Direction.Axis.Y, entityBoundingBox, entityWorldBorderCollisions, velY);
}
Expand All @@ -76,42 +63,28 @@ private static Vec3 canaryCollideMultiAxisMovement(@Nullable Entity entity, Vec3
}
}
}

boolean velXSmallerVelZ = Math.abs(velX) < Math.abs(velZ);

if (velXSmallerVelZ) {
velZ = Shapes.collide(Direction.Axis.Z, entityBoundingBox, blockCollisions, velZ);
if (velZ != 0.0) {

if (!otherCollisions.isEmpty()) {
velZ = Shapes.collide(Direction.Axis.Z, entityBoundingBox, otherCollisions, velZ);
if (entityWorldBorderCollisions == null && getEntityCollisions) {
entityWorldBorderCollisions = CanaryEntityCollisions.getEntityWorldBorderCollisions(world, entity, movementSpace, entity != null);
}

if (velZ != 0.0 && getEntityCollisions) {
if (entityWorldBorderCollisions == null) {
entityWorldBorderCollisions = CanaryEntityCollisions.getEntityWorldBorderCollisions(world, entity, movementSpace, entity != null);
}

if (getEntityCollisions) {
velZ = Shapes.collide(Direction.Axis.Z, entityBoundingBox, entityWorldBorderCollisions, velZ);
}

if (velZ != 0.0) {
entityBoundingBox = entityBoundingBox.move(0.0, 0.0, velZ);
}
}
}

if (velX != 0.0) {
velX = Shapes.collide(Direction.Axis.X, entityBoundingBox, blockCollisions, velX);
if (velX != 0.0) {
if (!otherCollisions.isEmpty()) {
velX = Shapes.collide(Direction.Axis.X, entityBoundingBox, otherCollisions, velX);
if (entityWorldBorderCollisions == null && getEntityCollisions) {
entityWorldBorderCollisions = CanaryEntityCollisions.getEntityWorldBorderCollisions(world, entity, movementSpace, entity != null);
}
if (velX != 0.0 && getEntityCollisions) {
if (entityWorldBorderCollisions == null) {
entityWorldBorderCollisions = CanaryEntityCollisions.getEntityWorldBorderCollisions(world, entity, movementSpace, entity != null);
}

if (getEntityCollisions) {
velX = Shapes.collide(Direction.Axis.X, entityBoundingBox, entityWorldBorderCollisions, velX);
}
if (velX != 0.0) {
Expand All @@ -121,17 +94,12 @@ private static Vec3 canaryCollideMultiAxisMovement(@Nullable Entity entity, Vec3
}
if (!velXSmallerVelZ && velZ != 0.0) {
velZ = Shapes.collide(Direction.Axis.Z, entityBoundingBox, blockCollisions, velZ);
if (velZ != 0.0) {
if (!otherCollisions.isEmpty()) {
velZ = Shapes.collide(Direction.Axis.Z, entityBoundingBox, otherCollisions, velZ);
if (velZ != 0.0 && getEntityCollisions) {
if (entityWorldBorderCollisions == null) {
entityWorldBorderCollisions = CanaryEntityCollisions.getEntityWorldBorderCollisions(world, entity, movementSpace, entity != null);
}
if (velZ != 0.0 && getEntityCollisions) {
if (entityWorldBorderCollisions == null) {
entityWorldBorderCollisions = CanaryEntityCollisions.getEntityWorldBorderCollisions(world, entity, movementSpace, entity != null);
}

velZ = Shapes.collide(Direction.Axis.Z, entityBoundingBox, entityWorldBorderCollisions, velZ);
}
velZ = Shapes.collide(Direction.Axis.Z, entityBoundingBox, entityWorldBorderCollisions, velZ);
}
}
return new Vec3(velX, velY, velZ);
Expand All @@ -145,6 +113,6 @@ private static Vec3 canaryCollideMultiAxisMovement(@Nullable Entity entity, Vec3
)
)
private List<VoxelShape> getEntitiesLater(Level world, Entity entity, AABB box) {
return List.of();
return GET_ENTITIES_LATER;
}
}
}

0 comments on commit fe4ba0a

Please sign in to comment.