Reduced some code duplication in the ProtectedRegion hierarchy and made a public intersectsRegion method.

This commit is contained in:
TomyLobo 2014-03-01 19:46:12 +01:00
parent 4422d536b7
commit 96b0f5f25d
3 changed files with 36 additions and 51 deletions

View File

@ -129,30 +129,14 @@ public class ProtectedCuboidRegion extends ProtectedRegion {
*/ */
@Override @Override
public List<ProtectedRegion> getIntersectingRegions(List<ProtectedRegion> regions) throws UnsupportedIntersectionException { public boolean intersectsRegion(ProtectedRegion region) {
List<ProtectedRegion> intersectingRegions = new ArrayList<ProtectedRegion>(); if (region instanceof ProtectedPolygonalRegion) {
return intersectsRegionHelper(region);
for (ProtectedRegion region : regions) { } else if (region instanceof ProtectedCuboidRegion) {
if (!intersectsBoundingBox(region)) continue; return intersectsBoundingBox(region);
} else {
// If both regions are Cuboids and their bounding boxes intersect, they intersect throw new UnsupportedOperationException("Not supported yet.");
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.");
}
} }
return intersectingRegions;
} }
@Override @Override

View File

@ -23,7 +23,6 @@ import java.util.List;
import com.sk89q.worldedit.BlockVector2D; import com.sk89q.worldedit.BlockVector2D;
import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.Vector;
import com.sk89q.worldguard.protection.UnsupportedIntersectionException;
public class ProtectedPolygonalRegion extends ProtectedRegion { public class ProtectedPolygonalRegion extends ProtectedRegion {
@ -122,30 +121,6 @@ public class ProtectedPolygonalRegion extends ProtectedRegion {
return inside; return inside;
} }
@Override
public List<ProtectedRegion> getIntersectingRegions(List<ProtectedRegion> regions) throws UnsupportedIntersectionException {
List<ProtectedRegion> intersectingRegions = new ArrayList<ProtectedRegion>();
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. * Return the type of region as a user-friendly name.
* *

View File

@ -28,6 +28,7 @@ import com.sk89q.worldguard.protection.UnsupportedIntersectionException;
import com.sk89q.worldguard.protection.flags.Flag; import com.sk89q.worldguard.protection.flags.Flag;
import java.awt.geom.Line2D; import java.awt.geom.Line2D;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -487,6 +488,24 @@ public abstract class ProtectedRegion implements Comparable<ProtectedRegion> {
return id.compareTo(other.id); 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. * Return the type of region as a user-friendly, lowercase name.
* *
@ -501,9 +520,16 @@ public abstract class ProtectedRegion implements Comparable<ProtectedRegion> {
* @return The elements of {@code regions} that intersect with this region * @return The elements of {@code regions} that intersect with this region
* @throws UnsupportedIntersectionException if an invalid intersection is detected * @throws UnsupportedIntersectionException if an invalid intersection is detected
*/ */
public abstract List<ProtectedRegion> getIntersectingRegions( public List<ProtectedRegion> getIntersectingRegions(List<ProtectedRegion> regions) throws UnsupportedIntersectionException {
List<ProtectedRegion> regions) List<ProtectedRegion> intersectingRegions = new ArrayList<ProtectedRegion>();
throws UnsupportedIntersectionException;
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 * Checks if the bounding box of a region intersects with with the bounding