@@ -134,41 +134,50 @@ public ChunkRegion() {
134134 }
135135
136136 @ Contract (pure = true )
137- public @ Nullable Chunk getChunk (int localIndex ) {
137+ public Chunk getChunk (int localIndex ) {
138138 Objects .checkIndex (localIndex , ChunkUtils .CHUNKS_PRE_REGION );
139-
140- return chunks [localIndex ];
139+ Chunk chunk = chunks [localIndex ];
140+ if (chunk == null ) {
141+ chunks [localIndex ] = chunk = new Chunk (this , localIndex );
142+ }
143+ return chunk ;
141144 }
142145
143146 @ Contract (pure = true )
144- public @ Nullable Chunk getChunk (int x , int z ) {
147+ public Chunk getChunk (int x , int z ) {
145148 Objects .checkIndex (x , ChunkUtils .CHUNKS_PER_REGION_SIDE );
146149 Objects .checkIndex (z , ChunkUtils .CHUNKS_PER_REGION_SIDE );
147150
148- return chunks [ ChunkUtils .toLocalIndex (x , z )] ;
151+ return getChunk ( ChunkUtils .toLocalIndex (x , z )) ;
149152 }
150153
151154 @ Contract (mutates = "this,param2" )
152- public void setChunk (int localIndex , @ Nullable Chunk chunk ) {
155+ public void setChunk (int localIndex , Chunk chunk ) {
153156 Objects .checkIndex (localIndex , ChunkUtils .CHUNKS_PRE_REGION );
157+ Objects .requireNonNull (chunk );
154158
155159 Chunk old = chunks [localIndex ];
156160 if (old != null ) {
157161 old .setRegion (null , -1 );
158162 }
159163
160- if (chunk != null ) {
161- if (chunk .getParent () != null ) {
162- // The chunk is already in another region, so we need to remove it from its old region first.
163- ChunkRegion oldRegion = chunk .getParent ();
164- oldRegion .remove (chunk );
165- }
166-
167- chunk .setRegion (this , localIndex );
164+ if (chunk .getParent () != null ) {
165+ // The chunk is already in another region, so we need to remove it from its old region first.
166+ ChunkRegion oldRegion = chunk .getParent ();
167+ oldRegion .remove (chunk );
168168 }
169+
170+ chunk .setRegion (this , localIndex );
169171 chunks [localIndex ] = chunk ;
170172 }
171173
174+ @ Contract (mutates = "this,param3" )
175+ public void setChunk (int x , int z , Chunk chunk ) {
176+ Objects .checkIndex (x , ChunkUtils .CHUNKS_PER_REGION_SIDE );
177+ Objects .checkIndex (z , ChunkUtils .CHUNKS_PER_REGION_SIDE );
178+ setChunk (ChunkUtils .toLocalIndex (x , z ), chunk );
179+ }
180+
172181 @ Override
173182 @ Contract (mutates = "this,param1" )
174183 public void remove (Chunk chunk ) throws IllegalArgumentException {
@@ -182,9 +191,8 @@ public void remove(Chunk chunk) throws IllegalArgumentException {
182191 if (old != chunk ) {
183192 throw new AssertionError ("Expected " + chunk + ", but got " + old );
184193 }
185-
186- chunks [localIndex ] = null ;
187194 chunk .setRegion (null , -1 );
195+ chunks [localIndex ] = null ;
188196 }
189197
190198 @ Override
0 commit comments