Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
import com.sk89q.worldguard.protection.regions.RegionQuery;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;
import org.bukkit.Location;

Expand All @@ -36,12 +38,14 @@ public Optional<Region> getRegion(Location location) {
RegionQuery regionQuery = this.regionContainer.createQuery();
ApplicableRegionSet applicableRegions = regionQuery.getApplicableRegions(BukkitAdapter.adapt(location));

for (ProtectedRegion region : applicableRegions.getRegions()) {
if (!this.isCombatRegion(region)) {
continue;
}
ProtectedRegion highestPriorityRegion = this.highestPriorityRegion(applicableRegions);

if (highestPriorityRegion == null) {
return Optional.empty();
}

return Optional.of(new WorldGuardRegion(location.getWorld(), region));
if (this.isCombatRegion(highestPriorityRegion)) {
return Optional.of(new WorldGuardRegion(location.getWorld(), highestPriorityRegion));
}

return Optional.empty();
Expand Down Expand Up @@ -104,4 +108,23 @@ public Location getMax() {
}
}

ProtectedRegion highestPriorityRegion(ApplicableRegionSet applicableRegions) {

Set<ProtectedRegion> protectedRegions = applicableRegions.getRegions();

if (protectedRegions.isEmpty()) {
return null;
}

if (protectedRegions.size() == 1) {
return protectedRegions.iterator().next();
}

return protectedRegions
.stream()
.sorted(Comparator.comparingInt(ProtectedRegion::getPriority))
.findFirst()
.get();
}

}
Loading