12
12
import java .io .IOException ;
13
13
import java .io .RandomAccessFile ;
14
14
import java .util .Arrays ;
15
+ import java .util .Iterator ;
16
+ import java .util .Map ;
17
+ import java .util .TreeMap ;
18
+
15
19
import static net .querz .mca .LoadFlags .*;
16
20
17
- public class Chunk {
21
+ public class Chunk implements Iterable < Section > {
18
22
19
23
public static final int DEFAULT_DATA_VERSION = 2567 ;
20
24
@@ -31,7 +35,7 @@ public class Chunk {
31
35
private int [] biomes ;
32
36
private CompoundTag heightMaps ;
33
37
private CompoundTag carvingMasks ;
34
- private Section [] sections = new Section [ 16 ]; //always initialized with size = 16 for fast access
38
+ private Map < Integer , Section > sections = new TreeMap <>();
35
39
private ListTag <CompoundTag > entities ;
36
40
private ListTag <CompoundTag > tileEntities ;
37
41
private ListTag <CompoundTag > tileTicks ;
@@ -112,15 +116,9 @@ private void initReferences(long loadFlags) {
112
116
}
113
117
if ((loadFlags & (BLOCK_LIGHTS |BLOCK_STATES |SKY_LIGHT )) != 0 && level .containsKey ("Sections" )) {
114
118
for (CompoundTag section : level .getListTag ("Sections" ).asCompoundTagList ()) {
115
- int sectionIndex = section .getByte ("Y" );
116
- if (sectionIndex > 15 || sectionIndex < 0 ) {
117
- continue ;
118
- }
119
+ int sectionIndex = section .getNumber ("Y" ).byteValue ();
119
120
Section newSection = new Section (section , dataVersion , loadFlags );
120
- if (newSection .isEmpty ()) {
121
- continue ;
122
- }
123
- sections [sectionIndex ] = newSection ;
121
+ sections .put (sectionIndex , newSection );
124
122
}
125
123
}
126
124
@@ -285,7 +283,7 @@ int getBiomeIndex(int biomeX, int biomeY, int biomeZ) {
285
283
}
286
284
287
285
public CompoundTag getBlockStateAt (int blockX , int blockY , int blockZ ) {
288
- Section section = sections [ MCAUtil .blockToChunk (blockY )] ;
286
+ Section section = sections . get ( MCAUtil .blockToChunk (blockY )) ;
289
287
if (section == null ) {
290
288
return null ;
291
289
}
@@ -306,9 +304,9 @@ public CompoundTag getBlockStateAt(int blockX, int blockY, int blockZ) {
306
304
public void setBlockStateAt (int blockX , int blockY , int blockZ , CompoundTag state , boolean cleanup ) {
307
305
checkRaw ();
308
306
int sectionIndex = MCAUtil .blockToChunk (blockY );
309
- Section section = sections [ sectionIndex ] ;
307
+ Section section = sections . get ( sectionIndex ) ;
310
308
if (section == null ) {
311
- section = sections [ sectionIndex ] = Section .newSection ();
309
+ sections . put ( sectionIndex , section = Section .newSection () );
312
310
}
313
311
section .setBlockStateAt (blockX , blockY , blockZ , state , cleanup );
314
312
}
@@ -328,7 +326,7 @@ public int getDataVersion() {
328
326
public void setDataVersion (int dataVersion ) {
329
327
checkRaw ();
330
328
this .dataVersion = dataVersion ;
331
- for (Section section : sections ) {
329
+ for (Section section : sections . values () ) {
332
330
if (section != null ) {
333
331
section .dataVersion = dataVersion ;
334
332
}
@@ -373,7 +371,7 @@ public void setStatus(String status) {
373
371
* @return The Section.
374
372
*/
375
373
public Section getSection (int sectionY ) {
376
- return sections [ sectionY ] ;
374
+ return sections . get ( sectionY ) ;
377
375
}
378
376
379
377
/**
@@ -383,7 +381,7 @@ public Section getSection(int sectionY) {
383
381
*/
384
382
public void setSection (int sectionY , Section section ) {
385
383
checkRaw ();
386
- sections [ sectionY ] = section ;
384
+ sections . put ( sectionY , section ) ;
387
385
}
388
386
389
387
/**
@@ -623,7 +621,7 @@ int getBlockIndex(int blockX, int blockZ) {
623
621
624
622
public void cleanupPalettesAndBlockStates () {
625
623
checkRaw ();
626
- for (Section section : sections ) {
624
+ for (Section section : sections . values () ) {
627
625
if (section != null ) {
628
626
section .cleanupPaletteAndBlockStates ();
629
627
}
@@ -712,12 +710,17 @@ public CompoundTag updateHandle(int xPos, int zPos) {
712
710
level .put ("Structures" , structures );
713
711
}
714
712
ListTag <CompoundTag > sections = new ListTag <>(CompoundTag .class );
715
- for (int i = 0 ; i < this .sections .length ; i ++ ) {
716
- if (this . sections [ i ] != null ) {
717
- sections .add (this . sections [ i ]. updateHandle (i ));
713
+ for (Section section : this .sections .values () ) {
714
+ if (section != null ) {
715
+ sections .add (section . updateHandle ());
718
716
}
719
717
}
720
718
level .put ("Sections" , sections );
721
719
return data ;
722
720
}
721
+
722
+ @ Override
723
+ public Iterator <Section > iterator () {
724
+ return sections .values ().iterator ();
725
+ }
723
726
}
0 commit comments