Skip to content

Commit

Permalink
Add chunk batching flag, enable by default
Browse files Browse the repository at this point in the history
  • Loading branch information
octylFractal committed Oct 10, 2018
1 parent ff391ca commit 7d4906c
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public EditSession createEditSession(Player player) {

EditSession editSession = WorldEdit.getInstance().getEditSessionFactory()
.getEditSession(wePlayer.getWorld(), session.getBlockChangeLimit(), blockBag, wePlayer);
editSession.enableQueue();
editSession.enableStandardMode();

return editSession;
}
Expand Down
30 changes: 30 additions & 0 deletions worldedit-core/src/main/java/com/sk89q/worldedit/EditSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
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;
Expand Down Expand Up @@ -151,6 +152,7 @@ public enum Stage {

private @Nullable FastModeExtent fastModeExtent;
private final SurvivalModeExtent survivalExtent;
private @Nullable ChunkBatchingExtent chunkBatchingExtent;
private @Nullable ChunkLoadingExtent chunkLoadingExtent;
private @Nullable LastAccessExtentCache cacheExtent;
private @Nullable BlockQuirkExtent quirkExtent;
Expand Down Expand Up @@ -190,6 +192,7 @@ public enum Stage {
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);
Expand Down Expand Up @@ -229,6 +232,16 @@ private Extent wrapExtent(Extent extent, EventBus eventBus, EditSessionEvent eve
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.
*
Expand Down Expand Up @@ -378,6 +391,23 @@ public Map<BlockType, Integer> popMissingBlocks() {
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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public EditSession undo(@Nullable BlockBag newBlockBag, Player player) {
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;
Expand All @@ -249,7 +249,7 @@ public EditSession redo(@Nullable BlockBag newBlockBag, Player player) {
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public Operation call(CommandArgs args, CommandLocals locals) throws CommandExce
Region selection = session.getSelection(player.getWorld());

EditSession editSession = session.createEditSession(player);
editSession.enableQueue();
editSession.enableStandardMode();
locals.put(EditSession.class, editSession);
session.tellVersion(player);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,40 @@ public class ChunkBatchingExtent extends AbstractDelegateExtent {
.thenComparing(Vector2D.COMPARING_GRID_ARRANGEMENT);

private final SortedMap<BlockVector2D, LocatedBlockList> 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;
}

@Override
protected Operation commitBefore() {
if (!enabled) {
return null;
}
return new Operation() {

private final Iterator<LocatedBlockList> batchIterator = batches.values().iterator();
Expand All @@ -78,6 +98,7 @@ public Operation resume(RunContext run) throws WorldEditException {
return null;
}
new SetLocatedBlocks(getExtent(), batchIterator.next()).resume(run);
batchIterator.remove();
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public EditSession getEditSession(ArgumentStack context) throws ParameterExcepti
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public EditSession remember() {
EditSession editSession = controller.getEditSessionFactory()
.getEditSession(player.getWorld(),
session.getBlockChangeLimit(), session.getBlockBag(player), player);
editSession.enableQueue();
editSession.enableStandardMode();
editSessions.add(editSession);
return editSession;
}
Expand Down

0 comments on commit 7d4906c

Please sign in to comment.