2011-02-24 11:19:35 +01:00
|
|
|
/*
|
2014-07-13 04:29:29 +02:00
|
|
|
* WorldGuard, a suite of tools for Minecraft
|
|
|
|
* Copyright (C) sk89q <http://www.sk89q.com>
|
|
|
|
* Copyright (C) WorldGuard team and contributors
|
2011-02-24 11:19:35 +01:00
|
|
|
*
|
2014-07-13 04:29:29 +02:00
|
|
|
* 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
|
2011-02-24 11:19:35 +01:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2014-07-13 04:29:29 +02:00
|
|
|
* 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.
|
2011-02-24 11:19:35 +01:00
|
|
|
*
|
2014-07-13 04:29:29 +02:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
2011-02-24 11:19:35 +01:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2014-07-13 04:29:29 +02:00
|
|
|
*/
|
2011-02-24 11:19:35 +01:00
|
|
|
|
|
|
|
package com.sk89q.worldguard.protection.regions;
|
|
|
|
|
|
|
|
import com.sk89q.worldedit.BlockVector;
|
2011-10-18 22:05:19 +02:00
|
|
|
import com.sk89q.worldedit.BlockVector2D;
|
2011-02-24 11:19:35 +01:00
|
|
|
import com.sk89q.worldedit.Vector;
|
|
|
|
import com.sk89q.worldguard.LocalPlayer;
|
|
|
|
import com.sk89q.worldguard.domains.DefaultDomain;
|
2011-03-27 00:59:06 +01:00
|
|
|
import com.sk89q.worldguard.protection.flags.Flag;
|
2014-08-09 05:22:05 +02:00
|
|
|
import com.sk89q.worldguard.util.ChangeTracked;
|
|
|
|
import com.sk89q.worldguard.util.Normal;
|
2011-02-24 11:19:35 +01:00
|
|
|
|
2014-08-09 05:22:05 +02:00
|
|
|
import javax.annotation.Nullable;
|
2012-11-03 05:19:20 +01:00
|
|
|
import java.awt.geom.Line2D;
|
2014-08-09 05:22:05 +02:00
|
|
|
import java.util.Collection;
|
2012-11-03 05:19:20 +01:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
2014-07-25 03:49:40 +02:00
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
2014-08-09 05:22:05 +02:00
|
|
|
import java.util.concurrent.ConcurrentMap;
|
2012-11-03 05:19:20 +01:00
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
2014-08-09 05:22:05 +02:00
|
|
|
import static com.google.common.base.Preconditions.checkNotNull;
|
|
|
|
|
2011-02-24 11:19:35 +01:00
|
|
|
/**
|
2014-08-09 05:22:05 +02:00
|
|
|
* Represents a region that can be indexed and have spatial queries performed
|
|
|
|
* against it.
|
|
|
|
*
|
|
|
|
* <p>Instances can be modified and access from several threads at a time.</p>
|
2011-02-24 11:19:35 +01:00
|
|
|
*/
|
2014-08-09 05:22:05 +02:00
|
|
|
public abstract class ProtectedRegion implements ChangeTracked, Comparable<ProtectedRegion> {
|
|
|
|
|
|
|
|
private static final Pattern VALID_ID_PATTERN = Pattern.compile("^[A-Za-z0-9_,'\\-\\+/]{1,}$");
|
2011-09-27 12:13:19 +02:00
|
|
|
|
2011-10-18 22:05:19 +02:00
|
|
|
protected BlockVector min;
|
|
|
|
protected BlockVector max;
|
|
|
|
|
2014-08-09 05:22:05 +02:00
|
|
|
private final String id;
|
2011-02-24 11:19:35 +01:00
|
|
|
private int priority = 0;
|
2011-04-02 23:11:12 +02:00
|
|
|
private ProtectedRegion parent;
|
2011-02-24 11:19:35 +01:00
|
|
|
private DefaultDomain owners = new DefaultDomain();
|
|
|
|
private DefaultDomain members = new DefaultDomain();
|
2014-08-09 05:22:05 +02:00
|
|
|
private ConcurrentMap<Flag<?>, Object> flags = new ConcurrentHashMap<Flag<?>, Object>();
|
|
|
|
private boolean dirty = true;
|
2011-02-24 11:19:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct a new instance of this region.
|
|
|
|
*
|
2014-08-09 05:22:05 +02:00
|
|
|
* @param id the name of this region
|
|
|
|
* @throws IllegalArgumentException thrown if the ID is invalid (see {@link #isValidId(String)}
|
2011-02-24 11:19:35 +01:00
|
|
|
*/
|
2014-08-09 05:22:05 +02:00
|
|
|
ProtectedRegion(String id) { // Package private because we can't have people creating their own region types
|
|
|
|
checkNotNull(id);
|
|
|
|
|
|
|
|
if (!isValidId(id)) {
|
|
|
|
throw new IllegalArgumentException("Invalid region ID: " + id);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.id = Normal.normalize(id);
|
2011-02-24 11:19:35 +01:00
|
|
|
}
|
|
|
|
|
2011-10-18 22:05:19 +02:00
|
|
|
/**
|
2014-08-09 05:22:05 +02:00
|
|
|
* Set the minimum and maximum points of the bounding box for a region
|
2011-10-18 22:05:19 +02:00
|
|
|
*
|
2014-08-09 05:22:05 +02:00
|
|
|
* @param points the points to set with at least one entry
|
2011-10-18 22:05:19 +02:00
|
|
|
*/
|
|
|
|
protected void setMinMaxPoints(List<Vector> points) {
|
|
|
|
int minX = points.get(0).getBlockX();
|
|
|
|
int minY = points.get(0).getBlockY();
|
|
|
|
int minZ = points.get(0).getBlockZ();
|
|
|
|
int maxX = minX;
|
|
|
|
int maxY = minY;
|
|
|
|
int maxZ = minZ;
|
|
|
|
|
|
|
|
for (Vector v : points) {
|
|
|
|
int x = v.getBlockX();
|
|
|
|
int y = v.getBlockY();
|
|
|
|
int z = v.getBlockZ();
|
|
|
|
|
|
|
|
if (x < minX) minX = x;
|
|
|
|
if (y < minY) minY = y;
|
|
|
|
if (z < minZ) minZ = z;
|
|
|
|
|
|
|
|
if (x > maxX) maxX = x;
|
|
|
|
if (y > maxY) maxY = y;
|
|
|
|
if (z > maxZ) maxZ = z;
|
|
|
|
}
|
2014-08-09 05:22:05 +02:00
|
|
|
|
|
|
|
setDirty(true);
|
2011-10-18 22:05:19 +02:00
|
|
|
min = new BlockVector(minX, minY, minZ);
|
|
|
|
max = new BlockVector(maxX, maxY, maxZ);
|
|
|
|
}
|
|
|
|
|
2011-02-24 11:19:35 +01:00
|
|
|
/**
|
2014-08-09 05:22:05 +02:00
|
|
|
* Gets the name of this region
|
2012-02-21 00:14:55 +01:00
|
|
|
*
|
2014-08-09 05:22:05 +02:00
|
|
|
* @return the name
|
2011-02-24 11:19:35 +01:00
|
|
|
*/
|
|
|
|
public String getId() {
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-08-09 05:22:05 +02:00
|
|
|
* Get a vector containing the smallest X, Y, and Z components for the
|
|
|
|
* corner of the axis-aligned bounding box that contains this region.
|
2011-02-24 11:19:35 +01:00
|
|
|
*
|
2014-08-09 05:22:05 +02:00
|
|
|
* @return the minimum point
|
2011-02-24 11:19:35 +01:00
|
|
|
*/
|
2011-10-18 22:05:19 +02:00
|
|
|
public BlockVector getMinimumPoint() {
|
|
|
|
return min;
|
|
|
|
}
|
2011-02-24 11:19:35 +01:00
|
|
|
|
|
|
|
/**
|
2014-08-09 05:22:05 +02:00
|
|
|
* Get a vector containing the highest X, Y, and Z components for the
|
|
|
|
* corner of the axis-aligned bounding box that contains this region.
|
2011-02-24 11:19:35 +01:00
|
|
|
*
|
2014-08-09 05:22:05 +02:00
|
|
|
* @return the maximum point
|
2011-02-24 11:19:35 +01:00
|
|
|
*/
|
2011-10-18 22:05:19 +02:00
|
|
|
public BlockVector getMaximumPoint() {
|
|
|
|
return max;
|
|
|
|
}
|
2011-02-24 11:19:35 +01:00
|
|
|
|
|
|
|
/**
|
2014-08-09 05:22:05 +02:00
|
|
|
* Get the priority of the region, where higher numbers indicate a higher
|
|
|
|
* priority.
|
|
|
|
*
|
2011-02-24 11:19:35 +01:00
|
|
|
* @return the priority
|
|
|
|
*/
|
|
|
|
public int getPriority() {
|
|
|
|
return priority;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-08-09 05:22:05 +02:00
|
|
|
* Set the priority of the region, where higher numbers indicate a higher
|
|
|
|
* priority.
|
|
|
|
*
|
|
|
|
* @param priority the priority to set
|
2011-02-24 11:19:35 +01:00
|
|
|
*/
|
|
|
|
public void setPriority(int priority) {
|
2014-08-09 05:22:05 +02:00
|
|
|
setDirty(true);
|
2011-02-24 11:19:35 +01:00
|
|
|
this.priority = priority;
|
|
|
|
}
|
2011-09-27 12:13:19 +02:00
|
|
|
|
2011-02-24 11:19:35 +01:00
|
|
|
/**
|
2014-08-09 05:22:05 +02:00
|
|
|
* Get the parent of the region, if one exists.
|
|
|
|
*
|
|
|
|
* @return the parent, or {@code null}
|
2011-02-24 11:19:35 +01:00
|
|
|
*/
|
2014-08-09 05:22:05 +02:00
|
|
|
@Nullable
|
2011-02-24 11:19:35 +01:00
|
|
|
public ProtectedRegion getParent() {
|
|
|
|
return parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-08-09 05:22:05 +02:00
|
|
|
* Set the parent of this region. This checks to make sure that it will
|
|
|
|
* not result in circular inheritance.
|
2011-09-27 12:13:19 +02:00
|
|
|
*
|
2014-08-09 05:22:05 +02:00
|
|
|
* @param parent the new parent
|
2012-02-21 00:14:55 +01:00
|
|
|
* @throws CircularInheritanceException when circular inheritance is detected
|
2011-02-24 11:19:35 +01:00
|
|
|
*/
|
2014-08-09 05:22:05 +02:00
|
|
|
public void setParent(@Nullable ProtectedRegion parent) throws CircularInheritanceException {
|
|
|
|
setDirty(true);
|
|
|
|
|
2011-02-24 11:19:35 +01:00
|
|
|
if (parent == null) {
|
|
|
|
this.parent = null;
|
|
|
|
return;
|
|
|
|
}
|
2011-09-27 12:13:19 +02:00
|
|
|
|
2011-02-24 11:19:35 +01:00
|
|
|
if (parent == this) {
|
|
|
|
throw new CircularInheritanceException();
|
|
|
|
}
|
2011-09-27 12:13:19 +02:00
|
|
|
|
2011-02-24 11:19:35 +01:00
|
|
|
ProtectedRegion p = parent.getParent();
|
|
|
|
while (p != null) {
|
|
|
|
if (p == this) {
|
|
|
|
throw new CircularInheritanceException();
|
|
|
|
}
|
|
|
|
p = p.getParent();
|
|
|
|
}
|
2011-09-27 12:13:19 +02:00
|
|
|
|
2011-02-24 11:19:35 +01:00
|
|
|
this.parent = parent;
|
|
|
|
}
|
|
|
|
|
2014-08-09 05:22:05 +02:00
|
|
|
/**
|
|
|
|
* Clear the parent (set the parent to {@code null}).
|
|
|
|
*/
|
|
|
|
public void clearParent() {
|
|
|
|
setDirty(true);
|
|
|
|
this.parent = null;
|
|
|
|
}
|
2011-02-28 17:19:51 +01:00
|
|
|
|
2011-02-24 11:19:35 +01:00
|
|
|
/**
|
2014-08-09 05:22:05 +02:00
|
|
|
* Get the domain that contains the owners of this region.
|
|
|
|
*
|
|
|
|
* @return the domain
|
2011-02-24 11:19:35 +01:00
|
|
|
*/
|
|
|
|
public DefaultDomain getOwners() {
|
|
|
|
return owners;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-08-09 05:22:05 +02:00
|
|
|
* Set the owner domain.
|
|
|
|
*
|
|
|
|
* @param owners the new domain
|
2011-02-24 11:19:35 +01:00
|
|
|
*/
|
|
|
|
public void setOwners(DefaultDomain owners) {
|
2014-08-09 05:22:05 +02:00
|
|
|
checkNotNull(owners);
|
|
|
|
setDirty(true);
|
2011-02-24 11:19:35 +01:00
|
|
|
this.owners = owners;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-08-09 05:22:05 +02:00
|
|
|
* Get the domain that contains the members of this region, which does
|
|
|
|
* not automatically include the owners.
|
|
|
|
*
|
2011-02-24 11:19:35 +01:00
|
|
|
* @return the members
|
|
|
|
*/
|
|
|
|
public DefaultDomain getMembers() {
|
|
|
|
return members;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-08-09 05:22:05 +02:00
|
|
|
* Set the members domain.
|
|
|
|
*
|
|
|
|
* @param members the new domain
|
2011-02-24 11:19:35 +01:00
|
|
|
*/
|
|
|
|
public void setMembers(DefaultDomain members) {
|
2014-08-09 05:22:05 +02:00
|
|
|
checkNotNull(members);
|
|
|
|
setDirty(true);
|
2011-02-24 11:19:35 +01:00
|
|
|
this.members = members;
|
|
|
|
}
|
2011-06-27 19:11:23 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether a region has members or owners.
|
2011-09-27 12:13:19 +02:00
|
|
|
*
|
2011-06-27 19:11:23 +02:00
|
|
|
* @return whether there are members or owners
|
|
|
|
*/
|
|
|
|
public boolean hasMembersOrOwners() {
|
|
|
|
return owners.size() > 0 || members.size() > 0;
|
|
|
|
}
|
2011-09-27 12:13:19 +02:00
|
|
|
|
2011-02-24 11:19:35 +01:00
|
|
|
/**
|
|
|
|
* Checks whether a player is an owner of region or any of its parents.
|
2011-09-27 12:13:19 +02:00
|
|
|
*
|
2011-06-27 19:11:23 +02:00
|
|
|
* @param player player to check
|
|
|
|
* @return whether an owner
|
2011-02-24 11:19:35 +01:00
|
|
|
*/
|
|
|
|
public boolean isOwner(LocalPlayer player) {
|
2014-08-09 05:22:05 +02:00
|
|
|
checkNotNull(player);
|
|
|
|
|
2011-02-24 11:19:35 +01:00
|
|
|
if (owners.contains(player)) {
|
|
|
|
return true;
|
|
|
|
}
|
2011-09-27 12:13:19 +02:00
|
|
|
|
2011-02-28 17:19:51 +01:00
|
|
|
ProtectedRegion curParent = getParent();
|
|
|
|
while (curParent != null) {
|
|
|
|
if (curParent.getOwners().contains(player)) {
|
2011-02-24 11:19:35 +01:00
|
|
|
return true;
|
|
|
|
}
|
2011-09-27 12:13:19 +02:00
|
|
|
|
2011-02-28 17:19:51 +01:00
|
|
|
curParent = curParent.getParent();
|
2011-02-24 11:19:35 +01:00
|
|
|
}
|
2011-09-27 12:13:19 +02:00
|
|
|
|
2011-02-24 11:19:35 +01:00
|
|
|
return false;
|
|
|
|
}
|
2011-02-27 16:24:07 +01:00
|
|
|
|
2012-03-23 14:44:58 +01:00
|
|
|
/**
|
|
|
|
* Checks whether a player is an owner of region or any of its parents.
|
|
|
|
*
|
|
|
|
* @param playerName player name to check
|
|
|
|
* @return whether an owner
|
2014-08-09 05:22:05 +02:00
|
|
|
* @deprecated Names are deprecated
|
2012-03-23 14:44:58 +01:00
|
|
|
*/
|
2014-08-09 05:22:05 +02:00
|
|
|
@Deprecated
|
2012-03-23 14:44:58 +01:00
|
|
|
public boolean isOwner(String playerName) {
|
2014-08-09 05:22:05 +02:00
|
|
|
checkNotNull(playerName);
|
|
|
|
|
2012-03-23 14:44:58 +01:00
|
|
|
if (owners.contains(playerName)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ProtectedRegion curParent = getParent();
|
|
|
|
while (curParent != null) {
|
|
|
|
if (curParent.getOwners().contains(playerName)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
curParent = curParent.getParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-02-24 11:19:35 +01:00
|
|
|
/**
|
2011-06-27 19:11:23 +02:00
|
|
|
* Checks whether a player is a member OR OWNER of the region
|
|
|
|
* or any of its parents.
|
2011-09-27 12:13:19 +02:00
|
|
|
*
|
2011-06-27 19:11:23 +02:00
|
|
|
* @param player player to check
|
|
|
|
* @return whether an owner or member
|
2011-02-24 11:19:35 +01:00
|
|
|
*/
|
|
|
|
public boolean isMember(LocalPlayer player) {
|
2014-08-09 05:22:05 +02:00
|
|
|
checkNotNull(player);
|
|
|
|
|
2012-03-23 14:44:58 +01:00
|
|
|
if (isOwner(player)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (members.contains(player)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ProtectedRegion curParent = getParent();
|
|
|
|
while (curParent != null) {
|
|
|
|
if (curParent.getMembers().contains(player)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
curParent = curParent.getParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether a player is a member OR OWNER of the region
|
|
|
|
* or any of its parents.
|
|
|
|
*
|
|
|
|
* @param playerName player name to check
|
|
|
|
* @return whether an owner or member
|
2014-08-09 05:22:05 +02:00
|
|
|
* @deprecated Names are deprecated
|
2012-03-23 14:44:58 +01:00
|
|
|
*/
|
2014-08-09 05:22:05 +02:00
|
|
|
@Deprecated
|
2012-03-23 14:44:58 +01:00
|
|
|
public boolean isMember(String playerName) {
|
2014-08-09 05:22:05 +02:00
|
|
|
checkNotNull(playerName);
|
|
|
|
|
2012-03-23 14:44:58 +01:00
|
|
|
if (isOwner(playerName)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (members.contains(playerName)) {
|
2011-02-24 11:19:35 +01:00
|
|
|
return true;
|
|
|
|
}
|
2011-09-27 12:13:19 +02:00
|
|
|
|
2011-02-28 17:19:51 +01:00
|
|
|
ProtectedRegion curParent = getParent();
|
|
|
|
while (curParent != null) {
|
2012-03-23 14:44:58 +01:00
|
|
|
if (curParent.getMembers().contains(playerName)) {
|
2011-02-24 11:19:35 +01:00
|
|
|
return true;
|
|
|
|
}
|
2011-09-27 12:13:19 +02:00
|
|
|
|
2011-02-28 17:19:51 +01:00
|
|
|
curParent = curParent.getParent();
|
2011-02-24 11:19:35 +01:00
|
|
|
}
|
2011-09-27 12:13:19 +02:00
|
|
|
|
2011-02-24 11:19:35 +01:00
|
|
|
return false;
|
|
|
|
}
|
2011-06-27 19:11:23 +02:00
|
|
|
|
|
|
|
/**
|
2014-08-09 05:22:05 +02:00
|
|
|
* Checks whether a player is a member of the region or any of its parents.
|
2011-06-27 19:11:23 +02:00
|
|
|
*
|
|
|
|
* @param player player to check
|
|
|
|
* @return whether an member
|
|
|
|
*/
|
|
|
|
public boolean isMemberOnly(LocalPlayer player) {
|
2014-08-09 05:22:05 +02:00
|
|
|
checkNotNull(player);
|
|
|
|
|
2011-06-27 19:11:23 +02:00
|
|
|
if (members.contains(player)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ProtectedRegion curParent = getParent();
|
|
|
|
while (curParent != null) {
|
|
|
|
if (curParent.getMembers().contains(player)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
curParent = curParent.getParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2011-09-27 12:13:19 +02:00
|
|
|
|
2011-02-24 11:19:35 +01:00
|
|
|
/**
|
2011-03-27 00:59:06 +01:00
|
|
|
* Get a flag's value.
|
2011-09-27 12:13:19 +02:00
|
|
|
*
|
2014-08-09 05:22:05 +02:00
|
|
|
* @param flag the flag to check
|
|
|
|
* @return the value or null if isn't defined
|
|
|
|
* @param <T> the flag type
|
|
|
|
* @param <V> the type of the flag's value
|
2011-02-24 11:19:35 +01:00
|
|
|
*/
|
2011-03-27 00:59:06 +01:00
|
|
|
@SuppressWarnings("unchecked")
|
2014-08-09 05:22:05 +02:00
|
|
|
@Nullable
|
2011-03-27 00:59:06 +01:00
|
|
|
public <T extends Flag<V>, V> V getFlag(T flag) {
|
2014-08-09 05:22:05 +02:00
|
|
|
checkNotNull(flag);
|
|
|
|
|
2011-03-27 00:59:06 +01:00
|
|
|
Object obj = flags.get(flag);
|
|
|
|
V val;
|
2014-08-09 05:22:05 +02:00
|
|
|
|
2011-03-27 00:59:06 +01:00
|
|
|
if (obj != null) {
|
|
|
|
val = (V) obj;
|
|
|
|
} else {
|
2011-03-27 04:33:29 +02:00
|
|
|
return null;
|
2011-02-28 17:19:51 +01:00
|
|
|
}
|
2014-08-09 05:22:05 +02:00
|
|
|
|
2011-03-27 00:59:06 +01:00
|
|
|
return val;
|
|
|
|
}
|
2011-09-27 12:13:19 +02:00
|
|
|
|
2011-03-27 00:59:06 +01:00
|
|
|
/**
|
|
|
|
* Set a flag's value.
|
2011-09-27 12:13:19 +02:00
|
|
|
*
|
2014-08-09 05:22:05 +02:00
|
|
|
* @param flag the flag to check
|
|
|
|
* @param val the value to set
|
|
|
|
* @param <T> the flag type
|
|
|
|
* @param <V> the type of the flag's value
|
2011-03-27 00:59:06 +01:00
|
|
|
*/
|
2014-08-09 05:22:05 +02:00
|
|
|
public <T extends Flag<V>, V> void setFlag(T flag, @Nullable V val) {
|
|
|
|
checkNotNull(flag);
|
|
|
|
setDirty(true);
|
|
|
|
|
2011-04-01 22:39:29 +02:00
|
|
|
if (val == null) {
|
|
|
|
flags.remove(flag);
|
|
|
|
} else {
|
|
|
|
flags.put(flag, val);
|
|
|
|
}
|
2011-02-24 11:19:35 +01:00
|
|
|
}
|
2011-09-27 12:13:19 +02:00
|
|
|
|
2011-04-02 07:11:57 +02:00
|
|
|
/**
|
|
|
|
* Get the map of flags.
|
2011-09-27 12:13:19 +02:00
|
|
|
*
|
2014-08-09 05:22:05 +02:00
|
|
|
* @return the map of flags currently used for this region
|
2011-04-02 07:11:57 +02:00
|
|
|
*/
|
|
|
|
public Map<Flag<?>, Object> getFlags() {
|
|
|
|
return flags;
|
|
|
|
}
|
2011-09-27 12:13:19 +02:00
|
|
|
|
2011-04-02 09:33:07 +02:00
|
|
|
/**
|
2014-08-09 05:22:05 +02:00
|
|
|
* Set the map of flags.
|
2011-09-27 12:13:19 +02:00
|
|
|
*
|
2014-08-09 05:22:05 +02:00
|
|
|
* <p>A copy of the map will be used.</p>
|
|
|
|
*
|
|
|
|
* @param flags the flags to set
|
2011-04-02 09:33:07 +02:00
|
|
|
*/
|
|
|
|
public void setFlags(Map<Flag<?>, Object> flags) {
|
2014-08-09 05:22:05 +02:00
|
|
|
checkNotNull(flags);
|
|
|
|
|
|
|
|
setDirty(true);
|
|
|
|
this.flags = new ConcurrentHashMap<Flag<?>, Object>(flags);
|
2011-04-02 09:33:07 +02:00
|
|
|
}
|
2011-02-25 01:36:47 +01:00
|
|
|
|
2014-08-15 06:22:39 +02:00
|
|
|
/**
|
|
|
|
* Copy attributes from another region.
|
|
|
|
*
|
|
|
|
* @param other the other region
|
|
|
|
*/
|
|
|
|
public void copyFrom(ProtectedRegion other) {
|
|
|
|
checkNotNull(other);
|
|
|
|
setMembers(other.getMembers());
|
|
|
|
setOwners(other.getOwners());
|
|
|
|
setFlags(other.getFlags());
|
|
|
|
setPriority(other.getPriority());
|
|
|
|
try {
|
|
|
|
setParent(other.getParent());
|
|
|
|
} catch (CircularInheritanceException ignore) {
|
|
|
|
// This should not be thrown
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-18 22:05:19 +02:00
|
|
|
/**
|
2014-08-09 05:22:05 +02:00
|
|
|
* Get points of the region projected onto the X-Z plane.
|
2011-10-18 22:05:19 +02:00
|
|
|
*
|
2014-08-09 05:22:05 +02:00
|
|
|
* @return the points
|
2011-10-18 22:05:19 +02:00
|
|
|
*/
|
|
|
|
public abstract List<BlockVector2D> getPoints();
|
|
|
|
|
2011-02-25 01:36:47 +01:00
|
|
|
/**
|
2014-08-09 05:22:05 +02:00
|
|
|
* Get the number of blocks in this region.
|
2011-09-27 12:13:19 +02:00
|
|
|
*
|
2012-02-21 00:14:55 +01:00
|
|
|
* @return the volume of this region in blocks
|
2011-02-25 01:36:47 +01:00
|
|
|
*/
|
2011-03-30 21:01:11 +02:00
|
|
|
public abstract int volume();
|
2011-09-27 12:13:19 +02:00
|
|
|
|
2011-02-24 11:19:35 +01:00
|
|
|
/**
|
|
|
|
* Check to see if a point is inside this region.
|
2011-09-27 12:13:19 +02:00
|
|
|
*
|
2012-02-21 00:14:55 +01:00
|
|
|
* @param pt The point to check
|
|
|
|
* @return Whether {@code pt} is in this region
|
2011-02-24 11:19:35 +01:00
|
|
|
*/
|
|
|
|
public abstract boolean contains(Vector pt);
|
2011-09-27 12:13:19 +02:00
|
|
|
|
2011-10-18 22:05:19 +02:00
|
|
|
/**
|
2014-08-09 05:22:05 +02:00
|
|
|
* Check to see if a position is contained within this region.
|
2011-10-18 22:05:19 +02:00
|
|
|
*
|
2014-08-09 05:22:05 +02:00
|
|
|
* @param position the position to check
|
|
|
|
* @return whether {@code position} is in this region
|
2011-10-18 22:05:19 +02:00
|
|
|
*/
|
2014-08-09 05:22:05 +02:00
|
|
|
public boolean contains(BlockVector2D position) {
|
|
|
|
checkNotNull(position);
|
|
|
|
return contains(new Vector(position.getBlockX(), min.getBlockY(), position.getBlockZ()));
|
2011-10-18 22:05:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check to see if a point is inside this region.
|
|
|
|
*
|
2014-08-09 05:22:05 +02:00
|
|
|
* @param x the x coordinate to check
|
|
|
|
* @param y the y coordinate to check
|
|
|
|
* @param z the z coordinate to check
|
|
|
|
* @return whether this region contains the point
|
2011-10-18 22:05:19 +02:00
|
|
|
*/
|
|
|
|
public boolean contains(int x, int y, int z) {
|
|
|
|
return contains(new Vector(x, y, z));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-08-09 05:22:05 +02:00
|
|
|
* Check to see if any of the points are inside this region projected
|
|
|
|
* onto the X-Z plane.
|
2011-10-18 22:05:19 +02:00
|
|
|
*
|
2014-08-09 05:22:05 +02:00
|
|
|
* @param positions a list of positions
|
2014-08-03 03:14:25 +02:00
|
|
|
* @return true if contained
|
2011-10-18 22:05:19 +02:00
|
|
|
*/
|
2014-08-09 05:22:05 +02:00
|
|
|
public boolean containsAny(List<BlockVector2D> positions) {
|
|
|
|
checkNotNull(positions);
|
|
|
|
|
|
|
|
for (BlockVector2D pt : positions) {
|
2011-10-18 22:05:19 +02:00
|
|
|
if (contains(pt)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2014-08-09 05:22:05 +02:00
|
|
|
|
2011-10-18 22:05:19 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-02-24 11:19:35 +01:00
|
|
|
/**
|
2014-08-09 05:22:05 +02:00
|
|
|
* Get the type of region.
|
2011-09-27 12:13:19 +02:00
|
|
|
*
|
2014-08-09 05:22:05 +02:00
|
|
|
* @return the type
|
2011-02-24 11:19:35 +01:00
|
|
|
*/
|
2014-08-09 05:22:05 +02:00
|
|
|
public abstract RegionType getType();
|
2011-03-27 04:33:29 +02:00
|
|
|
|
2011-02-24 11:19:35 +01:00
|
|
|
/**
|
|
|
|
* Return the type of region as a user-friendly, lowercase name.
|
2011-09-27 12:13:19 +02:00
|
|
|
*
|
2011-02-24 11:19:35 +01:00
|
|
|
* @return type of region
|
2014-08-09 05:22:05 +02:00
|
|
|
* @deprecated use {@link #getType()}
|
2011-02-24 11:19:35 +01:00
|
|
|
*/
|
2014-08-09 05:22:05 +02:00
|
|
|
@Deprecated
|
|
|
|
public final String getTypeName() {
|
|
|
|
return getType().getName();
|
|
|
|
}
|
2011-02-27 18:16:09 +01:00
|
|
|
|
2011-05-04 09:52:17 +02:00
|
|
|
/**
|
2014-08-09 05:22:05 +02:00
|
|
|
* Return a list of regions from the given list of regions that intersect
|
|
|
|
* with this region.
|
2011-09-27 12:13:19 +02:00
|
|
|
*
|
2014-08-09 05:22:05 +02:00
|
|
|
* @param regions a list of regions to source from
|
|
|
|
* @return the elements of {@code regions} that intersect with this region
|
2011-05-04 09:52:17 +02:00
|
|
|
*/
|
2014-08-09 05:22:05 +02:00
|
|
|
public abstract List<ProtectedRegion> getIntersectingRegions(Collection<ProtectedRegion> regions);
|
2011-09-27 12:13:19 +02:00
|
|
|
|
2011-10-18 22:05:19 +02:00
|
|
|
/**
|
|
|
|
* Checks if the bounding box of a region intersects with with the bounding
|
2014-08-09 05:22:05 +02:00
|
|
|
* box of this region.
|
2011-10-18 22:05:19 +02:00
|
|
|
*
|
2014-08-09 05:22:05 +02:00
|
|
|
* @param region the region to check
|
2012-02-21 00:14:55 +01:00
|
|
|
* @return whether the given region intersects
|
2011-10-18 22:05:19 +02:00
|
|
|
*/
|
|
|
|
protected boolean intersectsBoundingBox(ProtectedRegion region) {
|
|
|
|
BlockVector rMaxPoint = region.getMaximumPoint();
|
|
|
|
BlockVector min = getMinimumPoint();
|
|
|
|
|
|
|
|
if (rMaxPoint.getBlockX() < min.getBlockX()) return false;
|
|
|
|
if (rMaxPoint.getBlockY() < min.getBlockY()) return false;
|
|
|
|
if (rMaxPoint.getBlockZ() < min.getBlockZ()) return false;
|
|
|
|
|
|
|
|
BlockVector rMinPoint = region.getMinimumPoint();
|
|
|
|
BlockVector max = getMaximumPoint();
|
|
|
|
|
|
|
|
if (rMinPoint.getBlockX() > max.getBlockX()) return false;
|
|
|
|
if (rMinPoint.getBlockY() > max.getBlockY()) return false;
|
|
|
|
if (rMinPoint.getBlockZ() > max.getBlockZ()) return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-10-19 00:52:25 +02:00
|
|
|
/**
|
2014-08-09 05:22:05 +02:00
|
|
|
* Compares all edges of two regions to see if any of them intersect.
|
2011-10-19 00:52:25 +02:00
|
|
|
*
|
2014-08-09 05:22:05 +02:00
|
|
|
* @param region the region to check
|
2012-02-21 00:14:55 +01:00
|
|
|
* @return whether any edges of a region intersect
|
2011-10-19 00:52:25 +02:00
|
|
|
*/
|
|
|
|
protected boolean intersectsEdges(ProtectedRegion region) {
|
|
|
|
List<BlockVector2D> pts1 = getPoints();
|
|
|
|
List<BlockVector2D> pts2 = region.getPoints();
|
|
|
|
BlockVector2D lastPt1 = pts1.get(pts1.size() - 1);
|
|
|
|
BlockVector2D lastPt2 = pts2.get(pts2.size() - 1);
|
2012-11-03 05:19:20 +01:00
|
|
|
for (BlockVector2D aPts1 : pts1) {
|
|
|
|
for (BlockVector2D aPts2 : pts2) {
|
2011-10-19 00:52:25 +02:00
|
|
|
|
|
|
|
Line2D line1 = new Line2D.Double(
|
|
|
|
lastPt1.getBlockX(),
|
|
|
|
lastPt1.getBlockZ(),
|
2012-11-03 05:19:20 +01:00
|
|
|
aPts1.getBlockX(),
|
|
|
|
aPts1.getBlockZ());
|
2011-10-19 00:52:25 +02:00
|
|
|
|
|
|
|
if (line1.intersectsLine(
|
2012-11-03 05:19:20 +01:00
|
|
|
lastPt2.getBlockX(),
|
|
|
|
lastPt2.getBlockZ(),
|
|
|
|
aPts2.getBlockX(),
|
|
|
|
aPts2.getBlockZ())) {
|
2011-10-19 00:52:25 +02:00
|
|
|
return true;
|
|
|
|
}
|
2012-11-03 05:19:20 +01:00
|
|
|
lastPt2 = aPts2;
|
2011-10-19 00:52:25 +02:00
|
|
|
}
|
2012-11-03 05:19:20 +01:00
|
|
|
lastPt1 = aPts1;
|
2011-10-19 00:52:25 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-08-09 05:22:05 +02:00
|
|
|
@Override
|
|
|
|
public boolean isDirty() {
|
|
|
|
return dirty || owners.isDirty() || members.isDirty();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setDirty(boolean dirty) {
|
|
|
|
this.dirty = dirty;
|
|
|
|
owners.setDirty(dirty);
|
|
|
|
members.setDirty(dirty);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int compareTo(ProtectedRegion other) {
|
|
|
|
if (getPriority() > other.getPriority()) {
|
|
|
|
return -1;
|
|
|
|
} else if (getPriority() < other.getPriority()) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return getId().compareTo(other.getId());
|
2011-04-02 11:03:32 +02:00
|
|
|
}
|
2011-09-27 12:13:19 +02:00
|
|
|
|
2011-05-27 06:55:23 +02:00
|
|
|
@Override
|
|
|
|
public int hashCode(){
|
|
|
|
return id.hashCode();
|
|
|
|
}
|
2011-09-27 12:13:19 +02:00
|
|
|
|
2011-05-27 06:55:23 +02:00
|
|
|
@Override
|
|
|
|
public boolean equals(Object obj) {
|
|
|
|
if (!(obj instanceof ProtectedRegion)) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-09-27 12:13:19 +02:00
|
|
|
|
2011-05-27 06:55:23 +02:00
|
|
|
ProtectedRegion other = (ProtectedRegion) obj;
|
|
|
|
return other.getId().equals(getId());
|
|
|
|
}
|
2011-09-27 12:13:19 +02:00
|
|
|
|
2014-08-09 05:22:05 +02:00
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return "ProtectedRegion{" +
|
|
|
|
"id='" + id + "', " +
|
|
|
|
"type='" + getType() + '\'' +
|
|
|
|
'}';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks to see if the given ID is a valid ID.
|
|
|
|
*
|
|
|
|
* @param id the id to check
|
|
|
|
* @return whether the region id given is valid
|
|
|
|
*/
|
|
|
|
public static boolean isValidId(String id) {
|
|
|
|
checkNotNull(id);
|
|
|
|
return VALID_ID_PATTERN.matcher(id).matches();
|
|
|
|
}
|
|
|
|
|
2011-02-24 11:19:35 +01:00
|
|
|
/**
|
2014-08-09 05:22:05 +02:00
|
|
|
* Thrown when setting a parent would create a circular inheritance
|
2011-02-24 11:19:35 +01:00
|
|
|
* situation.
|
|
|
|
*/
|
|
|
|
public static class CircularInheritanceException extends Exception {
|
|
|
|
}
|
2014-08-03 03:14:25 +02:00
|
|
|
|
2011-02-24 11:19:35 +01:00
|
|
|
}
|