@@ -16,7 +16,7 @@ public final class StructurePieceSampler
1616 public static List <StructurePiece > getStructurePieces (StructureStart structureStart ) {
1717 List <StructurePiece > structurePieces = structureStart .getPieces ();
1818
19- if (Structurify .getConfig ().getDebugData ().getSamplingMode () == DebugData .SamplingMode .MINIMAL ) {
19+ if (Structurify .getConfig ().getDebugData ().isEnabled () && Structurify . getConfig (). getDebugData (). getSamplingMode () == DebugData .SamplingMode .MINIMAL ) {
2020 return structurePieces ;
2121 }
2222
@@ -32,13 +32,13 @@ public static int[][] getStructurePieceSamples(List<StructurePiece> structurePie
3232
3333 int [][] pieceSamples = getStructurePieceCorners (structurePieces );
3434
35- if (Structurify .getConfig ().getDebugData ().getSamplingMode () == DebugData .SamplingMode .MINIMAL ) {
35+ if (Structurify .getConfig ().getDebugData ().isEnabled () && Structurify . getConfig (). getDebugData (). getSamplingMode () == DebugData .SamplingMode .MINIMAL ) {
3636 return pieceSamples ;
3737 }
3838
3939 pieceSamples = mergeTouchingPositions (pieceSamples );
4040
41- if (Structurify .getConfig ().getDebugData ().getSamplingMode () == DebugData .SamplingMode .MERGED_SAMPLES ) {
41+ if (Structurify .getConfig ().getDebugData ().isEnabled () && Structurify . getConfig (). getDebugData (). getSamplingMode () == DebugData .SamplingMode .MERGED_SAMPLES ) {
4242 return pieceSamples ;
4343 }
4444
@@ -105,18 +105,22 @@ private static int[][] getStructurePieceCorners(List<StructurePiece> pieces) {
105105 private static int [][] mergeTouchingPositions (int [][] inputPositions ) {
106106 Set <Long > uniqueKeys = new HashSet <>();
107107 List <int []> uniquePositions = new ArrayList <>();
108+
108109 for (int [] position : inputPositions ) {
109- long key = ChunkPosUtil .getChunkPosAsLong ( ChunkPosUtil .createChunkPos (position [0 ], position [1 ]));
110+ long key = (((long ) position [0 ]) << 32 ) ^ (position [1 ] & 0xFFFFFFFFL );
111+
110112 if (uniqueKeys .add (key )) {
111- uniquePositions .add (position );
113+ uniquePositions .add (new int [] { position [ 0 ], position [ 1 ] } );
112114 }
113115 }
114116
115117 List <int []> mergedPositions = new ArrayList <>();
116118 boolean [] visited = new boolean [uniquePositions .size ()];
117119
118120 for (int i = 0 ; i < uniquePositions .size (); i ++) {
119- if (visited [i ]) continue ;
121+ if (visited [i ]) {
122+ continue ;
123+ }
120124
121125 List <int []> group = new ArrayList <>();
122126 Queue <Integer > queue = new ArrayDeque <>();
@@ -129,39 +133,52 @@ private static int[][] mergeTouchingPositions(int[][] inputPositions) {
129133 group .add (current );
130134
131135 for (int j = 0 ; j < uniquePositions .size (); j ++) {
132- if (visited [j ]) continue ;
136+ if (visited [j ]) {
137+ continue ;
138+ }
139+
133140 int [] candidate = uniquePositions .get (j );
134141
135- if (Math .abs (candidate [0 ] - current [0 ]) <= 1 &&
136- Math .abs (candidate [1 ] - current [1 ]) <= 1 ) {
142+ if (Math .abs (candidate [0 ] - current [0 ]) <= 1 && Math .abs (candidate [1 ] - current [1 ]) <= 1 ) {
137143 visited [j ] = true ;
138144 queue .add (j );
139145 }
140146 }
141147 }
142148
143- int [] best = group .get (0 );
144- double bestDistance = Double .MAX_VALUE ;
149+ int minX = Integer .MAX_VALUE ;
150+ int minZ = Integer .MAX_VALUE ;
151+ int maxX = Integer .MIN_VALUE ;
152+ int maxZ = Integer .MIN_VALUE ;
153+
154+ for (int [] position : group ) {
155+ minX = Math .min (minX , position [0 ]);
156+ minZ = Math .min (minZ , position [1 ]);
157+ maxX = Math .max (maxX , position [0 ]);
158+ maxZ = Math .max (maxZ , position [1 ]);
159+ }
145160
146- double avgX = group .stream ().mapToInt (p -> p [0 ]).average ().orElse (0 );
147- double avgZ = group .stream ().mapToInt (p -> p [1 ]).average ().orElse (0 );
161+ int centerX = Math .floorDiv (minX + maxX , 2 );
162+ int centerZ = Math .floorDiv (minZ + maxZ , 2 );
163+
164+ int [] best = group .get (0 );
165+ long bestDistance = Long .MAX_VALUE ;
148166
149- for (int [] pos : group ) {
150- double dx = pos [0 ] - avgX ;
151- double dz = pos [1 ] - avgZ ;
152- double dist = dx * dx + dz * dz ;
167+ for (int [] position : group ) {
168+ long dx = position [0 ] - centerX ;
169+ long dz = position [1 ] - centerZ ;
170+ long distance = dx * dx + dz * dz ;
153171
154- if (dist < bestDistance ||
155- (dist == bestDistance && (pos [0 ] < best [0 ] || (pos [0 ] == best [0 ] && pos [1 ] < best [1 ])))) {
156- best = pos ;
157- bestDistance = dist ;
172+ if (distance < bestDistance || distance == bestDistance && (position [0 ] < best [0 ] || position [0 ] == best [0 ] && position [1 ] < best [1 ])) {
173+ best = position ;
174+ bestDistance = distance ;
158175 }
159176 }
160177
161- mergedPositions .add (best );
178+ mergedPositions .add (new int [] { best [ 0 ], best [ 1 ] } );
162179 }
163180
164- return mergedPositions .toArray (new int [mergedPositions . size () ][]);
181+ return mergedPositions .toArray (new int [0 ][]);
165182 }
166183
167184 private static int [][] orderStructureCornersByDistance (int [][] samples , BlockPos structureCenter ) {
0 commit comments