From 7d4906cfe9fc48ea125f967264e6258001f40ec7 Mon Sep 17 00:00:00 2001 From: Kenzie Togami Date: Thu, 4 Oct 2018 10:37:58 -0700 Subject: [PATCH] Add chunk batching flag, enable by default --- .../worldedit/bukkit/WorldEditPlugin.java | 2 +- .../java/com/sk89q/worldedit/EditSession.java | 30 +++++++++++++++++++ .../com/sk89q/worldedit/LocalSession.java | 4 +-- .../command/composition/SelectionCommand.java | 2 +- .../extent/reorder/ChunkBatchingExtent.java | 23 +++++++++++++- .../internal/command/WorldEditBinding.java | 2 +- .../scripting/CraftScriptContext.java | 2 +- 7 files changed, 58 insertions(+), 7 deletions(-) diff --git a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/WorldEditPlugin.java b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/WorldEditPlugin.java index 8cc6f544b..41ab53a7c 100644 --- a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/WorldEditPlugin.java +++ b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/WorldEditPlugin.java @@ -272,7 +272,7 @@ public class WorldEditPlugin extends JavaPlugin implements TabCompleter { EditSession editSession = WorldEdit.getInstance().getEditSessionFactory() .getEditSession(wePlayer.getWorld(), session.getBlockChangeLimit(), blockBag, wePlayer); - editSession.enableQueue(); + editSession.enableStandardMode(); return editSession; } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/EditSession.java b/worldedit-core/src/main/java/com/sk89q/worldedit/EditSession.java index 593f6ec91..2049be07d 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/EditSession.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/EditSession.java @@ -36,6 +36,7 @@ import com.sk89q.worldedit.extent.buffer.ForgetfulExtentBuffer; import com.sk89q.worldedit.extent.cache.LastAccessExtentCache; import com.sk89q.worldedit.extent.inventory.BlockBag; import com.sk89q.worldedit.extent.inventory.BlockBagExtent; +import com.sk89q.worldedit.extent.reorder.ChunkBatchingExtent; import com.sk89q.worldedit.extent.reorder.MultiStageReorder; import com.sk89q.worldedit.extent.validation.BlockChangeLimiter; import com.sk89q.worldedit.extent.validation.DataValidatorExtent; @@ -151,6 +152,7 @@ public class EditSession implements Extent { private @Nullable FastModeExtent fastModeExtent; private final SurvivalModeExtent survivalExtent; + private @Nullable ChunkBatchingExtent chunkBatchingExtent; private @Nullable ChunkLoadingExtent chunkLoadingExtent; private @Nullable LastAccessExtentCache cacheExtent; private @Nullable BlockQuirkExtent quirkExtent; @@ -190,6 +192,7 @@ public class EditSession implements Extent { extent = fastModeExtent = new FastModeExtent(world, false); extent = survivalExtent = new SurvivalModeExtent(extent, world); extent = quirkExtent = new BlockQuirkExtent(extent, world); + extent = chunkBatchingExtent = new ChunkBatchingExtent(extent); extent = chunkLoadingExtent = new ChunkLoadingExtent(extent, world); extent = cacheExtent = new LastAccessExtentCache(extent); extent = wrapExtent(extent, eventBus, event, Stage.BEFORE_CHANGE); @@ -229,6 +232,16 @@ public class EditSession implements Extent { return event.getExtent(); } + /** + * Turns on specific features for a normal WorldEdit session, such as + * {@link #enableQueue() queuing} and {@link #setBatchingChunks(boolean) + * chunk batching}. + */ + public void enableStandardMode() { + enableQueue(); + setBatchingChunks(true); + } + /** * Get the world. * @@ -378,6 +391,23 @@ public class EditSession implements Extent { return blockBagExtent.popMissing(); } + public boolean isBatchingChunks() { + return chunkBatchingExtent != null && chunkBatchingExtent.isEnabled(); + } + + public void setBatchingChunks(boolean batchingChunks) { + if (chunkBatchingExtent == null) { + if (batchingChunks) { + throw new UnsupportedOperationException("Chunk batching not supported by this session."); + } + return; + } + if (!batchingChunks) { + flushQueue(); + } + chunkBatchingExtent.setEnabled(batchingChunks); + } + /** * Get the number of blocks changed, including repeated block changes. * diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/LocalSession.java b/worldedit-core/src/main/java/com/sk89q/worldedit/LocalSession.java index 85ca1d12a..d30e4ec6f 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/LocalSession.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/LocalSession.java @@ -226,7 +226,7 @@ public class LocalSession { EditSession editSession = history.get(historyPointer); EditSession newEditSession = WorldEdit.getInstance().getEditSessionFactory() .getEditSession(editSession.getWorld(), -1, newBlockBag, player); - newEditSession.enableQueue(); + newEditSession.enableStandardMode(); newEditSession.setFastMode(fastMode); editSession.undo(newEditSession); return editSession; @@ -249,7 +249,7 @@ public class LocalSession { EditSession editSession = history.get(historyPointer); EditSession newEditSession = WorldEdit.getInstance().getEditSessionFactory() .getEditSession(editSession.getWorld(), -1, newBlockBag, player); - newEditSession.enableQueue(); + newEditSession.enableStandardMode(); newEditSession.setFastMode(fastMode); editSession.redo(newEditSession); ++historyPointer; diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/composition/SelectionCommand.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/composition/SelectionCommand.java index 087963aed..ffb06db2e 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/composition/SelectionCommand.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/composition/SelectionCommand.java @@ -72,7 +72,7 @@ public class SelectionCommand extends SimpleCommand { Region selection = session.getSelection(player.getWorld()); EditSession editSession = session.createEditSession(player); - editSession.enableQueue(); + editSession.enableStandardMode(); locals.put(EditSession.class, editSession); session.tellVersion(player); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/reorder/ChunkBatchingExtent.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/reorder/ChunkBatchingExtent.java index 1a665ec15..c5be5c565 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/reorder/ChunkBatchingExtent.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/reorder/ChunkBatchingExtent.java @@ -54,13 +54,30 @@ public class ChunkBatchingExtent extends AbstractDelegateExtent { .thenComparing(Vector2D.COMPARING_GRID_ARRANGEMENT); private final SortedMap batches = new TreeMap<>(REGION_OPTIMIZED_SORT); + private boolean enabled; - protected ChunkBatchingExtent(Extent extent) { + public ChunkBatchingExtent(Extent extent) { + this(extent, true); + } + + public ChunkBatchingExtent(Extent extent, boolean enabled) { super(extent); + this.enabled = enabled; + } + + public boolean isEnabled() { + return enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; } @Override public boolean setBlock(Vector location, BlockStateHolder block) throws WorldEditException { + if (!enabled) { + return getExtent().setBlock(location, block); + } BlockVector2D chunkPos = new BlockVector2D(location.getBlockX() >> 4, location.getBlockZ() >> 4); batches.computeIfAbsent(chunkPos, k -> new LocatedBlockList()).add(location, block); return true; @@ -68,6 +85,9 @@ public class ChunkBatchingExtent extends AbstractDelegateExtent { @Override protected Operation commitBefore() { + if (!enabled) { + return null; + } return new Operation() { private final Iterator batchIterator = batches.values().iterator(); @@ -78,6 +98,7 @@ public class ChunkBatchingExtent extends AbstractDelegateExtent { return null; } new SetLocatedBlocks(getExtent(), batchIterator.next()).resume(run); + batchIterator.remove(); return this; } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/internal/command/WorldEditBinding.java b/worldedit-core/src/main/java/com/sk89q/worldedit/internal/command/WorldEditBinding.java index 664af4c4e..1bdeee5c5 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/internal/command/WorldEditBinding.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/internal/command/WorldEditBinding.java @@ -103,7 +103,7 @@ public class WorldEditBinding extends BindingHelper { Player sender = getPlayer(context); LocalSession session = worldEdit.getSessionManager().get(sender); EditSession editSession = session.createEditSession(sender); - editSession.enableQueue(); + editSession.enableStandardMode(); context.getContext().getLocals().put(EditSession.class, editSession); session.tellVersion(sender); return editSession; diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/scripting/CraftScriptContext.java b/worldedit-core/src/main/java/com/sk89q/worldedit/scripting/CraftScriptContext.java index 98c62ec59..543a28e4f 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/scripting/CraftScriptContext.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/scripting/CraftScriptContext.java @@ -65,7 +65,7 @@ public class CraftScriptContext extends CraftScriptEnvironment { EditSession editSession = controller.getEditSessionFactory() .getEditSession(player.getWorld(), session.getBlockChangeLimit(), session.getBlockBag(player), player); - editSession.enableQueue(); + editSession.enableStandardMode(); editSessions.add(editSession); return editSession; }