Add nonplayer-protection-domains flag (#1763)

This commit is contained in:
stonar96 2021-06-07 01:28:00 +02:00 committed by GitHub
parent 46a662e1a7
commit 0a74d02fc9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 0 deletions

View File

@ -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<ProtectedRegion> source;
private boolean useMaxPriorityAssociation;
private int maxPriority;
private Set<ProtectedRegion> maxPriorityRegions;
protected AbstractRegionOverlapAssociation(@Nullable Set<ProtectedRegion> source, boolean useMaxPriorityAssociation) {
this.source = source;
@ -43,13 +48,39 @@ protected AbstractRegionOverlapAssociation(@Nullable Set<ProtectedRegion> source
protected void calcMaxPriority() {
checkNotNull(source);
int best = 0;
Set<ProtectedRegion> 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<? extends ProtectedRegion> source, Collection<?> domains) {
if (source == null || domains == null || domains.isEmpty()) {
return false;
}
for (ProtectedRegion region : source) {
Set<String> 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<ProtectedRegion> regions) {
return Association.OWNER;
}
}
Set<ProtectedRegion> 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;

View File

@ -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<String> 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));