Added proper support for global regions and a region ID validation regex.

This commit is contained in:
sk89q 2011-04-02 02:03:32 -07:00
parent 949c6599b3
commit 521ecd438e
3 changed files with 59 additions and 3 deletions

View File

@ -55,7 +55,15 @@ public static void define(CommandContext args, WorldGuardPlugin plugin,
Player player = plugin.checkPlayer(sender);
WorldEditPlugin worldEdit = plugin.getWorldEdit();
String id = args.getString(0).replace(".", "");
String id = args.getString(0);
if (!ProtectedRegion.isValidId(id)) {
throw new CommandException("Invalid region ID specified!");
}
if (id.equalsIgnoreCase("__global__")) {
throw new CommandException("A region cannot be named __global__");
}
// Attempt to get the player's selection from WorldEdit
Selection sel = worldEdit.getSelection(player);
@ -110,6 +118,10 @@ public static void redefine(CommandContext args, WorldGuardPlugin plugin,
WorldEditPlugin worldEdit = plugin.getWorldEdit();
LocalPlayer localPlayer = plugin.wrapPlayer(player);
String id = args.getString(0);
if (id.equalsIgnoreCase("__global__")) {
throw new CommandException("The region cannot be named __global__");
}
RegionManager mgr = plugin.getGlobalRegionManager().get(world);
ProtectedRegion existing = mgr.getRegion(id);
@ -182,7 +194,15 @@ public static void claim(CommandContext args, WorldGuardPlugin plugin,
Player player = plugin.checkPlayer(sender);
LocalPlayer localPlayer = plugin.wrapPlayer(player);
WorldEditPlugin worldEdit = plugin.getWorldEdit();
String id = args.getString(0).replace(".", "");
String id = args.getString(0);
if (!ProtectedRegion.isValidId(id)) {
throw new CommandException("Invalid region ID specified!");
}
if (id.equalsIgnoreCase("__global__")) {
throw new CommandException("A region cannot be named __global__");
}
// Attempt to get the player's selection from WorldEdit
Selection sel = worldEdit.getSelection(player);
@ -310,6 +330,10 @@ public static void info(CommandContext args, WorldGuardPlugin plugin,
id = args.getString(1).toLowerCase();
}
if (!ProtectedRegion.isValidId(id)) {
throw new CommandException("Invalid region ID specified!");
}
RegionManager mgr = plugin.getGlobalRegionManager().get(world);
if (!mgr.hasRegion(id)) {
@ -435,7 +459,12 @@ public static void flag(CommandContext args, WorldGuardPlugin plugin,
ProtectedRegion region = mgr.getRegion(id);
if (region == null) {
throw new CommandException("Could not find a region by that ID.");
if (id.equalsIgnoreCase("__global__")) {
region = new GlobalProtectedRegion(id);
mgr.addRegion(region);
} else {
throw new CommandException("Could not find a region by that ID.");
}
}
if (region.isOwner(localPlayer)) {
@ -518,6 +547,10 @@ public static void setPriority(CommandContext args, WorldGuardPlugin plugin,
String id = args.getString(0);
int priority = args.getInteger(1);
if (id.equalsIgnoreCase("__global__")) {
throw new CommandException("The region cannot be named __global__");
}
RegionManager mgr = plugin.getGlobalRegionManager().get(world);
ProtectedRegion region = mgr.getRegion(id);
@ -560,6 +593,10 @@ public static void setParent(CommandContext args, WorldGuardPlugin plugin,
String id = args.getString(0);
String parentId = args.getString(1);
if (id.equalsIgnoreCase("__global__")) {
throw new CommandException("The region cannot be named __global__");
}
RegionManager mgr = plugin.getGlobalRegionManager().get(world);
ProtectedRegion region = mgr.getRegion(id);
ProtectedRegion parent = mgr.getRegion(parentId);

View File

@ -33,6 +33,7 @@
import com.sk89q.worldguard.domains.DefaultDomain;
import com.sk89q.worldguard.protection.flags.DefaultFlag;
import com.sk89q.worldguard.protection.flags.Flag;
import com.sk89q.worldguard.protection.regions.GlobalProtectedRegion;
import com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion;
import com.sk89q.worldguard.protection.regions.ProtectedPolygonalRegion;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
@ -86,6 +87,8 @@ public void load() throws IOException {
Integer maxY = checkNonNull(node.getInt("max-y"));
List<BlockVector2D> points = node.getBlockVector2dList("points", null);
region = new ProtectedPolygonalRegion(id, points, minY, maxY);
} else if (type.equals("global")) {
region = new GlobalProtectedRegion(id);
} else {
logger.warning("Unknown region type for region '" + id + '"');
continue;
@ -181,6 +184,8 @@ public void save() throws IOException {
}
node.setProperty("points", points);
} else if (region instanceof GlobalProtectedRegion) {
node.setProperty("type", "global");
} else {
node.setProperty("type", region.getClass().getCanonicalName());
}

View File

@ -28,6 +28,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
/**
* Represents a region of any shape and size that can be protected.
@ -35,6 +36,9 @@
* @author sk89q
*/
public abstract class ProtectedRegion implements Comparable<ProtectedRegion> {
private static final Pattern idPattern = Pattern.compile("^[A-Za-z0-9_,'\\-\\+/]{1,}$");
/**
* Holds the region's ID.
*/
@ -343,6 +347,16 @@ public abstract List<ProtectedRegion> getIntersectingRegions(
List<ProtectedRegion> regions)
throws UnsupportedIntersectionException;
/**
* Checks to see if the given ID is accurate.
*
* @param id
* @return
*/
public static boolean isValidId(String id) {
return idPattern.matcher(id).matches();
}
/**
* Thrown when setting a curParent would create a circular inheritance
* situation.