@@ -142,26 +142,27 @@ private boolean nextSection() {
142
142
this .chunkYIndex ++;
143
143
this .cachedChunkSection = this .cachedChunk .getSections ()[this .chunkYIndex ];
144
144
} else {
145
- this .chunkYIndex = Mth .clamp (
146
- Pos .SectionYIndex .fromBlockCoord (this .world , expandMin (this .minY )),
147
- Pos .SectionYIndex .getMinYSectionIndex (this .world ),
148
- Pos .SectionYIndex .getMaxYSectionIndexInclusive (this .world )
149
- );
150
-
151
145
if (this .chunkX < Pos .ChunkCoord .fromBlockCoord (expandMax (this .maxX ))) {
152
146
//first initialization takes this branch
153
147
this .chunkX ++;
154
148
} else {
155
- this .chunkX = Pos .ChunkCoord .fromBlockCoord (expandMin (this .minX ));
156
-
157
149
if (this .chunkZ < Pos .ChunkCoord .fromBlockCoord (expandMax (this .maxZ ))) {
150
+ this .chunkX = Pos .ChunkCoord .fromBlockCoord (expandMin (this .minX ));
158
151
this .chunkZ ++;
159
152
} else {
153
+ //Important: No field assignment / mutation happens in the code path to this, so
154
+ // consecutive nextSection calls keep returning false, instead of working on invalid data
155
+ // Otherwise this additional chunk sections to be iterated wrongly: https://github.com/CaffeineMC/lithium/issues/628
160
156
return false ; //no more sections to iterate
161
157
}
162
158
}
163
159
this .cachedChunk = this .world .getChunk (this .chunkX , this .chunkZ , ChunkStatus .FULL , false );
164
160
if (this .cachedChunk != null ) {
161
+ this .chunkYIndex = Mth .clamp (
162
+ Pos .SectionYIndex .fromBlockCoord (this .world , expandMin (this .minY )),
163
+ Pos .SectionYIndex .getMinYSectionIndex (this .world ),
164
+ Pos .SectionYIndex .getMaxYSectionIndexInclusive (this .world )
165
+ );
165
166
this .cachedChunkSection = this .cachedChunk .getSections ()[this .chunkYIndex ];
166
167
}
167
168
}
0 commit comments