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).
This commit is contained in:
aumgn 2012-03-14 11:21:41 +01:00 committed by TomyLobo
parent 1359a7fada
commit 8506bb437e
6 changed files with 111 additions and 96 deletions

View File

@ -259,8 +259,7 @@ public class RegionCommands {
if (args.hasFlag('s')) { if (args.hasFlag('s')) {
try { try {
Region region = session.getSelection(player.getWorld()); Region region = session.getSelection(player.getWorld());
region.expand(dir.multiply(count)); region.shift(dir.multiply(count));
region.contract(dir.multiply(count));
session.getRegionSelector(player.getWorld()).learnChanges(); session.getRegionSelector(player.getWorld()).learnChanges();
session.getRegionSelector(player.getWorld()).explainRegionAdjust(player, session); session.getRegionSelector(player.getWorld()).explainRegionAdjust(player, session);
@ -303,8 +302,7 @@ public class RegionCommands {
final Vector size = region.getMaximumPoint().subtract(region.getMinimumPoint()); final Vector size = region.getMaximumPoint().subtract(region.getMinimumPoint());
final Vector shiftVector = dir.multiply(count * (Math.abs(dir.dot(size))+1)); final Vector shiftVector = dir.multiply(count * (Math.abs(dir.dot(size))+1));
region.expand(shiftVector); region.shift(shiftVector);
region.contract(shiftVector);
session.getRegionSelector(player.getWorld()).learnChanges(); session.getRegionSelector(player.getWorld()).learnChanges();
session.getRegionSelector(player.getWorld()).explainRegionAdjust(player, session); session.getRegionSelector(player.getWorld()).explainRegionAdjust(player, session);

View File

@ -266,6 +266,7 @@ public class SelectionCommands {
max = 3 max = 3
) )
@CommandPermissions("worldedit.selection.expand") @CommandPermissions("worldedit.selection.expand")
@SuppressWarnings("deprecation")
public void expand(CommandContext args, LocalSession session, LocalPlayer player, public void expand(CommandContext args, LocalSession session, LocalPlayer player,
EditSession editSession) throws WorldEditException { EditSession editSession) throws WorldEditException {
@ -278,8 +279,9 @@ public class SelectionCommands {
Region region = session.getSelection(player.getWorld()); Region region = session.getSelection(player.getWorld());
try { try {
int oldSize = region.getArea(); int oldSize = region.getArea();
region.expand(new Vector(0, (player.getWorld().getMaxY() + 1), 0)); region.expand(
region.expand(new Vector(0, -(player.getWorld().getMaxY() + 1), 0)); new Vector(0, (player.getWorld().getMaxY() + 1), 0),
new Vector(0, -(player.getWorld().getMaxY() + 1), 0));
session.getRegionSelector(player.getWorld()).learnChanges(); session.getRegionSelector(player.getWorld()).learnChanges();
int newSize = region.getArea(); int newSize = region.getArea();
session.getRegionSelector(player.getWorld()).explainRegionAdjust(player, session); session.getRegionSelector(player.getWorld()).explainRegionAdjust(player, session);
@ -341,6 +343,7 @@ public class SelectionCommands {
max = 3 max = 3
) )
@CommandPermissions("worldedit.selection.contract") @CommandPermissions("worldedit.selection.contract")
@SuppressWarnings("deprecation")
public void contract(CommandContext args, LocalSession session, LocalPlayer player, public void contract(CommandContext args, LocalSession session, LocalPlayer player,
EditSession editSession) throws WorldEditException { EditSession editSession) throws WorldEditException {

View File

@ -52,16 +52,14 @@ public abstract class AbstractRegion implements Region {
this.world = world; this.world = world;
} }
public void expand(Vector... changes) throws RegionOperationException { @Deprecated
for (Vector change : changes) { public void expand(Vector change) throws RegionOperationException {
expand(change); expand(new Vector[] { change });
}
} }
public void contract(Vector... changes) throws RegionOperationException { @Deprecated
for (Vector change : changes) { public void contract(Vector change) throws RegionOperationException {
contract(change); contract(new Vector[] { change });
}
} }
public void shift(Vector change) throws RegionOperationException { public void shift(Vector change) throws RegionOperationException {

View File

@ -141,46 +141,48 @@ public class CuboidRegion extends AbstractRegion {
* *
* @param change * @param change
*/ */
public void expand(Vector change) { public void expand(Vector... changes) {
if (change.getX() > 0) { for (Vector change : changes) {
if (Math.max(pos1.getX(), pos2.getX()) == pos1.getX()) { if (change.getX() > 0) {
pos1 = pos1.add(new Vector(change.getX(), 0, 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 { } 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 (change.getY() > 0) {
if (Math.max(pos1.getY(), pos2.getY()) == pos1.getY()) { if (Math.max(pos1.getY(), pos2.getY()) == pos1.getY()) {
pos1 = pos1.add(new Vector(0, change.getY(), 0)); pos1 = pos1.add(new Vector(0, change.getY(), 0));
} else {
pos2 = pos2.add(new Vector(0, change.getY(), 0));
}
} else { } 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 (change.getZ() > 0) {
if (Math.max(pos1.getZ(), pos2.getZ()) == pos1.getZ()) { if (Math.max(pos1.getZ(), pos2.getZ()) == pos1.getZ()) {
pos1 = pos1.add(new Vector(0, 0, change.getZ())); pos1 = pos1.add(new Vector(0, 0, change.getZ()));
} else {
pos2 = pos2.add(new Vector(0, 0, change.getZ()));
}
} else { } 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 { } else {
if (Math.min(pos1.getZ(), pos2.getZ()) == pos1.getZ()) { pos2 = pos2.add(new Vector(0, 0, change.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 * @param change
*/ */
public void contract(Vector change) { public void contract(Vector... changes) {
if (change.getX() < 0) { for (Vector change : changes) {
if (Math.max(pos1.getX(), pos2.getX()) == pos1.getX()) { if (change.getX() < 0) {
pos1 = pos1.add(new Vector(change.getX(), 0, 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 { } 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 (change.getY() < 0) {
if (Math.max(pos1.getY(), pos2.getY()) == pos1.getY()) { if (Math.max(pos1.getY(), pos2.getY()) == pos1.getY()) {
pos1 = pos1.add(new Vector(0, change.getY(), 0)); pos1 = pos1.add(new Vector(0, change.getY(), 0));
} else {
pos2 = pos2.add(new Vector(0, change.getY(), 0));
}
} else { } 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 (change.getZ() < 0) {
if (Math.max(pos1.getZ(), pos2.getZ()) == pos1.getZ()) { if (Math.max(pos1.getZ(), pos2.getZ()) == pos1.getZ()) {
pos1 = pos1.add(new Vector(0, 0, change.getZ())); pos1 = pos1.add(new Vector(0, 0, change.getZ()));
} else {
pos2 = pos2.add(new Vector(0, 0, change.getZ()));
}
} else { } 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 { } else {
if (Math.min(pos1.getZ(), pos2.getZ()) == pos1.getZ()) { pos2 = pos2.add(new Vector(0, 0, change.getZ()));
pos1 = pos1.add(new Vector(0, 0, change.getZ())); }
} else {
pos2 = pos2.add(new Vector(0, 0, change.getZ()));
} }
} }

View File

@ -269,16 +269,20 @@ public class Polygonal2DRegion extends AbstractRegion {
* *
* @param change * @param change
*/ */
public void expand(Vector change) throws RegionOperationException { public void expand(Vector... changes) throws RegionOperationException {
if (change.getBlockX() != 0 || change.getBlockZ() != 0) { for (Vector change : changes) {
throw new RegionOperationException("Polygons can only be expanded vertically."); if (change.getBlockX() != 0 || change.getBlockZ() != 0) {
throw new RegionOperationException("Polygons can only be expanded vertically.");
}
} }
int changeY = change.getBlockY(); for (Vector change : changes) {
if (changeY > 0) { int changeY = change.getBlockY();
maxY += changeY; if (changeY > 0) {
} else { maxY += changeY;
minY += changeY; } else {
minY += changeY;
}
} }
recalculate(); recalculate();
} }
@ -288,16 +292,20 @@ public class Polygonal2DRegion extends AbstractRegion {
* *
* @param change * @param change
*/ */
public void contract(Vector change) throws RegionOperationException { public void contract(Vector... changes) throws RegionOperationException {
if (change.getBlockX() != 0 || change.getBlockZ() != 0) { for (Vector change : changes) {
throw new RegionOperationException("Polygons can only be contracted vertically."); if (change.getBlockX() != 0 || change.getBlockZ() != 0) {
throw new RegionOperationException("Polygons can only be contracted vertically.");
}
} }
int changeY = change.getBlockY(); for (Vector change : changes) {
if (changeY > 0) { int changeY = change.getBlockY();
minY += changeY; if (changeY > 0) {
} else { minY += changeY;
maxY += changeY; } else {
maxY += changeY;
}
} }
recalculate(); recalculate();
} }

View File

@ -75,9 +75,11 @@ public interface Region extends Iterable<BlockVector> {
/** /**
* Expand the region. * Expand the region.
* *
* @deprecated will be seamlessly replaced by {@link #expand(Vector...)
* @param change * @param change
* @throws RegionOperationException * @throws RegionOperationException
*/ */
@Deprecated
public void expand(Vector change) throws RegionOperationException; public void expand(Vector change) throws RegionOperationException;
/** /**
@ -91,9 +93,11 @@ public interface Region extends Iterable<BlockVector> {
/** /**
* Contract the region. * Contract the region.
* *
* @deprecated will be seamlessly replaced by {@link #contract(Vector...)
* @param change * @param change
* @throws RegionOperationException * @throws RegionOperationException
*/ */
@Deprecated
public void contract(Vector change) throws RegionOperationException; public void contract(Vector change) throws RegionOperationException;
/** /**