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 boolean intersectsWith(ProtectedRegion region) throws UnsupportedIntersec
*/
@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 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

View File

@ -23,7 +23,6 @@
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 boolean contains(Vector pt) {
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.
*

View File

@ -28,6 +28,7 @@
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 int compareTo(ProtectedRegion other) {
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 int compareTo(ProtectedRegion other) {
* @return The elements of {@code regions} that intersect with this region
* @throws UnsupportedIntersectionException if an invalid intersection is detected
*/
public abstract List<ProtectedRegion> getIntersectingRegions(
List<ProtectedRegion> regions)
throws UnsupportedIntersectionException;
public List<ProtectedRegion> getIntersectingRegions(List<ProtectedRegion> regions) throws UnsupportedIntersectionException {
List<ProtectedRegion> intersectingRegions = new ArrayList<ProtectedRegion>();
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