From a3f1c71d97abb42e85aceb6ff514ac75f479343a Mon Sep 17 00:00:00 2001 From: Kenzie Togami Date: Sat, 20 Oct 2018 19:50:35 -0700 Subject: [PATCH] Make EditSession closeable for easy flushing --- .../java/com/sk89q/worldedit/EditSession.java | 10 ++++- .../worldedit/command/tool/AreaPickaxe.java | 34 +++++++-------- .../worldedit/command/tool/BlockReplacer.java | 20 ++++----- .../worldedit/command/tool/BrushTool.java | 41 ++++++++++--------- .../command/tool/RecursivePickaxe.java | 22 +++++----- .../worldedit/command/tool/SinglePickaxe.java | 8 +--- 6 files changed, 70 insertions(+), 65 deletions(-) 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 e9c9cf8e4d..aa630bfa1e 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/EditSession.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/EditSession.java @@ -134,7 +134,7 @@ * using the {@link ChangeSetExtent}.

*/ @SuppressWarnings({"FieldCanBeLocal"}) -public class EditSession implements Extent { +public class EditSession implements Extent, AutoCloseable { private static final Logger log = Logger.getLogger(EditSession.class.getCanonicalName()); @@ -636,6 +636,14 @@ public List 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, * and that it should apply them to the world. diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/tool/AreaPickaxe.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/tool/AreaPickaxe.java index 33dcb2d81a..1af33599ca 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/tool/AreaPickaxe.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/tool/AreaPickaxe.java @@ -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.flushSession(); - session.remember(editSession); } return true; diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/tool/BlockReplacer.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/tool/BlockReplacer.java index 1ddfae39e1..38619396b5 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/tool/BlockReplacer.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/tool/BlockReplacer.java @@ -52,15 +52,16 @@ 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); - editSession.disableBuffering(); - - 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 { - session.remember(editSession); if (bag != null) { bag.flushChanges(); } @@ -72,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); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/tool/BrushTool.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/tool/BrushTool.java index e1b6c906de..55d2947f75 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/tool/BrushTool.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/tool/BrushTool.java @@ -172,29 +172,30 @@ 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 { - session.remember(editSession); - editSession.flushSession(); if (bag != null) { bag.flushChanges(); } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/tool/RecursivePickaxe.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/tool/RecursivePickaxe.java index 3b35abf426..afeb4728d2 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/tool/RecursivePickaxe.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/tool/RecursivePickaxe.java @@ -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.flushSession(); - 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; diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/tool/SinglePickaxe.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/tool/SinglePickaxe.java index 1ceabcbe2f..fbf1874ce2 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/tool/SinglePickaxe.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/tool/SinglePickaxe.java @@ -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.flushSession(); } world.playEffect(clicked.toVector(), 2001, blockType.getLegacyId());