Switch loadingChunks to AtomicInteger to be safe (in case of multi-threaded)

This commit is contained in:
dordsor21 2022-02-23 17:30:50 +00:00 committed by Jordan
parent 5c0e538231
commit a71af4e76f

View File

@ -75,12 +75,12 @@ public final class BukkitChunkCoordinator extends ChunkCoordinator {
private final boolean unloadAfter;
private final int totalSize;
private final AtomicInteger expectedSize;
private final AtomicInteger loadingChunks = new AtomicInteger();
private int batchSize;
private PlotSquaredTask task;
private boolean shouldCancel;
private boolean finished;
private int loadingChunks = 0;
@Inject
private BukkitChunkCoordinator(
@ -152,7 +152,7 @@ public final class BukkitChunkCoordinator extends ChunkCoordinator {
Chunk chunk = this.availableChunks.poll();
if (chunk == null) {
if (this.availableChunks.isEmpty()) {
if (this.requestedChunks.isEmpty() && loadingChunks == 0) {
if (this.requestedChunks.isEmpty() && loadingChunks.get() == 0) {
finish();
} else {
requestBatch();
@ -205,11 +205,11 @@ public final class BukkitChunkCoordinator extends ChunkCoordinator {
BlockVector2 chunk;
for (int i = 0; i < this.batchSize && (chunk = this.requestedChunks.poll()) != null; i++) {
// This required PaperLib to be bumped to version 1.0.4 to mark the request as urgent
loadingChunks++;
loadingChunks.incrementAndGet();
PaperLib
.getChunkAtAsync(this.bukkitWorld, chunk.getX(), chunk.getZ(), true, true)
.whenComplete((chunkObject, throwable) -> {
loadingChunks--;
loadingChunks.decrementAndGet();
if (throwable != null) {
throwable.printStackTrace();
// We want one less because this couldn't be processed