Add chunk batching flag, enable by default

This commit is contained in:
Kenzie Togami 2018-10-04 10:37:58 -07:00
parent ff391ca0b3
commit 7d4906cfe9
No known key found for this signature in database
GPG Key ID: 5D200B325E157A81
7 changed files with 58 additions and 7 deletions

View File

@ -272,7 +272,7 @@ public class WorldEditPlugin extends JavaPlugin implements TabCompleter {
EditSession editSession = WorldEdit.getInstance().getEditSessionFactory() EditSession editSession = WorldEdit.getInstance().getEditSessionFactory()
.getEditSession(wePlayer.getWorld(), session.getBlockChangeLimit(), blockBag, wePlayer); .getEditSession(wePlayer.getWorld(), session.getBlockChangeLimit(), blockBag, wePlayer);
editSession.enableQueue(); editSession.enableStandardMode();
return editSession; return editSession;
} }

View File

@ -36,6 +36,7 @@ import com.sk89q.worldedit.extent.buffer.ForgetfulExtentBuffer;
import com.sk89q.worldedit.extent.cache.LastAccessExtentCache; import com.sk89q.worldedit.extent.cache.LastAccessExtentCache;
import com.sk89q.worldedit.extent.inventory.BlockBag; import com.sk89q.worldedit.extent.inventory.BlockBag;
import com.sk89q.worldedit.extent.inventory.BlockBagExtent; 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.reorder.MultiStageReorder;
import com.sk89q.worldedit.extent.validation.BlockChangeLimiter; import com.sk89q.worldedit.extent.validation.BlockChangeLimiter;
import com.sk89q.worldedit.extent.validation.DataValidatorExtent; import com.sk89q.worldedit.extent.validation.DataValidatorExtent;
@ -151,6 +152,7 @@ public class EditSession implements Extent {
private @Nullable FastModeExtent fastModeExtent; private @Nullable FastModeExtent fastModeExtent;
private final SurvivalModeExtent survivalExtent; private final SurvivalModeExtent survivalExtent;
private @Nullable ChunkBatchingExtent chunkBatchingExtent;
private @Nullable ChunkLoadingExtent chunkLoadingExtent; private @Nullable ChunkLoadingExtent chunkLoadingExtent;
private @Nullable LastAccessExtentCache cacheExtent; private @Nullable LastAccessExtentCache cacheExtent;
private @Nullable BlockQuirkExtent quirkExtent; private @Nullable BlockQuirkExtent quirkExtent;
@ -190,6 +192,7 @@ public class EditSession implements Extent {
extent = fastModeExtent = new FastModeExtent(world, false); extent = fastModeExtent = new FastModeExtent(world, false);
extent = survivalExtent = new SurvivalModeExtent(extent, world); extent = survivalExtent = new SurvivalModeExtent(extent, world);
extent = quirkExtent = new BlockQuirkExtent(extent, world); extent = quirkExtent = new BlockQuirkExtent(extent, world);
extent = chunkBatchingExtent = new ChunkBatchingExtent(extent);
extent = chunkLoadingExtent = new ChunkLoadingExtent(extent, world); extent = chunkLoadingExtent = new ChunkLoadingExtent(extent, world);
extent = cacheExtent = new LastAccessExtentCache(extent); extent = cacheExtent = new LastAccessExtentCache(extent);
extent = wrapExtent(extent, eventBus, event, Stage.BEFORE_CHANGE); extent = wrapExtent(extent, eventBus, event, Stage.BEFORE_CHANGE);
@ -229,6 +232,16 @@ public class EditSession implements Extent {
return event.getExtent(); 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. * Get the world.
* *
@ -378,6 +391,23 @@ public class EditSession implements Extent {
return blockBagExtent.popMissing(); 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. * Get the number of blocks changed, including repeated block changes.
* *

View File

@ -226,7 +226,7 @@ public class LocalSession {
EditSession editSession = history.get(historyPointer); EditSession editSession = history.get(historyPointer);
EditSession newEditSession = WorldEdit.getInstance().getEditSessionFactory() EditSession newEditSession = WorldEdit.getInstance().getEditSessionFactory()
.getEditSession(editSession.getWorld(), -1, newBlockBag, player); .getEditSession(editSession.getWorld(), -1, newBlockBag, player);
newEditSession.enableQueue(); newEditSession.enableStandardMode();
newEditSession.setFastMode(fastMode); newEditSession.setFastMode(fastMode);
editSession.undo(newEditSession); editSession.undo(newEditSession);
return editSession; return editSession;
@ -249,7 +249,7 @@ public class LocalSession {
EditSession editSession = history.get(historyPointer); EditSession editSession = history.get(historyPointer);
EditSession newEditSession = WorldEdit.getInstance().getEditSessionFactory() EditSession newEditSession = WorldEdit.getInstance().getEditSessionFactory()
.getEditSession(editSession.getWorld(), -1, newBlockBag, player); .getEditSession(editSession.getWorld(), -1, newBlockBag, player);
newEditSession.enableQueue(); newEditSession.enableStandardMode();
newEditSession.setFastMode(fastMode); newEditSession.setFastMode(fastMode);
editSession.redo(newEditSession); editSession.redo(newEditSession);
++historyPointer; ++historyPointer;

View File

@ -72,7 +72,7 @@ public class SelectionCommand extends SimpleCommand<Operation> {
Region selection = session.getSelection(player.getWorld()); Region selection = session.getSelection(player.getWorld());
EditSession editSession = session.createEditSession(player); EditSession editSession = session.createEditSession(player);
editSession.enableQueue(); editSession.enableStandardMode();
locals.put(EditSession.class, editSession); locals.put(EditSession.class, editSession);
session.tellVersion(player); session.tellVersion(player);

View File

@ -54,13 +54,30 @@ public class ChunkBatchingExtent extends AbstractDelegateExtent {
.thenComparing(Vector2D.COMPARING_GRID_ARRANGEMENT); .thenComparing(Vector2D.COMPARING_GRID_ARRANGEMENT);
private final SortedMap<BlockVector2D, LocatedBlockList> batches = new TreeMap<>(REGION_OPTIMIZED_SORT); 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); super(extent);
this.enabled = enabled;
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
} }
@Override @Override
public boolean setBlock(Vector location, BlockStateHolder block) throws WorldEditException { 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); BlockVector2D chunkPos = new BlockVector2D(location.getBlockX() >> 4, location.getBlockZ() >> 4);
batches.computeIfAbsent(chunkPos, k -> new LocatedBlockList()).add(location, block); batches.computeIfAbsent(chunkPos, k -> new LocatedBlockList()).add(location, block);
return true; return true;
@ -68,6 +85,9 @@ public class ChunkBatchingExtent extends AbstractDelegateExtent {
@Override @Override
protected Operation commitBefore() { protected Operation commitBefore() {
if (!enabled) {
return null;
}
return new Operation() { return new Operation() {
private final Iterator<LocatedBlockList> batchIterator = batches.values().iterator(); private final Iterator<LocatedBlockList> batchIterator = batches.values().iterator();
@ -78,6 +98,7 @@ public class ChunkBatchingExtent extends AbstractDelegateExtent {
return null; return null;
} }
new SetLocatedBlocks(getExtent(), batchIterator.next()).resume(run); new SetLocatedBlocks(getExtent(), batchIterator.next()).resume(run);
batchIterator.remove();
return this; return this;
} }

View File

@ -103,7 +103,7 @@ public class WorldEditBinding extends BindingHelper {
Player sender = getPlayer(context); Player sender = getPlayer(context);
LocalSession session = worldEdit.getSessionManager().get(sender); LocalSession session = worldEdit.getSessionManager().get(sender);
EditSession editSession = session.createEditSession(sender); EditSession editSession = session.createEditSession(sender);
editSession.enableQueue(); editSession.enableStandardMode();
context.getContext().getLocals().put(EditSession.class, editSession); context.getContext().getLocals().put(EditSession.class, editSession);
session.tellVersion(sender); session.tellVersion(sender);
return editSession; return editSession;

View File

@ -65,7 +65,7 @@ public class CraftScriptContext extends CraftScriptEnvironment {
EditSession editSession = controller.getEditSessionFactory() EditSession editSession = controller.getEditSessionFactory()
.getEditSession(player.getWorld(), .getEditSession(player.getWorld(),
session.getBlockChangeLimit(), session.getBlockBag(player), player); session.getBlockChangeLimit(), session.getBlockBag(player), player);
editSession.enableQueue(); editSession.enableStandardMode();
editSessions.add(editSession); editSessions.add(editSession);
return editSession; return editSession;
} }