From 4b15855b2f9f13d3cfbbb1113c7fd66ca9a0c3f6 Mon Sep 17 00:00:00 2001 From: sk89q Date: Wed, 6 Feb 2013 17:19:09 -0800 Subject: [PATCH] Revert "Removed parents from regions." Removal of parents will make upgrading extremely difficult and so consider them pending for the itme being. This reverts commit df54d4de91d0c5bd30d3421010b6a0248b0cf546. --- .../com/sk89q/worldguard/region/Region.java | 41 +++++++++++++++++++ .../sk89q/worldguard/region/RegionTest.java | 22 ++++++++++ 2 files changed, 63 insertions(+) diff --git a/src/main/java/com/sk89q/worldguard/region/Region.java b/src/main/java/com/sk89q/worldguard/region/Region.java index 23d1c0a6..e17cc9bb 100644 --- a/src/main/java/com/sk89q/worldguard/region/Region.java +++ b/src/main/java/com/sk89q/worldguard/region/Region.java @@ -40,6 +40,7 @@ public class Region implements Comparable { private final String id; private IndexableShape shape; private int priority = 0; + private Region parent; private Map attributes = new HashMap(); /** @@ -105,6 +106,46 @@ public int getPriority() { public void setPriority(int priority) { this.priority = priority; } + + /** + * Get the parent of the region. Parents can determine how multiple overlapping + * regions are handled in regards to some flags, but it is dependent on the flag. + * + * @return parent region or null + */ + public Region getParent() { + return parent; + } + + /** + * Set the parent of this region. + * + * @see #getParent() for an explanation of parents + * @param parent the new parent, or null + * @throws IllegalArgumentException when circular inheritance is detected + */ + public synchronized void setParent(Region parent) throws IllegalArgumentException { + if (parent == null) { + this.parent = null; + } else { + if (parent == this) { + throw new IllegalArgumentException( + "Circular region inheritance detected"); + } + + Region p = parent.getParent(); + while (p != null) { + if (p == this) { + throw new IllegalArgumentException( + "Circular region inheritance detected"); + } + + p = p.getParent(); + } + + this.parent = parent; + } + } /** * Set an attribute to this region. diff --git a/src/test/java/com/sk89q/worldguard/region/RegionTest.java b/src/test/java/com/sk89q/worldguard/region/RegionTest.java index 1fa04ff4..8cf0100f 100644 --- a/src/test/java/com/sk89q/worldguard/region/RegionTest.java +++ b/src/test/java/com/sk89q/worldguard/region/RegionTest.java @@ -60,6 +60,28 @@ public void testCompareTo() { assertEquals(regionB.compareTo(regionB), 0); } + @Test + public void testSetParent() { + // Test self setting + Region regionA = new Region("A", mock(IndexableShape.class)); + thrown.expect(IllegalArgumentException.class); + regionA.setParent(regionA); + + // Test regular setting + Region child = new Region("A", mock(IndexableShape.class)); + Region parent = new Region("B", mock(IndexableShape.class)); + child.setParent(parent); + assertEquals(child.getParent(), parent); + assertEquals(parent.getParent(), null); + + // Test circular + child = new Region("A", mock(IndexableShape.class)); + parent = new Region("B", mock(IndexableShape.class)); + child.setParent(parent); + thrown.expect(IllegalArgumentException.class); + parent.setParent(child); + } + @Test public void testGetPriority() { Region regionA = new Region("A", mock(IndexableShape.class));