From 521ecd438e9bfab8f0195162b516c4335f96bc52 Mon Sep 17 00:00:00 2001 From: sk89q Date: Sat, 2 Apr 2011 02:03:32 -0700 Subject: [PATCH] Added proper support for global regions and a region ID validation regex. --- .../bukkit/commands/RegionCommands.java | 43 +++++++++++++++++-- .../protection/databases/YAMLDatabase.java | 5 +++ .../protection/regions/ProtectedRegion.java | 14 ++++++ 3 files changed, 59 insertions(+), 3 deletions(-) diff --git a/src/com/sk89q/worldguard/bukkit/commands/RegionCommands.java b/src/com/sk89q/worldguard/bukkit/commands/RegionCommands.java index cd4c9d53..dd4b71db 100644 --- a/src/com/sk89q/worldguard/bukkit/commands/RegionCommands.java +++ b/src/com/sk89q/worldguard/bukkit/commands/RegionCommands.java @@ -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); diff --git a/src/com/sk89q/worldguard/protection/databases/YAMLDatabase.java b/src/com/sk89q/worldguard/protection/databases/YAMLDatabase.java index 93bbcc97..4ceb9fc1 100644 --- a/src/com/sk89q/worldguard/protection/databases/YAMLDatabase.java +++ b/src/com/sk89q/worldguard/protection/databases/YAMLDatabase.java @@ -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 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()); } diff --git a/src/com/sk89q/worldguard/protection/regions/ProtectedRegion.java b/src/com/sk89q/worldguard/protection/regions/ProtectedRegion.java index 8b0a9d98..a6721952 100644 --- a/src/com/sk89q/worldguard/protection/regions/ProtectedRegion.java +++ b/src/com/sk89q/worldguard/protection/regions/ProtectedRegion.java @@ -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 { + + private static final Pattern idPattern = Pattern.compile("^[A-Za-z0-9_,'\\-\\+/]{1,}$"); + /** * Holds the region's ID. */ @@ -343,6 +347,16 @@ public abstract List getIntersectingRegions( List 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.