From 8506bb437e368857aff36f3a929f40f9e0899d8f Mon Sep 17 00:00:00 2001 From: aumgn Date: Wed, 14 Mar 2012 11:21:41 +0100 Subject: [PATCH] Deprecate expand/contract(Vector) in favor of varargs versions This keeps things simpler by not dealing with redundant implementations for single change and multiple changes. This also let regions handle related changes in a more efficient way (for example recalculates region components only when needed). --- .../worldedit/commands/RegionCommands.java | 6 +- .../worldedit/commands/SelectionCommands.java | 7 +- .../worldedit/regions/AbstractRegion.java | 14 +- .../sk89q/worldedit/regions/CuboidRegion.java | 136 +++++++++--------- .../worldedit/regions/Polygonal2DRegion.java | 40 +++--- .../com/sk89q/worldedit/regions/Region.java | 4 + 6 files changed, 111 insertions(+), 96 deletions(-) diff --git a/src/main/java/com/sk89q/worldedit/commands/RegionCommands.java b/src/main/java/com/sk89q/worldedit/commands/RegionCommands.java index ed9666830..b9452e5dc 100644 --- a/src/main/java/com/sk89q/worldedit/commands/RegionCommands.java +++ b/src/main/java/com/sk89q/worldedit/commands/RegionCommands.java @@ -259,8 +259,7 @@ public class RegionCommands { if (args.hasFlag('s')) { try { Region region = session.getSelection(player.getWorld()); - region.expand(dir.multiply(count)); - region.contract(dir.multiply(count)); + region.shift(dir.multiply(count)); session.getRegionSelector(player.getWorld()).learnChanges(); session.getRegionSelector(player.getWorld()).explainRegionAdjust(player, session); @@ -303,8 +302,7 @@ public class RegionCommands { final Vector size = region.getMaximumPoint().subtract(region.getMinimumPoint()); final Vector shiftVector = dir.multiply(count * (Math.abs(dir.dot(size))+1)); - region.expand(shiftVector); - region.contract(shiftVector); + region.shift(shiftVector); session.getRegionSelector(player.getWorld()).learnChanges(); session.getRegionSelector(player.getWorld()).explainRegionAdjust(player, session); diff --git a/src/main/java/com/sk89q/worldedit/commands/SelectionCommands.java b/src/main/java/com/sk89q/worldedit/commands/SelectionCommands.java index 6fd57d66a..03cc81a6e 100644 --- a/src/main/java/com/sk89q/worldedit/commands/SelectionCommands.java +++ b/src/main/java/com/sk89q/worldedit/commands/SelectionCommands.java @@ -266,6 +266,7 @@ public class SelectionCommands { max = 3 ) @CommandPermissions("worldedit.selection.expand") + @SuppressWarnings("deprecation") public void expand(CommandContext args, LocalSession session, LocalPlayer player, EditSession editSession) throws WorldEditException { @@ -278,8 +279,9 @@ public class SelectionCommands { Region region = session.getSelection(player.getWorld()); try { int oldSize = region.getArea(); - region.expand(new Vector(0, (player.getWorld().getMaxY() + 1), 0)); - region.expand(new Vector(0, -(player.getWorld().getMaxY() + 1), 0)); + region.expand( + new Vector(0, (player.getWorld().getMaxY() + 1), 0), + new Vector(0, -(player.getWorld().getMaxY() + 1), 0)); session.getRegionSelector(player.getWorld()).learnChanges(); int newSize = region.getArea(); session.getRegionSelector(player.getWorld()).explainRegionAdjust(player, session); @@ -341,6 +343,7 @@ public class SelectionCommands { max = 3 ) @CommandPermissions("worldedit.selection.contract") + @SuppressWarnings("deprecation") public void contract(CommandContext args, LocalSession session, LocalPlayer player, EditSession editSession) throws WorldEditException { diff --git a/src/main/java/com/sk89q/worldedit/regions/AbstractRegion.java b/src/main/java/com/sk89q/worldedit/regions/AbstractRegion.java index 37054d3fd..8896f9833 100644 --- a/src/main/java/com/sk89q/worldedit/regions/AbstractRegion.java +++ b/src/main/java/com/sk89q/worldedit/regions/AbstractRegion.java @@ -52,16 +52,14 @@ public abstract class AbstractRegion implements Region { this.world = world; } - public void expand(Vector... changes) throws RegionOperationException { - for (Vector change : changes) { - expand(change); - } + @Deprecated + public void expand(Vector change) throws RegionOperationException { + expand(new Vector[] { change }); } - public void contract(Vector... changes) throws RegionOperationException { - for (Vector change : changes) { - contract(change); - } + @Deprecated + public void contract(Vector change) throws RegionOperationException { + contract(new Vector[] { change }); } public void shift(Vector change) throws RegionOperationException { diff --git a/src/main/java/com/sk89q/worldedit/regions/CuboidRegion.java b/src/main/java/com/sk89q/worldedit/regions/CuboidRegion.java index 58f10a563..1fb70d62f 100644 --- a/src/main/java/com/sk89q/worldedit/regions/CuboidRegion.java +++ b/src/main/java/com/sk89q/worldedit/regions/CuboidRegion.java @@ -141,46 +141,48 @@ public class CuboidRegion extends AbstractRegion { * * @param change */ - public void expand(Vector change) { - if (change.getX() > 0) { - if (Math.max(pos1.getX(), pos2.getX()) == pos1.getX()) { - pos1 = pos1.add(new Vector(change.getX(), 0, 0)); + public void expand(Vector... changes) { + for (Vector change : changes) { + if (change.getX() > 0) { + if (Math.max(pos1.getX(), pos2.getX()) == pos1.getX()) { + pos1 = pos1.add(new Vector(change.getX(), 0, 0)); + } else { + pos2 = pos2.add(new Vector(change.getX(), 0, 0)); + } } else { - pos2 = pos2.add(new Vector(change.getX(), 0, 0)); + if (Math.min(pos1.getX(), pos2.getX()) == pos1.getX()) { + pos1 = pos1.add(new Vector(change.getX(), 0, 0)); + } else { + pos2 = pos2.add(new Vector(change.getX(), 0, 0)); + } } - } else { - if (Math.min(pos1.getX(), pos2.getX()) == pos1.getX()) { - pos1 = pos1.add(new Vector(change.getX(), 0, 0)); - } else { - pos2 = pos2.add(new Vector(change.getX(), 0, 0)); - } - } - if (change.getY() > 0) { - if (Math.max(pos1.getY(), pos2.getY()) == pos1.getY()) { - pos1 = pos1.add(new Vector(0, change.getY(), 0)); + if (change.getY() > 0) { + if (Math.max(pos1.getY(), pos2.getY()) == pos1.getY()) { + pos1 = pos1.add(new Vector(0, change.getY(), 0)); + } else { + pos2 = pos2.add(new Vector(0, change.getY(), 0)); + } } else { - pos2 = pos2.add(new Vector(0, change.getY(), 0)); + if (Math.min(pos1.getY(), pos2.getY()) == pos1.getY()) { + pos1 = pos1.add(new Vector(0, change.getY(), 0)); + } else { + pos2 = pos2.add(new Vector(0, change.getY(), 0)); + } } - } else { - if (Math.min(pos1.getY(), pos2.getY()) == pos1.getY()) { - pos1 = pos1.add(new Vector(0, change.getY(), 0)); - } else { - pos2 = pos2.add(new Vector(0, change.getY(), 0)); - } - } - if (change.getZ() > 0) { - if (Math.max(pos1.getZ(), pos2.getZ()) == pos1.getZ()) { - pos1 = pos1.add(new Vector(0, 0, change.getZ())); + if (change.getZ() > 0) { + if (Math.max(pos1.getZ(), pos2.getZ()) == pos1.getZ()) { + pos1 = pos1.add(new Vector(0, 0, change.getZ())); + } else { + pos2 = pos2.add(new Vector(0, 0, change.getZ())); + } } else { - pos2 = pos2.add(new Vector(0, 0, change.getZ())); - } - } else { - if (Math.min(pos1.getZ(), pos2.getZ()) == pos1.getZ()) { - pos1 = pos1.add(new Vector(0, 0, change.getZ())); - } else { - pos2 = pos2.add(new Vector(0, 0, change.getZ())); + if (Math.min(pos1.getZ(), pos2.getZ()) == pos1.getZ()) { + pos1 = pos1.add(new Vector(0, 0, change.getZ())); + } else { + pos2 = pos2.add(new Vector(0, 0, change.getZ())); + } } } @@ -192,46 +194,48 @@ public class CuboidRegion extends AbstractRegion { * * @param change */ - public void contract(Vector change) { - if (change.getX() < 0) { - if (Math.max(pos1.getX(), pos2.getX()) == pos1.getX()) { - pos1 = pos1.add(new Vector(change.getX(), 0, 0)); + public void contract(Vector... changes) { + for (Vector change : changes) { + if (change.getX() < 0) { + if (Math.max(pos1.getX(), pos2.getX()) == pos1.getX()) { + pos1 = pos1.add(new Vector(change.getX(), 0, 0)); + } else { + pos2 = pos2.add(new Vector(change.getX(), 0, 0)); + } } else { - pos2 = pos2.add(new Vector(change.getX(), 0, 0)); + if (Math.min(pos1.getX(), pos2.getX()) == pos1.getX()) { + pos1 = pos1.add(new Vector(change.getX(), 0, 0)); + } else { + pos2 = pos2.add(new Vector(change.getX(), 0, 0)); + } } - } else { - if (Math.min(pos1.getX(), pos2.getX()) == pos1.getX()) { - pos1 = pos1.add(new Vector(change.getX(), 0, 0)); - } else { - pos2 = pos2.add(new Vector(change.getX(), 0, 0)); - } - } - if (change.getY() < 0) { - if (Math.max(pos1.getY(), pos2.getY()) == pos1.getY()) { - pos1 = pos1.add(new Vector(0, change.getY(), 0)); + if (change.getY() < 0) { + if (Math.max(pos1.getY(), pos2.getY()) == pos1.getY()) { + pos1 = pos1.add(new Vector(0, change.getY(), 0)); + } else { + pos2 = pos2.add(new Vector(0, change.getY(), 0)); + } } else { - pos2 = pos2.add(new Vector(0, change.getY(), 0)); + if (Math.min(pos1.getY(), pos2.getY()) == pos1.getY()) { + pos1 = pos1.add(new Vector(0, change.getY(), 0)); + } else { + pos2 = pos2.add(new Vector(0, change.getY(), 0)); + } } - } else { - if (Math.min(pos1.getY(), pos2.getY()) == pos1.getY()) { - pos1 = pos1.add(new Vector(0, change.getY(), 0)); - } else { - pos2 = pos2.add(new Vector(0, change.getY(), 0)); - } - } - if (change.getZ() < 0) { - if (Math.max(pos1.getZ(), pos2.getZ()) == pos1.getZ()) { - pos1 = pos1.add(new Vector(0, 0, change.getZ())); + if (change.getZ() < 0) { + if (Math.max(pos1.getZ(), pos2.getZ()) == pos1.getZ()) { + pos1 = pos1.add(new Vector(0, 0, change.getZ())); + } else { + pos2 = pos2.add(new Vector(0, 0, change.getZ())); + } } else { - pos2 = pos2.add(new Vector(0, 0, change.getZ())); - } - } else { - if (Math.min(pos1.getZ(), pos2.getZ()) == pos1.getZ()) { - pos1 = pos1.add(new Vector(0, 0, change.getZ())); - } else { - pos2 = pos2.add(new Vector(0, 0, change.getZ())); + if (Math.min(pos1.getZ(), pos2.getZ()) == pos1.getZ()) { + pos1 = pos1.add(new Vector(0, 0, change.getZ())); + } else { + pos2 = pos2.add(new Vector(0, 0, change.getZ())); + } } } diff --git a/src/main/java/com/sk89q/worldedit/regions/Polygonal2DRegion.java b/src/main/java/com/sk89q/worldedit/regions/Polygonal2DRegion.java index d9f49cd8d..41cfbc222 100644 --- a/src/main/java/com/sk89q/worldedit/regions/Polygonal2DRegion.java +++ b/src/main/java/com/sk89q/worldedit/regions/Polygonal2DRegion.java @@ -269,16 +269,20 @@ public class Polygonal2DRegion extends AbstractRegion { * * @param change */ - public void expand(Vector change) throws RegionOperationException { - if (change.getBlockX() != 0 || change.getBlockZ() != 0) { - throw new RegionOperationException("Polygons can only be expanded vertically."); + public void expand(Vector... changes) throws RegionOperationException { + for (Vector change : changes) { + if (change.getBlockX() != 0 || change.getBlockZ() != 0) { + throw new RegionOperationException("Polygons can only be expanded vertically."); + } } - int changeY = change.getBlockY(); - if (changeY > 0) { - maxY += changeY; - } else { - minY += changeY; + for (Vector change : changes) { + int changeY = change.getBlockY(); + if (changeY > 0) { + maxY += changeY; + } else { + minY += changeY; + } } recalculate(); } @@ -288,16 +292,20 @@ public class Polygonal2DRegion extends AbstractRegion { * * @param change */ - public void contract(Vector change) throws RegionOperationException { - if (change.getBlockX() != 0 || change.getBlockZ() != 0) { - throw new RegionOperationException("Polygons can only be contracted vertically."); + public void contract(Vector... changes) throws RegionOperationException { + for (Vector change : changes) { + if (change.getBlockX() != 0 || change.getBlockZ() != 0) { + throw new RegionOperationException("Polygons can only be contracted vertically."); + } } - int changeY = change.getBlockY(); - if (changeY > 0) { - minY += changeY; - } else { - maxY += changeY; + for (Vector change : changes) { + int changeY = change.getBlockY(); + if (changeY > 0) { + minY += changeY; + } else { + maxY += changeY; + } } recalculate(); } diff --git a/src/main/java/com/sk89q/worldedit/regions/Region.java b/src/main/java/com/sk89q/worldedit/regions/Region.java index 9d8aa9739..82d7eff74 100644 --- a/src/main/java/com/sk89q/worldedit/regions/Region.java +++ b/src/main/java/com/sk89q/worldedit/regions/Region.java @@ -75,9 +75,11 @@ public interface Region extends Iterable { /** * Expand the region. * + * @deprecated will be seamlessly replaced by {@link #expand(Vector...) * @param change * @throws RegionOperationException */ + @Deprecated public void expand(Vector change) throws RegionOperationException; /** @@ -91,9 +93,11 @@ public interface Region extends Iterable { /** * Contract the region. * + * @deprecated will be seamlessly replaced by {@link #contract(Vector...) * @param change * @throws RegionOperationException */ + @Deprecated public void contract(Vector change) throws RegionOperationException; /**