Reduced some code duplication in the regions.

This commit is contained in:
TomyLobo 2013-07-31 10:35:47 +02:00 committed by wizjany
parent c838ef7b25
commit 22798f49c8
5 changed files with 120 additions and 173 deletions

View File

@ -20,13 +20,17 @@
package com.sk89q.worldedit.regions; package com.sk89q.worldedit.regions;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Set;
import com.sk89q.worldedit.BlockVector; import com.sk89q.worldedit.BlockVector;
import com.sk89q.worldedit.BlockVector2D; import com.sk89q.worldedit.BlockVector2D;
import com.sk89q.worldedit.LocalWorld; import com.sk89q.worldedit.LocalWorld;
import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.Vector2D;
import com.sk89q.worldedit.data.ChunkStore;
public abstract class AbstractRegion implements Region { public abstract class AbstractRegion implements Region {
/** /**
@ -91,4 +95,109 @@ public abstract class AbstractRegion implements Region {
return points; return points;
} }
/**
* Get the number of blocks in the region.
*
* @return number of blocks
*/
public int getArea() {
Vector min = getMinimumPoint();
Vector max = getMaximumPoint();
return (int)((max.getX() - min.getX() + 1) *
(max.getY() - min.getY() + 1) *
(max.getZ() - min.getZ() + 1));
}
/**
* Get X-size.
*
* @return width
*/
public int getWidth() {
Vector min = getMinimumPoint();
Vector max = getMaximumPoint();
return (int) (max.getX() - min.getX() + 1);
}
/**
* Get Y-size.
*
* @return height
*/
public int getHeight() {
Vector min = getMinimumPoint();
Vector max = getMaximumPoint();
return (int) (max.getY() - min.getY() + 1);
}
/**
* Get Z-size.
*
* @return length
*/
public int getLength() {
Vector min = getMinimumPoint();
Vector max = getMaximumPoint();
return (int) (max.getZ() - min.getZ() + 1);
}
/**
* Get a list of chunks.
*
* @return
*/
public Set<Vector2D> getChunks() {
final Set<Vector2D> chunks = new HashSet<Vector2D>();
final Vector min = getMinimumPoint();
final Vector max = getMaximumPoint();
final int minY = min.getBlockY();
for (int x = min.getBlockX(); x <= max.getBlockX(); ++x) {
for (int z = min.getBlockZ(); z <= max.getBlockZ(); ++z) {
if (!contains(new Vector(x, minY, z))) {
continue;
}
chunks.add(new BlockVector2D(
x >> ChunkStore.CHUNK_SHIFTS,
z >> ChunkStore.CHUNK_SHIFTS
));
}
}
return chunks;
}
@Override
public Set<Vector> getChunkCubes() {
final Set<Vector> chunks = new HashSet<Vector>();
final Vector min = getMinimumPoint();
final Vector max = getMaximumPoint();
for (int x = min.getBlockX(); x <= max.getBlockX(); ++x) {
for (int y = min.getBlockY(); y <= max.getBlockY(); ++y) {
for (int z = min.getBlockZ(); z <= max.getBlockZ(); ++z) {
if (!contains(new Vector(x, y, z))) {
continue;
}
chunks.add(new BlockVector(
x >> ChunkStore.CHUNK_SHIFTS,
y >> ChunkStore.CHUNK_SHIFTS,
z >> ChunkStore.CHUNK_SHIFTS
));
}
}
}
return chunks;
}
} }

View File

