Skip to content

Commit

Permalink
Added first trial of a queued operation system
Browse files Browse the repository at this point in the history
  • Loading branch information
me4502 committed Aug 22, 2018
1 parent 760bd96 commit a79e797
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -103,6 +104,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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
45 changes: 45 additions & 0 deletions worldedit-core/src/main/java/com/sk89q/worldedit/LocalSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -92,6 +94,8 @@ public class LocalSession {
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;
Expand Down Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ public interface Platform {
*/
@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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -30,6 +37,8 @@ public final class Operations {
private Operations() {
}

private static RunContext BASIC_CONTEXT = new RunContext();

/**
* Complete a given operation synchronously until it completes.
*
Expand All @@ -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);
}
}

Expand All @@ -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) {
Expand All @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down

0 comments on commit a79e797

Please sign in to comment.