55
66public class Distances {
77
8- public static double getMinChunkToBlockDistanceL2Sq (BlockPos origin , int chunkX , int chunkZ ) {
8+ public static long getMinChunkToBlockDistanceL2Sq (BlockPos origin , int chunkX , int chunkZ ) {
99 int chunkMinX = SectionPos .sectionToBlockCoord (chunkX );
1010 int chunkMinZ = SectionPos .sectionToBlockCoord (chunkZ );
1111
@@ -18,7 +18,7 @@ public static double getMinChunkToBlockDistanceL2Sq(BlockPos origin, int chunkX,
1818 zDistance = Math .max (0 , zDistance - 15 );
1919 }
2020
21- return xDistance * xDistance + zDistance * zDistance ;
21+ return ( long ) xDistance * ( long ) xDistance + ( long ) zDistance * ( long ) zDistance ;
2222 }
2323
2424 public static boolean isWithinSquareRadius (BlockPos origin , int radius , BlockPos pos ) {
@@ -35,12 +35,27 @@ public static int getClosestBlockCoordInSection(int blockCoord, int sectionCoord
3535 return Math .min (Math .max (blockCoord , minBlockInSection ), minBlockInSection + 15 );
3636 }
3737
38- public static double getMinSectionDistanceSq (BlockPos origin , int chunkX , int chunkY , int chunkZ ) {
39- final int originX = origin .getX (), originY = origin .getY (), originZ = origin .getZ ();
40- final int distX = getClosestBlockCoordInSection (originX , chunkX ) - originX ;
41- final int distY = getClosestBlockCoordInSection (originY , chunkY ) - originY ;
42- final int distZ = getClosestBlockCoordInSection (originZ , chunkZ ) - originZ ;
38+ public static long getMinSectionDistanceSq (BlockPos origin , int chunkX , int chunkY , int chunkZ ) {
39+ int originX = origin .getX (), originY = origin .getY (), originZ = origin .getZ ();
40+ long distX = getClosestBlockCoordInSection (originX , chunkX ) - originX ;
41+ long distY = getClosestBlockCoordInSection (originY , chunkY ) - originY ;
42+ long distZ = getClosestBlockCoordInSection (originZ , chunkZ ) - originZ ;
4343
4444 return distX * distX + distY * distY + distZ * distZ ;
4545 }
46+
47+ public static long distanceSq (BlockPos a , BlockPos b ) {
48+ long dx = a .getX () - b .getX ();
49+ long dy = a .getY () - b .getY ();
50+ long dz = a .getZ () - b .getZ ();
51+ return dx * dx + dy * dy + dz * dz ;
52+ }
53+
54+ public static int distanceSqInt (BlockPos a , BlockPos b ) {
55+ int dx = a .getX () - b .getX ();
56+ int dy = a .getY () - b .getY ();
57+ int dz = a .getZ () - b .getZ ();
58+ // Check overflows to avoid silent incorrect results
59+ return Math .addExact (Math .addExact (Math .multiplyExact (dx , dx ), Math .multiplyExact (dy , dy )), Math .multiplyExact (dz , dz ));
60+ }
4661}
0 commit comments