Permalink
Browse files

Added first trial of a queued operation system

  • Loading branch information...
me4502 committed Aug 22, 2018
1 parent bc9ca4d commit ed313f21bc8cf5f36d680375dd7ead5a3893ec82
@@ -115,6 +115,11 @@ public BukkitWorld matchWorld(com.sk89q.worldedit.world.World world) {
}
}
@Override
public Actor getConsoleCommandSender() {
return plugin.wrapCommandSender(Bukkit.getConsoleSender());
}
@Override
public void registerCommands(Dispatcher dispatcher) {
List<CommandInfo> toRegister = new ArrayList<>();
@@ -37,6 +37,7 @@
import com.sk89q.worldedit.extension.platform.Capability;
import com.sk89q.worldedit.extension.platform.Platform;
import com.sk89q.worldedit.extent.inventory.BlockBag;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
@@ -102,6 +103,8 @@ public void onEnable() {
// Forge WorldEdit and there's (probably) not going to be any other
// platforms to be worried about... at the current time of writing
WorldEdit.getInstance().getEventBus().post(new PlatformReadyEvent());
Bukkit.getScheduler().runTaskTimer(this, () -> WorldEdit.getInstance().getSessionManager().tickQueues(), 1, 1);
}
private void loadConfig() {
@@ -25,8 +25,6 @@
import static com.sk89q.worldedit.regions.Regions.maximumBlockY;
import static com.sk89q.worldedit.regions.Regions.minimumBlockY;
import com.sk89q.worldedit.function.block.BlockDistributionCounter;
import com.sk89q.worldedit.world.block.BaseBlock;
import com.sk89q.worldedit.entity.BaseEntity;
import com.sk89q.worldedit.entity.Entity;
import com.sk89q.worldedit.event.extent.EditSessionEvent;
@@ -47,6 +45,7 @@
import com.sk89q.worldedit.extent.world.SurvivalModeExtent;
import com.sk89q.worldedit.function.GroundFunction;
import com.sk89q.worldedit.function.RegionMaskingFilter;
import com.sk89q.worldedit.function.block.BlockDistributionCounter;
import com.sk89q.worldedit.function.block.BlockReplace;
import com.sk89q.worldedit.function.block.Counter;
import com.sk89q.worldedit.function.block.Naturalizer;
@@ -112,8 +111,6 @@
import com.sk89q.worldedit.world.registry.LegacyMapper;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
@@ -36,6 +36,8 @@
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.extent.inventory.BlockBag;
import com.sk89q.worldedit.function.mask.Mask;
import com.sk89q.worldedit.function.operation.Operation;
import com.sk89q.worldedit.function.operation.RunContext;
import com.sk89q.worldedit.internal.cui.CUIEvent;
import com.sk89q.worldedit.internal.cui.CUIRegion;
import com.sk89q.worldedit.internal.cui.SelectionShapeEvent;
@@ -92,6 +94,8 @@
private transient Mask mask;
private transient TimeZone timezone = TimeZone.getDefault();
private transient Vector cuiTemporaryBlock;
private transient LinkedList<Operation> operationQueue = new LinkedList<>();
private transient boolean operationQueuePaused = false;
// Saved properties
private String lastScript;
@@ -894,4 +898,45 @@ public void setMask(Mask mask) {
this.mask = mask;
}
/**
* Add a {@link Operation} to the queue.
*
* @param actor The actor, if present
* @param operation The operation
*/
public void enqeueOperation(@Nullable Actor actor, Operation operation) {
checkNotNull(operation);
operationQueue.addLast(operation);
if (actor != null) {
actor.print("Queued Operation");
}
}
/**
* Run the next item in the operation queue.
*
* @param actor The actor for feedback, if present
*/
public void runQueue(@Nullable Actor actor) {
if (isRunningOperations()) {
Operation op = operationQueue.poll();
try {
op = op.resume(new RunContext());
if (op != null) {
operationQueue.addFirst(op);
}
} catch (WorldEditException e) {
throw new RuntimeException(e);
}
}
}
/**
* Checks if the operation queue is running
*
* @return If the session is performing a queue
*/
public boolean isRunningOperations() {
return !operationQueuePaused && !operationQueue.isEmpty();
}
}
@@ -96,6 +96,13 @@
*/
@Nullable World matchWorld(World world);
/**
* Gets a command sender for the console.
*
* @return The console command sender
*/
Actor getConsoleCommandSender();
/**
* Register the commands contained within the given command dispatcher.
*
@@ -19,8 +19,15 @@
package com.sk89q.worldedit.function.operation;
import static com.google.common.base.Preconditions.checkNotNull;
import com.sk89q.worldedit.MaxChangedBlocksException;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.extension.platform.Capability;
import javax.annotation.Nullable;
/**
* Operation helper methods.
@@ -30,6 +37,8 @@
private Operations() {
}
private static RunContext BASIC_CONTEXT = new RunContext();
/**
* Complete a given operation synchronously until it completes.
*
@@ -38,7 +47,7 @@ private Operations() {
*/
public static void complete(Operation op) throws WorldEditException {
while (op != null) {
op = op.resume(new RunContext());
op = op.resume(BASIC_CONTEXT);
}
}
@@ -52,7 +61,7 @@ public static void complete(Operation op) throws WorldEditException {
public static void completeLegacy(Operation op) throws MaxChangedBlocksException {
while (op != null) {
try {
op = op.resume(new RunContext());
op = op.resume(BASIC_CONTEXT);
} catch (MaxChangedBlocksException e) {
throw e;
} catch (WorldEditException e) {
@@ -63,19 +72,35 @@ public static void completeLegacy(Operation op) throws MaxChangedBlocksException
/**
* Complete a given operation synchronously until it completes. Re-throw all
* {@link com.sk89q.worldedit.WorldEditException} exceptions as
* {@link java.lang.RuntimeException}s.
* {@link WorldEditException} exceptions as
* {@link RuntimeException}s.
*
* @param op operation to execute
*/
public static void completeBlindly(Operation op) {
while (op != null) {
try {
op = op.resume(new RunContext());
op = op.resume(BASIC_CONTEXT);
} catch (WorldEditException e) {
throw new RuntimeException(e);
}
}
}
/**
* Complete a given operation in a queue until it completes.
*
* @param op operation to execute
* @param actor The actor to complete this with, or null for console
*/
public static void completeQueued(Operation op, @Nullable Actor actor) {
checkNotNull(op);
if (actor == null) {
actor = WorldEdit.getInstance().getPlatformManager().queryCapability(Capability.USER_COMMANDS).getConsoleCommandSender();
}
WorldEdit.getInstance().getSessionManager().get(actor).enqeueOperation(actor, op);
}
}
@@ -256,6 +256,15 @@ protected UUID getKey(SessionKey key) {
}
}
/**
* Update the queues held inside this session manager.
*/
public synchronized void tickQueues() {
for (Map.Entry<UUID, SessionHolder> sessionEntry : sessions.entrySet()) {
sessionEntry.getValue().session.runQueue(null);
}
}
/**
* Remove the session for the given owner if one exists.
*
@@ -288,7 +297,7 @@ private synchronized void saveChangedSessions() {
while (it.hasNext()) {
SessionHolder stored = it.next();
if (stored.key.isActive()) {
if (stored.key.isActive() || stored.session.isRunningOperations()) {
stored.lastActive = now;
if (stored.session.compareAndResetDirty()) {
@@ -119,6 +119,11 @@ public World matchWorld(World world) {
}
}
@Override
public Actor getConsoleCommandSender() {
return null; // TODO
}
@Override
public void registerCommands(Dispatcher dispatcher) {
if (server == null) return;
@@ -121,6 +121,11 @@ public World matchWorld(World world) {
}
}
@Override
public Actor getConsoleCommandSender() {
return mod.wrapCommandSource(Sponge.getServer().getConsole());
}
@Override
public void registerCommands(Dispatcher dispatcher) {
for (CommandMapping command : dispatcher.getCommands()) {

0 comments on commit ed313f2

Please sign in to comment.