Update IslandLevelCalculator.java (#314)

Fix for occasional errors likely due to the use of the remove() method within a lambda expression inside the thenAccept method. This lambda expression is executed asynchronously, which means that the iterator may not be in a consistent state when remove() is called.
This commit is contained in:
tastybento 2024-05-31 13:11:57 -07:00 committed by GitHub
parent 5dee0d2426
commit 0bb6eacaf7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -639,6 +639,7 @@ public class IslandLevelCalculator {
private void handleStackedBlocks() { private void handleStackedBlocks() {
// Deal with any stacked blocks // Deal with any stacked blocks
List<Location> toRemove = new ArrayList<>();
Iterator<Location> it = stackedBlocks.iterator(); Iterator<Location> it = stackedBlocks.iterator();
while (it.hasNext()) { while (it.hasNext()) {
Location v = it.next(); Location v = it.next();
@ -657,8 +658,17 @@ public class IslandLevelCalculator {
checkBlock(stackedBlock.getType(), belowSeaLevel); checkBlock(stackedBlock.getType(), belowSeaLevel);
} }
} }
it.remove(); synchronized (toRemove) {
toRemove.add(v);
}
}); });
} }
// Wait for all asynchronous tasks to complete before removing elements
// Remove the elements collected in toRemove
synchronized (toRemove) {
stackedBlocks.removeAll(toRemove);
} }
} }
}