@@ -69,6 +69,16 @@ public class LinearRegion implements Region {
6969 private final Path regionFile ;
7070 private final Vector2i regionPos ;
7171
72+ private boolean initialized = false ;
73+
74+ private byte version ;
75+ private long newestTimestamp ;
76+ private byte compressionLevel ;
77+ private short chunkCount ;
78+ private int dataLength ;
79+ private long dataHash ;
80+ private byte [] compressedData ;
81+
7282 public LinearRegion (MCAWorld world , Path regionFile ) throws IllegalArgumentException {
7383 this .world = world ;
7484 this .regionFile = regionFile ;
@@ -86,26 +96,14 @@ public LinearRegion(MCAWorld world, Vector2i regionPos) throws IllegalArgumentEx
8696 this .regionFile = world .getRegionFolder ().resolve (getRegionFileName (regionPos .getX (), regionPos .getY ()));
8797 }
8898
89- @ Override
90- public void iterateAllChunks (ChunkConsumer consumer ) throws IOException {
99+ private synchronized void init () throws IOException {
100+ if (initialized ) return ;
101+
91102 if (Files .notExists (regionFile )) return ;
92103
93104 long fileLength = Files .size (regionFile );
94105 if (fileLength == 0 ) return ;
95106
96- int chunkStartX = regionPos .getX () * 32 ;
97- int chunkStartZ = regionPos .getY () * 32 ;
98-
99- byte [] chunkDataBuffer = null ;
100- byte [] compressedData ;
101-
102- byte version ;
103- long newestTimestamp ;
104- byte compressionLevel ;
105- short chunkCount ;
106- int dataLength ;
107- long dataHash ;
108-
109107 try (
110108 InputStream in = Files .newInputStream (regionFile , StandardOpenOption .READ );
111109 BufferedInputStream bIn = new BufferedInputStream (in );
@@ -136,6 +134,18 @@ public void iterateAllChunks(ChunkConsumer consumer) throws IOException {
136134
137135 }
138136
137+ initialized = true ;
138+ }
139+
140+ @ Override
141+ public void iterateAllChunks (ChunkConsumer consumer ) throws IOException {
142+ if (!initialized ) init ();
143+
144+ int chunkStartX = regionPos .getX () * 32 ;
145+ int chunkStartZ = regionPos .getY () * 32 ;
146+
147+ byte [] chunkDataBuffer = null ;
148+
139149 try (
140150 InputStream in = new ZstdInputStream (new ByteArrayInputStream (compressedData ));
141151 BufferedInputStream bIn = new BufferedInputStream (in );
0 commit comments