/** * Create a new region result set. * * <p>The list of regions may be first sorted with * {@link NormativeOrders}. If that is the case, {@code sorted} should be * {@code true}. Otherwise, the list will be sorted in-place.</p> * * @param applicable the regions contained in this set * @param globalRegion the global region, set aside for special handling. * @param sorted true if the list is already sorted with {@link NormativeOrders} */ public RegionResultSet(List<ProtectedRegion> applicable, @Nullable ProtectedRegion globalRegion, boolean sorted) { checkNotNull(applicable); if (!sorted) { NormativeOrders.sort(applicable); } this.applicable = applicable; this.flagValueCalculator = new FlagValueCalculator(applicable, globalRegion); }
for (ProtectedRegion region : getApplicable()) { if (getPriority(region) < minimumPriority) { break; if (getEffectiveFlag(region, Flags.PASSTHROUGH, subject) == State.ALLOW) { continue; minimumPriority = getPriority(region); addParents(ignoredRegions, region); } else { return Result.FAIL;
@Override public <V> Collection<V> queryAllValues(@Nullable RegionAssociable subject, Flag<V> flag) { return flagValueCalculator.queryAllValues(subject, flag); }
@Override @Nullable public <V> V queryValue(@Nullable RegionAssociable subject, Flag<V> flag) { return flagValueCalculator.queryValue(subject, flag); }
@Test public void testGetEffectiveFlagGlobalRegionBuild() throws Exception { MockApplicableRegionSet mock = new MockApplicableRegionSet(); ProtectedRegion global = mock.global(); FlagValueCalculator result = mock.getFlagCalculator(); assertThat(result.getEffectiveFlag(global, Flags.BUILD, null), equalTo(null)); }
@Test public void testGetMembershipWilderness() throws Exception { MockApplicableRegionSet mock = new MockApplicableRegionSet(); LocalPlayer player = mock.createPlayer(); FlagValueCalculator result = mock.getFlagCalculator(); assertThat(result.getMembership(player), is(Result.NO_REGIONS)); }
@Override @Nullable public State queryState(@Nullable RegionAssociable subject, StateFlag... flags) { return flagValueCalculator.queryState(subject, flags); }
@Test public void testGetEffectivePriority() throws Exception { MockApplicableRegionSet mock = new MockApplicableRegionSet(); ProtectedRegion region = mock.add(30); FlagValueCalculator result = mock.getFlagCalculator(); assertThat(result.getPriority(region), is(30)); }
/** * Get the effective value for a list of state flags. The rules of * states is observed here; that is, {@code DENY} overrides {@code ALLOW}, * and {@code ALLOW} overrides {@code NONE}. * * <p>This method is the same as * {@link #queryState(RegionAssociable, StateFlag...)}.</p> * * @param subject an optional subject, which would be used to determine the region group to apply * @param flag a flag to check * @return a state */ @Nullable public State queryState(@Nullable RegionAssociable subject, StateFlag flag) { return queryValue(subject, flag); }
@Test public void testGetEffectiveFlagGlobalRegionBuildDeny() throws Exception { MockApplicableRegionSet mock = new MockApplicableRegionSet(); ProtectedRegion global = mock.global(); global.setFlag(Flags.BUILD, State.DENY); FlagValueCalculator result = mock.getFlagCalculator(); // Cannot let users override BUILD on GLOBAL assertThat(result.getEffectiveFlag(global, Flags.BUILD, null), equalTo(State.DENY)); }
@Test public void testGetMembershipWildernessWithGlobalRegion() throws Exception { MockApplicableRegionSet mock = new MockApplicableRegionSet(); LocalPlayer player = mock.createPlayer(); ProtectedRegion global = mock.global(); FlagValueCalculator result = mock.getFlagCalculator(); assertThat(result.getMembership(player), is(Result.NO_REGIONS)); }
@Test public void testQueryStateWilderness() throws Exception { MockApplicableRegionSet mock = new MockApplicableRegionSet(); StateFlag flag1 = new StateFlag("test1", false); StateFlag flag2 = new StateFlag("test2", true); FlagValueCalculator result = mock.getFlagCalculator(); assertThat(result.queryState(null, flag1), is((State) null)); assertThat(result.queryState(null, flag2), is(State.ALLOW)); }
@Test public void testGetEffectivePriorityGlobalRegion() throws Exception { MockApplicableRegionSet mock = new MockApplicableRegionSet(); ProtectedRegion region = mock.global(); FlagValueCalculator result = mock.getFlagCalculator(); assertThat(result.getPriority(region), is(Integer.MIN_VALUE)); }
@Test public void testQueryValueSingleRegion() throws Exception { MockApplicableRegionSet mock = new MockApplicableRegionSet(); StateFlag flag1 = new StateFlag("test1", false); StateFlag flag2 = new StateFlag("test2", false); ProtectedRegion region = mock.add(0); region.setFlag(flag2, State.DENY); FlagValueCalculator result = mock.getFlagCalculator(); assertThat(result.queryValue(null, flag1), is((State) null)); assertThat(result.queryValue(null, flag2), is(State.DENY)); }
Set<ProtectedRegion> ignoredParents = new HashSet<ProtectedRegion>(); for (ProtectedRegion region : getApplicable()) { if (getPriority(region) < minimumPriority) { break; V value = getEffectiveFlag(region, flag, subject); int priority = getPriority(region); addParents(ignoredParents, region); && getEffectiveFlag(region, Flags.PASSTHROUGH, subject) != State.ALLOW) { minimumPriority = getPriority(region); switch (getMembership(subject)) { case FAIL: return ImmutableList.of();
@Test public void testGetEffectiveFlagGlobalRegionBuildAllow() throws Exception { MockApplicableRegionSet mock = new MockApplicableRegionSet(); ProtectedRegion global = mock.global(); global.setFlag(Flags.BUILD, State.ALLOW); FlagValueCalculator result = mock.getFlagCalculator(); // Cannot let users override BUILD on GLOBAL assertThat(result.getEffectiveFlag(global, Flags.BUILD, null), equalTo(null)); } }
@Test public void testGetMembershipGlobalRegion() throws Exception { MockApplicableRegionSet mock = new MockApplicableRegionSet(); ProtectedRegion global = mock.global(); LocalPlayer player = mock.createPlayer(); FlagValueCalculator result = mock.getFlagCalculator(); assertThat(result.getMembership(player), is(Result.NO_REGIONS)); }
/** * Get the effective values for a flag, returning a collection of all * values. It is up to the caller to determine which value, if any, * from the collection will be used. * * <p>A subject can be provided that is used to determine whether the value * of a flag on a particular region should be used. For example, if a * flag's region group is set to {@link RegionGroup#MEMBERS} and the given * subject is not a member, then the region would be skipped when * querying that flag. If {@code null} is provided for the subject, then * only flags that use {@link RegionGroup#ALL}, * {@link RegionGroup#NON_MEMBERS}, etc. will apply.</p> * * @param subject an optional subject, which would be used to determine the region group to apply * @param flag the flag * @return a collection of values */ public <V> Collection<V> queryAllValues(@Nullable RegionAssociable subject, Flag<V> flag) { return queryAllValues(subject, flag, false); }
public FlagValueCalculator getFlagCalculator() { NormativeOrders.sort(regions); return new FlagValueCalculator(regions, global); }
@Test public void testQueryValueAllowOverridesNone() throws Exception { MockApplicableRegionSet mock = new MockApplicableRegionSet(); ProtectedRegion region = mock.add(0); StateFlag flag1 = new StateFlag("test1", false); StateFlag flag2 = new StateFlag("test2", false); region = mock.add(0); region.setFlag(flag2, State.ALLOW); FlagValueCalculator result = mock.getFlagCalculator(); assertThat(result.queryValue(null, flag1), is((State) null)); assertThat(result.queryValue(null, flag2), is(State.ALLOW)); }