Skip to content

Commit 62ce024

Browse files
committed
Better tentative fix for #24
1 parent de7b0cb commit 62ce024

File tree

3 files changed

+45
-48
lines changed

3 files changed

+45
-48
lines changed

BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/RenderManager.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ public void addRenderTask(RenderTask task) {
6666
}
6767

6868
public RenderTicket createTicket(MapType mapType, Vector2i tile) {
69-
RenderTicket ticket = new RenderTicket(mapType, tile);
69+
return createTicket(new RenderTicket(mapType, tile));
70+
}
71+
72+
private RenderTicket createTicket(RenderTicket ticket) {
7073
synchronized (renderTickets) {
7174
if (renderTicketMap.putIfAbsent(ticket, ticket) == null) {
7275
renderTickets.add(ticket);
@@ -138,7 +141,12 @@ private void renderThread() {
138141
try {
139142
ticket.render();
140143
} catch (IOException e) {
141-
Logger.global.logError("Failed to render tile " + ticket.getTile() + " of map '" + ticket.getMapType().getId() + "'!", e);
144+
if (ticket.getRenderAttempts() < 3) {
145+
Logger.global.logDebug("Failed to render tile " + ticket.getTile() + " of map '" + ticket.getMapType().getId() + "', rescheduling for " + (ticket.getRenderAttempts() + 1) + ". attempt..");
146+
createTicket(ticket); //this might be a temporary issue, so we reschedule ticket for another attempt
147+
} else {
148+
Logger.global.logError("Failed to render tile " + ticket.getTile() + " of map '" + ticket.getMapType().getId() + "'!", e);
149+
}
142150
}
143151
} else {
144152
try {

BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/RenderTicket.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,20 @@ public class RenderTicket {
1010
private final MapType map;
1111
private final Vector2i tile;
1212

13+
private int renderAttempts;
1314
private boolean finished;
1415

1516
public RenderTicket(MapType map, Vector2i tile) {
1617
this.map = map;
1718
this.tile = tile;
19+
20+
this.renderAttempts = 0;
1821
this.finished = false;
1922
}
2023

2124
public synchronized void render() throws IOException {
25+
renderAttempts++;
26+
2227
if (!finished) {
2328
map.renderTile(tile);
2429

@@ -34,6 +39,10 @@ public Vector2i getTile() {
3439
return tile;
3540
}
3641

42+
public int getRenderAttempts() {
43+
return renderAttempts;
44+
}
45+
3746
public boolean isFinished() {
3847
return finished;
3948
}

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

Lines changed: 26 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -217,58 +217,38 @@ public Chunk getChunk(Vector2i chunkPos) throws IOException {
217217
private Chunk loadChunk(Vector2i chunkPos) throws IOException {
218218
Vector2i regionPos = chunkToRegion(chunkPos);
219219
Path regionPath = getMCAFilePath(regionPos);
220+
221+
try (RandomAccessFile raf = new RandomAccessFile(regionPath.toFile(), "r")) {
220222

221-
Throwable exception = null;
222-
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-
}
230-
}
223+
int xzChunk = Math.floorMod(chunkPos.getY(), 32) * 32 + Math.floorMod(chunkPos.getX(), 32);
231224

232-
try (RandomAccessFile raf = new RandomAccessFile(regionPath.toFile(), "r")) {
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;
233230

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) {
231+
int size = raf.readByte() * 4096;
232+
if (size == 0) {
264233
return Chunk.empty(this, chunkPos);
265-
} catch (Throwable ex) {
266-
exception = ex;
234+
}
235+
236+
raf.seek(offset + 4); // +4 skip chunk size
237+
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()));
267250
}
268251
}
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);
272252
}
273253

274254
@Override

0 commit comments

Comments
 (0)