From b78b086f2e5d6c3bb4199fa24e8d9d594127629f Mon Sep 17 00:00:00 2001 From: sk89q Date: Tue, 1 Apr 2014 16:35:28 -0700 Subject: [PATCH] Added RunContext parameter to Operation.resume(). --- .../worldedit/extent/FastModeExtent.java | 3 +- .../extent/reorder/MultiStageReorder.java | 3 +- .../operation/BlockMapEntryPlacer.java | 2 +- .../function/operation/ChangeSetExecutor.java | 2 +- .../function/operation/DelegateOperation.java | 4 +- .../function/operation/ForwardExtentCopy.java | 2 +- .../function/operation/Operation.java | 25 ++++++------ .../function/operation/OperationQueue.java | 4 +- .../function/operation/Operations.java | 6 +-- .../function/operation/RunContext.java | 38 +++++++++++++++++++ .../function/visitor/BreadthFirstSearch.java | 3 +- .../function/visitor/FlatRegionVisitor.java | 3 +- .../function/visitor/LayerVisitor.java | 3 +- .../function/visitor/RegionVisitor.java | 3 +- 14 files changed, 74 insertions(+), 27 deletions(-) create mode 100644 src/main/java/com/sk89q/worldedit/function/operation/RunContext.java diff --git a/src/main/java/com/sk89q/worldedit/extent/FastModeExtent.java b/src/main/java/com/sk89q/worldedit/extent/FastModeExtent.java index 50f0853c7..6914f5993 100644 --- a/src/main/java/com/sk89q/worldedit/extent/FastModeExtent.java +++ b/src/main/java/com/sk89q/worldedit/extent/FastModeExtent.java @@ -25,6 +25,7 @@ import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.WorldEditException; import com.sk89q.worldedit.blocks.BaseBlock; import com.sk89q.worldedit.function.operation.Operation; +import com.sk89q.worldedit.function.operation.RunContext; import java.util.HashSet; import java.util.Set; @@ -94,7 +95,7 @@ public class FastModeExtent extends ExtentDelegate { protected Operation commitBefore() { return new Operation() { @Override - public Operation resume() throws WorldEditException { + public Operation resume(RunContext run) throws WorldEditException { if (dirtyChunks.size() > 0) { world.fixAfterFastMode(dirtyChunks); } diff --git a/src/main/java/com/sk89q/worldedit/extent/reorder/MultiStageReorder.java b/src/main/java/com/sk89q/worldedit/extent/reorder/MultiStageReorder.java index 0347dfd10..6d9a90a1f 100644 --- a/src/main/java/com/sk89q/worldedit/extent/reorder/MultiStageReorder.java +++ b/src/main/java/com/sk89q/worldedit/extent/reorder/MultiStageReorder.java @@ -32,6 +32,7 @@ import com.sk89q.worldedit.extent.ExtentDelegate; import com.sk89q.worldedit.function.operation.BlockMapEntryPlacer; import com.sk89q.worldedit.function.operation.Operation; import com.sk89q.worldedit.function.operation.OperationQueue; +import com.sk89q.worldedit.function.operation.RunContext; import com.sk89q.worldedit.util.collection.TupleArrayList; import java.util.*; @@ -122,7 +123,7 @@ public class MultiStageReorder extends ExtentDelegate implements ReorderingExten private class Stage3Committer implements Operation { @Override - public Operation resume() throws WorldEditException { + public Operation resume(RunContext run) throws WorldEditException { Extent extent = getExtent(); final Set blocks = new HashSet(); diff --git a/src/main/java/com/sk89q/worldedit/function/operation/BlockMapEntryPlacer.java b/src/main/java/com/sk89q/worldedit/function/operation/BlockMapEntryPlacer.java index 1ceb1c75b..5a2c43c72 100644 --- a/src/main/java/com/sk89q/worldedit/function/operation/BlockMapEntryPlacer.java +++ b/src/main/java/com/sk89q/worldedit/function/operation/BlockMapEntryPlacer.java @@ -52,7 +52,7 @@ public class BlockMapEntryPlacer implements Operation { } @Override - public Operation resume() throws WorldEditException { + public Operation resume(RunContext run) throws WorldEditException { while (iterator.hasNext()) { Map.Entry entry = iterator.next(); extent.setBlock(entry.getKey(), entry.getValue()); diff --git a/src/main/java/com/sk89q/worldedit/function/operation/ChangeSetExecutor.java b/src/main/java/com/sk89q/worldedit/function/operation/ChangeSetExecutor.java index a023fd1b7..f75733bde 100644 --- a/src/main/java/com/sk89q/worldedit/function/operation/ChangeSetExecutor.java +++ b/src/main/java/com/sk89q/worldedit/function/operation/ChangeSetExecutor.java @@ -62,7 +62,7 @@ public class ChangeSetExecutor implements Operation { } @Override - public Operation resume() throws WorldEditException { + public Operation resume(RunContext run) throws WorldEditException { while (iterator.hasNext()) { Change change = iterator.next(); if (type == Type.UNDO) { diff --git a/src/main/java/com/sk89q/worldedit/function/operation/DelegateOperation.java b/src/main/java/com/sk89q/worldedit/function/operation/DelegateOperation.java index f8600b171..d3408069b 100644 --- a/src/main/java/com/sk89q/worldedit/function/operation/DelegateOperation.java +++ b/src/main/java/com/sk89q/worldedit/function/operation/DelegateOperation.java @@ -46,8 +46,8 @@ public class DelegateOperation implements Operation { } @Override - public Operation resume() throws WorldEditException { - delegate = delegate.resume(); + public Operation resume(RunContext run) throws WorldEditException { + delegate = delegate.resume(run); return delegate != null ? this : original; } diff --git a/src/main/java/com/sk89q/worldedit/function/operation/ForwardExtentCopy.java b/src/main/java/com/sk89q/worldedit/function/operation/ForwardExtentCopy.java index c0262bd5e..3ebb794ea 100644 --- a/src/main/java/com/sk89q/worldedit/function/operation/ForwardExtentCopy.java +++ b/src/main/java/com/sk89q/worldedit/function/operation/ForwardExtentCopy.java @@ -169,7 +169,7 @@ public class ForwardExtentCopy implements Operation { } @Override - public Operation resume() throws WorldEditException { + public Operation resume(RunContext run) throws WorldEditException { if (lastVisitor != null) { affected += lastVisitor.getAffected(); lastVisitor = null; diff --git a/src/main/java/com/sk89q/worldedit/function/operation/Operation.java b/src/main/java/com/sk89q/worldedit/function/operation/Operation.java index 494ccc802..6e43f2df4 100644 --- a/src/main/java/com/sk89q/worldedit/function/operation/Operation.java +++ b/src/main/java/com/sk89q/worldedit/function/operation/Operation.java @@ -22,27 +22,30 @@ package com.sk89q.worldedit.function.operation; import com.sk89q.worldedit.WorldEditException; /** - * An task that may be split into multiple steps to be run sequentially immediately - * or at a varying or fixed interval. Operations should attempt to break apart tasks - * into smaller tasks that can be completed in quicker successions. + * An task that may be split into multiple steps to be run sequentially + * immediately or at a varying or fixed interval. Operations should attempt + * to break apart tasks into smaller tasks that can be completed in quicker + * successions. */ public interface Operation { /** - * Complete the next step. If this method returns true, then the method may be - * called again in the future, or possibly never. If this method returns false, - * then this method should not be called again. + * Complete the next step. If this method returns true, then the method may + * be called again in the future, or possibly never. If this method + * returns false, then this method should not be called again. * + * @param run describes information about the current run * @return another operation to run that operation again, or null to stop * @throws WorldEditException an error */ - Operation resume() throws WorldEditException; + Operation resume(RunContext run) throws WorldEditException; /** - * Abort the current task. After the this method is called, {@link #resume()} should - * not be called at any point in the future. This method should not be called after - * successful completion of the operation. This method must be called if - * the operation is interrupted before completion. + * Abort the current task. After the this method is called, + * {@link #resume(RunContext)} should not be called at any point in the + * future. This method should not be called after successful completion of + * the operation. This method must be called if the operation is + * interrupted before completion. */ void cancel(); diff --git a/src/main/java/com/sk89q/worldedit/function/operation/OperationQueue.java b/src/main/java/com/sk89q/worldedit/function/operation/OperationQueue.java index 0792394db..fafc8a5c6 100644 --- a/src/main/java/com/sk89q/worldedit/function/operation/OperationQueue.java +++ b/src/main/java/com/sk89q/worldedit/function/operation/OperationQueue.java @@ -76,13 +76,13 @@ public class OperationQueue implements Operation { } @Override - public Operation resume() throws WorldEditException { + public Operation resume(RunContext run) throws WorldEditException { if (current == null && queue.size() > 0) { current = queue.poll(); } if (current != null) { - current = current.resume(); + current = current.resume(run); if (current == null) { current = queue.poll(); diff --git a/src/main/java/com/sk89q/worldedit/function/operation/Operations.java b/src/main/java/com/sk89q/worldedit/function/operation/Operations.java index 4e84305d8..b2582ff4a 100644 --- a/src/main/java/com/sk89q/worldedit/function/operation/Operations.java +++ b/src/main/java/com/sk89q/worldedit/function/operation/Operations.java @@ -38,7 +38,7 @@ public final class Operations { */ public static void complete(Operation op) throws WorldEditException { while (op != null) { - op = op.resume(); + op = op.resume(new RunContext()); } } @@ -52,7 +52,7 @@ public final class Operations { public static void completeLegacy(Operation op) throws MaxChangedBlocksException { while (op != null) { try { - op = op.resume(); + op = op.resume(new RunContext()); } catch (MaxChangedBlocksException e) { throw e; } catch (WorldEditException e) { @@ -71,7 +71,7 @@ public final class Operations { public static void completeBlindly(Operation op) { while (op != null) { try { - op = op.resume(); + op = op.resume(new RunContext()); } catch (WorldEditException e) { throw new RuntimeException(e); } diff --git a/src/main/java/com/sk89q/worldedit/function/operation/RunContext.java b/src/main/java/com/sk89q/worldedit/function/operation/RunContext.java new file mode 100644 index 000000000..d77887e41 --- /dev/null +++ b/src/main/java/com/sk89q/worldedit/function/operation/RunContext.java @@ -0,0 +1,38 @@ +/* + * WorldEdit, a Minecraft world manipulation toolkit + * Copyright (C) sk89q + * Copyright (C) WorldEdit team and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.sk89q.worldedit.function.operation; + +/** + * Describes the current run. + */ +public class RunContext { + + /** + * Return whether the current operation should still continue running. + *

+ * This method can be called frequently. + * + * @return true if the operation should continue running + */ + public boolean shouldContinue() { + return true; + } + +} diff --git a/src/main/java/com/sk89q/worldedit/function/visitor/BreadthFirstSearch.java b/src/main/java/com/sk89q/worldedit/function/visitor/BreadthFirstSearch.java index b034a834a..f7d60a3ca 100644 --- a/src/main/java/com/sk89q/worldedit/function/visitor/BreadthFirstSearch.java +++ b/src/main/java/com/sk89q/worldedit/function/visitor/BreadthFirstSearch.java @@ -24,6 +24,7 @@ import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.WorldEditException; import com.sk89q.worldedit.function.operation.Operation; import com.sk89q.worldedit.function.RegionFunction; +import com.sk89q.worldedit.function.operation.RunContext; import java.util.*; @@ -154,7 +155,7 @@ public abstract class BreadthFirstSearch implements Operation { } @Override - public Operation resume() throws WorldEditException { + public Operation resume(RunContext run) throws WorldEditException { Vector position; while ((position = queue.poll()) != null) { diff --git a/src/main/java/com/sk89q/worldedit/function/visitor/FlatRegionVisitor.java b/src/main/java/com/sk89q/worldedit/function/visitor/FlatRegionVisitor.java index 546dc0f2b..5c369d068 100644 --- a/src/main/java/com/sk89q/worldedit/function/visitor/FlatRegionVisitor.java +++ b/src/main/java/com/sk89q/worldedit/function/visitor/FlatRegionVisitor.java @@ -23,6 +23,7 @@ import com.sk89q.worldedit.Vector2D; import com.sk89q.worldedit.WorldEditException; import com.sk89q.worldedit.function.FlatRegionFunction; import com.sk89q.worldedit.function.operation.Operation; +import com.sk89q.worldedit.function.operation.RunContext; import com.sk89q.worldedit.regions.FlatRegion; import static com.google.common.base.Preconditions.checkNotNull; @@ -60,7 +61,7 @@ public class FlatRegionVisitor implements Operation { } @Override - public Operation resume() throws WorldEditException { + public Operation resume(RunContext run) throws WorldEditException { for (Vector2D pt : flatRegion.asFlatRegion()) { if (function.apply(pt)) { affected++; diff --git a/src/main/java/com/sk89q/worldedit/function/visitor/LayerVisitor.java b/src/main/java/com/sk89q/worldedit/function/visitor/LayerVisitor.java index 3437ec6d0..24f7fefc6 100644 --- a/src/main/java/com/sk89q/worldedit/function/visitor/LayerVisitor.java +++ b/src/main/java/com/sk89q/worldedit/function/visitor/LayerVisitor.java @@ -26,6 +26,7 @@ import com.sk89q.worldedit.function.LayerFunction; import com.sk89q.worldedit.function.mask.Mask2D; import com.sk89q.worldedit.function.mask.Masks; import com.sk89q.worldedit.function.operation.Operation; +import com.sk89q.worldedit.function.operation.RunContext; import com.sk89q.worldedit.regions.FlatRegion; import static com.google.common.base.Preconditions.checkArgument; @@ -88,7 +89,7 @@ public class LayerVisitor implements Operation { } @Override - public Operation resume() throws WorldEditException { + public Operation resume(RunContext run) throws WorldEditException { for (Vector2D column : flatRegion.asFlatRegion()) { if (!mask.test(column)) { continue; diff --git a/src/main/java/com/sk89q/worldedit/function/visitor/RegionVisitor.java b/src/main/java/com/sk89q/worldedit/function/visitor/RegionVisitor.java index 99cf2d273..b8fffb5ec 100644 --- a/src/main/java/com/sk89q/worldedit/function/visitor/RegionVisitor.java +++ b/src/main/java/com/sk89q/worldedit/function/visitor/RegionVisitor.java @@ -23,6 +23,7 @@ import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.WorldEditException; import com.sk89q.worldedit.function.operation.Operation; import com.sk89q.worldedit.function.RegionFunction; +import com.sk89q.worldedit.function.operation.RunContext; import com.sk89q.worldedit.regions.Region; /** @@ -49,7 +50,7 @@ public class RegionVisitor implements Operation { } @Override - public Operation resume() throws WorldEditException { + public Operation resume(RunContext run) throws WorldEditException { for (Vector pt : region) { if (function.apply(pt)) { affected++;