Skip to content

Commit

Permalink
Add clipboard support to //deform
Browse files Browse the repository at this point in the history
  • Loading branch information
TomyLobo committed Jul 14, 2024
1 parent 2fb41d3 commit 52e6fb2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.extent.InputExtent;
import com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard;
import com.sk89q.worldedit.extent.clipboard.Clipboard;
import com.sk89q.worldedit.function.GroundFunction;
import com.sk89q.worldedit.function.RegionFunction;
import com.sk89q.worldedit.function.block.BlockReplace;
Expand All @@ -50,6 +51,7 @@
import com.sk89q.worldedit.internal.expression.ExpressionException;
import com.sk89q.worldedit.internal.util.TransformUtil;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.math.Vector3;
import com.sk89q.worldedit.math.convolution.GaussianKernel;
import com.sk89q.worldedit.math.convolution.HeightMap;
import com.sk89q.worldedit.math.convolution.HeightMapFilter;
Expand Down Expand Up @@ -499,13 +501,28 @@ public int deform(Actor actor, LocalSession session, EditSession editSession,
@Switch(name = 'o', desc = "Use the placement's coordinate origin")
boolean offsetPlacement,
@Switch(name = 'c', desc = "Use the selection's center as origin")
boolean offsetCenter) throws WorldEditException {
boolean offsetCenter,
@Switch(name = 'l', desc = "Fetch from the clipboard instead of the world")
boolean useClipboard) throws WorldEditException {
final Transform outputTransform = TransformUtil.createTransformForExpressionCommand(actor, session, region, useRawCoords, offsetPlacement, offsetCenter);

final Transform transform = TransformUtil.createTransformForExpressionCommand(actor, session, region, useRawCoords, offsetPlacement, offsetCenter);
final InputExtent inputExtent = editSession.getWorld();
final InputExtent inputExtent;
final Transform inputTransform;
if (useClipboard) {
final Clipboard clipboard = session.getClipboard().getClipboard();
inputExtent = clipboard;

final Vector3 clipboardMin = clipboard.getMinimumPoint().toVector3();
final Vector3 clipboardMax = clipboard.getMaximumPoint().toVector3();

inputTransform = TransformUtil.createTransformForExpressionCommand(useRawCoords, offsetPlacement, offsetCenter, clipboardMin, clipboardMax, clipboardMin);
} else {
inputExtent = editSession.getWorld();
inputTransform = outputTransform;
}

try {
final int affected = editSession.deformRegion(region, transform, String.join(" ", expression), session.getTimeout(), inputExtent, transform);
final int affected = editSession.deformRegion(region, outputTransform, String.join(" ", expression), session.getTimeout(), inputExtent, inputTransform);
if (actor instanceof Player) {
((Player) actor).findFreePosition();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,33 @@ private TransformUtil() {
* @return A transform from the expression coordinate system to the raw coordinate system
*/
public static Transform createTransformForExpressionCommand(Actor actor, LocalSession session, Region region, boolean useRawCoords, boolean offsetPlacement, boolean offsetCenter) throws IncompleteRegionException {
final Vector3 placement = session.getPlacementPosition(actor).toVector3();
final Vector3 min = region.getMinimumPoint().toVector3();
final Vector3 max = region.getMaximumPoint().toVector3();

return createTransformForExpressionCommand(useRawCoords, offsetPlacement, offsetCenter, min, max, placement);
}

/**
* Creates a {@link Transform} for the //deform command with clipboard support.
*
* @param useRawCoords Use the game's coordinate origin
* @param offsetPlacement Use the placement's coordinate origin
* @param offsetCenter Use the selection's center as origin
* @param min Minimum of the selection/clipboard
* @param max Maximum of the selection/clipboard
* @param placement Placement position
* @return A transform from the expression coordinate system to the world/clipboard coordinate system
*/
public static Transform createTransformForExpressionCommand(boolean useRawCoords, boolean offsetPlacement, boolean offsetCenter, Vector3 min, Vector3 max, Vector3 placement) {
if (useRawCoords) {
return new Identity();
}

if (offsetPlacement) {
final Vector3 placement = session.getPlacementPosition(actor).toVector3();

return new SimpleTransform(placement, Vector3.ONE);
}

final Vector3 min = region.getMinimumPoint().toVector3();
final Vector3 max = region.getMaximumPoint().toVector3();
final Vector3 center = max.add(min).multiply(0.5);

if (offsetCenter) {
Expand Down

0 comments on commit 52e6fb2

Please sign in to comment.