From df4cd09c2c85fa5b0502c392c9db27478f038cef Mon Sep 17 00:00:00 2001 From: TomyLobo Date: Wed, 17 Jul 2024 08:03:17 +0200 Subject: [PATCH] Add clipboard to //brush deform --- .../com/sk89q/worldedit/LocalSession.java | 10 +++++ .../worldedit/command/BrushCommands.java | 5 ++- .../worldedit/function/factory/Deform.java | 37 +++++++++++++++++-- 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/LocalSession.java b/worldedit-core/src/main/java/com/sk89q/worldedit/LocalSession.java index 7eb1a73837..b89da133c7 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/LocalSession.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/LocalSession.java @@ -69,6 +69,7 @@ import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.TimeZone; import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.Consumer; @@ -451,6 +452,15 @@ public ClipboardHolder getClipboard() throws EmptyClipboardException { return clipboard; } + /** + * Gets the clipboard. + * + * @return clipboard + */ + public Optional getClipboardOptional() { + return Optional.ofNullable(clipboard); + } + /** * Sets the clipboard. * 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 120ba27db8..f2155b3e96 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 @@ -506,7 +506,9 @@ public void deform(Player player, LocalSession localSession, @Switch(name = 'r', desc = "Use the game's coordinate origin") boolean useRawCoords, @Switch(name = 'o', desc = "Use the placement position as the origin") - boolean usePlacement) throws WorldEditException { + boolean usePlacement, + @Switch(name = 'l', desc = "Fetch from the clipboard instead of the world") + boolean useClipboard) throws WorldEditException { Deform deform = new Deform(expression); if (useRawCoords) { deform.setMode(Deform.Mode.RAW_COORD); @@ -514,6 +516,7 @@ public void deform(Player player, LocalSession localSession, deform.setMode(Deform.Mode.OFFSET); deform.setOffset(localSession.getPlacementPosition(player).toVector3()); } + deform.setUseClipboard(useClipboard); setOperationBasedBrush(player, localSession, radius, deform, shape, "worldedit.brush.deform"); } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/function/factory/Deform.java b/worldedit-core/src/main/java/com/sk89q/worldedit/function/factory/Deform.java index 984d3514dd..6136a969b2 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/function/factory/Deform.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/function/factory/Deform.java @@ -27,6 +27,7 @@ import com.sk89q.worldedit.extent.Extent; import com.sk89q.worldedit.extent.InputExtent; import com.sk89q.worldedit.extent.NullExtent; +import com.sk89q.worldedit.extent.clipboard.Clipboard; import com.sk89q.worldedit.function.Contextual; import com.sk89q.worldedit.function.EditContext; import com.sk89q.worldedit.function.operation.Operation; @@ -38,11 +39,14 @@ import com.sk89q.worldedit.math.transform.Transform; import com.sk89q.worldedit.regions.NullRegion; import com.sk89q.worldedit.regions.Region; +import com.sk89q.worldedit.session.ClipboardHolder; import com.sk89q.worldedit.util.formatting.text.Component; import com.sk89q.worldedit.util.formatting.text.TextComponent; import com.sk89q.worldedit.util.formatting.text.TranslatableComponent; import com.sk89q.worldedit.util.formatting.text.format.TextColor; +import java.util.Optional; + import static com.google.common.base.Preconditions.checkNotNull; import static com.sk89q.worldedit.util.GuavaUtil.firstNonNull; @@ -53,6 +57,7 @@ public class Deform implements Contextual { private final Expression expression; private Mode mode; private Vector3 offset = Vector3.ZERO; + private boolean useClipboard; public Deform(String expression) { this(new NullExtent(), new NullRegion(), expression); @@ -114,6 +119,14 @@ public void setOffset(Vector3 offset) { this.offset = offset; } + public boolean useClipboard() { + return useClipboard; + } + + public void setUseClipboard(boolean useClipboard) { + this.useClipboard = useClipboard; + } + @Override public String toString() { return "deformation of " + expression.getSource(); @@ -126,11 +139,29 @@ public Operation createFromContext(final EditContext context) { final Vector3 min = region.getMinimumPoint().toVector3(); final Vector3 max = region.getMaximumPoint().toVector3(); - final Transform transform = TransformUtil.createTransformForExpressionCommand(mode, min, max, offset); LocalSession session = context.getSession(); EditSession editSession = (EditSession) context.getDestination(); - return new DeformOperation(context.getDestination(), region, transform, expression, - session == null ? WorldEdit.getInstance().getConfiguration().calculationTimeout : session.getTimeout(), editSession.getWorld(), transform); + final Optional clipboardOptional = Optional.ofNullable(session) + .flatMap(LocalSession::getClipboardOptional) + .map(ClipboardHolder::getClipboard); + + final Transform targetTransform = TransformUtil.createTransformForExpressionCommand(mode, min, max, offset); + final InputExtent sourceExtent; + final Transform sourceTransform; + if (useClipboard && clipboardOptional.isPresent()) { + final Clipboard clipboard = clipboardOptional.get(); + final Vector3 clipboardMin = clipboard.getMinimumPoint().toVector3(); + final Vector3 clipboardMax = clipboard.getMaximumPoint().toVector3(); + + sourceExtent = clipboard; + sourceTransform = TransformUtil.createTransformForExpressionCommand(mode, clipboardMin, clipboardMax, offset); + } else { + sourceExtent = editSession.getWorld(); + sourceTransform = targetTransform; + } + + return new DeformOperation(context.getDestination(), region, targetTransform, expression, + session == null ? WorldEdit.getInstance().getConfiguration().calculationTimeout : session.getTimeout(), sourceExtent, sourceTransform); } private record DeformOperation(