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

@ -638,27 +638,37 @@ public class IslandLevelCalculator {
} }
private void handleStackedBlocks() { private void handleStackedBlocks() {
// Deal with any stacked blocks // Deal with any stacked blocks
Iterator<Location> it = stackedBlocks.iterator(); List<Location> toRemove = new ArrayList<>();
while (it.hasNext()) { Iterator<Location> it = stackedBlocks.iterator();
Location v = it.next(); while (it.hasNext()) {
Util.getChunkAtAsync(v).thenAccept(c -> { Location v = it.next();
Block stackedBlock = v.getBlock(); Util.getChunkAtAsync(v).thenAccept(c -> {
boolean belowSeaLevel = seaHeight > 0 && v.getBlockY() <= seaHeight; Block stackedBlock = v.getBlock();
if (WildStackerAPI.getWildStacker().getSystemManager().isStackedBarrel(stackedBlock)) { boolean belowSeaLevel = seaHeight > 0 && v.getBlockY() <= seaHeight;
StackedBarrel barrel = WildStackerAPI.getStackedBarrel(stackedBlock); if (WildStackerAPI.getWildStacker().getSystemManager().isStackedBarrel(stackedBlock)) {
int barrelAmt = WildStackerAPI.getBarrelAmount(stackedBlock); StackedBarrel barrel = WildStackerAPI.getStackedBarrel(stackedBlock);
for (int _x = 0; _x < barrelAmt; _x++) { int barrelAmt = WildStackerAPI.getBarrelAmount(stackedBlock);
checkBlock(barrel.getType(), belowSeaLevel); for (int _x = 0; _x < barrelAmt; _x++) {
} checkBlock(barrel.getType(), belowSeaLevel);
} else if (WildStackerAPI.getWildStacker().getSystemManager().isStackedSpawner(stackedBlock)) { }
int spawnerAmt = WildStackerAPI.getSpawnersAmount((CreatureSpawner) stackedBlock.getState()); } else if (WildStackerAPI.getWildStacker().getSystemManager().isStackedSpawner(stackedBlock)) {
for (int _x = 0; _x < spawnerAmt; _x++) { int spawnerAmt = WildStackerAPI.getSpawnersAmount((CreatureSpawner) stackedBlock.getState());
checkBlock(stackedBlock.getType(), belowSeaLevel); for (int _x = 0; _x < spawnerAmt; _x++) {
} 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);
} }
} }
}