@ -95,56 +95,6 @@ public class CuboidRegion extends AbstractRegion implements FlatRegion {
return Math.max(pos1.getBlockY(), pos2.getBlockY()); return Math.max(pos1.getBlockY(), pos2.getBlockY());
} }
/**
* Get the number of blocks in the region.
*
* @return number of blocks
*/
public int getArea() {
Vector min = getMinimumPoint();
Vector max = getMaximumPoint();
return (int)((max.getX() - min.getX() + 1) *
(max.getY() - min.getY() + 1) *
(max.getZ() - min.getZ() + 1));
}
/**
* Get X-size.
*
* @return width
*/
public int getWidth() {
Vector min = getMinimumPoint();
Vector max = getMaximumPoint();
return (int) (max.getX() - min.getX() + 1);
}
/**
* Get Y-size.
*
* @return height
*/
public int getHeight() {
Vector min = getMinimumPoint();
Vector max = getMaximumPoint();
return (int) (max.getY() - min.getY() + 1);
}
/**
* Get Z-size.
*
* @return length
*/
public int getLength() {
Vector min = getMinimumPoint();
Vector max = getMaximumPoint();
return (int) (max.getZ() - min.getZ() + 1);
}
/** /**
* Expands the cuboid in a direction. * Expands the cuboid in a direction.
* *

View File

@ -20,17 +20,13 @@
package com.sk89q.worldedit.regions; package com.sk89q.worldedit.regions;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Set;
import com.sk89q.worldedit.BlockVector; import com.sk89q.worldedit.BlockVector;
import com.sk89q.worldedit.BlockVector2D; import com.sk89q.worldedit.BlockVector2D;
import com.sk89q.worldedit.LocalWorld; import com.sk89q.worldedit.LocalWorld;
import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.Vector2D; import com.sk89q.worldedit.Vector2D;
import com.sk89q.worldedit.data.ChunkStore;
/** /**
* Represents a cylindrical region. * Represents a cylindrical region.
@ -314,49 +310,6 @@ public class CylinderRegion extends AbstractRegion implements FlatRegion {
return pt.toVector2D().subtract(center).divide(radius).lengthSq() <= 1; return pt.toVector2D().subtract(center).divide(radius).lengthSq() <= 1;
} }
/**
* Get a list of chunks.
*
* @return
*/
public Set<Vector2D> getChunks() {
Set<Vector2D> chunks = new HashSet<Vector2D>();
Vector min = getMinimumPoint();
Vector max = getMaximumPoint();
for (int x = min.getBlockX(); x <= max.getBlockX(); ++x) {
for (int z = min.getBlockZ(); z <= max.getBlockZ(); ++z) {
if (contains(new BlockVector(x, minY, z))) {
chunks.add(new BlockVector2D(x >> ChunkStore.CHUNK_SHIFTS,
z >> ChunkStore.CHUNK_SHIFTS));
}
}
}
return chunks;
}
@Override
public Set<Vector> getChunkCubes() {
Set<Vector> chunks = new HashSet<Vector>();
Vector min = getMinimumPoint();
Vector max = getMaximumPoint();
for (int x = min.getBlockX(); x <= max.getBlockX(); ++x) {
for (int y = min.getBlockY(); y <= max.getBlockY(); ++y) {
for (int z = min.getBlockZ(); z <= max.getBlockZ(); ++z) {
if (contains(new BlockVector(x, y, z))) {
chunks.add(new BlockVector(x >> ChunkStore.CHUNK_SHIFTS,
y >> ChunkStore.CHUNK_SHIFTS, z >> ChunkStore.CHUNK_SHIFTS));
}
}
}
}
return chunks;
}
/** /**
* Sets the height of the cylinder to fit the specified Y. * Sets the height of the cylinder to fit the specified Y.

View File

@ -213,40 +213,22 @@ public class EllipsoidRegion extends AbstractRegion {
* @return * @return
*/ */
public Set<Vector2D> getChunks() { public Set<Vector2D> getChunks() {
Set<Vector2D> chunks = new HashSet<Vector2D>(); final Set<Vector2D> chunks = new HashSet<Vector2D>();
Vector min = getMinimumPoint(); final Vector min = getMinimumPoint();
Vector max = getMaximumPoint(); final Vector max = getMaximumPoint();
final int centerY = getCenter().getBlockY();
for (int x = min.getBlockX(); x <= max.getBlockX(); ++x) { for (int x = min.getBlockX(); x <= max.getBlockX(); ++x) {
for (int y = min.getBlockY(); y <= max.getBlockY(); ++y) { for (int z = min.getBlockZ(); z <= max.getBlockZ(); ++z) {
for (int z = min.getBlockZ(); z <= max.getBlockZ(); ++z) { if (!contains(new BlockVector(x, centerY, z))) {
if (contains(new BlockVector(x, y, z))) { continue;
chunks.add(new BlockVector2D(x >> ChunkStore.CHUNK_SHIFTS,
z >> ChunkStore.CHUNK_SHIFTS));
}
} }
}
}
return chunks; chunks.add(new BlockVector2D(
} x >> ChunkStore.CHUNK_SHIFTS,
z >> ChunkStore.CHUNK_SHIFTS
@Override ));
public Set<Vector> getChunkCubes() {
Set<Vector> chunks = new HashSet<Vector>();
Vector min = getMinimumPoint();
Vector max = getMaximumPoint();
for (int x = min.getBlockX(); x <= max.getBlockX(); ++x) {
for (int y = min.getBlockY(); y <= max.getBlockY(); ++y) {
for (int z = min.getBlockZ(); z <= max.getBlockZ(); ++z) {
if (contains(new BlockVector(x, y, z))) {
chunks.add(new BlockVector(x >> ChunkStore.CHUNK_SHIFTS,
y >> ChunkStore.CHUNK_SHIFTS, z >> ChunkStore.CHUNK_SHIFTS));
}
}
} }
} }

View File

@ -21,16 +21,13 @@ package com.sk89q.worldedit.regions;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Set;
import com.sk89q.worldedit.BlockVector; import com.sk89q.worldedit.BlockVector;
import com.sk89q.worldedit.BlockVector2D; import com.sk89q.worldedit.BlockVector2D;
import com.sk89q.worldedit.LocalWorld; import com.sk89q.worldedit.LocalWorld;
import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.Vector2D; import com.sk89q.worldedit.Vector2D;
import com.sk89q.worldedit.data.ChunkStore;
/** /**
* Represents a 2D polygonal region. * Represents a 2D polygonal region.
@ -409,50 +406,6 @@ public class Polygonal2DRegion extends AbstractRegion implements FlatRegion {
return inside; return inside;
} }
/**
* Get a list of chunks.
*
* @return
*/
public Set<Vector2D> getChunks() {
Set<Vector2D> chunks = new HashSet<Vector2D>();
Vector min = getMinimumPoint();
Vector max = getMaximumPoint();
for (int x = min.getBlockX(); x <= max.getBlockX(); ++x) {
for (int z = min.getBlockZ(); z <= max.getBlockZ(); ++z) {
if (contains(new BlockVector(x, minY, z))) { // Not the best
chunks.add(new BlockVector2D(x >> ChunkStore.CHUNK_SHIFTS,
z >> ChunkStore.CHUNK_SHIFTS));
}
}
}
return chunks;
}
@Override
public Set<Vector> getChunkCubes() {
Set<Vector> chunks = new HashSet<Vector>();
Vector min = getMinimumPoint();
Vector max = getMaximumPoint();
for (int x = min.getBlockX(); x <= max.getBlockX(); ++x) {
for (int y = min.getBlockY(); y <= max.getBlockY(); ++y) {
for (int z = min.getBlockZ(); z <= max.getBlockZ(); ++z) {
if (contains(new BlockVector(x, y, z))) { // Not the best
chunks.add(new BlockVector(x >> ChunkStore.CHUNK_SHIFTS,
y >> ChunkStore.CHUNK_SHIFTS, z >> ChunkStore.CHUNK_SHIFTS));
}
}
}
}
return chunks;
}
/** /**
* Return the number of points. * Return the number of points.
* *