From 3041ff3e50fa420e41be1cb6232445175d3942fc Mon Sep 17 00:00:00 2001 From: Jordan Date: Thu, 25 May 2023 18:06:20 +0100 Subject: [PATCH] feat: add -r (random rotate) flag to schem load and clipboard brush (#2244) - Implements #1873 --- .../worldedit/command/BrushCommands.java | 15 ++++---- .../worldedit/command/SchematicCommands.java | 14 +++++++- .../command/tool/brush/ClipboardBrush.java | 36 +++++++++++++++++++ 3 files changed, 58 insertions(+), 7 deletions(-) diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/BrushCommands.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/BrushCommands.java index 2149951e0..d378decb1 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/BrushCommands.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/BrushCommands.java @@ -1259,13 +1259,12 @@ public class BrushCommands { @Command( name = "clipboard", - desc = "@Deprecated use instead: `/br copypaste`)", + desc = "Paste your clipboard at the brush location. Includes any transforms.", descFooter = "Choose the clipboard brush.\n" + "Without the -o flag, the paste will appear centered at the target location. " + "With the flag, then the paste will appear relative to where you had " + "stood relative to the copied area when you copied it." ) - @Deprecated @CommandPermissions("worldedit.brush.clipboard") public void clipboardBrush( Player player, LocalSession session, @@ -1279,7 +1278,11 @@ public class BrushCommands { boolean pasteBiomes, @ArgFlag(name = 'm', desc = "Skip blocks matching this mask in the clipboard") @ClipboardMask - Mask sourceMask, InjectedValueAccess context + Mask sourceMask, InjectedValueAccess context, + //FAWE start - random rotation + @Switch(name = 'r', desc = "Apply random rotation on paste, combines with existing clipboard transforms") + boolean randomRotate + //FAWE end ) throws WorldEditException { ClipboardHolder holder = session.getClipboard(); @@ -1295,9 +1298,9 @@ public class BrushCommands { set( context, - new ClipboardBrush(newHolder, ignoreAir, usingOrigin, pasteEntities, pasteBiomes, - sourceMask - ), + //FAWE start - random rotation + new ClipboardBrush(newHolder, ignoreAir, usingOrigin, pasteEntities, pasteBiomes, sourceMask, randomRotate), + //FAWE end "worldedit.brush.clipboard" ); } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/SchematicCommands.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/SchematicCommands.java index 20dee10c3..c677734ef 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/SchematicCommands.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/SchematicCommands.java @@ -45,6 +45,7 @@ import com.sk89q.worldedit.extent.clipboard.io.ClipboardReader; import com.sk89q.worldedit.extent.clipboard.io.ClipboardWriter; import com.sk89q.worldedit.function.operation.Operations; import com.sk89q.worldedit.internal.util.LogManagerCompat; +import com.sk89q.worldedit.math.transform.AffineTransform; import com.sk89q.worldedit.math.transform.Transform; import com.sk89q.worldedit.session.ClipboardHolder; import com.sk89q.worldedit.util.formatting.component.ErrorFormat; @@ -89,6 +90,7 @@ import java.util.Map; import java.util.Objects; import java.util.UUID; import java.util.concurrent.Callable; +import java.util.concurrent.ThreadLocalRandom; import java.util.function.Function; import java.util.regex.Pattern; @@ -312,7 +314,11 @@ public class SchematicCommands { @Arg(desc = "File name.") String filename, @Arg(desc = "Format name.", def = "fast") - String formatName + String formatName, + //FAWE start - random rotation + @Switch(name = 'r', desc = "Apply random rotation to the clipboard") + boolean randomRotate + //FAWE end ) throws FilenameException { LocalConfiguration config = worldEdit.getConfiguration(); @@ -394,6 +400,12 @@ public class SchematicCommands { uri = file.toURI(); } format.hold(actor, uri, in); + if (randomRotate) { + AffineTransform transform = new AffineTransform(); + int rotate = 90 * ThreadLocalRandom.current().nextInt(4); + transform = transform.rotateY(rotate); + session.getClipboard().setTransform(transform); + } actor.print(Caption.of("fawe.worldedit.schematic.schematic.loaded", filename)); } catch (IllegalArgumentException e) { actor.print(Caption.of("worldedit.schematic.unknown-filename", TextComponent.of(filename))); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/tool/brush/ClipboardBrush.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/tool/brush/ClipboardBrush.java index aac36df2f..b9c7dc45c 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/tool/brush/ClipboardBrush.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/tool/brush/ClipboardBrush.java @@ -27,9 +27,13 @@ import com.sk89q.worldedit.function.operation.Operation; import com.sk89q.worldedit.function.operation.Operations; import com.sk89q.worldedit.function.pattern.Pattern; import com.sk89q.worldedit.math.BlockVector3; +import com.sk89q.worldedit.math.transform.AffineTransform; +import com.sk89q.worldedit.math.transform.Transform; import com.sk89q.worldedit.regions.Region; import com.sk89q.worldedit.session.ClipboardHolder; +import java.util.concurrent.ThreadLocalRandom; + public class ClipboardBrush implements Brush { private final ClipboardHolder holder; @@ -38,6 +42,9 @@ public class ClipboardBrush implements Brush { private final boolean pasteEntities; private final boolean pasteBiomes; private final Mask sourceMask; + //FAWE start - random rotation + private final boolean randomRotate; + //FAWE end public ClipboardBrush(ClipboardHolder holder, boolean ignoreAirBlocks, boolean usingOrigin) { this.holder = holder; @@ -46,23 +53,48 @@ public class ClipboardBrush implements Brush { this.pasteBiomes = false; this.pasteEntities = false; this.sourceMask = null; + //FAWE start - random rotation + this.randomRotate = false; + //FAWE end } public ClipboardBrush( ClipboardHolder holder, boolean ignoreAirBlocks, boolean usingOrigin, boolean pasteEntities, boolean pasteBiomes, Mask sourceMask ) { + //FAWE start - random rotation + this(holder, ignoreAirBlocks, usingOrigin, pasteEntities, pasteBiomes, sourceMask, false); + } + + public ClipboardBrush( + ClipboardHolder holder, boolean ignoreAirBlocks, boolean usingOrigin, boolean pasteEntities, + boolean pasteBiomes, Mask sourceMask, boolean randomRotate + ) { + //FAWE end this.holder = holder; this.ignoreAirBlocks = ignoreAirBlocks; this.usingOrigin = usingOrigin; this.pasteEntities = pasteEntities; this.pasteBiomes = pasteBiomes; this.sourceMask = sourceMask; + this.randomRotate = true; } @Override public void build(EditSession editSession, BlockVector3 position, Pattern pattern, double size) throws MaxChangedBlocksException { + //FAWE start - random rotation + Transform originalTransform = holder.getTransform(); + Transform transform = new AffineTransform(); + if (this.randomRotate) { + int rotate = 90 * ThreadLocalRandom.current().nextInt(4); + transform = ((AffineTransform) transform).rotateY(rotate); + if (originalTransform != null) { + transform = originalTransform.combine(transform); + } + } + holder.setTransform(transform); + //FAWE end Clipboard clipboard = holder.getClipboard(); Region region = clipboard.getRegion(); BlockVector3 centerOffset = region.getCenter().toBlockPoint().subtract(clipboard.getOrigin()); @@ -77,6 +109,10 @@ public class ClipboardBrush implements Brush { .build(); Operations.completeLegacy(operation); + //FAWE start - random rotation + // reset transform + holder.setTransform(originalTransform); + //FAWE end } }