Skip to content

Commit de7b0cb

Browse files
committed
Tentative fix for #24
1 parent 85f5d89 commit de7b0cb

File tree

1 file changed

+45
-28
lines changed
  • BlueMapCore/src/main/java/de/bluecolored/bluemap/core/mca

1 file changed

+45
-28
lines changed

BlueMapCore/src/main/java/de/bluecolored/bluemap/core/mca/MCAWorld.java

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -218,40 +218,57 @@ private Chunk loadChunk(Vector2i chunkPos) throws IOException {
218218
Vector2i regionPos = chunkToRegion(chunkPos);
219219
Path regionPath = getMCAFilePath(regionPos);
220220

221-
try (RandomAccessFile raf = new RandomAccessFile(regionPath.toFile(), "r")) {
221+
Throwable exception = null;
222222

223-
int xzChunk = Math.floorMod(chunkPos.getY(), 32) * 32 + Math.floorMod(chunkPos.getX(), 32);
224-
225-
raf.seek(xzChunk * 4);
226-
int offset = raf.read() << 16;
227-
offset |= (raf.read() & 0xFF) << 8;
228-
offset |= raf.read() & 0xFF;
229-
offset *= 4096;
230-
231-
int size = raf.readByte() * 4096;
232-
if (size == 0) {
233-
return Chunk.empty(this, chunkPos);
223+
for (int tries = 1; tries <= 5; tries++) {
224+
if (tries > 1) {
225+
try {
226+
Thread.sleep(200);
227+
} catch (InterruptedException interrupt) {
228+
throw new IOException("Interrupted while waiting for the " + tries + "th try to load a chunk..");
229+
}
234230
}
235231

236-
raf.seek(offset + 4); // +4 skip chunk size
232+
try (RandomAccessFile raf = new RandomAccessFile(regionPath.toFile(), "r")) {
237233

238-
byte compressionTypeByte = raf.readByte();
239-
CompressionType compressionType = CompressionType.getFromID(compressionTypeByte);
240-
if (compressionType == null) {
241-
throw new IOException("invalid compression type " + compressionTypeByte);
242-
}
243-
244-
DataInputStream dis = new DataInputStream(new BufferedInputStream(compressionType.decompress(new FileInputStream(raf.getFD()))));
245-
Tag<?> tag = Tag.deserialize(dis, Tag.DEFAULT_MAX_DEPTH);
246-
if (tag instanceof CompoundTag) {
247-
return Chunk.create(this, (CompoundTag) tag);
248-
} else {
249-
throw new IOException("invalid data tag: " + (tag == null ? "null" : tag.getClass().getName()));
234+
int xzChunk = Math.floorMod(chunkPos.getY(), 32) * 32 + Math.floorMod(chunkPos.getX(), 32);
235+
236+
raf.seek(xzChunk * 4);
237+
int offset = raf.read() << 16;
238+
offset |= (raf.read() & 0xFF) << 8;
239+
offset |= raf.read() & 0xFF;
240+
offset *= 4096;
241+
242+
int size = raf.readByte() * 4096;
243+
if (size == 0) {
244+
return Chunk.empty(this, chunkPos);
245+
}
246+
247+
raf.seek(offset + 4); // +4 skip chunk size
248+
249+
byte compressionTypeByte = raf.readByte();
250+
CompressionType compressionType = CompressionType.getFromID(compressionTypeByte);
251+
if (compressionType == null) {
252+
throw new IOException("invalid compression type " + compressionTypeByte);
253+
}
254+
255+
DataInputStream dis = new DataInputStream(new BufferedInputStream(compressionType.decompress(new FileInputStream(raf.getFD()))));
256+
Tag<?> tag = Tag.deserialize(dis, Tag.DEFAULT_MAX_DEPTH);
257+
if (tag instanceof CompoundTag) {
258+
return Chunk.create(this, (CompoundTag) tag);
259+
} else {
260+
throw new IOException("invalid data tag: " + (tag == null ? "null" : tag.getClass().getName()));
261+
}
262+
263+
} catch (FileNotFoundException ex) {
264+
return Chunk.empty(this, chunkPos);
265+
} catch (Throwable ex) {
266+
exception = ex;
250267
}
251-
252-
} catch (FileNotFoundException ex) {
253-
return Chunk.empty(this, chunkPos);
254268
}
269+
270+
if (exception == null) throw new IOException("Failed to load chunk after multiple attempts!");
271+
throw new IOException("Failed to load chunk after multiple attempts!", exception);
255272
}
256273

257274
@Override

0 commit comments

Comments
 (0)