From 22798f49c8aa99c68cdc6a4d3629fa131137918c Mon Sep 17 00:00:00 2001 From: TomyLobo Date: Wed, 31 Jul 2013 10:35:47 +0200 Subject: [PATCH] Reduced some code duplication in the regions. --- .../worldedit/regions/AbstractRegion.java | 109 ++++++++++++++++++ .../sk89q/worldedit/regions/CuboidRegion.java | 50 -------- .../worldedit/regions/CylinderRegion.java | 47 -------- .../worldedit/regions/EllipsoidRegion.java | 40 ++----- .../worldedit/regions/Polygonal2DRegion.java | 47 -------- 5 files changed, 120 insertions(+), 173 deletions(-) diff --git a/src/main/java/com/sk89q/worldedit/regions/AbstractRegion.java b/src/main/java/com/sk89q/worldedit/regions/AbstractRegion.java index b82e70dc1..90a4e7e59 100644 --- a/src/main/java/com/sk89q/worldedit/regions/AbstractRegion.java +++ b/src/main/java/com/sk89q/worldedit/regions/AbstractRegion.java @@ -20,13 +20,17 @@ package com.sk89q.worldedit.regions; import java.util.ArrayList; +import java.util.HashSet; import java.util.Iterator; import java.util.List; +import java.util.Set; import com.sk89q.worldedit.BlockVector; import com.sk89q.worldedit.BlockVector2D; import com.sk89q.worldedit.LocalWorld; import com.sk89q.worldedit.Vector; +import com.sk89q.worldedit.Vector2D; +import com.sk89q.worldedit.data.ChunkStore; public abstract class AbstractRegion implements Region { /** @@ -91,4 +95,109 @@ public abstract class AbstractRegion implements Region { 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 getChunks() { + final Set chunks = new HashSet(); + + 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 getChunkCubes() { + final Set chunks = new HashSet(); + + 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; + } } diff --git a/src/main/java/com/sk89q/worldedit/regions/CuboidRegion.java b/src/main/java/com/sk89q/worldedit/regions/CuboidRegion.java index 4fd9c92c7..8ba1d5ae6 100644 --- a/src/main/java/com/sk89q/worldedit/regions/CuboidRegion.java +++ b/src/main/java/com/sk89q/worldedit/regions/CuboidRegion.java @@ -95,56 +95,6 @@ public class CuboidRegion extends AbstractRegion implements FlatRegion { 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. * diff --git a/src/main/java/com/sk89q/worldedit/regions/CylinderRegion.java b/src/main/java/com/sk89q/worldedit/regions/CylinderRegion.java index 7f8c3ce73..998c998db 100644 --- a/src/main/java/com/sk89q/worldedit/regions/CylinderRegion.java +++ b/src/main/java/com/sk89q/worldedit/regions/CylinderRegion.java @@ -20,17 +20,13 @@ package com.sk89q.worldedit.regions; import java.util.ArrayList; -import java.util.HashSet; import java.util.Iterator; import java.util.List; -import java.util.Set; - import com.sk89q.worldedit.BlockVector; import com.sk89q.worldedit.BlockVector2D; import com.sk89q.worldedit.LocalWorld; import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.Vector2D; -import com.sk89q.worldedit.data.ChunkStore; /** * Represents a cylindrical region. @@ -314,49 +310,6 @@ public class CylinderRegion extends AbstractRegion implements FlatRegion { return pt.toVector2D().subtract(center).divide(radius).lengthSq() <= 1; } - /** - * Get a list of chunks. - * - * @return - */ - public Set getChunks() { - Set chunks = new HashSet(); - - 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 getChunkCubes() { - Set chunks = new HashSet(); - - 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. diff --git a/src/main/java/com/sk89q/worldedit/regions/EllipsoidRegion.java b/src/main/java/com/sk89q/worldedit/regions/EllipsoidRegion.java index 42e671f9a..55c0688ff 100644 --- a/src/main/java/com/sk89q/worldedit/regions/EllipsoidRegion.java +++ b/src/main/java/com/sk89q/worldedit/regions/EllipsoidRegion.java @@ -213,40 +213,22 @@ public class EllipsoidRegion extends AbstractRegion { * @return */ public Set getChunks() { - Set chunks = new HashSet(); + final Set chunks = new HashSet(); - Vector min = getMinimumPoint(); - Vector max = getMaximumPoint(); + final Vector min = getMinimumPoint(); + final Vector max = getMaximumPoint(); + final int centerY = getCenter().getBlockY(); 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 BlockVector2D(x >> ChunkStore.CHUNK_SHIFTS, - z >> ChunkStore.CHUNK_SHIFTS)); - } + for (int z = min.getBlockZ(); z <= max.getBlockZ(); ++z) { + if (!contains(new BlockVector(x, centerY, z))) { + continue; } - } - } - return chunks; - } - - @Override - public Set getChunkCubes() { - Set chunks = new HashSet(); - - 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)); - } - } + chunks.add(new BlockVector2D( + x >> ChunkStore.CHUNK_SHIFTS, + z >> ChunkStore.CHUNK_SHIFTS + )); } } diff --git a/src/main/java/com/sk89q/worldedit/regions/Polygonal2DRegion.java b/src/main/java/com/sk89q/worldedit/regions/Polygonal2DRegion.java index 204200ddd..0cfbb5d11 100644 --- a/src/main/java/com/sk89q/worldedit/regions/Polygonal2DRegion.java +++ b/src/main/java/com/sk89q/worldedit/regions/Polygonal2DRegion.java @@ -21,16 +21,13 @@ package com.sk89q.worldedit.regions; import java.util.ArrayList; import java.util.Collections; -import java.util.HashSet; import java.util.Iterator; import java.util.List; -import java.util.Set; import com.sk89q.worldedit.BlockVector; import com.sk89q.worldedit.BlockVector2D; import com.sk89q.worldedit.LocalWorld; import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.Vector2D; -import com.sk89q.worldedit.data.ChunkStore; /** * Represents a 2D polygonal region. @@ -409,50 +406,6 @@ public class Polygonal2DRegion extends AbstractRegion implements FlatRegion { return inside; } - /** - * Get a list of chunks. - * - * @return - */ - public Set getChunks() { - Set chunks = new HashSet(); - - 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 getChunkCubes() { - Set chunks = new HashSet(); - - 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. *