From 881846d30104bfd874631a2e192e934c132788f6 Mon Sep 17 00:00:00 2001 From: Redecouverte Date: Sun, 27 Feb 2011 16:24:07 +0100 Subject: [PATCH] fixed tnt block npe thanks to Brettflan, added claim-only-inside-existing-regions and max-region-count-per-player region options, updated ApplicableRegionSet for regions --- config.yml | 6 +- .../bukkit/WorldGuardEntityListener.java | 6 +- .../bukkit/WorldGuardWorldConfiguration.java | 6 +- .../bukkit/commands/CommandRegionClaim.java | 39 +++++-- .../protection/ApplicableRegionSet.java | 12 +- .../regionmanager/FlatRegionManager.java | 108 +++++++++++------- .../regionmanager/PRTreeRegionManager.java | 76 +++++++----- .../regionmanager/RegionManager.java | 15 ++- .../regions/ProtectedCuboidRegion.java | 37 +++++- .../regions/ProtectedPolygonalRegion.java | 70 ++++++++---- .../protection/regions/ProtectedRegion.java | 29 +---- 11 files changed, 264 insertions(+), 140 deletions(-) diff --git a/config.yml b/config.yml index bfe2f2a0..1e2a4c31 100644 --- a/config.yml +++ b/config.yml @@ -70,8 +70,10 @@ player-damage: regions: enable: on - wand: 287 - max-claim-volume: 1000 + wand: 287 + max-claim-volume: 30000 + claim-only-inside-existing-regions: off + max-region-count-per-player: 7 default: build: on chest-access: off diff --git a/src/com/sk89q/worldguard/bukkit/WorldGuardEntityListener.java b/src/com/sk89q/worldguard/bukkit/WorldGuardEntityListener.java index fd352043..ddb0ec7f 100644 --- a/src/com/sk89q/worldguard/bukkit/WorldGuardEntityListener.java +++ b/src/com/sk89q/worldguard/bukkit/WorldGuardEntityListener.java @@ -261,7 +261,7 @@ public void onEntityExplode(EntityExplodeEvent event) { } WorldGuardConfiguration cfg = plugin.getWgConfiguration(); - WorldGuardWorldConfiguration wcfg = cfg.getWorldConfig(event.getEntity().getWorld().getName()); + WorldGuardWorldConfiguration wcfg = cfg.getWorldConfig(event.getLocation().getWorld().getName()); if (event.getEntity() instanceof LivingEntity) { @@ -278,7 +278,7 @@ public void onEntityExplode(EntityExplodeEvent event) { if (wcfg.useRegions) { Vector pt = toVector(event.getEntity().getLocation()); - RegionManager mgr = plugin.getGlobalRegionManager().getRegionManager(event.getEntity().getWorld().getName()); + RegionManager mgr = plugin.getGlobalRegionManager().getRegionManager(event.getLocation().getWorld().getName()); if (!mgr.getApplicableRegions(pt) .allowsFlag(AreaFlags.FLAG_CREEPER_EXPLOSION)) { @@ -294,7 +294,7 @@ public void onEntityExplode(EntityExplodeEvent event) { if (wcfg.useRegions && event.getEntity() != null) { Vector pt = toVector(event.getEntity().getLocation()); - RegionManager mgr = plugin.getGlobalRegionManager().getRegionManager(event.getEntity().getWorld().getName()); + RegionManager mgr = plugin.getGlobalRegionManager().getRegionManager(event.getLocation().getWorld().getName()); if (!mgr.getApplicableRegions(pt) .allowsFlag(AreaFlags.FLAG_TNT)) { diff --git a/src/com/sk89q/worldguard/bukkit/WorldGuardWorldConfiguration.java b/src/com/sk89q/worldguard/bukkit/WorldGuardWorldConfiguration.java index b1b646b1..586443d6 100644 --- a/src/com/sk89q/worldguard/bukkit/WorldGuardWorldConfiguration.java +++ b/src/com/sk89q/worldguard/bukkit/WorldGuardWorldConfiguration.java @@ -93,6 +93,8 @@ public class WorldGuardWorldConfiguration { public boolean buyOnClaim; public double buyOnClaimPrice; public int maxClaimVolume; + public boolean claimOnlyInsideExistingRegions; + public int maxRegionCountPerPlayer; /* Configuration data end */ @@ -201,7 +203,9 @@ private void loadConfiguration() { useRegions = config.getBoolean("regions.enable", true); regionWand = config.getInt("regions.wand", 287); - maxClaimVolume = config.getInt("regions.max-claim-volume", 1000); + maxClaimVolume = config.getInt("regions.max-claim-volume", 30000); + claimOnlyInsideExistingRegions = config.getBoolean("regions.claim-only-inside-existing-regions", false); + maxRegionCountPerPlayer = config.getInt("regions.max-region-count-per-player", 7); useiConomy = config.getBoolean("iconomy.enable", false); buyOnClaim = config.getBoolean("iconomy.buy-on-claim", false); diff --git a/src/com/sk89q/worldguard/bukkit/commands/CommandRegionClaim.java b/src/com/sk89q/worldguard/bukkit/commands/CommandRegionClaim.java index c1afd8bb..0e082d55 100644 --- a/src/com/sk89q/worldguard/bukkit/commands/CommandRegionClaim.java +++ b/src/com/sk89q/worldguard/bukkit/commands/CommandRegionClaim.java @@ -15,8 +15,7 @@ * * You should have received a copy of the GNU General Public License * along with this program. If not, see . -*/ - + */ package com.sk89q.worldguard.bukkit.commands; import com.sk89q.worldedit.BlockVector; @@ -36,6 +35,8 @@ import com.sk89q.worldguard.protection.regions.ProtectedRegion; import com.nijiko.coelho.iConomy.*; import com.nijiko.coelho.iConomy.system.*; +import com.sk89q.worldguard.LocalPlayer; +import com.sk89q.worldguard.protection.ApplicableRegionSet; import java.io.IOException; import org.bukkit.ChatColor; @@ -70,10 +71,17 @@ public boolean handle(CommandSender sender, String senderName, String command, S String id = args[0].toLowerCase(); RegionManager mgr = cfg.getWorldGuardPlugin().getGlobalRegionManager().getRegionManager(player.getWorld().getName()); + LocalPlayer lPlayer = BukkitPlayer.wrapPlayer(cfg, player); + + if (mgr.getRegionCountOfPlayer(lPlayer) >= wcfg.maxRegionCountPerPlayer) { + player.sendMessage(ChatColor.RED + "You own too much regions, delete one first to claim a new one."); + return true; + } + ProtectedRegion existing = mgr.getRegion(id); if (existing != null) { - if (!existing.getOwners().contains(BukkitPlayer.wrapPlayer(cfg, player))) { + if (!existing.getOwners().contains(lPlayer)) { player.sendMessage(ChatColor.RED + "You don't own this region."); return true; } @@ -100,9 +108,20 @@ public boolean handle(CommandSender sender, String senderName, String command, S region = new ProtectedCuboidRegion(id, min, max); } - if (mgr.overlapsUnownedRegion(region, BukkitPlayer.wrapPlayer(cfg, player))) { - player.sendMessage(ChatColor.RED + "This region overlaps with someone else's region."); - return true; + ApplicableRegionSet regions = mgr.getApplicableRegions(region); + if (regions.isAnyRegionAffected()) { + if (!regions.isOwner(lPlayer)) { + player.sendMessage(ChatColor.RED + "This region overlaps with someone else's region."); + return true; + } + } + else + { + if(wcfg.claimOnlyInsideExistingRegions) + { + player.sendMessage(ChatColor.RED + "You may only claim regions inside existing regions that you or your group own."); + return true; + } } if (region.countBlocks() > wcfg.maxClaimVolume) { @@ -120,13 +139,13 @@ public boolean handle(CommandSender sender, String senderName, String command, S double regionCosts = region.countBlocks() * wcfg.buyOnClaimPrice; if (balance >= regionCosts) { account.subtract(regionCosts); - player.sendMessage(ChatColor.YELLOW + "You have bought that region for " + - iConomy.getBank().format(regionCosts)); + player.sendMessage(ChatColor.YELLOW + "You have bought that region for " + + iConomy.getBank().format(regionCosts)); account.save(); } else { player.sendMessage(ChatColor.RED + "You have not enough money."); - player.sendMessage(ChatColor.RED + "The region you want to claim costs " + - iConomy.getBank().format(regionCosts)); + player.sendMessage(ChatColor.RED + "The region you want to claim costs " + + iConomy.getBank().format(regionCosts)); player.sendMessage(ChatColor.RED + "You have " + iConomy.getBank().format(balance)); return true; } diff --git a/src/com/sk89q/worldguard/protection/ApplicableRegionSet.java b/src/com/sk89q/worldguard/protection/ApplicableRegionSet.java index 25f88bd1..9c5e45d9 100644 --- a/src/com/sk89q/worldguard/protection/ApplicableRegionSet.java +++ b/src/com/sk89q/worldguard/protection/ApplicableRegionSet.java @@ -20,7 +20,6 @@ import com.sk89q.worldguard.protection.regions.ProtectedRegion; import java.util.Iterator; -import com.sk89q.worldedit.Vector; import com.sk89q.worldguard.LocalPlayer; import com.sk89q.worldguard.protection.regions.AreaFlags; import com.sk89q.worldguard.protection.regions.AreaFlags.State; @@ -36,7 +35,6 @@ public class ApplicableRegionSet { private GlobalFlags global; - private Vector pt; private List applicable; private ProtectedRegion affectedRegion; @@ -47,9 +45,7 @@ public class ApplicableRegionSet { * @param regions * @param global */ - public ApplicableRegionSet(Vector pt, List applicable, - GlobalFlags global) { - this.pt = pt; + public ApplicableRegionSet(List applicable, GlobalFlags global) { this.applicable = applicable; this.global = global; @@ -256,6 +252,12 @@ private ProtectedRegion getAffectedRegion() { } + public boolean isAnyRegionAffected() + { + return this.applicable.size() > 0; + } + + /** * Determines the region with the hightest priority that is not a parent. * diff --git a/src/com/sk89q/worldguard/protection/regionmanager/FlatRegionManager.java b/src/com/sk89q/worldguard/protection/regionmanager/FlatRegionManager.java index 20243d36..c6dd8005 100644 --- a/src/com/sk89q/worldguard/protection/regionmanager/FlatRegionManager.java +++ b/src/com/sk89q/worldguard/protection/regionmanager/FlatRegionManager.java @@ -15,8 +15,7 @@ * * You should have received a copy of the GNU General Public License * along with this program. If not, see . -*/ - + */ package com.sk89q.worldguard.protection.regionmanager; import java.io.IOException; @@ -31,8 +30,6 @@ import com.sk89q.worldguard.protection.regions.ProtectedRegion; import com.sk89q.worldguard.protection.UnsupportedIntersectionException; import com.sk89q.worldguard.protection.dbs.ProtectionDatabase; -import java.util.HashMap; - /** * A very simple implementation of the region manager that uses a flat list @@ -42,40 +39,40 @@ * @author sk89q */ public class FlatRegionManager extends RegionManager { + /** * List of protected regions. */ - private Map regions; - + private Map regions; /** * Construct the manager. */ public FlatRegionManager(GlobalFlags global, ProtectionDatabase regionloader) throws IOException { - super(global, regionloader); - regions = new TreeMap(); - this.load(); + super(global, regionloader); + regions = new TreeMap(); + this.load(); } - + /** * Get a list of protected regions. * * @return */ - public Map getRegions() { + public Map getRegions() { return regions; } - + /** * Set a list of protected regions. * * @return */ - public void setRegions(Map regions) { - this.regions = new TreeMap(regions); + public void setRegions(Map regions) { + this.regions = new TreeMap(regions); } - + /** * Adds a region. * @@ -85,7 +82,7 @@ public void setRegions(Map regions) { public void addRegion(ProtectedRegion region) { regions.put(region.getId(), region); } - + /** * Removes a region and its children. * @@ -93,9 +90,9 @@ public void addRegion(ProtectedRegion region) { */ public void removeRegion(String id) { ProtectedRegion region = regions.get(id); - + regions.remove(id); - + if (region != null) { for (Map.Entry entry : regions.entrySet()) { if (entry.getValue().getParent() == region) { @@ -104,7 +101,7 @@ public void removeRegion(String id) { } } } - + /** * Return whether a region exists by an ID. * @@ -114,7 +111,7 @@ public void removeRegion(String id) { public boolean hasRegion(String id) { return regions.containsKey(id); } - + /** * Get a region by its ID. * @@ -123,7 +120,7 @@ public boolean hasRegion(String id) { public ProtectedRegion getRegion(String id) { return regions.get(id); } - + /** * Get an object for a point for rules to be applied with. * @@ -132,18 +129,39 @@ public ProtectedRegion getRegion(String id) { */ public ApplicableRegionSet getApplicableRegions(Vector pt) { + List appRegions = new ArrayList(); + + for (ProtectedRegion region : regions.values()) { + if (region.contains(pt)) { + appRegions.add(region); + } + } + + return new ApplicableRegionSet(appRegions, global); + } + + /** + * Get an object for a region for rules to be applied with. + * + * @param pt + * @return + */ + public ApplicableRegionSet getApplicableRegions(ProtectedRegion checkRegion) { List appRegions = new ArrayList(); - for (Map.Entry entry : regions.entrySet()) { - if (entry.getValue().contains(pt)) { - appRegions.add(entry.getValue()); + for (ProtectedRegion region : regions.values()) { + try { + if (region.intersectsWith(checkRegion)) { + appRegions.add(region); + } + } catch (UnsupportedIntersectionException ex) { } } - - return new ApplicableRegionSet(pt, appRegions, global); + + return new ApplicableRegionSet(appRegions, global); } - + /** * Get a list of region IDs that contain a point. * @@ -152,16 +170,16 @@ public ApplicableRegionSet getApplicableRegions(Vector pt) { */ public List getApplicableRegionsIDs(Vector pt) { List applicable = new ArrayList(); - - for (Map.Entry entry : regions.entrySet()) { + + for (Map.Entry entry : regions.entrySet()) { if (entry.getValue().contains(pt)) { applicable.add(entry.getKey()); } } - + return applicable; } - + /** * Returns true if the provided region overlaps with any other region that * is not owned by the player. @@ -175,19 +193,19 @@ public boolean overlapsUnownedRegion(ProtectedRegion region, LocalPlayer player) if (other.getOwners().contains(player)) { continue; } - + try { - if (ProtectedRegion.intersects(region, other)) { + if (region.intersectsWith(other)) { return true; } } catch (UnsupportedIntersectionException e) { // TODO: Maybe do something here } } - + return false; } - + /** * Get the number of regions. * @@ -197,20 +215,28 @@ public int size() { return regions.size(); } - /** * Save the list of regions. * * @throws IOException */ - public void save() throws IOException - { - if(this.regionloader == null) - { + public void save() throws IOException { + if (this.regionloader == null) { return; } - + regionloader.save(this); } + public int getRegionCountOfPlayer(LocalPlayer player) { + int count = 0; + + for (ProtectedRegion region : regions.values()) { + if (region.getOwners().contains(player)) { + count++; + } + } + + return count; + } } diff --git a/src/com/sk89q/worldguard/protection/regionmanager/PRTreeRegionManager.java b/src/com/sk89q/worldguard/protection/regionmanager/PRTreeRegionManager.java index 84e43aba..63637baf 100644 --- a/src/com/sk89q/worldguard/protection/regionmanager/PRTreeRegionManager.java +++ b/src/com/sk89q/worldguard/protection/regionmanager/PRTreeRegionManager.java @@ -15,8 +15,7 @@ * * You should have received a copy of the GNU General Public License * along with this program. If not, see . -*/ - + */ package com.sk89q.worldguard.protection.regionmanager; import java.util.ArrayList; @@ -34,22 +33,18 @@ import com.sk89q.worldguard.protection.UnsupportedIntersectionException; import com.sk89q.worldguard.protection.dbs.ProtectionDatabase; import java.io.IOException; -import java.util.HashMap; public class PRTreeRegionManager extends RegionManager { - private static final int BRANCH_FACTOR = 30; + private static final int BRANCH_FACTOR = 30; /** * List of protected regions. */ - private Map regions; - + private Map regions; /** * Converter to get coordinates of the tree. */ - private MBRConverter converter - = new ProtectedRegionMBRConverter(); - + private MBRConverter converter = new ProtectedRegionMBRConverter(); /** * Priority R-tree. */ @@ -60,27 +55,27 @@ public class PRTreeRegionManager extends RegionManager { */ public PRTreeRegionManager(GlobalFlags global, ProtectionDatabase regionloader) throws IOException { super(global, regionloader); - regions = new TreeMap(); + regions = new TreeMap(); tree = new PRTree(converter, BRANCH_FACTOR); this.load(); } - + /** * Get a list of protected regions. * * @return */ - public Map getRegions() { + public Map getRegions() { return regions; } - + /** * Set a list of protected regions. * * @return */ - public void setRegions(Map regions) { - this.regions = new TreeMap(regions); + public void setRegions(Map regions) { + this.regions = new TreeMap(regions); tree = new PRTree(converter, BRANCH_FACTOR); tree.load(regions.values()); } @@ -115,7 +110,7 @@ public boolean hasRegion(String id) { public ProtectedRegion getRegion(String id) { return regions.get(id); } - + /** * Removes a region and its children. * @@ -123,9 +118,9 @@ public ProtectedRegion getRegion(String id) { */ public void removeRegion(String id) { ProtectedRegion region = regions.get(id); - + regions.remove(id); - + if (region != null) { for (Map.Entry entry : regions.entrySet()) { if (entry.getValue().getParent() == region) { @@ -157,7 +152,22 @@ public ApplicableRegionSet getApplicableRegions(Vector pt) { } } - return new ApplicableRegionSet(pt, appRegions, global); + return new ApplicableRegionSet(appRegions, global); + } + + public ApplicableRegionSet getApplicableRegions(ProtectedRegion checkRegion) { + List appRegions = new ArrayList(); + + for (ProtectedRegion region : regions.values()) { + try { + if (region.intersectsWith(checkRegion)) { + appRegions.add(region); + } + } catch (UnsupportedIntersectionException ex) { + } + } + + return new ApplicableRegionSet(appRegions, global); } /** @@ -171,16 +181,16 @@ public List getApplicableRegionsIDs(Vector pt) { int x = pt.getBlockX(); int z = pt.getBlockZ(); - + for (ProtectedRegion region : tree.find(x, z, x, z)) { if (region.contains(pt)) { applicable.add(region.getId()); } } - + return applicable; } - + /** * Returns true if the provided region overlaps with any other region that * is not owned by the player. @@ -194,19 +204,19 @@ public boolean overlapsUnownedRegion(ProtectedRegion region, LocalPlayer player) if (other.getOwners().contains(player)) { continue; } - + try { - if (ProtectedRegion.intersects(region, other)) { + if (region.intersectsWith(other)) { return true; } } catch (UnsupportedIntersectionException e) { // TODO: Maybe do something here } } - + return false; } - + /** * Get the number of regions. * @@ -221,9 +231,19 @@ public int size() { * * @throws IOException */ - public void save() throws IOException - { + public void save() throws IOException { regionloader.save(this); } + public int getRegionCountOfPlayer(LocalPlayer player) { + int count = 0; + + for (Map.Entry entry : regions.entrySet()) { + if (entry.getValue().getOwners().contains(player)) { + count++; + } + } + + return count; + } } diff --git a/src/com/sk89q/worldguard/protection/regionmanager/RegionManager.java b/src/com/sk89q/worldguard/protection/regionmanager/RegionManager.java index a317c1d3..07f0394e 100644 --- a/src/com/sk89q/worldguard/protection/regionmanager/RegionManager.java +++ b/src/com/sk89q/worldguard/protection/regionmanager/RegionManager.java @@ -142,7 +142,17 @@ public void load() throws IOException * @return */ public abstract ApplicableRegionSet getApplicableRegions(Vector pt); - + + + /** + * Get an object for a point for rules to be applied with. + * + * @param pt + * @return + */ + public abstract ApplicableRegionSet getApplicableRegions(ProtectedRegion region); + + /** * Get a list of region IDs that contain a point. * @@ -177,4 +187,7 @@ public void setGlobalFlags(GlobalFlags globalflags) { global = globalflags; } + + + public abstract int getRegionCountOfPlayer(LocalPlayer player); } diff --git a/src/com/sk89q/worldguard/protection/regions/ProtectedCuboidRegion.java b/src/com/sk89q/worldguard/protection/regions/ProtectedCuboidRegion.java index 9bb22155..e01ae518 100644 --- a/src/com/sk89q/worldguard/protection/regions/ProtectedCuboidRegion.java +++ b/src/com/sk89q/worldguard/protection/regions/ProtectedCuboidRegion.java @@ -15,11 +15,11 @@ * * You should have received a copy of the GNU General Public License * along with this program. If not, see . -*/ - + */ package com.sk89q.worldguard.protection.regions; import com.sk89q.worldedit.*; +import com.sk89q.worldguard.protection.UnsupportedIntersectionException; /** * Represents a cuboid region that can be protected. @@ -27,6 +27,7 @@ * @author sk89q */ public class ProtectedCuboidRegion extends ProtectedRegion { + /** * Store the first point. */ @@ -98,7 +99,37 @@ public boolean contains(Vector pt) { && y >= min.getBlockY() && y <= max.getBlockY() && z >= min.getBlockZ() && z <= max.getBlockZ(); } - + + /** + * Checks if two region intersects. + * + * @param region + * @throws UnsupportedIntersectionException + * @return + */ + public boolean intersectsWith(ProtectedRegion region) throws UnsupportedIntersectionException { + + if (region instanceof ProtectedCuboidRegion) { + ProtectedCuboidRegion r1 = (ProtectedCuboidRegion) this; + ProtectedCuboidRegion r2 = (ProtectedCuboidRegion) region; + BlockVector min1 = r1.getMinimumPoint(); + BlockVector max1 = r1.getMaximumPoint(); + BlockVector min2 = r2.getMinimumPoint(); + BlockVector max2 = r2.getMaximumPoint(); + + return !(min1.getBlockX() > max2.getBlockX() + || min1.getBlockY() > max2.getBlockY() + || min1.getBlockZ() > max2.getBlockZ() + || max1.getBlockX() < min2.getBlockX() + || max1.getBlockY() < min2.getBlockY() + || max1.getBlockZ() < min2.getBlockZ()); + } else if (region instanceof ProtectedPolygonalRegion) { + throw new UnsupportedIntersectionException(); + } else { + throw new UnsupportedIntersectionException(); + } + } + /** * Return the type of region as a user-friendly name. * diff --git a/src/com/sk89q/worldguard/protection/regions/ProtectedPolygonalRegion.java b/src/com/sk89q/worldguard/protection/regions/ProtectedPolygonalRegion.java index 6518df3d..c1c3d7a2 100644 --- a/src/com/sk89q/worldguard/protection/regions/ProtectedPolygonalRegion.java +++ b/src/com/sk89q/worldguard/protection/regions/ProtectedPolygonalRegion.java @@ -15,41 +15,49 @@ * * You should have received a copy of the GNU General Public License * along with this program. If not, see . -*/ - + */ package com.sk89q.worldguard.protection.regions; import java.util.List; import com.sk89q.worldedit.BlockVector; import com.sk89q.worldedit.BlockVector2D; import com.sk89q.worldedit.Vector; - +import com.sk89q.worldguard.protection.UnsupportedIntersectionException; public class ProtectedPolygonalRegion extends ProtectedRegion { + protected List points; protected int minY; protected int maxY; private BlockVector min; private BlockVector max; - + public ProtectedPolygonalRegion(String id, List points, int minY, int maxY) { super(id); this.points = points; this.minY = minY; this.maxY = maxY; - + int minX = points.get(0).getBlockX(); int minZ = points.get(0).getBlockZ(); int maxX = points.get(0).getBlockX(); int maxZ = points.get(0).getBlockZ(); - + for (BlockVector2D v : points) { int x = v.getBlockX(); int z = v.getBlockZ(); - if (x < minX) minX = x; - if (z < minZ) minZ = z; - if (x > maxX) maxX = x; - if (z > maxZ) maxZ = z; + if (x < minX) { + minX = x; + } + if (z < minZ) { + minZ = z; + } + if (x > maxX) { + maxX = x; + } + if (z > maxZ) { + maxZ = z; + } } min = new BlockVector(minX, minY, minZ); @@ -74,13 +82,13 @@ public boolean contains(Vector pt) { int targetX = pt.getBlockX(); //wide int targetY = pt.getBlockY(); //height int targetZ = pt.getBlockZ(); //depth - + if (targetY < minY || targetY > maxY) { return false; } //Quick and dirty check. - if(targetX < min.getBlockX() || targetX > max.getBlockX() || targetZ < min.getBlockZ() || targetZ > max.getBlockZ()){ - return false; + if (targetX < min.getBlockX() || targetX > max.getBlockX() || targetZ < min.getBlockZ() || targetZ > max.getBlockZ()) { + return false; } boolean inside = false; int npoints = points.size(); @@ -92,13 +100,13 @@ public boolean contains(Vector pt) { xOld = points.get(npoints - 1).getBlockX(); zOld = points.get(npoints - 1).getBlockZ(); - + for (i = 0; i < npoints; i++) { xNew = points.get(i).getBlockX(); zNew = points.get(i).getBlockZ(); //Check for corner - if(xNew == targetX && zNew == targetZ){ - return true; + if (xNew == targetX && zNew == targetZ) { + return true; } if (xNew > xOld) { x1 = xOld; @@ -113,16 +121,34 @@ public boolean contains(Vector pt) { } if ((xNew < targetX) == (targetX <= xOld) && ((long) targetZ - (long) z1) * (long) (x2 - x1) <= ((long) z2 - (long) z1) - * (long) (targetX - x1)) { + * (long) (targetX - x1)) { inside = !inside; } xOld = xNew; zOld = zNew; } - + return inside; } + /** + * Checks if two region intersects. + * + * @param region + * @throws UnsupportedIntersectionException + * @return + */ + public boolean intersectsWith(ProtectedRegion region) throws UnsupportedIntersectionException { + + if (region instanceof ProtectedCuboidRegion) { + throw new UnsupportedIntersectionException(); + } else if (region instanceof ProtectedPolygonalRegion) { + throw new UnsupportedIntersectionException(); + } else { + throw new UnsupportedIntersectionException(); + } + } + /** * Return the type of region as a user-friendly name. * @@ -141,7 +167,7 @@ public String getTypeName() { public int countBlocks() { int volume = 0; int numPoints = points.size(); - if(numPoints < 3) { + if (numPoints < 3) { return 0; } @@ -152,12 +178,12 @@ public int countBlocks() { xa = points.get(i).getBlockX(); za = points.get(i).getBlockZ(); - if (points.get(i + 1) == null ) { + if (points.get(i + 1) == null) { z1 = points.get(0).getBlockZ(); } else { z1 = points.get(i + 1).getBlockZ(); } - if (points.get(i - 1) == null ) { + if (points.get(i - 1) == null) { z2 = points.get(numPoints - 1).getBlockZ(); } else { z2 = points.get(i - 1).getBlockZ(); @@ -171,7 +197,7 @@ public int countBlocks() { area = area + (xa * (points.get(1).getBlockZ() - points.get(numPoints - 1).getBlockZ())); - volume = (Math.abs(maxY - minY) + 1) * (int)Math.ceil((Math.abs(area) / 2)); + volume = (Math.abs(maxY - minY) + 1) * (int) Math.ceil((Math.abs(area) / 2)); return volume; } diff --git a/src/com/sk89q/worldguard/protection/regions/ProtectedRegion.java b/src/com/sk89q/worldguard/protection/regions/ProtectedRegion.java index 15a8826a..e339df84 100644 --- a/src/com/sk89q/worldguard/protection/regions/ProtectedRegion.java +++ b/src/com/sk89q/worldguard/protection/regions/ProtectedRegion.java @@ -225,7 +225,8 @@ public boolean isOwner(LocalPlayer player) { return false; } - + + /** * Checks whether a player is a member of the region or any of its parents. * @@ -300,32 +301,12 @@ public int compareTo(ProtectedRegion other) { /** * Checks if two region intersects. * - * @param region1 - * @param region2 + * @param region * @throws UnsupportedIntersectionException * @return */ - public static boolean intersects(ProtectedRegion region1, ProtectedRegion region2) - throws UnsupportedIntersectionException { - if (region1 instanceof ProtectedCuboidRegion - && region2 instanceof ProtectedCuboidRegion) { - ProtectedCuboidRegion r1 = (ProtectedCuboidRegion)region1; - ProtectedCuboidRegion r2 = (ProtectedCuboidRegion)region2; - BlockVector min1 = r1.getMinimumPoint(); - BlockVector max1 = r1.getMaximumPoint(); - BlockVector min2 = r2.getMinimumPoint(); - BlockVector max2 = r2.getMaximumPoint(); - - return !(min1.getBlockX() > max2.getBlockX() - || min1.getBlockY() > max2.getBlockY() - || min1.getBlockZ() > max2.getBlockZ() - || max1.getBlockX() < min2.getBlockX() - || max1.getBlockY() < min2.getBlockY() - || max1.getBlockZ() < min2.getBlockZ()); - } else { - throw new UnsupportedIntersectionException(); - } - } + public abstract boolean intersectsWith(ProtectedRegion region) throws UnsupportedIntersectionException; + /** * Thrown when setting a parent would create a circular inheritance