mirror of
https://github.com/IntellectualSites/PlotSquared.git
synced 2025-06-25 02:04:44 +02:00
Clean up chunk coordinators and queue coordinators (#3208)
This commit is contained in:
@ -25,24 +25,7 @@
|
||||
*/
|
||||
package com.plotsquared.core.queue;
|
||||
|
||||
import com.plotsquared.core.util.task.PlotSquaredTask;
|
||||
|
||||
public abstract class ChunkCoordinator implements PlotSquaredTask {
|
||||
|
||||
private boolean cancelled = false;
|
||||
|
||||
@Override
|
||||
public abstract void runTask();
|
||||
|
||||
@Override
|
||||
public boolean isCancelled() {
|
||||
return cancelled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cancel() {
|
||||
this.cancelled = true;
|
||||
}
|
||||
public abstract class ChunkCoordinator implements Runnable {
|
||||
|
||||
/**
|
||||
* Starts the chunk coordinator. This will usually (implementation-specific-permitting) mark chunks to be loaded in batches,
|
||||
@ -51,6 +34,11 @@ public abstract class ChunkCoordinator implements PlotSquaredTask {
|
||||
*/
|
||||
public abstract void start();
|
||||
|
||||
/**
|
||||
* Cancel the chunk coordinator.
|
||||
*/
|
||||
public abstract void cancel();
|
||||
|
||||
/**
|
||||
* Get the amount of remaining chunks (at the time of the method call)
|
||||
*
|
||||
|
@ -29,18 +29,12 @@ import com.plotsquared.core.PlotSquared;
|
||||
import com.sk89q.worldedit.world.World;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ConcurrentLinkedDeque;
|
||||
|
||||
public class GlobalBlockQueue {
|
||||
|
||||
private final ConcurrentLinkedDeque<QueueCoordinator> activeQueues;
|
||||
private QueueProvider provider;
|
||||
|
||||
public GlobalBlockQueue(@NonNull QueueProvider provider) {
|
||||
this.provider = provider;
|
||||
this.activeQueues = new ConcurrentLinkedDeque<>();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -64,33 +58,4 @@ public class GlobalBlockQueue {
|
||||
this.provider = provider;
|
||||
}
|
||||
|
||||
/**
|
||||
* Place an instance of {@link QueueCoordinator} into a list incase access is needed
|
||||
* and then start it.
|
||||
*
|
||||
* @param queue {@link QueueCoordinator} instance to start.
|
||||
* @return true if added to queue, false otherwise
|
||||
*/
|
||||
public boolean enqueue(@NonNull QueueCoordinator queue) {
|
||||
boolean success = false;
|
||||
if (queue.size() > 0 && !activeQueues.contains(queue)) {
|
||||
success = activeQueues.add(queue);
|
||||
queue.start();
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
public void dequeue(@NonNull QueueCoordinator queue) {
|
||||
queue.cancel();
|
||||
activeQueues.remove(queue);
|
||||
}
|
||||
|
||||
public @NonNull List<QueueCoordinator> getActiveQueues() {
|
||||
return new ArrayList<>(activeQueues);
|
||||
}
|
||||
|
||||
public boolean isDone() {
|
||||
return activeQueues.size() == 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -39,6 +39,9 @@ import com.sk89q.worldedit.world.World;
|
||||
import com.sk89q.worldedit.world.biome.BiomeType;
|
||||
import com.sk89q.worldedit.world.block.BaseBlock;
|
||||
import com.sk89q.worldedit.world.block.BlockState;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
|
||||
@ -51,6 +54,7 @@ public abstract class QueueCoordinator {
|
||||
private boolean forceSync = false;
|
||||
@Nullable
|
||||
private Object chunkObject;
|
||||
private final AtomicBoolean enqueued = new AtomicBoolean();
|
||||
|
||||
@Inject
|
||||
private GlobalBlockQueue blockQueue;
|
||||
@ -319,12 +323,17 @@ public abstract class QueueCoordinator {
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueue the queue with the {@link GlobalBlockQueue}
|
||||
* Enqueue the queue to start it
|
||||
*
|
||||
* @return success or not
|
||||
*/
|
||||
public boolean enqueue() {
|
||||
return blockQueue.enqueue(this);
|
||||
boolean success = false;
|
||||
if (enqueued.compareAndSet(false, true)) {
|
||||
success = true;
|
||||
start();
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -333,7 +342,7 @@ public abstract class QueueCoordinator {
|
||||
public abstract void start();
|
||||
|
||||
/**
|
||||
* Cancel the queue. Not yet implemented.
|
||||
* Cancel the queue
|
||||
*/
|
||||
public abstract void cancel();
|
||||
|
||||
|
@ -107,11 +107,7 @@ public class DefaultProgressSubscriber implements ProgressSubscriber {
|
||||
@Override
|
||||
public void notifyProgress(@NonNull ChunkCoordinator coordinator, double progress) {
|
||||
this.progress.set(progress);
|
||||
if (coordinator.isCancelled() || progress >= 1) {
|
||||
if (task != null) {
|
||||
task.cancel();
|
||||
}
|
||||
} else if (started.compareAndSet(false, true)) {
|
||||
if (started.compareAndSet(false, true)) {
|
||||
TaskManager.getPlatformImplementation().taskLater(() -> task = TaskManager
|
||||
.getPlatformImplementation()
|
||||
.taskRepeat(() -> {
|
||||
|
Reference in New Issue
Block a user