From fcbbc72a198f98be2fd96ef3085be43ca339072c Mon Sep 17 00:00:00 2001 From: dordsor21 Date: Mon, 13 Sep 2021 13:19:57 +0100 Subject: [PATCH] Refactor the try-catch for copy/cut commands, switch to `saveDiskClipboard` instead of closing the clipboard on error Address #1291 --- .../worldedit/command/ClipboardCommands.java | 136 +++++++++--------- 1 file changed, 68 insertions(+), 68 deletions(-) diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/ClipboardCommands.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/ClipboardCommands.java index e270d389a..329bbb2b8 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/ClipboardCommands.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/ClipboardCommands.java @@ -140,40 +140,40 @@ public class ClipboardCommands { session.setClipboard(null); Clipboard clipboard = new BlockArrayClipboard(region, actor.getUniqueId()); - try { - clipboard.setOrigin(centerClipboard ? region.getCenter().toBlockPoint() : session.getPlacementPosition(actor)); - ForwardExtentCopy copy = new ForwardExtentCopy(editSession, region, clipboard, region.getMinimumPoint()); - copy.setCopyingEntities(copyEntities); - copy.setCopyingBiomes(copyBiomes); + clipboard.setOrigin(centerClipboard ? region.getCenter().toBlockPoint() : session.getPlacementPosition(actor)); + ForwardExtentCopy copy = new ForwardExtentCopy(editSession, region, clipboard, region.getMinimumPoint()); + copy.setCopyingEntities(copyEntities); + copy.setCopyingBiomes(copyBiomes); - Mask sourceMask = editSession.getSourceMask(); - Region[] regions = editSession.getAllowedRegions(); - Region allowedRegion; - if (regions == null || regions.length == 0) { - allowedRegion = new NullRegion(); - } else { - allowedRegion = new RegionIntersection(regions); - } - final Mask firstSourceMask = mask != null ? mask : sourceMask; - final Mask finalMask = MaskIntersection.of(firstSourceMask, new RegionMask(allowedRegion)).optimize(); - if (finalMask != Masks.alwaysTrue()) { - copy.setSourceMask(finalMask); - } - if (sourceMask != null) { - editSession.setSourceMask(null); - new MaskTraverser(sourceMask).reset(editSession); - editSession.setSourceMask(null); - } - - Operations.completeLegacy(copy); - saveDiskClipboard(clipboard); - session.setClipboard(new ClipboardHolder(clipboard)); - - copy.getStatusMessages().forEach(actor::print); - } catch (Throwable e) { - clipboard.close(); - throw e; + Mask sourceMask = editSession.getSourceMask(); + Region[] regions = editSession.getAllowedRegions(); + Region allowedRegion; + if (regions == null || regions.length == 0) { + allowedRegion = new NullRegion(); + } else { + allowedRegion = new RegionIntersection(regions); } + final Mask firstSourceMask = mask != null ? mask : sourceMask; + final Mask finalMask = MaskIntersection.of(firstSourceMask, new RegionMask(allowedRegion)).optimize(); + if (finalMask != Masks.alwaysTrue()) { + copy.setSourceMask(finalMask); + } + if (sourceMask != null) { + editSession.setSourceMask(null); + new MaskTraverser(sourceMask).reset(editSession); + editSession.setSourceMask(null); + } + + try { + Operations.completeLegacy(copy); + } catch (Throwable e) { + throw e; + } finally { + saveDiskClipboard(clipboard); + } + session.setClipboard(new ClipboardHolder(clipboard)); + + copy.getStatusMessages().forEach(actor::print); //FAWE end } @@ -277,43 +277,43 @@ public class ClipboardCommands { session.setClipboard(null); BlockArrayClipboard clipboard = new BlockArrayClipboard(region, actor.getUniqueId()); - try { - clipboard.setOrigin(session.getPlacementPosition(actor)); - ForwardExtentCopy copy = new ForwardExtentCopy(editSession, region, clipboard, region.getMinimumPoint()); - copy.setSourceFunction(new BlockReplace(editSession, leavePattern)); - copy.setCopyingEntities(copyEntities); - copy.setRemovingEntities(true); - copy.setCopyingBiomes(copyBiomes); - Mask sourceMask = editSession.getSourceMask(); - Region[] regions = editSession.getAllowedRegions(); - Region allowedRegion; - if (regions == null || regions.length == 0) { - allowedRegion = new NullRegion(); - } else { - allowedRegion = new RegionIntersection(regions); - } - final Mask firstSourceMask = mask != null ? mask : sourceMask; - final Mask finalMask = MaskIntersection.of(firstSourceMask, new RegionMask(allowedRegion)).optimize(); - if (finalMask != Masks.alwaysTrue()) { - copy.setSourceMask(finalMask); - } - if (sourceMask != null) { - editSession.setSourceMask(null); - new MaskTraverser(sourceMask).reset(editSession); - editSession.setSourceMask(null); - } - Operations.completeLegacy(copy); - saveDiskClipboard(clipboard); - session.setClipboard(new ClipboardHolder(clipboard)); - - if (!actor.hasPermission("fawe.tips")) { - actor.print(Caption.of("fawe.tips.tip.lazycut")); - } - copy.getStatusMessages().forEach(actor::print); - } catch (Throwable e) { - clipboard.close(); - throw e; + clipboard.setOrigin(session.getPlacementPosition(actor)); + ForwardExtentCopy copy = new ForwardExtentCopy(editSession, region, clipboard, region.getMinimumPoint()); + copy.setSourceFunction(new BlockReplace(editSession, leavePattern)); + copy.setCopyingEntities(copyEntities); + copy.setRemovingEntities(true); + copy.setCopyingBiomes(copyBiomes); + Mask sourceMask = editSession.getSourceMask(); + Region[] regions = editSession.getAllowedRegions(); + Region allowedRegion; + if (regions == null || regions.length == 0) { + allowedRegion = new NullRegion(); + } else { + allowedRegion = new RegionIntersection(regions); } + final Mask firstSourceMask = mask != null ? mask : sourceMask; + final Mask finalMask = MaskIntersection.of(firstSourceMask, new RegionMask(allowedRegion)).optimize(); + if (finalMask != Masks.alwaysTrue()) { + copy.setSourceMask(finalMask); + } + if (sourceMask != null) { + editSession.setSourceMask(null); + new MaskTraverser(sourceMask).reset(editSession); + editSession.setSourceMask(null); + } + try { + Operations.completeLegacy(copy); + } catch (Throwable e) { + throw e; + } finally { + saveDiskClipboard(clipboard); + } + session.setClipboard(new ClipboardHolder(clipboard)); + + if (!actor.hasPermission("fawe.tips")) { + actor.print(Caption.of("fawe.tips.tip.lazycut")); + } + copy.getStatusMessages().forEach(actor::print); //FAWE end }