From 0a74d02fc92aa0bfa8ce57a50d76fef14706f256 Mon Sep 17 00:00:00 2001 From: stonar96 Date: Mon, 7 Jun 2021 01:28:00 +0200 Subject: [PATCH] Add nonplayer-protection-domains flag (#1763) --- .../AbstractRegionOverlapAssociation.java | 43 +++++++++++++++++++ .../worldguard/protection/flags/Flags.java | 1 + 2 files changed, 44 insertions(+) diff --git a/worldguard-core/src/main/java/com/sk89q/worldguard/protection/association/AbstractRegionOverlapAssociation.java b/worldguard-core/src/main/java/com/sk89q/worldguard/protection/association/AbstractRegionOverlapAssociation.java index c4da34f1..b33c0ac3 100644 --- a/worldguard-core/src/main/java/com/sk89q/worldguard/protection/association/AbstractRegionOverlapAssociation.java +++ b/worldguard-core/src/main/java/com/sk89q/worldguard/protection/association/AbstractRegionOverlapAssociation.java @@ -22,9 +22,13 @@ import static com.google.common.base.Preconditions.checkNotNull; import com.sk89q.worldguard.domains.Association; +import com.sk89q.worldguard.protection.flags.Flags; import com.sk89q.worldguard.protection.regions.ProtectedRegion; import javax.annotation.Nullable; +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; import java.util.List; import java.util.Set; @@ -34,6 +38,7 @@ public abstract class AbstractRegionOverlapAssociation implements RegionAssociab protected Set source; private boolean useMaxPriorityAssociation; private int maxPriority; + private Set maxPriorityRegions; protected AbstractRegionOverlapAssociation(@Nullable Set source, boolean useMaxPriorityAssociation) { this.source = source; @@ -43,13 +48,39 @@ protected AbstractRegionOverlapAssociation(@Nullable Set source protected void calcMaxPriority() { checkNotNull(source); int best = 0; + Set bestRegions = new HashSet<>(); for (ProtectedRegion region : source) { int priority = region.getPriority(); if (priority > best) { best = priority; + bestRegions.clear(); + bestRegions.add(region); + } else if (priority == best) { + bestRegions.add(region); } } this.maxPriority = best; + this.maxPriorityRegions = bestRegions; + } + + private boolean checkNonplayerProtectionDomains(Iterable source, Collection domains) { + if (source == null || domains == null || domains.isEmpty()) { + return false; + } + + for (ProtectedRegion region : source) { + Set regionDomains = region.getFlag(Flags.NONPLAYER_PROTECTION_DOMAINS); + + if (regionDomains == null || regionDomains.isEmpty()) { + continue; + } + + if (!Collections.disjoint(regionDomains, domains)) { + return true; + } + } + + return false; } @Override @@ -70,6 +101,18 @@ public Association getAssociation(List regions) { return Association.OWNER; } } + + Set source; + + if (useMaxPriorityAssociation) { + source = maxPriorityRegions; + } else { + source = this.source; + } + + if (checkNonplayerProtectionDomains(source, region.getFlag(Flags.NONPLAYER_PROTECTION_DOMAINS))) { + return Association.OWNER; + } } return Association.NON_MEMBER; diff --git a/worldguard-core/src/main/java/com/sk89q/worldguard/protection/flags/Flags.java b/worldguard-core/src/main/java/com/sk89q/worldguard/protection/flags/Flags.java index 53179124..177e1f26 100644 --- a/worldguard-core/src/main/java/com/sk89q/worldguard/protection/flags/Flags.java +++ b/worldguard-core/src/main/java/com/sk89q/worldguard/protection/flags/Flags.java @@ -46,6 +46,7 @@ public final class Flags { // Overrides membership check public static final StateFlag PASSTHROUGH = register(new StateFlag("passthrough", false)); + public static final SetFlag NONPLAYER_PROTECTION_DOMAINS = register(new SetFlag<>("nonplayer-protection-domains", new StringFlag(null))); // This flag is unlike the others. It forces the checking of region membership public static final StateFlag BUILD = register(new BuildFlag("build", true));