Fixed bugs in the PRTreeRegionManager class:

* Point would not be returned if the player was at point which exceeded the flat decimal maximum of a region.
* The getAplicableRegionIDs method would not return the parents of a region.
* All test would fail
This commit is contained in:
Dark Arc 2012-10-24 12:51:09 -04:00 committed by sk89q
parent f199b977eb
commit 08ce207636

View File

@ -18,24 +18,18 @@
*/ */
package com.sk89q.worldguard.protection.managers; package com.sk89q.worldguard.protection.managers;
import java.util.ArrayList; import com.sk89q.worldedit.Vector;
import java.util.Collections; import com.sk89q.worldguard.LocalPlayer;
import java.util.List; import com.sk89q.worldguard.protection.ApplicableRegionSet;
import java.util.Map; import com.sk89q.worldguard.protection.databases.ProtectionDatabase;
import java.util.TreeMap; import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import com.sk89q.worldguard.protection.regions.ProtectedRegionMBRConverter;
import org.khelekore.prtree.MBR; import org.khelekore.prtree.MBR;
import org.khelekore.prtree.MBRConverter; import org.khelekore.prtree.MBRConverter;
import org.khelekore.prtree.PRTree; import org.khelekore.prtree.PRTree;
import org.khelekore.prtree.SimpleMBR; import org.khelekore.prtree.SimpleMBR;
import com.sk89q.worldedit.Vector; import java.util.*;
import com.sk89q.worldguard.LocalPlayer;
import com.sk89q.worldguard.protection.ApplicableRegionSet;
import com.sk89q.worldguard.protection.databases.ProtectionDatabaseException;
import com.sk89q.worldguard.protection.databases.ProtectionDatabase;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import com.sk89q.worldguard.protection.regions.ProtectedRegionMBRConverter;
public class PRTreeRegionManager extends RegionManager { public class PRTreeRegionManager extends RegionManager {
@ -57,13 +51,11 @@ public class PRTreeRegionManager extends RegionManager {
* Construct the manager. * Construct the manager.
* *
* @param regionLoader The region loader to use * @param regionLoader The region loader to use
* @throws ProtectionDatabaseException when an error occurs while loading
*/ */
public PRTreeRegionManager(ProtectionDatabase regionLoader) throws ProtectionDatabaseException { public PRTreeRegionManager(ProtectionDatabase regionLoader) {
super(regionLoader); super(regionLoader);
regions = new TreeMap<String, ProtectedRegion>(); regions = new TreeMap<String, ProtectedRegion>();
tree = new PRTree<ProtectedRegion>(converter, BRANCH_FACTOR); tree = new PRTree<ProtectedRegion>(converter, BRANCH_FACTOR);
this.load();
} }
@Override @Override
@ -109,18 +101,21 @@ public void removeRegion(String id) {
} }
} }
tree = new PRTree<ProtectedRegion>(converter, BRANCH_FACTOR); tree = new PRTree<ProtectedRegion>(converter, BRANCH_FACTOR);
tree.load(regions.values()); tree.load(regions.values());
} }
@Override @Override
public ApplicableRegionSet getApplicableRegions(Vector pt) { public ApplicableRegionSet getApplicableRegions(Vector pt) {
List<ProtectedRegion> appRegions =
new ArrayList<ProtectedRegion>(); // Floor the vector to ensure we get accurate points
pt = pt.floor();
List<ProtectedRegion> appRegions = new ArrayList<ProtectedRegion>();
MBR pointMBR = new SimpleMBR(pt.getX(), pt.getX(), pt.getY(), pt.getY(), pt.getZ(), pt.getZ()); MBR pointMBR = new SimpleMBR(pt.getX(), pt.getX(), pt.getY(), pt.getY(), pt.getZ(), pt.getZ());
for (ProtectedRegion region : tree.find(pointMBR)) { for (ProtectedRegion region : tree.find(pointMBR)) {
if (region.contains(pt)) { if (region.contains(pt) && !appRegions.contains(region)) {
appRegions.add(region); appRegions.add(region);
ProtectedRegion parent = region.getParent(); ProtectedRegion parent = region.getParent();
@ -157,12 +152,26 @@ public ApplicableRegionSet getApplicableRegions(ProtectedRegion checkRegion) {
@Override @Override
public List<String> getApplicableRegionsIDs(Vector pt) { public List<String> getApplicableRegionsIDs(Vector pt) {
// Floor the vector to ensure we get accurate points
pt = pt.floor();
List<String> applicable = new ArrayList<String>(); List<String> applicable = new ArrayList<String>();
MBR pointMBR = new SimpleMBR(pt.getX(), pt.getX(), pt.getY(), pt.getY(), pt.getZ(), pt.getZ()); MBR pointMBR = new SimpleMBR(pt.getX(), pt.getX(), pt.getY(), pt.getY(), pt.getZ(), pt.getZ());
for (ProtectedRegion region : tree.find(pointMBR)) { for (ProtectedRegion region : tree.find(pointMBR)) {
if (region.contains(pt)) { if (region.contains(pt) && !applicable.contains(region.getId())) {
applicable.add(region.getId()); applicable.add(region.getId());
ProtectedRegion parent = region.getParent();
while (parent != null) {
if (!applicable.contains(parent.getId())) {
applicable.add(parent.getId());
}
parent = parent.getParent();
}
} }
} }