From 6708b21016451a6c2d4d8fea6aa0cfbff5ba5ce0 Mon Sep 17 00:00:00 2001 From: wizjany Date: Mon, 3 Aug 2020 10:46:40 -0400 Subject: [PATCH] Abstract RegionOverlapAssociation, deprecate/fix packaging. --- .../bukkit/listener/AbstractListener.java | 2 +- .../DelayedRegionOverlapAssociation.java | 75 ++---------------- .../AbstractRegionOverlapAssociation.java | 77 ++++++++++++++++++ .../DelayedRegionOverlapAssociation.java | 78 +++++++++++++++++++ .../association/RegionOverlapAssociation.java | 50 ++---------- 5 files changed, 167 insertions(+), 115 deletions(-) create mode 100644 worldguard-core/src/main/java/com/sk89q/worldguard/protection/association/AbstractRegionOverlapAssociation.java create mode 100644 worldguard-core/src/main/java/com/sk89q/worldguard/protection/association/DelayedRegionOverlapAssociation.java diff --git a/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/listener/AbstractListener.java b/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/listener/AbstractListener.java index 9f09349a..8b53db21 100644 --- a/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/listener/AbstractListener.java +++ b/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/listener/AbstractListener.java @@ -31,7 +31,7 @@ import com.sk89q.worldguard.config.ConfigurationManager; import com.sk89q.worldguard.config.WorldConfiguration; import com.sk89q.worldguard.domains.Association; -import com.sk89q.worldguard.protection.DelayedRegionOverlapAssociation; +import com.sk89q.worldguard.protection.association.DelayedRegionOverlapAssociation; import com.sk89q.worldguard.protection.association.Associables; import com.sk89q.worldguard.protection.association.RegionAssociable; import com.sk89q.worldguard.protection.regions.RegionQuery; diff --git a/worldguard-core/src/main/java/com/sk89q/worldguard/protection/DelayedRegionOverlapAssociation.java b/worldguard-core/src/main/java/com/sk89q/worldguard/protection/DelayedRegionOverlapAssociation.java index 4a5f0d1e..c362d092 100644 --- a/worldguard-core/src/main/java/com/sk89q/worldguard/protection/DelayedRegionOverlapAssociation.java +++ b/worldguard-core/src/main/java/com/sk89q/worldguard/protection/DelayedRegionOverlapAssociation.java @@ -20,16 +20,9 @@ package com.sk89q.worldguard.protection; import com.sk89q.worldedit.util.Location; -import com.sk89q.worldguard.domains.Association; -import com.sk89q.worldguard.protection.association.RegionAssociable; -import com.sk89q.worldguard.protection.regions.ProtectedRegion; import com.sk89q.worldguard.protection.regions.RegionQuery; -import javax.annotation.Nullable; import java.util.List; -import java.util.Set; - -import static com.google.common.base.Preconditions.checkNotNull; /** * Determines that the association to a region is {@code OWNER} if the input @@ -37,76 +30,18 @@ * *

This class only performs a spatial query if its * {@link #getAssociation(List)} method is called.

+ * + * @deprecated Use {@link com.sk89q.worldguard.protection.association.DelayedRegionOverlapAssociation} instead. This class is mis-packaged. */ -public class DelayedRegionOverlapAssociation implements RegionAssociable { - - private final RegionQuery query; - private final Location location; - @Nullable - private Set source; - private boolean useMaxPriorityAssociation; - private int maxPriority; // only used for useMaxPriorityAssociation - +@Deprecated +public class DelayedRegionOverlapAssociation extends com.sk89q.worldguard.protection.association.DelayedRegionOverlapAssociation { /** * Create a new instance. * @param query the query * @param location the location */ public DelayedRegionOverlapAssociation(RegionQuery query, Location location) { - this(query, location, false); - } - - /** - * Create a new instance. - * @param query the query - * @param location the location - * @param useMaxPriorityAssociation whether to use the max priority from regions to determine association - */ - public DelayedRegionOverlapAssociation(RegionQuery query, Location location, boolean useMaxPriorityAssociation) { - checkNotNull(query); - checkNotNull(location); - this.query = query; - this.location = location; - this.useMaxPriorityAssociation = useMaxPriorityAssociation; - } - - @Override - public Association getAssociation(List regions) { - if (source == null) { - ApplicableRegionSet result = query.getApplicableRegions(location); - source = result.getRegions(); - assert source != null; - - if (useMaxPriorityAssociation) { - int best = 0; - for (ProtectedRegion region : source) { - int priority = region.getPriority(); - if (priority > best) { - best = priority; - } - } - maxPriority = best; - } - } - - for (ProtectedRegion region : regions) { - if ((region.getId().equals(ProtectedRegion.GLOBAL_REGION) && source.isEmpty())) { - return Association.OWNER; - } - - if (source.contains(region)) { - if (useMaxPriorityAssociation) { - int priority = region.getPriority(); - if (priority == maxPriority) { - return Association.OWNER; - } - } else { - return Association.OWNER; - } - } - } - - return Association.NON_MEMBER; + super(query, location, false); } } 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 new file mode 100644 index 00000000..c4da34f1 --- /dev/null +++ b/worldguard-core/src/main/java/com/sk89q/worldguard/protection/association/AbstractRegionOverlapAssociation.java @@ -0,0 +1,77 @@ +/* + * WorldGuard, a suite of tools for Minecraft + * Copyright (C) sk89q + * Copyright (C) WorldGuard team and contributors + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package com.sk89q.worldguard.protection.association; + +import static com.google.common.base.Preconditions.checkNotNull; + +import com.sk89q.worldguard.domains.Association; +import com.sk89q.worldguard.protection.regions.ProtectedRegion; + +import javax.annotation.Nullable; +import java.util.List; +import java.util.Set; + +public abstract class AbstractRegionOverlapAssociation implements RegionAssociable { + + @Nullable + protected Set source; + private boolean useMaxPriorityAssociation; + private int maxPriority; + + protected AbstractRegionOverlapAssociation(@Nullable Set source, boolean useMaxPriorityAssociation) { + this.source = source; + this.useMaxPriorityAssociation = useMaxPriorityAssociation; + } + + protected void calcMaxPriority() { + checkNotNull(source); + int best = 0; + for (ProtectedRegion region : source) { + int priority = region.getPriority(); + if (priority > best) { + best = priority; + } + } + this.maxPriority = best; + } + + @Override + public Association getAssociation(List regions) { + checkNotNull(source); + for (ProtectedRegion region : regions) { + if ((region.getId().equals(ProtectedRegion.GLOBAL_REGION) && source.isEmpty())) { + return Association.OWNER; + } + + if (source.contains(region)) { + if (useMaxPriorityAssociation) { + int priority = region.getPriority(); + if (priority == maxPriority) { + return Association.OWNER; + } + } else { + return Association.OWNER; + } + } + } + + return Association.NON_MEMBER; + } +} diff --git a/worldguard-core/src/main/java/com/sk89q/worldguard/protection/association/DelayedRegionOverlapAssociation.java b/worldguard-core/src/main/java/com/sk89q/worldguard/protection/association/DelayedRegionOverlapAssociation.java new file mode 100644 index 00000000..49af1f78 --- /dev/null +++ b/worldguard-core/src/main/java/com/sk89q/worldguard/protection/association/DelayedRegionOverlapAssociation.java @@ -0,0 +1,78 @@ +/* + * WorldGuard, a suite of tools for Minecraft + * Copyright (C) sk89q + * Copyright (C) WorldGuard team and contributors + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package com.sk89q.worldguard.protection.association; + +import static com.google.common.base.Preconditions.checkNotNull; + +import com.sk89q.worldedit.util.Location; +import com.sk89q.worldguard.domains.Association; +import com.sk89q.worldguard.protection.ApplicableRegionSet; +import com.sk89q.worldguard.protection.regions.ProtectedRegion; +import com.sk89q.worldguard.protection.regions.RegionQuery; + +import java.util.List; + +/** + * Determines that the association to a region is {@code OWNER} if the input + * region is in a set of source regions. + * + *

This class only performs a spatial query if its + * {@link #getAssociation(List)} method is called.

+ */ +public class DelayedRegionOverlapAssociation extends AbstractRegionOverlapAssociation { + + private final RegionQuery query; + private final Location location; + + /** + * Create a new instance. + * @param query the query + * @param location the location + */ + public DelayedRegionOverlapAssociation(RegionQuery query, Location location) { + this(query, location, false); + } + + /** + * Create a new instance. + * @param query the query + * @param location the location + * @param useMaxPriorityAssociation whether to use the max priority from regions to determine association + */ + public DelayedRegionOverlapAssociation(RegionQuery query, Location location, boolean useMaxPriorityAssociation) { + super(null, useMaxPriorityAssociation); + checkNotNull(query); + checkNotNull(location); + this.query = query; + this.location = location; + } + + @Override + public Association getAssociation(List regions) { + if (source == null) { + ApplicableRegionSet result = query.getApplicableRegions(location); + source = result.getRegions(); + calcMaxPriority(); + } + + return super.getAssociation(regions); + } + +} diff --git a/worldguard-core/src/main/java/com/sk89q/worldguard/protection/association/RegionOverlapAssociation.java b/worldguard-core/src/main/java/com/sk89q/worldguard/protection/association/RegionOverlapAssociation.java index 04fc2c19..95e07edb 100644 --- a/worldguard-core/src/main/java/com/sk89q/worldguard/protection/association/RegionOverlapAssociation.java +++ b/worldguard-core/src/main/java/com/sk89q/worldguard/protection/association/RegionOverlapAssociation.java @@ -19,30 +19,23 @@ package com.sk89q.worldguard.protection.association; -import com.sk89q.worldguard.domains.Association; import com.sk89q.worldguard.protection.regions.ProtectedRegion; -import java.util.List; +import javax.annotation.Nonnull; import java.util.Set; -import static com.google.common.base.Preconditions.checkNotNull; - /** * Determines that the association to a region is {@code OWNER} if the input * region is in a set of source regions. */ -public class RegionOverlapAssociation implements RegionAssociable { - - private final Set source; - private boolean useMaxPriorityAssociation; - private int maxPriority; // only used for useMaxPriorityAssociation +public class RegionOverlapAssociation extends AbstractRegionOverlapAssociation { /** * Create a new instance. * * @param source set of regions that input regions must be contained within */ - public RegionOverlapAssociation(Set source) { + public RegionOverlapAssociation(@Nonnull Set source) { this(source, false); } @@ -52,40 +45,9 @@ public RegionOverlapAssociation(Set source) { * @param source set of regions that input regions must be contained within * @param useMaxPriorityAssociation whether to use the max priority from regions to determine association */ - public RegionOverlapAssociation(Set source, boolean useMaxPriorityAssociation) { - checkNotNull(source); - this.source = source; - this.useMaxPriorityAssociation = useMaxPriorityAssociation; - int best = 0; - for (ProtectedRegion region : source) { - int priority = region.getPriority(); - if (priority > best) { - best = priority; - } - } - this.maxPriority = best; - } - - @Override - public Association getAssociation(List regions) { - for (ProtectedRegion region : regions) { - if ((region.getId().equals(ProtectedRegion.GLOBAL_REGION) && source.isEmpty())) { - return Association.OWNER; - } - - if (source.contains(region)) { - if (useMaxPriorityAssociation) { - int priority = region.getPriority(); - if (priority == maxPriority) { - return Association.OWNER; - } - } else { - return Association.OWNER; - } - } - } - - return Association.NON_MEMBER; + public RegionOverlapAssociation(@Nonnull Set source, boolean useMaxPriorityAssociation) { + super(source, useMaxPriorityAssociation); + calcMaxPriority(); } }