Don't perform spatial queries for location->location checks until needed.

This commit is contained in:
sk89q 2014-08-18 11:26:02 -07:00
parent bb1826bdb1
commit 937a5758f7
2 changed files with 78 additions and 4 deletions

View File

@ -31,6 +31,7 @@
import com.sk89q.worldguard.bukkit.event.entity.DestroyEntityEvent;
import com.sk89q.worldguard.bukkit.event.entity.SpawnEntityEvent;
import com.sk89q.worldguard.bukkit.event.entity.UseEntityEvent;
import com.sk89q.worldguard.bukkit.util.DelayedRegionOverlapAssociation;
import com.sk89q.worldguard.bukkit.util.Entities;
import com.sk89q.worldguard.bukkit.util.Materials;
import com.sk89q.worldguard.domains.Association;
@ -114,12 +115,10 @@ private RegionAssociable createRegionAssociable(Cause cause) {
return getPlugin().wrapPlayer((Player) rootCause);
} else if (rootCause instanceof Entity) {
RegionQuery query = getPlugin().getRegionContainer().createQuery();
ApplicableRegionSet source = query.getApplicableRegions(((Entity) rootCause).getLocation());
return new RegionOverlapAssociation(source.getRegions());
return new DelayedRegionOverlapAssociation(query, ((Entity) rootCause).getLocation());
} else if (rootCause instanceof Block) {
RegionQuery query = getPlugin().getRegionContainer().createQuery();
ApplicableRegionSet source = query.getApplicableRegions(((Block) rootCause).getLocation());
return new RegionOverlapAssociation(source.getRegions());
return new DelayedRegionOverlapAssociation(query, ((Block) rootCause).getLocation());
} else {
return Associables.constant(Association.NON_MEMBER);
}

View File

@ -0,0 +1,75 @@
/*
* WorldGuard, a suite of tools for Minecraft
* Copyright (C) sk89q <http://www.sk89q.com>
* 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 <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldguard.bukkit.util;
import com.sk89q.worldguard.bukkit.RegionQuery;
import com.sk89q.worldguard.domains.Association;
import com.sk89q.worldguard.protection.ApplicableRegionSet;
import com.sk89q.worldguard.protection.association.RegionAssociable;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import org.bukkit.Location;
import javax.annotation.Nullable;
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.
*
* <p>This class only performs a spatial query if its
* {@link #getAssociation(ProtectedRegion)} method is called.</p>
*/
public class DelayedRegionOverlapAssociation implements RegionAssociable {
private final RegionQuery query;
private final Location location;
@Nullable
private Set<ProtectedRegion> source;
/**
* Create a new instance.
*
* @param query the query
* @param location the location
*/
public DelayedRegionOverlapAssociation(RegionQuery query, Location location) {
checkNotNull(query);
checkNotNull(location);
this.query = query;
this.location = location;
}
@Override
public Association getAssociation(ProtectedRegion region) {
if (source == null) {
ApplicableRegionSet result = query.getApplicableRegions(location);
source = result.getRegions();
}
if (source.contains(region)) {
return Association.OWNER;
} else {
return Association.NON_MEMBER;
}
}
}