Skip to content

Commit

Permalink
Merge pull request #433 from sk89q/bugfix/flushing-when-done
Browse files Browse the repository at this point in the history
Flush or disable buffers in tools
  • Loading branch information
me4502 committed Oct 21, 2018
2 parents dd2fcba + d1312c6 commit 93a6964
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 77 deletions.
Expand Up @@ -285,7 +285,7 @@ public void remember(Player player, EditSession editSession) {
LocalSession session = WorldEdit.getInstance().getSessionManager().get(wePlayer);

session.remember(editSession);
editSession.flushQueue();
editSession.flushSession();

WorldEdit.getInstance().flushBlockBag(wePlayer, editSession);
}
Expand Down
58 changes: 48 additions & 10 deletions worldedit-core/src/main/java/com/sk89q/worldedit/EditSession.java
Expand Up @@ -134,7 +134,7 @@
* using the {@link ChangeSetExtent}.</p>
*/
@SuppressWarnings({"FieldCanBeLocal"})
public class EditSession implements Extent {
public class EditSession implements Extent, AutoCloseable {

private static final Logger log = Logger.getLogger(EditSession.class.getCanonicalName());

Expand Down Expand Up @@ -298,13 +298,13 @@ public void enableQueue() {
}

/**
* Disable the queue. This will flush the queue.
* Disable the queue. This will {@linkplain #flushSession() flush the session}.
*/
public void disableQueue() {
if (isQueueEnabled()) {
flushQueue();
flushSession();
}
reorderExtent.setEnabled(true);
reorderExtent.setEnabled(false);
}

/**
Expand Down Expand Up @@ -393,23 +393,52 @@ public Map<BlockType, Integer> popMissingBlocks() {
return blockBagExtent.popMissing();
}

/**
* Returns chunk batching status.
*
* @return whether chunk batching is enabled
*/
public boolean isBatchingChunks() {
return chunkBatchingExtent != null && chunkBatchingExtent.isEnabled();
}

/**
* Enable or disable chunk batching. Disabling will
* {@linkplain #flushSession() flush the session}.
*
* @param batchingChunks {@code true} to enable, {@code false} to disable
*/
public void setBatchingChunks(boolean batchingChunks) {
if (chunkBatchingExtent == null) {
if (batchingChunks) {
throw new UnsupportedOperationException("Chunk batching not supported by this session.");
}
return;
}
if (!batchingChunks) {
flushQueue();
if (!batchingChunks && isBatchingChunks()) {
flushSession();
}
chunkBatchingExtent.setEnabled(batchingChunks);
}

/**
* Disable all buffering extents.
*
* @see #disableQueue()
* @see #setBatchingChunks(boolean)
*/
public void disableBuffering() {
// We optimize here to avoid repeated calls to flushSession.
boolean needsFlush = isQueueEnabled() || isBatchingChunks();
if (needsFlush) {
flushSession();
}
reorderExtent.setEnabled(false);
if (chunkBatchingExtent != null) {
chunkBatchingExtent.setEnabled(false);
}
}

/**
* Get the number of blocks changed, including repeated block changes.
*
Expand Down Expand Up @@ -569,7 +598,7 @@ public void undo(EditSession editSession) {
UndoContext context = new UndoContext();
context.setExtent(editSession.bypassHistory);
Operations.completeBlindly(ChangeSetExecutor.createUndo(changeSet, context));
editSession.flushQueue();
editSession.flushSession();
}

/**
Expand All @@ -581,7 +610,7 @@ public void redo(EditSession editSession) {
UndoContext context = new UndoContext();
context.setExtent(editSession.bypassHistory);
Operations.completeBlindly(ChangeSetExecutor.createRedo(changeSet, context));
editSession.flushQueue();
editSession.flushSession();
}

/**
Expand Down Expand Up @@ -614,9 +643,18 @@ public List<? extends Entity> getEntities() {
}

/**
* Finish off the queue.
* Closing an EditSession {@linkplain #flushSession() flushes its buffers}.
*/
@Override
public void close() {
flushSession();
}

/**
* Communicate to the EditSession that all block changes are complete,
* and that it should apply them to the world.
*/
public void flushQueue() {
public void flushSession() {
Operations.completeBlindly(commit());
}

Expand Down
Expand Up @@ -628,7 +628,7 @@ public void runScript(Player player, File f, String[] args) throws WorldEditExce
logger.log(Level.WARNING, "Failed to execute script", e);
} finally {
for (EditSession editSession : scriptContext.getEditSessions()) {
editSession.flushQueue();
editSession.flushSession();
session.remember(editSession);
}
}
Expand Down
Expand Up @@ -460,7 +460,7 @@ public void butcher(Actor actor, CommandContext args) throws WorldEditException

if (editSession != null) {
session.remember(editSession);
editSession.flushQueue();
editSession.flushSession();
}
}

Expand Down Expand Up @@ -520,7 +520,7 @@ public void remove(Actor actor, CommandContext args) throws WorldEditException,

if (editSession != null) {
session.remember(editSession);
editSession.flushQueue();
editSession.flushSession();
}
}

Expand Down
Expand Up @@ -62,29 +62,29 @@ public boolean actPrimary(Platform server, LocalConfiguration config, Player pla
return true;
}

EditSession editSession = session.createEditSession(player);
editSession.getSurvivalExtent().setToolUse(config.superPickaxeManyDrop);
try (EditSession editSession = session.createEditSession(player)) {
editSession.getSurvivalExtent().setToolUse(config.superPickaxeManyDrop);

try {
for (int x = ox - range; x <= ox + range; ++x) {
for (int y = oy - range; y <= oy + range; ++y) {
for (int z = oz - range; z <= oz + range; ++z) {
Vector pos = new Vector(x, y, z);
if (editSession.getBlock(pos).getBlockType() != initialType) {
continue;
}
try {
for (int x = ox - range; x <= ox + range; ++x) {
for (int y = oy - range; y <= oy + range; ++y) {
for (int z = oz - range; z <= oz + range; ++z) {
Vector pos = new Vector(x, y, z);
if (editSession.getBlock(pos).getBlockType() != initialType) {
continue;
}

((World) clicked.getExtent()).queueBlockBreakEffect(server, pos, initialType, clicked.toVector().distanceSq(pos));
((World) clicked.getExtent()).queueBlockBreakEffect(server, pos, initialType, clicked.toVector().distanceSq(pos));

editSession.setBlock(pos, BlockTypes.AIR.getDefaultState());
editSession.setBlock(pos, BlockTypes.AIR.getDefaultState());
}
}
}
} catch (MaxChangedBlocksException e) {
player.printError("Max blocks change limit reached.");
} finally {
session.remember(editSession);
}
} catch (MaxChangedBlocksException e) {
player.printError("Max blocks change limit reached.");
} finally {
editSession.flushQueue();
session.remember(editSession);
}

return true;
Expand Down
Expand Up @@ -52,17 +52,19 @@ public boolean canUse(Actor player) {
public boolean actPrimary(Platform server, LocalConfiguration config, Player player, LocalSession session, com.sk89q.worldedit.util.Location clicked) {
BlockBag bag = session.getBlockBag(player);

EditSession editSession = session.createEditSession(player);

try {
Vector position = clicked.toVector();
editSession.setBlock(position, pattern.apply(position));
} catch (MaxChangedBlocksException ignored) {
try (EditSession editSession = session.createEditSession(player)) {
try {
editSession.disableBuffering();
Vector position = clicked.toVector();
editSession.setBlock(position, pattern.apply(position));
} catch (MaxChangedBlocksException ignored) {
} finally {
session.remember(editSession);
}
} finally {
if (bag != null) {
bag.flushChanges();
}
session.remember(editSession);
}

return true;
Expand All @@ -71,8 +73,7 @@ public boolean actPrimary(Platform server, LocalConfiguration config, Player pla

@Override
public boolean actSecondary(Platform server, LocalConfiguration config, Player player, LocalSession session, com.sk89q.worldedit.util.Location clicked) {
EditSession editSession = session.createEditSession(player);
BlockStateHolder targetBlock = editSession.getBlock(clicked.toVector());
BlockStateHolder targetBlock = player.getWorld().getBlock(clicked.toVector());

if (targetBlock != null) {
pattern = new BlockPattern(targetBlock);
Expand Down
Expand Up @@ -172,31 +172,33 @@ public boolean actPrimary(Platform server, LocalConfiguration config, Player pla

BlockBag bag = session.getBlockBag(player);

EditSession editSession = session.createEditSession(player);
Request.request().setEditSession(editSession);
if (mask != null) {
Mask existingMask = editSession.getMask();

if (existingMask == null) {
editSession.setMask(mask);
} else if (existingMask instanceof MaskIntersection) {
((MaskIntersection) existingMask).add(mask);
} else {
MaskIntersection newMask = new MaskIntersection(existingMask);
newMask.add(mask);
editSession.setMask(newMask);
try (EditSession editSession = session.createEditSession(player)) {
Request.request().setEditSession(editSession);
if (mask != null) {
Mask existingMask = editSession.getMask();

if (existingMask == null) {
editSession.setMask(mask);
} else if (existingMask instanceof MaskIntersection) {
((MaskIntersection) existingMask).add(mask);
} else {
MaskIntersection newMask = new MaskIntersection(existingMask);
newMask.add(mask);
editSession.setMask(newMask);
}
}
}

try {
brush.build(editSession, target.toVector(), material, size);
} catch (MaxChangedBlocksException e) {
player.printError("Max blocks change limit reached.");
try {
brush.build(editSession, target.toVector(), material, size);
} catch (MaxChangedBlocksException e) {
player.printError("Max blocks change limit reached.");
} finally {
session.remember(editSession);
}
} finally {
if (bag != null) {
bag.flushChanges();
}
session.remember(editSession);
}

return true;
Expand Down
Expand Up @@ -66,17 +66,17 @@ public boolean actPrimary(Platform server, LocalConfiguration config, Player pla
return true;
}

EditSession editSession = session.createEditSession(player);
editSession.getSurvivalExtent().setToolUse(config.superPickaxeManyDrop);

try {
recurse(server, editSession, world, clicked.toVector().toBlockVector(),
clicked.toVector(), range, initialType, new HashSet<>());
} catch (MaxChangedBlocksException e) {
player.printError("Max blocks change limit reached.");
} finally {
editSession.flushQueue();
session.remember(editSession);
try (EditSession editSession = session.createEditSession(player)) {
editSession.getSurvivalExtent().setToolUse(config.superPickaxeManyDrop);

try {
recurse(server, editSession, world, clicked.toVector().toBlockVector(),
clicked.toVector(), range, initialType, new HashSet<>());
} catch (MaxChangedBlocksException e) {
player.printError("Max blocks change limit reached.");
} finally {
session.remember(editSession);
}
}

return true;
Expand Down
Expand Up @@ -49,15 +49,11 @@ public boolean actPrimary(Platform server, LocalConfiguration config, Player pla
return true;
}

EditSession editSession = session.createEditSession(player);
editSession.getSurvivalExtent().setToolUse(config.superPickaxeDrop);

try {
try (EditSession editSession = session.createEditSession(player)) {
editSession.getSurvivalExtent().setToolUse(config.superPickaxeDrop);
editSession.setBlock(clicked.toVector(), BlockTypes.AIR.getDefaultState());
} catch (MaxChangedBlocksException e) {
player.printError("Max blocks change limit reached.");
} finally {
editSession.flushQueue();
}

world.playEffect(clicked.toVector(), 2001, blockType.getLegacyId());
Expand Down
Expand Up @@ -316,7 +316,7 @@ public void handleCommand(CommandEvent event) {

if (editSession != null) {
session.remember(editSession);
editSession.flushQueue();
editSession.flushSession();

if (config.profile) {
long time = System.currentTimeMillis() - start;
Expand Down

0 comments on commit 93a6964

Please sign in to comment.