Skip to content

Commit

Permalink
Add clipboard to //brush deform
Browse files Browse the repository at this point in the history
  • Loading branch information
TomyLobo committed Jul 25, 2024
1 parent 5561523 commit adbee4e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
10 changes: 10 additions & 0 deletions worldedit-core/src/main/java/com/sk89q/worldedit/LocalSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -451,6 +452,15 @@ public ClipboardHolder getClipboard() throws EmptyClipboardException {
return clipboard;
}

/**
* Gets the clipboard.
*
* @return clipboard
*/
public Optional<ClipboardHolder> getClipboardOptional() {
return Optional.ofNullable(clipboard);
}

/**
* Sets the clipboard.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -506,14 +506,17 @@ 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);
} else if (usePlacement) {
deform.setMode(Deform.Mode.OFFSET);
deform.setOffset(localSession.getPlacementPosition(player).toVector3());
}
deform.setUseClipboard(useClipboard);
setOperationBasedBrush(player, localSession, radius,
deform, shape, "worldedit.brush.deform");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -53,6 +57,7 @@ public class Deform implements Contextual<Operation> {
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);
Expand Down Expand Up @@ -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();
Expand All @@ -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<Clipboard> 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(
Expand Down

0 comments on commit adbee4e

Please sign in to comment.