Permalink
Browse files

Create a new TaskManager rather than using Operation queues. Allows f…

…or more custom code.
  • Loading branch information...
me4502 committed Aug 24, 2018
1 parent dfdf71f commit 1a20105f43e5ad58b13f0975d9b0e958550dac37
@@ -105,7 +105,7 @@ public void onEnable() {
// platforms to be worried about... at the current time of writing
WorldEdit.getInstance().getEventBus().post(new PlatformReadyEvent());
Bukkit.getScheduler().runTaskTimer(this, () -> WorldEdit.getInstance().tickOperationQueues(), 1, 1);
Bukkit.getScheduler().runTaskTimer(this, () -> WorldEdit.getInstance().getTaskManager().tickTaskQueues(), 1, 1);
}
private void loadConfig() {
@@ -61,14 +61,14 @@
import com.sk89q.worldedit.function.mask.Masks;
import com.sk89q.worldedit.function.mask.NoiseFilter2D;
import com.sk89q.worldedit.function.mask.RegionMask;
import com.sk89q.worldedit.function.operation.AffectedFutureOperation;
import com.sk89q.worldedit.function.operation.ChangeSetExecutor;
import com.sk89q.worldedit.function.operation.ForwardExtentCopy;
import com.sk89q.worldedit.function.operation.Operation;
import com.sk89q.worldedit.function.operation.OperationQueue;
import com.sk89q.worldedit.function.operation.Operations;
import com.sk89q.worldedit.function.pattern.BlockPattern;
import com.sk89q.worldedit.function.pattern.Pattern;
import com.sk89q.worldedit.function.task.Task;
import com.sk89q.worldedit.function.util.RegionOffset;
import com.sk89q.worldedit.function.visitor.DownwardVisitor;
import com.sk89q.worldedit.function.visitor.LayerVisitor;
@@ -118,7 +118,6 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -1105,7 +1104,7 @@ public int moveCuboidRegion(Region region, Vector dir, int distance, boolean cop
* @param radius the radius of the removal, where a value should be 0 or greater
* @return number of blocks affected
*/
public CompletableFuture<Integer> drainArea(@Nullable Actor actor, Vector origin, double radius) {
public Task<Integer> drainArea(@Nullable Actor actor, Vector origin, double radius) {
checkNotNull(origin);
checkArgument(radius >= 0, "radius >= 0 required");
@@ -1124,11 +1123,15 @@ public int moveCuboidRegion(Region region, Vector dir, int distance, boolean cop
}
}
CompletableFuture<Integer> future = new CompletableFuture<>();
Task<Integer> task = Operations.<Integer>completeQueued(visitor, actor, this)
.withSupplier(visitor::getAffected);
Operations.completeQueued(new AffectedFutureOperation(visitor, future), actor, this);
if (actor != null) {
task.withStatusConsumer(actor::print);
task.onExcept(e -> actor.printError(e.getMessage()));
}
return future;
return task;
}
/**
@@ -38,8 +38,7 @@
import com.sk89q.worldedit.extension.platform.Platform;
import com.sk89q.worldedit.extension.platform.PlatformManager;
import com.sk89q.worldedit.extent.inventory.BlockBag;
import com.sk89q.worldedit.function.operation.OperationQueue;
import com.sk89q.worldedit.function.operation.RunContext;
import com.sk89q.worldedit.function.task.TaskManager;
import com.sk89q.worldedit.scripting.CraftScriptContext;
import com.sk89q.worldedit.scripting.CraftScriptEngine;
import com.sk89q.worldedit.scripting.RhinoCraftScriptEngine;
@@ -67,7 +66,6 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -97,14 +95,13 @@
private final PlatformManager platformManager = new PlatformManager(this);
private final EditSessionFactory editSessionFactory = new EditSessionFactory.EditSessionFactoryImpl(eventBus);
private final SessionManager sessions = new SessionManager(this);
private final TaskManager taskManager = new TaskManager();
private final BlockFactory blockFactory = new BlockFactory(this);
private final ItemFactory itemFactory = new ItemFactory(this);
private final MaskFactory maskFactory = new MaskFactory(this);
private final PatternFactory patternFactory = new PatternFactory(this);
private final Map<Actor, OperationQueue> operationQueues = new WeakHashMap<>();
static {
WorldEditPrefixHandler.register("com.sk89q.worldedit");
getVersion();
@@ -315,25 +312,12 @@ private boolean checkFilename(String filename) {
}
/**
* Gets an {@link OperationQueue} for the given actor.
* Gets the TaskManager.
*
* @param actor The actor
* @return The task manager
*/
public OperationQueue getOperationQueue(Actor actor) {
return operationQueues.computeIfAbsent(actor, x -> new OperationQueue());
}
/**
* Tick all of the operation queues
*/
public void tickOperationQueues() {
for (Map.Entry<Actor, OperationQueue> queueEntry : operationQueues.entrySet()) {
try {
queueEntry.getValue().resume(new RunContext());
} catch (WorldEditException e) {
throw new RuntimeException(e);
}
}
public TaskManager getTaskManager() {
return this.taskManager;
}
/**
@@ -152,8 +152,8 @@ public void drain(Player player, LocalSession session, EditSession editSession,
double radius = Math.max(0, args.getDouble(0));
we.checkMaxRadius(radius);
editSession.drainArea(player, session.getPlacementPosition(player), radius).thenAccept(
affected -> player.print(affected + " block(s) have been changed.")
editSession.drainArea(player, session.getPlacementPosition(player), radius)
.withConsumer(affected -> player.print(affected + " block(s) have been changed.")
);
}
@@ -61,6 +61,8 @@
import com.sk89q.worldedit.event.platform.CommandSuggestionEvent;
import com.sk89q.worldedit.function.factory.Deform;
import com.sk89q.worldedit.function.factory.Deform.Mode;
import com.sk89q.worldedit.function.operation.FlushOperation;
import com.sk89q.worldedit.function.operation.Operations;
import com.sk89q.worldedit.internal.command.ActorAuthorizer;
import com.sk89q.worldedit.internal.command.CommandLoggingHandler;
import com.sk89q.worldedit.internal.command.UserCommandCompleter;
@@ -315,8 +317,7 @@ public void handleCommand(CommandEvent event) {
EditSession editSession = locals.get(EditSession.class);
if (editSession != null) {
session.remember(editSession);
editSession.flushQueue();
Operations.completeBlindly(new FlushOperation(actor, editSession));
if (config.profile) {
long time = System.currentTimeMillis() - start;
@@ -330,8 +331,6 @@ public void handleCommand(CommandEvent event) {
actor.printDebug((time / 1000.0) + "s elapsed.");
}
}
worldEdit.flushBlockBag(actor, editSession);
}
}
@@ -37,7 +37,6 @@
private final List<Operation> finishedOperations = Lists.newArrayList();
private final Deque<Operation> queue = new ArrayDeque<>();
private Operation current;
private boolean paused;
/**
* Create a new queue containing no operations.
@@ -81,9 +80,6 @@ public void offer(Operation operation) {
@Override
public Operation resume(RunContext run) throws WorldEditException {
if (isPaused()) {
return this; // Don't tick paused ones.
}
if (current == null && !queue.isEmpty()) {
current = queue.poll();
}
@@ -116,22 +112,4 @@ public void addStatusMessages(List<String> messages) {
// We've grabbed the status messages, remove them now.
finishedOperations.clear();
}
/**
* Checks whether this queue is paused.
*
* @return If it's paused
*/
public boolean isPaused() {
return this.paused;
}
/**
* Set whether this queue is paused.
*
* @param paused If it should be paused
*/
public void setPaused(boolean paused) {
this.paused = paused;
}
}
@@ -27,6 +27,7 @@
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.extension.platform.Capability;
import com.sk89q.worldedit.function.task.Task;
import javax.annotation.Nullable;
@@ -107,8 +108,9 @@ public static void completeQueued(Operation op) {
* @param op operation to execute
* @param actor The actor to complete this with, or null for console
* @param editSession The edit session
* @return The task that this creates
*/
public static void completeQueued(Operation op, @Nullable Actor actor, @Nullable EditSession editSession) {
public static <T> Task<T> completeQueued(Operation op, @Nullable Actor actor, @Nullable EditSession editSession) {
checkNotNull(op);
if (actor == null) {
@@ -120,7 +122,10 @@ public static void completeQueued(Operation op, @Nullable Actor actor, @Nullable
if (editSession != null) {
operation = new OperationQueue(op, new FlushOperation(actor, editSession));
}
WorldEdit.getInstance().getOperationQueue(actor).offer(operation);
Task<T> task = new Task<>(operation);
WorldEdit.getInstance().getTaskManager().getTaskQueue(actor).offer(task);
return task;
}
}
Oops, something went wrong.

0 comments on commit 1a20105

Please sign in to comment.