From 96b0f5f25d31edbed91fe4deacb93e684fe9f4ed Mon Sep 17 00:00:00 2001 From: TomyLobo Date: Sat, 1 Mar 2014 19:46:12 +0100 Subject: [PATCH] Reduced some code duplication in the ProtectedRegion hierarchy and made a public intersectsRegion method. --- .../regions/ProtectedCuboidRegion.java | 30 ++++------------- .../regions/ProtectedPolygonalRegion.java | 25 --------------- .../protection/regions/ProtectedRegion.java | 32 +++++++++++++++++-- 3 files changed, 36 insertions(+), 51 deletions(-) diff --git a/src/main/java/com/sk89q/worldguard/protection/regions/ProtectedCuboidRegion.java b/src/main/java/com/sk89q/worldguard/protection/regions/ProtectedCuboidRegion.java index f194e183..40218351 100644 --- a/src/main/java/com/sk89q/worldguard/protection/regions/ProtectedCuboidRegion.java +++ b/src/main/java/com/sk89q/worldguard/protection/regions/ProtectedCuboidRegion.java @@ -129,30 +129,14 @@ public class ProtectedCuboidRegion extends ProtectedRegion { */ @Override - public List getIntersectingRegions(List regions) throws UnsupportedIntersectionException { - List intersectingRegions = new ArrayList(); - - for (ProtectedRegion region : regions) { - if (!intersectsBoundingBox(region)) continue; - - // If both regions are Cuboids and their bounding boxes intersect, they intersect - if (region instanceof ProtectedCuboidRegion) { - intersectingRegions.add(region); - continue; - } else if (region instanceof ProtectedPolygonalRegion) { - // If either region contains the points of the other, - // or if any edges intersect, the regions intersect - if (containsAny(region.getPoints()) - || region.containsAny(getPoints()) - || intersectsEdges(region)) { - intersectingRegions.add(region); - continue; - } - } else { - throw new UnsupportedOperationException("Not supported yet."); - } + public boolean intersectsRegion(ProtectedRegion region) { + if (region instanceof ProtectedPolygonalRegion) { + return intersectsRegionHelper(region); + } else if (region instanceof ProtectedCuboidRegion) { + return intersectsBoundingBox(region); + } else { + throw new UnsupportedOperationException("Not supported yet."); } - return intersectingRegions; } @Override diff --git a/src/main/java/com/sk89q/worldguard/protection/regions/ProtectedPolygonalRegion.java b/src/main/java/com/sk89q/worldguard/protection/regions/ProtectedPolygonalRegion.java index e4a551ef..eecbae0b 100644 --- a/src/main/java/com/sk89q/worldguard/protection/regions/ProtectedPolygonalRegion.java +++ b/src/main/java/com/sk89q/worldguard/protection/regions/ProtectedPolygonalRegion.java @@ -23,7 +23,6 @@ import java.util.List; import com.sk89q.worldedit.BlockVector2D; import com.sk89q.worldedit.Vector; -import com.sk89q.worldguard.protection.UnsupportedIntersectionException; public class ProtectedPolygonalRegion extends ProtectedRegion { @@ -122,30 +121,6 @@ public class ProtectedPolygonalRegion extends ProtectedRegion { return inside; } - @Override - public List getIntersectingRegions(List regions) throws UnsupportedIntersectionException { - List intersectingRegions = new ArrayList(); - - for (ProtectedRegion region : regions) { - if (!intersectsBoundingBox(region)) continue; - - if (region instanceof ProtectedPolygonalRegion || region instanceof ProtectedCuboidRegion) { - // If either region contains the points of the other, - // or if any edges intersect, the regions intersect - if (containsAny(region.getPoints()) - || region.containsAny(getPoints()) - || intersectsEdges(region)) { - intersectingRegions.add(region); - continue; - } - } else { - throw new UnsupportedOperationException("Not supported yet."); - } - } - return intersectingRegions; - } - - /** * Return the type of region as a user-friendly name. * diff --git a/src/main/java/com/sk89q/worldguard/protection/regions/ProtectedRegion.java b/src/main/java/com/sk89q/worldguard/protection/regions/ProtectedRegion.java index 222382b9..29181947 100644 --- a/src/main/java/com/sk89q/worldguard/protection/regions/ProtectedRegion.java +++ b/src/main/java/com/sk89q/worldguard/protection/regions/ProtectedRegion.java @@ -28,6 +28,7 @@ import com.sk89q.worldguard.protection.UnsupportedIntersectionException; import com.sk89q.worldguard.protection.flags.Flag; import java.awt.geom.Line2D; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -487,6 +488,24 @@ public abstract class ProtectedRegion implements Comparable { return id.compareTo(other.id); } + protected boolean intersectsRegionHelper(ProtectedRegion region) { + if (!intersectsBoundingBox(region)) { + return false; + } + + // If either region contains the points of the other, + // or if any edges intersect, the regions intersect + return containsAny(region.getPoints()) || region.containsAny(getPoints()) || intersectsEdges(region); + } + + public boolean intersectsRegion(ProtectedRegion region) { + if (!(region instanceof ProtectedPolygonalRegion) && (!(region instanceof ProtectedCuboidRegion))) { + throw new UnsupportedOperationException("Not supported yet."); + } + + return intersectsRegionHelper(region); + } + /** * Return the type of region as a user-friendly, lowercase name. * @@ -501,9 +520,16 @@ public abstract class ProtectedRegion implements Comparable { * @return The elements of {@code regions} that intersect with this region * @throws UnsupportedIntersectionException if an invalid intersection is detected */ - public abstract List getIntersectingRegions( - List regions) - throws UnsupportedIntersectionException; + public List getIntersectingRegions(List regions) throws UnsupportedIntersectionException { + List intersectingRegions = new ArrayList(); + + for (ProtectedRegion region : regions) { + if (intersectsRegion(region)) { + intersectingRegions.add(region); + } + } + return intersectingRegions; + } /** * Checks if the bounding box of a region intersects with with the bounding