WorldGuard/src/main/java/com/sk89q/worldguard/protection/regions/ProtectedRegion.java

408 lines
9.1 KiB
Java
Raw Normal View History

// $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;
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;
import com.sk89q.worldguard.protection.flags.Flag;
/**
* Represents a region of any shape and size that can be protected.
2011-09-27 12:13:19 +02:00
*
* @author sk89q
*/
public abstract class ProtectedRegion implements Comparable<ProtectedRegion> {
2011-09-27 12:13:19 +02:00
private static final Pattern idPattern = Pattern.compile("^[A-Za-z0-9_,'\\-\\+/]{1,}$");
2011-09-27 12:13:19 +02:00
/**
* Holds the region's ID.
*/
private String id;
2011-09-27 12:13:19 +02:00
/**
* Priority.
*/
private int priority = 0;
2011-09-27 12:13:19 +02:00
/**
2011-02-28 17:19:51 +01:00
* Holds the curParent.
*/
2011-04-02 23:11:12 +02:00
private ProtectedRegion parent;
2011-09-27 12:13:19 +02:00
/**
* List of owners.
*/
private DefaultDomain owners = new DefaultDomain();
2011-09-27 12:13:19 +02:00
/**
* List of members.
*/
private DefaultDomain members = new DefaultDomain();
2011-09-27 12:13:19 +02:00
/**
* List of flags.
*/
private Map<Flag<?>, Object> flags = new HashMap<Flag<?>, Object>();
/**
* 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;
}
/**
* @param priority the priority to setFlag
*/
public void setPriority(int priority) {
this.priority = priority;
}
2011-09-27 12:13:19 +02:00
/**
2011-02-28 17:19:51 +01:00
* @return the curParent
*/
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
* in circular inheritance.
2011-09-27 12:13:19 +02:00
*
* @param parent the curParent to setFlag
2011-09-27 12:13:19 +02:00
* @throws CircularInheritanceException
*/
public void setParent(ProtectedRegion parent) throws CircularInheritanceException {
if (parent == null) {
this.parent = null;
return;
}
2011-09-27 12:13:19 +02:00
if (parent == this) {
throw new CircularInheritanceException();
}
2011-09-27 12:13:19 +02: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
this.parent = parent;
}
2011-02-28 17:19:51 +01:00
/**
* @return the owners
*/
public DefaultDomain getOwners() {
return owners;
}
/**
* @param owners the owners to setFlag
*/
public void setOwners(DefaultDomain owners) {
this.owners = owners;
}
/**
* @return the members
*/
public DefaultDomain getMembers() {
return members;
}
/**
* @param members the members to setFlag
*/
public void setMembers(DefaultDomain members) {
this.members = members;
}
/**
* Checks whether a region has members or owners.
2011-09-27 12:13:19 +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
/**
* Checks whether a player is an owner of region or any of its parents.
2011-09-27 12:13:19 +02:00
*
* @param player player to check
* @return whether an owner
*/
public boolean isOwner(LocalPlayer player) {
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)) {
return true;
}
2011-09-27 12:13:19 +02:00
2011-02-28 17:19:51 +01:00
curParent = curParent.getParent();
}
2011-09-27 12:13:19 +02:00
return false;
}
/**
* 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
*
* @param player player to check
* @return whether an owner or member
*/
public boolean isMember(LocalPlayer player) {
if (owners.contains(player) || members.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)
|| curParent.getMembers().contains(player)) {
return true;
}
2011-09-27 12:13:19 +02:00
2011-02-28 17:19:51 +01:00
curParent = curParent.getParent();
}
2011-09-27 12:13:19 +02:00
return false;
}
/**
* 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-09-27 12:13:19 +02:00
/**
* Get a flag's value.
2011-09-27 12:13:19 +02:00
*
* @param <T>
* @param <V>
* @param flag
* @return value or null if isn't defined
*/
@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 {
return null;
2011-02-28 17:19:51 +01:00
}
return val;
}
2011-09-27 12:13:19 +02:00
/**
* Set a flag's value.
2011-09-27 12:13:19 +02:00
*
* @param <T>
* @param <V>
* @param flag
* @param val
*/
public <T extends Flag<V>, V> void setFlag(T flag, V val) {
if (val == null) {
flags.remove(flag);
} else {
flags.put(flag, val);
}
}
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
*
2011-04-02 07:11:57 +02:00
* @return
*/
public Map<Flag<?>, Object> getFlags() {
return flags;
}
2011-09-27 12:13:19 +02:00
2011-04-02 09:33:07 +02:00
/**
* Get the map of flags.
2011-09-27 12:13:19 +02:00
*
* @param flags
2011-04-02 09:33:07 +02:00
*/
public void setFlags(Map<Flag<?>, Object> flags) {
this.flags = flags;
}
/**
2011-03-30 21:01:11 +02:00
* Get the number of blocks in this region
2011-09-27 12:13:19 +02:00
*
* @return
*/
2011-03-30 21:01:11 +02:00
public abstract int volume();
2011-09-27 12:13:19 +02:00
/**
* Check to see if a point is inside this region.
2011-09-27 12:13:19 +02:00
*
* @param pt
* @return
*/
public abstract boolean contains(Vector pt);
2011-09-27 12:13:19 +02:00
/**
* Compares to another region.
2011-09-27 12:13:19 +02:00
*
* @param other
* @return
*/
public int compareTo(ProtectedRegion other) {
if (id.equals(other.id)) {
return 0;
} else if (priority == other.priority) {
return 1;
} else if (priority > other.priority) {
return -1;
} else {
return 1;
}
}
/**
* Return the type of region as a user-friendly, lowercase name.
2011-09-27 12:13:19 +02:00
*
* @return type of region
*/
public abstract String getTypeName();
/**
* Get a list of intersecting regions.
2011-09-27 12:13:19 +02:00
*
* @param regions
* @return
* @throws UnsupportedIntersectionException
*/
public abstract List<ProtectedRegion> getIntersectingRegions(
List<ProtectedRegion> regions)
throws UnsupportedIntersectionException;
2011-09-27 12:13:19 +02:00
/**
* Checks to see if the given ID is accurate.
2011-09-27 12:13:19 +02:00
*
* @param id
* @return
*/
public static boolean isValidId(String id) {
return idPattern.matcher(id).matches();
}
2011-09-27 12:13:19 +02:00
/**
* Returns the hash code.
*/
@Override
public int hashCode(){
return id.hashCode();
}
2011-09-27 12:13:19 +02:00
/**
* Returns whether this region has the same ID as another region.
*/
@Override
public boolean equals(Object obj) {
if (!(obj instanceof ProtectedRegion)) {
return false;
}
2011-09-27 12:13:19 +02:00
ProtectedRegion other = (ProtectedRegion) obj;
return other.getId().equals(getId());
}
2011-09-27 12:13:19 +02:00
/**
2011-02-28 17:19:51 +01:00
* Thrown when setting a curParent would create a circular inheritance
* situation.
2011-09-27 12:13:19 +02:00
*
*/
public static class CircularInheritanceException extends Exception {
private static final long serialVersionUID = 7479613488496776022L;
}
}