Make EditSession closeable for easy flushing

This commit is contained in:
Kenzie Togami 2018-10-20 19:50:35 -07:00
parent 1fa1ff895b
commit a3f1c71d97
No known key found for this signature in database
GPG Key ID: 5D200B325E157A81
6 changed files with 69 additions and 64 deletions

View File

@ -134,7 +134,7 @@ import javax.annotation.Nullable;
* using the {@link ChangeSetExtent}.</p> * using the {@link ChangeSetExtent}.</p>
*/ */
@SuppressWarnings({"FieldCanBeLocal"}) @SuppressWarnings({"FieldCanBeLocal"})
public class EditSession implements Extent { public class EditSession implements Extent, AutoCloseable {
private static final Logger log = Logger.getLogger(EditSession.class.getCanonicalName()); private static final Logger log = Logger.getLogger(EditSession.class.getCanonicalName());
@ -636,6 +636,14 @@ public class EditSession implements Extent {
return bypassNone.getEntities(); return bypassNone.getEntities();
} }
/**
* Closing an EditSession {@linkplain #flushSession() flushes its buffers}.
*/
@Override
public void close() {
flushSession();
}
/** /**
* Communicate to the EditSession that all block changes are complete, * Communicate to the EditSession that all block changes are complete,
* and that it should apply them to the world. * and that it should apply them to the world.

View File

@ -62,29 +62,29 @@ public class AreaPickaxe implements BlockTool {
return true; return true;
} }
EditSession editSession = session.createEditSession(player); try (EditSession editSession = session.createEditSession(player)) {
editSession.getSurvivalExtent().setToolUse(config.superPickaxeManyDrop); editSession.getSurvivalExtent().setToolUse(config.superPickaxeManyDrop);
try { try {
for (int x = ox - range; x <= ox + range; ++x) { for (int x = ox - range; x <= ox + range; ++x) {
for (int y = oy - range; y <= oy + range; ++y) { for (int y = oy - range; y <= oy + range; ++y) {
for (int z = oz - range; z <= oz + range; ++z) { for (int z = oz - range; z <= oz + range; ++z) {
Vector pos = new Vector(x, y, z); Vector pos = new Vector(x, y, z);
if (editSession.getBlock(pos).getBlockType() != initialType) { if (editSession.getBlock(pos).getBlockType() != initialType) {
continue; continue;
}
((World) clicked.getExtent()).queueBlockBreakEffect(server, pos, initialType, clicked.toVector().distanceSq(pos));
editSession.setBlock(pos, BlockTypes.AIR.getDefaultState());
} }
((World) clicked.getExtent()).queueBlockBreakEffect(server, pos, initialType, clicked.toVector().distanceSq(pos));
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.flushSession();
session.remember(editSession);
} }
return true; return true;

View File

@ -52,15 +52,16 @@ public class BlockReplacer implements DoubleActionBlockTool {
public boolean actPrimary(Platform server, LocalConfiguration config, Player player, LocalSession session, com.sk89q.worldedit.util.Location clicked) { public boolean actPrimary(Platform server, LocalConfiguration config, Player player, LocalSession session, com.sk89q.worldedit.util.Location clicked) {
BlockBag bag = session.getBlockBag(player); BlockBag bag = session.getBlockBag(player);
EditSession editSession = session.createEditSession(player); try (EditSession editSession = session.createEditSession(player)) {
editSession.disableBuffering(); try {
editSession.disableBuffering();
try { Vector position = clicked.toVector();
Vector position = clicked.toVector(); editSession.setBlock(position, pattern.apply(position));
editSession.setBlock(position, pattern.apply(position)); } catch (MaxChangedBlocksException ignored) {
} catch (MaxChangedBlocksException ignored) { } finally {
session.remember(editSession);
}
} finally { } finally {
session.remember(editSession);
if (bag != null) { if (bag != null) {
bag.flushChanges(); bag.flushChanges();
} }
@ -72,8 +73,7 @@ public class BlockReplacer implements DoubleActionBlockTool {
@Override @Override
public boolean actSecondary(Platform server, LocalConfiguration config, Player player, LocalSession session, com.sk89q.worldedit.util.Location clicked) { public boolean actSecondary(Platform server, LocalConfiguration config, Player player, LocalSession session, com.sk89q.worldedit.util.Location clicked) {
EditSession editSession = session.createEditSession(player); BlockStateHolder targetBlock = player.getWorld().getBlock(clicked.toVector());
BlockStateHolder targetBlock = editSession.getBlock(clicked.toVector());
if (targetBlock != null) { if (targetBlock != null) {
pattern = new BlockPattern(targetBlock); pattern = new BlockPattern(targetBlock);

View File

@ -172,29 +172,30 @@ public class BrushTool implements TraceTool {
BlockBag bag = session.getBlockBag(player); BlockBag bag = session.getBlockBag(player);
EditSession editSession = session.createEditSession(player); try (EditSession editSession = session.createEditSession(player)) {
Request.request().setEditSession(editSession); Request.request().setEditSession(editSession);
if (mask != null) { if (mask != null) {
Mask existingMask = editSession.getMask(); Mask existingMask = editSession.getMask();
if (existingMask == null) { if (existingMask == null) {
editSession.setMask(mask); editSession.setMask(mask);
} else if (existingMask instanceof MaskIntersection) { } else if (existingMask instanceof MaskIntersection) {
((MaskIntersection) existingMask).add(mask); ((MaskIntersection) existingMask).add(mask);
} else { } else {
MaskIntersection newMask = new MaskIntersection(existingMask); MaskIntersection newMask = new MaskIntersection(existingMask);
newMask.add(mask); newMask.add(mask);
editSession.setMask(newMask); editSession.setMask(newMask);
}
} }
}
try { try {
brush.build(editSession, target.toVector(), material, size); brush.build(editSession, target.toVector(), material, size);
} catch (MaxChangedBlocksException e) { } catch (MaxChangedBlocksException e) {
player.printError("Max blocks change limit reached."); player.printError("Max blocks change limit reached.");
} finally {
session.remember(editSession);
}
} finally { } finally {
session.remember(editSession);
editSession.flushSession();
if (bag != null) { if (bag != null) {
bag.flushChanges(); bag.flushChanges();
} }

View File

@ -66,17 +66,17 @@ public class RecursivePickaxe implements BlockTool {
return true; return true;
} }
EditSession editSession = session.createEditSession(player); try (EditSession editSession = session.createEditSession(player)) {
editSession.getSurvivalExtent().setToolUse(config.superPickaxeManyDrop); editSession.getSurvivalExtent().setToolUse(config.superPickaxeManyDrop);
try { try {
recurse(server, editSession, world, clicked.toVector().toBlockVector(), recurse(server, editSession, world, clicked.toVector().toBlockVector(),
clicked.toVector(), range, initialType, new HashSet<>()); clicked.toVector(), range, initialType, new HashSet<>());
} catch (MaxChangedBlocksException e) { } catch (MaxChangedBlocksException e) {
player.printError("Max blocks change limit reached."); player.printError("Max blocks change limit reached.");
} finally { } finally {
editSession.flushSession(); session.remember(editSession);
session.remember(editSession); }
} }
return true; return true;

View File

@ -49,15 +49,11 @@ public class SinglePickaxe implements BlockTool {
return true; return true;
} }
EditSession editSession = session.createEditSession(player); try (EditSession editSession = session.createEditSession(player)) {
editSession.getSurvivalExtent().setToolUse(config.superPickaxeDrop); editSession.getSurvivalExtent().setToolUse(config.superPickaxeDrop);
try {
editSession.setBlock(clicked.toVector(), BlockTypes.AIR.getDefaultState()); editSession.setBlock(clicked.toVector(), BlockTypes.AIR.getDefaultState());
} catch (MaxChangedBlocksException e) { } catch (MaxChangedBlocksException e) {
player.printError("Max blocks change limit reached."); player.printError("Max blocks change limit reached.");
} finally {
editSession.flushSession();
} }
world.playEffect(clicked.toVector(), 2001, blockType.getLegacyId()); world.playEffect(clicked.toVector(), 2001, blockType.getLegacyId());