From d3f3489c7eb47bcfeb258986df89e338f2d9c278 Mon Sep 17 00:00:00 2001 From: sk89q Date: Fri, 15 Aug 2014 03:48:25 -0700 Subject: [PATCH] Fix child regions not inheriting parent flags. This also fixes a unit test. --- .../protection/ApplicableRegionSet.java | 27 ++++++++++++++++--- .../protection/RegionPriorityTest.java | 16 +++++++---- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/sk89q/worldguard/protection/ApplicableRegionSet.java b/src/main/java/com/sk89q/worldguard/protection/ApplicableRegionSet.java index 878aff2d..50bf78b0 100644 --- a/src/main/java/com/sk89q/worldguard/protection/ApplicableRegionSet.java +++ b/src/main/java/com/sk89q/worldguard/protection/ApplicableRegionSet.java @@ -251,8 +251,7 @@ private boolean internalGetState(StateFlag flag, @Nullable LocalPlayer player, @ lastPriority = region.getPriority(); // Ignore non-build regions - if (player != null - && region.getFlag(DefaultFlag.PASSTHROUGH) == State.ALLOW) { + if (player != null && getStateFlagIncludingParents(region, DefaultFlag.PASSTHROUGH) == State.ALLOW) { continue; } @@ -267,7 +266,7 @@ private boolean internalGetState(StateFlag flag, @Nullable LocalPlayer player, @ } } - State v = region.getFlag(flag); + State v = getStateFlagIncludingParents(region, flag); // Allow DENY to override everything if (v == State.DENY) { @@ -327,6 +326,28 @@ private void clearParents(Set needsClear, Set } } + /** + * Get a region's state flag, checking parent regions until a value for the + * flag can be found (if one even exists). + * + * @param region the region + * @param flag the flag + * @return the value + */ + private static State getStateFlagIncludingParents(ProtectedRegion region, StateFlag flag) { + while (region != null) { + State value = region.getFlag(flag); + + if (value != null) { + return value; + } + + region = region.getParent(); + } + + return null; + } + /** * Gets the value of a flag. Do not use this for state flags * (use {@link #allows(StateFlag, LocalPlayer)} for that). diff --git a/src/test/java/com/sk89q/worldguard/protection/RegionPriorityTest.java b/src/test/java/com/sk89q/worldguard/protection/RegionPriorityTest.java index 6b7be7bc..e08ca413 100644 --- a/src/test/java/com/sk89q/worldguard/protection/RegionPriorityTest.java +++ b/src/test/java/com/sk89q/worldguard/protection/RegionPriorityTest.java @@ -19,8 +19,6 @@ package com.sk89q.worldguard.protection; -import java.util.ArrayList; -import org.junit.Before; import com.sk89q.worldedit.BlockVector; import com.sk89q.worldedit.BlockVector2D; import com.sk89q.worldedit.Vector; @@ -29,9 +27,17 @@ import com.sk89q.worldguard.protection.flags.DefaultFlag; import com.sk89q.worldguard.protection.flags.StateFlag; import com.sk89q.worldguard.protection.managers.RegionManager; -import com.sk89q.worldguard.protection.regions.*; +import com.sk89q.worldguard.protection.regions.GlobalProtectedRegion; +import com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion; +import com.sk89q.worldguard.protection.regions.ProtectedPolygonalRegion; +import com.sk89q.worldguard.protection.regions.ProtectedRegion; +import org.junit.Before; import org.junit.Test; -import static org.junit.Assert.*; + +import java.util.ArrayList; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; public abstract class RegionPriorityTest { static String COURTYARD_ID = "courtyard"; @@ -143,7 +149,7 @@ public void testPriorities() throws Exception { public void testPriorities2() throws Exception { ApplicableRegionSet appl; - fountain.setPriority(0); + courtyard.setPriority(0); fountain.setPriority(5); appl = manager.getApplicableRegions(inCourtyard);