From 6116518d945b631e09b14c882de24b12b0d93b32 Mon Sep 17 00:00:00 2001 From: TomyLobo Date: Thu, 23 Mar 2023 03:37:59 +0100 Subject: [PATCH] WIP --- .../java/com/sk89q/worldedit/EditSession.java | 10 +++++ .../worldedit/command/BrushCommands.java | 35 +++++++++++++++--- .../worldedit/function/factory/Deform.java | 37 +++++++++++++------ .../sk89q/worldedit/session/Placement.java | 4 ++ 4 files changed, 69 insertions(+), 17 deletions(-) diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/EditSession.java b/worldedit-core/src/main/java/com/sk89q/worldedit/EditSession.java index 1c4699fa22..984cf274bd 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/EditSession.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/EditSession.java @@ -431,6 +431,16 @@ public World getWorld() { return world; } + /** + * Get the actor associated with this EditSession. + * + * @return the actor + */ + @Nullable + public Actor getActor() { + return actor; + } + /** * Get the underlying {@link ChangeSet}. * 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 ace6d22461..79b8557778 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 @@ -61,11 +61,15 @@ import com.sk89q.worldedit.function.operation.Operation; import com.sk89q.worldedit.function.pattern.Pattern; import com.sk89q.worldedit.internal.annotation.ClipboardMask; +import com.sk89q.worldedit.internal.annotation.Selection; import com.sk89q.worldedit.internal.annotation.VertHeight; import com.sk89q.worldedit.math.BlockVector3; +import com.sk89q.worldedit.math.Vector3; +import com.sk89q.worldedit.regions.Region; import com.sk89q.worldedit.regions.factory.CylinderRegionFactory; import com.sk89q.worldedit.regions.factory.RegionFactory; import com.sk89q.worldedit.session.ClipboardHolder; +import com.sk89q.worldedit.session.Placement; import com.sk89q.worldedit.session.request.RequestExtent; import com.sk89q.worldedit.util.HandSide; import com.sk89q.worldedit.util.TreeGenerator; @@ -505,14 +509,35 @@ public void deform(Player player, LocalSession localSession, boolean usePlacement, @Switch(name = 'l', desc = "Fetch from the clipboard instead of the world") boolean useClipboard) throws WorldEditException { - Deform deform = new Deform(expression); + Placement placement = localSession.getPlacement(); + + // bake placement + switch (placement.getPlacementType()) { + case PLAYER: // Shouldn't be baked, because people can just use "//placement here" instead + case WORLD: // Doesn't need to be baked + break; + + case POS1: + case MIN: + case MAX: + placement = placement.bake(localSession.getRegionSelector(player.getWorld()), player); + break; + + default: + throw new IllegalStateException("PlacementType " + placement.getPlacementType() + " not implemented"); + } + + final Deform.Mode mode; if (useRawCoords) { - deform.setMode(Deform.Mode.RAW_COORD); + mode = Deform.Mode.RAW_COORD; } else if (usePlacement) { - deform.setMode(Deform.Mode.OFFSET); - deform.setOffset(localSession.getPlacementPosition(player).toVector3()); + mode = Deform.Mode.OFFSET; + } else { + mode = Deform.Mode.UNIT_CUBE; } - deform.setUseClipboard(useClipboard); + + final Deform deform = new Deform(placement, expression, mode); + 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 3d4a8a602c..8001ef031d 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 @@ -34,12 +34,15 @@ import com.sk89q.worldedit.function.operation.RunContext; import com.sk89q.worldedit.internal.expression.Expression; import com.sk89q.worldedit.internal.expression.ExpressionException; +import com.sk89q.worldedit.math.BlockVector3; import com.sk89q.worldedit.math.Vector3; import com.sk89q.worldedit.math.transform.Identity; import com.sk89q.worldedit.math.transform.SimpleTransform; import com.sk89q.worldedit.math.transform.Transform; import com.sk89q.worldedit.regions.NullRegion; import com.sk89q.worldedit.regions.Region; +import com.sk89q.worldedit.session.Placement; +import com.sk89q.worldedit.session.PlacementType; import com.sk89q.worldedit.util.formatting.text.Component; import com.sk89q.worldedit.util.formatting.text.TextComponent; import com.sk89q.worldedit.util.formatting.text.TranslatableComponent; @@ -54,7 +57,7 @@ public class Deform implements Contextual { private Region region; private final Expression expression; private Mode mode; - private Vector3 offset = Vector3.ZERO; + Placement placement; private boolean useClipboard; public Deform(String expression) { @@ -81,6 +84,17 @@ public Deform(Extent destination, Region region, String expression, Mode mode) { this.mode = mode; } + public Deform(Placement placement, String expression, Mode mode) { + checkNotNull(mode, "mode"); + checkNotNull(expression, "expression"); + + this.placement = placement; + this.expression = Expression.compile(expression, "x", "y", "z"); + this.expression.optimize(); + this.mode = mode; + } + + public Extent getDestination() { return destination; } @@ -108,21 +122,17 @@ public void setMode(Mode mode) { this.mode = mode; } - public boolean getUseClipboard() { - return useClipboard; - } - - public void setUseClipboard(boolean useClipboard) { - this.useClipboard = useClipboard; - } - + @Deprecated public Vector3 getOffset() { - return offset; + if (this.placement.getPlacementType() != PlacementType.WORLD) { + throw new IllegalStateException("Deform.getOffset is deprecated and only supported after using setOffset."); + } + return placement.getOffset().toVector3(); } public void setOffset(Vector3 offset) { checkNotNull(offset, "offset"); - this.offset = offset; + this.placement = new Placement(PlacementType.WORLD, offset.toBlockPoint()); } @Override @@ -176,11 +186,14 @@ public Operation resume(RunContext run) throws WorldEditException { final Vector3 min = region.getMinimumPoint().toVector3(); final Vector3 max = region.getMaximumPoint().toVector3(); - final Transform outputTransform = createTransform(min, max, Deform.this.offset); final LocalSession session = context.getSession(); final EditSession editSession = (EditSession) context.getDestination(); + // TODO: deal with session == null + final BlockVector3 placement = Deform.this.placement.getPlacementPosition(session.getRegionSelector(editSession.getWorld()), editSession.getActor()); + final Transform outputTransform = createTransform(min, max, placement.toVector3()); + final InputExtent inputExtent; final Transform inputTransform; if (Deform.this.useClipboard && session != null) { diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/session/Placement.java b/worldedit-core/src/main/java/com/sk89q/worldedit/session/Placement.java index 0998d192a0..334decaed9 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/session/Placement.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/session/Placement.java @@ -65,4 +65,8 @@ public Component getInfo() { ); } } + + public Placement bake(RegionSelector selector, Actor actor) throws IncompleteRegionException { + return new Placement(PlacementType.WORLD, getPlacementPosition(selector, actor)); + } }