2011-02-24 11:19:35 +01:00
|
|
|
// $Id$
|
|
|
|
/*
|
|
|
|
* WorldGuard
|
|
|
|
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.sk89q.worldguard.protection.regions;
|
|
|
|
|
2011-07-16 23:07:29 +02:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
2011-02-24 11:19:35 +01:00
|
|
|
import com.sk89q.worldedit.BlockVector;
|
|
|
|
import com.sk89q.worldedit.Vector;
|
|
|
|
import com.sk89q.worldguard.LocalPlayer;
|
|
|
|
import com.sk89q.worldguard.domains.DefaultDomain;
|
|
|
|
import com.sk89q.worldguard.protection.UnsupportedIntersectionException;
|
2011-03-27 00:59:06 +01:00
|
|
|
import com.sk89q.worldguard.protection.flags.Flag;
|
2011-02-24 11:19:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents a region of any shape and size that can be protected.
|
|
|
|
*
|
|
|
|
* @author sk89q
|
|
|
|
*/
|
|
|
|
public abstract class ProtectedRegion implements Comparable<ProtectedRegion> {
|
2011-04-02 11:03:32 +02:00
|
|
|
|
|
|
|
private static final Pattern idPattern = Pattern.compile("^[A-Za-z0-9_,'\\-\\+/]{1,}$");
|
|
|
|
|
2011-02-24 11:19:35 +01:00
|
|
|
/**
|
|
|
|
* Holds the region's ID.
|
|
|
|
*/
|
|
|
|
private String id;
|
2011-05-04 09:52:17 +02:00
|
|
|
|
2011-02-24 11:19:35 +01:00
|
|
|
/**
|
|
|
|
* Priority.
|
|
|
|
*/
|
|
|
|
private int priority = 0;
|
2011-05-04 09:52:17 +02:00
|
|
|
|
2011-02-24 11:19:35 +01:00
|
|
|
/**
|
2011-02-28 17:19:51 +01:00
|
|
|
* Holds the curParent.
|
2011-02-24 11:19:35 +01:00
|
|
|
*/
|
2011-04-02 23:11:12 +02:00
|
|
|
private ProtectedRegion parent;
|
2011-05-04 09:52:17 +02:00
|
|
|
|
2011-02-24 11:19:35 +01:00
|
|
|
/**
|
|
|
|
* List of owners.
|
|
|
|
*/
|
|
|
|
private DefaultDomain owners = new DefaultDomain();
|
2011-05-04 09:52:17 +02:00
|
|
|
|
2011-02-24 11:19:35 +01:00
|
|
|
/**
|
|
|
|
* List of members.
|
|
|
|
*/
|
|
|
|
private DefaultDomain members = new DefaultDomain();
|
2011-05-04 09:52:17 +02:00
|
|
|
|
2011-02-24 11:19:35 +01:00
|
|
|
/**
|
2011-03-27 00:59:06 +01:00
|
|
|
* List of flags.
|
2011-02-24 11:19:35 +01:00
|
|
|
*/
|
2011-03-27 00:59:06 +01:00
|
|
|
private Map<Flag<?>, Object> flags = new HashMap<Flag<?>, Object>();
|
2011-02-24 11:19:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct a new instance of this region.
|
|
|
|
*
|
|
|
|
* @param id
|
|
|
|
*/
|
|
|
|
public ProtectedRegion(String id) {
|
|
|
|
this.id = id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the id
|
|
|
|
*/
|
|
|
|
public String getId() {
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the lower point of the cuboid.
|
|
|
|
*
|
|
|
|
* @return min point
|
|
|
|
*/
|
|
|
|
public abstract BlockVector getMinimumPoint();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the upper point of the cuboid.
|
|
|
|
*
|
|
|
|
* @return max point
|
|
|
|
*/
|
|
|
|
public abstract BlockVector getMaximumPoint();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the priority
|
|
|
|
*/
|
|
|
|
public int getPriority() {
|
|
|
|
return priority;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-02-25 20:58:48 +01:00
|
|
|
* @param priority the priority to setFlag
|
2011-02-24 11:19:35 +01:00
|
|
|
*/
|
|
|
|
public void setPriority(int priority) {
|
|
|
|
this.priority = priority;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-02-28 17:19:51 +01:00
|
|
|
* @return the curParent
|
2011-02-24 11:19:35 +01:00
|
|
|
*/
|
|
|
|
public ProtectedRegion getParent() {
|
|
|
|
return parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-02-28 17:19:51 +01:00
|
|
|
* Set the curParent. This checks to make sure that it will not result
|
2011-02-24 11:19:35 +01:00
|
|
|
* in circular inheritance.
|
|
|
|
*
|
2011-03-08 19:01:03 +01:00
|
|
|
* @param parent the curParent to setFlag
|
2011-02-24 11:19:35 +01:00
|
|
|
* @throws CircularInheritanceException
|
|
|
|
*/
|
|
|
|
public void setParent(ProtectedRegion parent) throws CircularInheritanceException {
|
|
|
|
if (parent == null) {
|
|
|
|
this.parent = null;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parent == this) {
|
|
|
|
throw new CircularInheritanceException();
|
|
|
|
}
|
|
|
|
|
|
|
|
ProtectedRegion p = parent.getParent();
|
|
|
|
while (p != null) {
|
|
|
|
if (p == this) {
|
|
|
|
throw new CircularInheritanceException();
|
|
|
|
}
|
|
|
|
p = p.getParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.parent = parent;
|
|
|
|
}
|
|
|
|
|
2011-02-28 17:19:51 +01:00
|
|
|
|
2011-02-24 11:19:35 +01:00
|
|
|
/**
|
|
|
|
* @return the owners
|
|
|
|
*/
|
|
|
|
public DefaultDomain getOwners() {
|
|
|
|
|
|
|
|
return owners;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-02-25 20:58:48 +01:00
|
|
|
* @param owners the owners to setFlag
|
2011-02-24 11:19:35 +01:00
|
|
|
*/
|
|
|
|
public void setOwners(DefaultDomain owners) {
|
|
|
|
this.owners = owners;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the members
|
|
|
|
*/
|
|
|
|
public DefaultDomain getMembers() {
|
|
|
|
return members;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-03-08 19:01:03 +01:00
|
|
|
* @param members the members to setFlag
|
2011-02-24 11:19:35 +01:00
|
|
|
*/
|
|
|
|
public void setMembers(DefaultDomain members) {
|
|
|
|
this.members = members;
|
|
|
|
}
|
2011-06-27 19:11:23 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether a region has members or owners.
|
|
|
|
*
|
|
|
|
* @return whether there are members or owners
|
|
|
|
*/
|
|
|
|
public boolean hasMembersOrOwners() {
|
|
|
|
return owners.size() > 0 || members.size() > 0;
|
|
|
|
}
|
2011-02-24 11:19:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether a player is an owner of region or any of its parents.
|
|
|
|
*
|
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) {
|
|
|
|
if (owners.contains(player)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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-02-28 17:19:51 +01:00
|
|
|
curParent = curParent.getParent();
|
2011-02-24 11:19:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2011-02-27 16:24:07 +01:00
|
|
|
|
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-02-24 11:19:35 +01: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) {
|
|
|
|
if (owners.contains(player) || members.contains(player)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-02-28 17:19:51 +01:00
|
|
|
ProtectedRegion curParent = getParent();
|
|
|
|
while (curParent != null) {
|
|
|
|
if (curParent.getOwners().contains(player)
|
|
|
|
|| curParent.getMembers().contains(player)) {
|
2011-02-24 11:19:35 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-02-28 17:19:51 +01:00
|
|
|
curParent = curParent.getParent();
|
2011-02-24 11:19:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2011-06-27 19:11:23 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether a player is a member of the region
|
|
|
|
* or any of its parents.
|
|
|
|
*
|
|
|
|
* @param player player to check
|
|
|
|
* @return whether an member
|
|
|
|
*/
|
|
|
|
public boolean isMemberOnly(LocalPlayer player) {
|
|
|
|
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-03-27 00:59:06 +01:00
|
|
|
|
2011-02-24 11:19:35 +01:00
|
|
|
/**
|
2011-03-27 00:59:06 +01:00
|
|
|
* Get a flag's value.
|
2011-02-24 11:19:35 +01:00
|
|
|
*
|
2011-03-27 00:59:06 +01:00
|
|
|
* @param <T>
|
|
|
|
* @param <V>
|
|
|
|
* @param flag
|
2011-03-27 04:33:29 +02:00
|
|
|
* @return value or null if isn't defined
|
2011-02-24 11:19:35 +01:00
|
|
|
*/
|
2011-03-27 00:59:06 +01:00
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public <T extends Flag<V>, V> V getFlag(T flag) {
|
|
|
|
Object obj = flags.get(flag);
|
|
|
|
V val;
|
|
|
|
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
|
|
|
}
|
2011-03-27 00:59:06 +01:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a flag's value.
|
|
|
|
*
|
|
|
|
* @param <T>
|
|
|
|
* @param <V>
|
|
|
|
* @param flag
|
|
|
|
* @param val
|
|
|
|
*/
|
|
|
|
public <T extends Flag<V>, V> void setFlag(T flag, V val) {
|
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-04-02 07:11:57 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the map of flags.
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public Map<Flag<?>, Object> getFlags() {
|
|
|
|
return flags;
|
|
|
|
}
|
2011-04-02 09:33:07 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the map of flags.
|
|
|
|
*
|
|
|
|
* @param flags
|
|
|
|
*/
|
|
|
|
public void setFlags(Map<Flag<?>, Object> flags) {
|
|
|
|
this.flags = flags;
|
|
|
|
}
|
2011-02-25 01:36:47 +01:00
|
|
|
|
|
|
|
/**
|
2011-03-30 21:01:11 +02:00
|
|
|
* Get the number of blocks in this region
|
2011-02-25 01:36:47 +01:00
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
2011-03-30 21:01:11 +02:00
|
|
|
public abstract int volume();
|
2011-02-24 11:19:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check to see if a point is inside this region.
|
|
|
|
*
|
|
|
|
* @param pt
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public abstract boolean contains(Vector pt);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compares to another region.
|
|
|
|
*
|
|
|
|
* @param other
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public int compareTo(ProtectedRegion other) {
|
2011-05-27 06:55:23 +02:00
|
|
|
if (id.equals(other.id)) {
|
2011-02-24 11:19:35 +01:00
|
|
|
return 0;
|
2011-05-27 06:55:23 +02:00
|
|
|
} else if (priority == other.priority) {
|
|
|
|
return 1;
|
2011-02-24 11:19:35 +01:00
|
|
|
} else if (priority > other.priority) {
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
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.
|
|
|
|
*
|
|
|
|
* @return type of region
|
|
|
|
*/
|
|
|
|
public abstract String getTypeName();
|
2011-02-27 18:16:09 +01:00
|
|
|
|
2011-05-04 09:52:17 +02:00
|
|
|
/**
|
|
|
|
* Get a list of intersecting regions.
|
|
|
|
*
|
|
|
|
* @param regions
|
|
|
|
* @return
|
|
|
|
* @throws UnsupportedIntersectionException
|
|
|
|
*/
|
2011-03-27 04:33:29 +02:00
|
|
|
public abstract List<ProtectedRegion> getIntersectingRegions(
|
|
|
|
List<ProtectedRegion> regions)
|
|
|
|
throws UnsupportedIntersectionException;
|
2011-02-24 11:19:35 +01:00
|
|
|
|
2011-04-02 11:03:32 +02:00
|
|
|
/**
|
|
|
|
* Checks to see if the given ID is accurate.
|
|
|
|
*
|
|
|
|
* @param id
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public static boolean isValidId(String id) {
|
|
|
|
return idPattern.matcher(id).matches();
|
|
|
|
}
|
|
|
|
|
2011-05-27 06:55:23 +02:00
|
|
|
/**
|
|
|
|
* Returns the hash code.
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public int hashCode(){
|
|
|
|
return id.hashCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether this region has the same ID as another region.
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object obj) {
|
|
|
|
if (!(obj instanceof ProtectedRegion)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ProtectedRegion other = (ProtectedRegion) obj;
|
|
|
|
return other.getId().equals(getId());
|
|
|
|
}
|
|
|
|
|
2011-02-24 11:19:35 +01:00
|
|
|
/**
|
2011-02-28 17:19:51 +01:00
|
|
|
* Thrown when setting a curParent would create a circular inheritance
|
2011-02-24 11:19:35 +01:00
|
|
|
* situation.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static class CircularInheritanceException extends Exception {
|
|
|
|
private static final long serialVersionUID = 7479613488496776022L;
|
|
|
|
}
|
|
|
|
}
|