feat: add -r (random rotate) flag to schem load and clipboard brush (#2244)

- Implements #1873
This commit is contained in:
Jordan 2023-05-25 18:06:20 +01:00 committed by GitHub
parent 362f6cc6e1
commit 3041ff3e50
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 58 additions and 7 deletions

View File

@ -1259,13 +1259,12 @@ public class BrushCommands {
@Command( @Command(
name = "clipboard", 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" descFooter = "Choose the clipboard brush.\n"
+ "Without the -o flag, the paste will appear centered at the target location. " + "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 " + "With the flag, then the paste will appear relative to where you had "
+ "stood relative to the copied area when you copied it." + "stood relative to the copied area when you copied it."
) )
@Deprecated
@CommandPermissions("worldedit.brush.clipboard") @CommandPermissions("worldedit.brush.clipboard")
public void clipboardBrush( public void clipboardBrush(
Player player, LocalSession session, Player player, LocalSession session,
@ -1279,7 +1278,11 @@ public class BrushCommands {
boolean pasteBiomes, boolean pasteBiomes,
@ArgFlag(name = 'm', desc = "Skip blocks matching this mask in the clipboard") @ArgFlag(name = 'm', desc = "Skip blocks matching this mask in the clipboard")
@ClipboardMask @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 { ) throws WorldEditException {
ClipboardHolder holder = session.getClipboard(); ClipboardHolder holder = session.getClipboard();
@ -1295,9 +1298,9 @@ public class BrushCommands {
set( set(
context, context,
new ClipboardBrush(newHolder, ignoreAir, usingOrigin, pasteEntities, pasteBiomes, //FAWE start - random rotation
sourceMask new ClipboardBrush(newHolder, ignoreAir, usingOrigin, pasteEntities, pasteBiomes, sourceMask, randomRotate),
), //FAWE end
"worldedit.brush.clipboard" "worldedit.brush.clipboard"
); );
} }

View File

@ -45,6 +45,7 @@ import com.sk89q.worldedit.extent.clipboard.io.ClipboardReader;
import com.sk89q.worldedit.extent.clipboard.io.ClipboardWriter; import com.sk89q.worldedit.extent.clipboard.io.ClipboardWriter;
import com.sk89q.worldedit.function.operation.Operations; import com.sk89q.worldedit.function.operation.Operations;
import com.sk89q.worldedit.internal.util.LogManagerCompat; 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.math.transform.Transform;
import com.sk89q.worldedit.session.ClipboardHolder; import com.sk89q.worldedit.session.ClipboardHolder;
import com.sk89q.worldedit.util.formatting.component.ErrorFormat; import com.sk89q.worldedit.util.formatting.component.ErrorFormat;
@ -89,6 +90,7 @@ import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.Function; import java.util.function.Function;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -312,7 +314,11 @@ public class SchematicCommands {
@Arg(desc = "File name.") @Arg(desc = "File name.")
String filename, String filename,
@Arg(desc = "Format name.", def = "fast") @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 { ) throws FilenameException {
LocalConfiguration config = worldEdit.getConfiguration(); LocalConfiguration config = worldEdit.getConfiguration();
@ -394,6 +400,12 @@ public class SchematicCommands {
uri = file.toURI(); uri = file.toURI();
} }
format.hold(actor, uri, in); 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)); actor.print(Caption.of("fawe.worldedit.schematic.schematic.loaded", filename));
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
actor.print(Caption.of("worldedit.schematic.unknown-filename", TextComponent.of(filename))); actor.print(Caption.of("worldedit.schematic.unknown-filename", TextComponent.of(filename)));

View File

@ -27,9 +27,13 @@ import com.sk89q.worldedit.function.operation.Operation;
import com.sk89q.worldedit.function.operation.Operations; import com.sk89q.worldedit.function.operation.Operations;
import com.sk89q.worldedit.function.pattern.Pattern; import com.sk89q.worldedit.function.pattern.Pattern;
import com.sk89q.worldedit.math.BlockVector3; 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.regions.Region;
import com.sk89q.worldedit.session.ClipboardHolder; import com.sk89q.worldedit.session.ClipboardHolder;
import java.util.concurrent.ThreadLocalRandom;
public class ClipboardBrush implements Brush { public class ClipboardBrush implements Brush {
private final ClipboardHolder holder; private final ClipboardHolder holder;
@ -38,6 +42,9 @@ public class ClipboardBrush implements Brush {
private final boolean pasteEntities; private final boolean pasteEntities;
private final boolean pasteBiomes; private final boolean pasteBiomes;
private final Mask sourceMask; private final Mask sourceMask;
//FAWE start - random rotation
private final boolean randomRotate;
//FAWE end
public ClipboardBrush(ClipboardHolder holder, boolean ignoreAirBlocks, boolean usingOrigin) { public ClipboardBrush(ClipboardHolder holder, boolean ignoreAirBlocks, boolean usingOrigin) {
this.holder = holder; this.holder = holder;
@ -46,23 +53,48 @@ public class ClipboardBrush implements Brush {
this.pasteBiomes = false; this.pasteBiomes = false;
this.pasteEntities = false; this.pasteEntities = false;
this.sourceMask = null; this.sourceMask = null;
//FAWE start - random rotation
this.randomRotate = false;
//FAWE end
} }
public ClipboardBrush( public ClipboardBrush(
ClipboardHolder holder, boolean ignoreAirBlocks, boolean usingOrigin, boolean pasteEntities, ClipboardHolder holder, boolean ignoreAirBlocks, boolean usingOrigin, boolean pasteEntities,
boolean pasteBiomes, Mask sourceMask 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.holder = holder;
this.ignoreAirBlocks = ignoreAirBlocks; this.ignoreAirBlocks = ignoreAirBlocks;
this.usingOrigin = usingOrigin; this.usingOrigin = usingOrigin;
this.pasteEntities = pasteEntities; this.pasteEntities = pasteEntities;
this.pasteBiomes = pasteBiomes; this.pasteBiomes = pasteBiomes;
this.sourceMask = sourceMask; this.sourceMask = sourceMask;
this.randomRotate = true;
} }
@Override @Override
public void build(EditSession editSession, BlockVector3 position, Pattern pattern, double size) throws public void build(EditSession editSession, BlockVector3 position, Pattern pattern, double size) throws
MaxChangedBlocksException { 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(); Clipboard clipboard = holder.getClipboard();
Region region = clipboard.getRegion(); Region region = clipboard.getRegion();
BlockVector3 centerOffset = region.getCenter().toBlockPoint().subtract(clipboard.getOrigin()); BlockVector3 centerOffset = region.getCenter().toBlockPoint().subtract(clipboard.getOrigin());
@ -77,6 +109,10 @@ public class ClipboardBrush implements Brush {
.build(); .build();
Operations.completeLegacy(operation); Operations.completeLegacy(operation);
//FAWE start - random rotation
// reset transform
holder.setTransform(originalTransform);
//FAWE end
} }
} }