From 3a828c97599c8891d567f9b13f28363952611c5b Mon Sep 17 00:00:00 2001 From: TomyLobo Date: Tue, 3 Jan 2012 16:13:50 +0100 Subject: [PATCH] Added Region.shift - Provided a default implementation using expand+contract in AbstractRegion - Overrid the implementation in the subtypes --- .../worldedit/commands/SelectionCommands.java | 5 ++--- .../sk89q/worldedit/regions/AbstractRegion.java | 6 ++++++ .../sk89q/worldedit/regions/CuboidRegion.java | 15 +++++++++++++-- .../worldedit/regions/EllipsoidRegion.java | 5 +++++ .../worldedit/regions/Polygonal2DRegion.java | 17 +++++++++++++++++ .../com/sk89q/worldedit/regions/Region.java | 8 ++++++++ 6 files changed, 51 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/sk89q/worldedit/commands/SelectionCommands.java b/src/main/java/com/sk89q/worldedit/commands/SelectionCommands.java index 97bfe5f96..c5b70e2c8 100644 --- a/src/main/java/com/sk89q/worldedit/commands/SelectionCommands.java +++ b/src/main/java/com/sk89q/worldedit/commands/SelectionCommands.java @@ -407,10 +407,9 @@ public class SelectionCommands { try { Region region = session.getSelection(player.getWorld()); - region.expand(dir.multiply(change)); - region.contract(dir.multiply(change)); + region.shift(dir.multiply(change)); session.getRegionSelector(player.getWorld()).learnChanges(); - + session.getRegionSelector(player.getWorld()).explainRegionAdjust(player, session); player.print("Region shifted."); diff --git a/src/main/java/com/sk89q/worldedit/regions/AbstractRegion.java b/src/main/java/com/sk89q/worldedit/regions/AbstractRegion.java index a5216de35..d8f3656c5 100644 --- a/src/main/java/com/sk89q/worldedit/regions/AbstractRegion.java +++ b/src/main/java/com/sk89q/worldedit/regions/AbstractRegion.java @@ -4,6 +4,7 @@ import java.util.Iterator; import com.sk89q.worldedit.BlockVector; import com.sk89q.worldedit.LocalWorld; +import com.sk89q.worldedit.Vector; public abstract class AbstractRegion implements Region { /** @@ -31,4 +32,9 @@ public abstract class AbstractRegion implements Region { public void setWorld(LocalWorld world) { this.world = world; } + + public void shift(Vector change) throws RegionOperationException { + expand(change); + contract(change); + } } diff --git a/src/main/java/com/sk89q/worldedit/regions/CuboidRegion.java b/src/main/java/com/sk89q/worldedit/regions/CuboidRegion.java index 62d612d31..a33ffe8f7 100644 --- a/src/main/java/com/sk89q/worldedit/regions/CuboidRegion.java +++ b/src/main/java/com/sk89q/worldedit/regions/CuboidRegion.java @@ -184,8 +184,7 @@ public class CuboidRegion extends AbstractRegion { } } - pos1 = pos1.clampY(0, world == null ? 127 : world.getMaxY()); - pos2 = pos2.clampY(0, world == null ? 127 : world.getMaxY()); + recalculate(); } /** @@ -236,10 +235,22 @@ public class CuboidRegion extends AbstractRegion { } } + recalculate(); + } + + private void recalculate() { pos1 = pos1.clampY(0, world == null ? 127 : world.getMaxY()); pos2 = pos2.clampY(0, world == null ? 127 : world.getMaxY()); } + @Override + public void shift(Vector change) throws RegionOperationException { + pos1 = pos1.add(change); + pos2 = pos2.add(change); + + recalculate(); + } + /** * Get position 1. * diff --git a/src/main/java/com/sk89q/worldedit/regions/EllipsoidRegion.java b/src/main/java/com/sk89q/worldedit/regions/EllipsoidRegion.java index ed7a4283e..7e8772cb3 100644 --- a/src/main/java/com/sk89q/worldedit/regions/EllipsoidRegion.java +++ b/src/main/java/com/sk89q/worldedit/regions/EllipsoidRegion.java @@ -136,6 +136,11 @@ public class EllipsoidRegion extends AbstractRegion { public void contract(Vector change) { } + @Override + public void shift(Vector change) throws RegionOperationException { + center = center.add(change); + } + /** * Get the center. * diff --git a/src/main/java/com/sk89q/worldedit/regions/Polygonal2DRegion.java b/src/main/java/com/sk89q/worldedit/regions/Polygonal2DRegion.java index a2b6c90fc..bfacf5ab5 100644 --- a/src/main/java/com/sk89q/worldedit/regions/Polygonal2DRegion.java +++ b/src/main/java/com/sk89q/worldedit/regions/Polygonal2DRegion.java @@ -302,6 +302,23 @@ public class Polygonal2DRegion extends AbstractRegion { recalculate(); } + @Override + public void shift(Vector change) throws RegionOperationException { + final double changeX = change.getX(); + final double changeY = change.getY(); + final double changeZ = change.getZ(); + + for (int i = 0; i < points.size(); ++i) { + BlockVector2D point = points.get(i); + points.set(i, new BlockVector2D(point.getX() + changeX, point.getZ() + changeZ)); + } + + minY += changeY; + maxY += changeY; + + recalculate(); + } + /** * Checks to see if a point is inside this region. */ diff --git a/src/main/java/com/sk89q/worldedit/regions/Region.java b/src/main/java/com/sk89q/worldedit/regions/Region.java index d75becee4..8eea7cdd7 100644 --- a/src/main/java/com/sk89q/worldedit/regions/Region.java +++ b/src/main/java/com/sk89q/worldedit/regions/Region.java @@ -88,6 +88,14 @@ public interface Region extends Iterable { */ public void contract(Vector change) throws RegionOperationException; + /** + * Shift the region. + * + * @param change + * @throws RegionOperationException + */ + public void shift(Vector multiply) throws RegionOperationException; + /** * Returns true based on whether the region contains the point, *