From 726e3a5c206cbdccfcc991f43d68e62762b84efe Mon Sep 17 00:00:00 2001 From: sk89q Date: Fri, 1 Apr 2011 13:39:29 -0700 Subject: [PATCH] Added /region flag. DON'T USE THIS VERSION ON A PRODUCTION SERVER. --- .../bukkit/commands/RegionCommands.java | 95 +++++++++++++++++-- .../protection/flags/BooleanFlag.java | 21 ++++ .../protection/flags/DoubleFlag.java | 15 +++ .../worldguard/protection/flags/Flag.java | 5 + .../protection/flags/IntegerFlag.java | 15 +++ .../protection/flags/InvalidFlagFormat.java | 29 ++++++ .../protection/flags/LocationFlag.java | 15 +++ .../protection/flags/RegionGroupFlag.java | 9 ++ .../protection/flags/StateFlag.java | 19 ++++ .../protection/flags/StringFlag.java | 9 ++ .../managers/FlatRegionManager.java | 16 ++-- .../managers/PRTreeRegionManager.java | 15 ++- .../protection/regions/ProtectedRegion.java | 6 +- 13 files changed, 245 insertions(+), 24 deletions(-) create mode 100644 src/com/sk89q/worldguard/protection/flags/InvalidFlagFormat.java diff --git a/src/com/sk89q/worldguard/bukkit/commands/RegionCommands.java b/src/com/sk89q/worldguard/bukkit/commands/RegionCommands.java index 3844fd51..0cf88acc 100644 --- a/src/com/sk89q/worldguard/bukkit/commands/RegionCommands.java +++ b/src/com/sk89q/worldguard/bukkit/commands/RegionCommands.java @@ -37,6 +37,7 @@ import com.sk89q.worldguard.protection.ApplicableRegionSet; import com.sk89q.worldguard.protection.flags.DefaultFlag; import com.sk89q.worldguard.protection.flags.Flag; +import com.sk89q.worldguard.protection.flags.InvalidFlagFormat; import com.sk89q.worldguard.protection.managers.RegionManager; import com.sk89q.worldguard.protection.regions.*; import com.sk89q.worldguard.util.RegionUtil; @@ -245,14 +246,14 @@ public static void info(CommandContext args, WorldGuardPlugin plugin, if (player != null) { if (region.isOwner(localPlayer)) { - plugin.checkPermission(sender, "region.info.own"); + plugin.checkPermission(sender, "worldguard.region.info.own"); } else if (region.isMember(localPlayer)) { - plugin.checkPermission(sender, "region.info.member"); + plugin.checkPermission(sender, "worldguard.region.info.member"); } else { - plugin.checkPermission(sender, "region.info"); + plugin.checkPermission(sender, "worldguard.region.info"); } } else { - plugin.checkPermission(sender, "region.info"); + plugin.checkPermission(sender, "worldguard.region.info"); } DefaultDomain owners = region.getOwners(); @@ -299,7 +300,7 @@ public static void list(CommandContext args, WorldGuardPlugin plugin, int page = 0; if (args.argsLength() > 0) { - page = args.getInteger(0); + page = Math.max(0, args.getInteger(0) - 1); } if (args.argsLength() > 1) { @@ -308,7 +309,7 @@ public static void list(CommandContext args, WorldGuardPlugin plugin, world = plugin.checkPlayer(sender).getWorld(); } - int listSize = 5; + int listSize = 10; RegionManager mgr = plugin.getGlobalRegionManager().get(world); Map regions = mgr.getRegions(); @@ -336,4 +337,86 @@ public static void list(CommandContext args, WorldGuardPlugin plugin, } } } + + @Command(aliases = {"flag"}, + usage = " [value]", + desc = "Set flags", + flags = "", min = 2, max = -1) + public static void flag(CommandContext args, WorldGuardPlugin plugin, + CommandSender sender) throws CommandException { + + Player player = plugin.checkPlayer(sender); + World world = player.getWorld(); + LocalPlayer localPlayer = plugin.wrapPlayer(player); + + String id = args.getString(0); + String flagName = args.getString(0); + String value = null; + + if (args.argsLength() >= 3) { + value = args.getJoinedStrings(2); + } + + RegionManager mgr = plugin.getGlobalRegionManager().get(world); + ProtectedRegion region = mgr.getRegion(id); + + if (region == null) { + throw new CommandException("Could not find a region by that ID."); + } + + if (region.isOwner(localPlayer)) { + plugin.checkPermission(sender, "worldguard.region.flag.own"); + } else if (region.isMember(localPlayer)) { + plugin.checkPermission(sender, "worldguard.region.flag.member"); + } else { + plugin.checkPermission(sender, "worldguard.region.flag"); + } + + Flag foundFlag = null; + + // Now time to find the flag! + for (Flag flag : DefaultFlag.getFlags()) { + // Try to detect the flag + if (flag.getName().replace("-", "").equalsIgnoreCase(flagName.replace("-", "")) + || flagName.equals(flag.getLegacyCode())) { + foundFlag = flag; + break; + } + } + + if (foundFlag == null) { + throw new CommandException("Unknown flag specified: " + flagName); + } + + if (value != null) { + try { + setFlag(region, foundFlag, plugin, sender, value); + } catch (InvalidFlagFormat e) { + throw new CommandException(e.getMessage()); + } + + sender.sendMessage(ChatColor.YELLOW + + "Region flag '" + foundFlag.getName() + " set."); + } else { + // Clear the flag + region.setFlag(foundFlag, null); + + sender.sendMessage(ChatColor.YELLOW + + "Region flag '" + foundFlag.getName() + " cleared."); + } + + try { + mgr.save(); + sender.sendMessage(ChatColor.YELLOW + "Region saved as " + id + "."); + } catch (IOException e) { + throw new CommandException("Failed to write regions file: " + + e.getMessage()); + } + } + + public static void setFlag(ProtectedRegion region, + Flag flag, WorldGuardPlugin plugin, CommandSender sender, String value) + throws InvalidFlagFormat { + region.setFlag(flag, flag.parseInput(plugin, sender, value)); + } } diff --git a/src/com/sk89q/worldguard/protection/flags/BooleanFlag.java b/src/com/sk89q/worldguard/protection/flags/BooleanFlag.java index 5f817400..6a3deb5e 100644 --- a/src/com/sk89q/worldguard/protection/flags/BooleanFlag.java +++ b/src/com/sk89q/worldguard/protection/flags/BooleanFlag.java @@ -18,6 +18,9 @@ */ package com.sk89q.worldguard.protection.flags; +import org.bukkit.command.CommandSender; +import com.sk89q.worldguard.bukkit.WorldGuardPlugin; + /** * * @author sk89q @@ -31,5 +34,23 @@ public BooleanFlag(String name, char legacyCode) { public BooleanFlag(String name) { super(name); } + + @Override + public Boolean parseInput(WorldGuardPlugin plugin, CommandSender sender, + String input) throws InvalidFlagFormat { + input = input.trim(); + + if (input.equalsIgnoreCase("true") || input.equalsIgnoreCase("yes") + || input.equalsIgnoreCase("on") + || input.equalsIgnoreCase("1")) { + return true; + } else if (input.equalsIgnoreCase("false") || input.equalsIgnoreCase("no") + || input.equalsIgnoreCase("off") + || input.equalsIgnoreCase("0")) { + return false; + } else { + throw new InvalidFlagFormat("Not a yes/no value: " + input); + } + } } diff --git a/src/com/sk89q/worldguard/protection/flags/DoubleFlag.java b/src/com/sk89q/worldguard/protection/flags/DoubleFlag.java index 9cda2407..f9bc8a4b 100644 --- a/src/com/sk89q/worldguard/protection/flags/DoubleFlag.java +++ b/src/com/sk89q/worldguard/protection/flags/DoubleFlag.java @@ -18,6 +18,9 @@ */ package com.sk89q.worldguard.protection.flags; +import org.bukkit.command.CommandSender; +import com.sk89q.worldguard.bukkit.WorldGuardPlugin; + /** * * @author sk89q @@ -31,4 +34,16 @@ public DoubleFlag(String name, char legacyCode) { public DoubleFlag(String name) { super(name); } + + @Override + public Double parseInput(WorldGuardPlugin plugin, CommandSender sender, + String input) throws InvalidFlagFormat { + input = input.trim(); + + try { + return Double.parseDouble(input); + } catch (NumberFormatException e) { + throw new InvalidFlagFormat("Not a number: " + input); + } + } } diff --git a/src/com/sk89q/worldguard/protection/flags/Flag.java b/src/com/sk89q/worldguard/protection/flags/Flag.java index abd8a2e2..9ac71df9 100644 --- a/src/com/sk89q/worldguard/protection/flags/Flag.java +++ b/src/com/sk89q/worldguard/protection/flags/Flag.java @@ -18,6 +18,9 @@ */ package com.sk89q.worldguard.protection.flags; +import org.bukkit.command.CommandSender; +import com.sk89q.worldguard.bukkit.WorldGuardPlugin; + /** * * @author sk89q @@ -46,4 +49,6 @@ public char getLegacyCode() { return legacyCode; } + public abstract T parseInput(WorldGuardPlugin plugin, CommandSender sender, + String input) throws InvalidFlagFormat; } diff --git a/src/com/sk89q/worldguard/protection/flags/IntegerFlag.java b/src/com/sk89q/worldguard/protection/flags/IntegerFlag.java index c99b4661..af9c4145 100644 --- a/src/com/sk89q/worldguard/protection/flags/IntegerFlag.java +++ b/src/com/sk89q/worldguard/protection/flags/IntegerFlag.java @@ -18,6 +18,9 @@ */ package com.sk89q.worldguard.protection.flags; +import org.bukkit.command.CommandSender; +import com.sk89q.worldguard.bukkit.WorldGuardPlugin; + /** * * @author sk89q @@ -31,4 +34,16 @@ public IntegerFlag(String name, char legacyCode) { public IntegerFlag(String name) { super(name); } + + @Override + public Integer parseInput(WorldGuardPlugin plugin, CommandSender sender, + String input) throws InvalidFlagFormat { + input = input.trim(); + + try { + return Integer.parseInt(input); + } catch (NumberFormatException e) { + throw new InvalidFlagFormat("Not a number: " + input); + } + } } diff --git a/src/com/sk89q/worldguard/protection/flags/InvalidFlagFormat.java b/src/com/sk89q/worldguard/protection/flags/InvalidFlagFormat.java new file mode 100644 index 00000000..5f27c1f3 --- /dev/null +++ b/src/com/sk89q/worldguard/protection/flags/InvalidFlagFormat.java @@ -0,0 +1,29 @@ +// $Id$ +/* + * WorldGuard + * Copyright (C) 2010 sk89q + * + * 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 . +*/ + +package com.sk89q.worldguard.protection.flags; + +public class InvalidFlagFormat extends Exception { + + private static final long serialVersionUID = 8101615074524004172L; + + public InvalidFlagFormat(String msg) { + super(msg); + } +} diff --git a/src/com/sk89q/worldguard/protection/flags/LocationFlag.java b/src/com/sk89q/worldguard/protection/flags/LocationFlag.java index 338fa524..7ab8f296 100644 --- a/src/com/sk89q/worldguard/protection/flags/LocationFlag.java +++ b/src/com/sk89q/worldguard/protection/flags/LocationFlag.java @@ -19,6 +19,9 @@ package com.sk89q.worldguard.protection.flags; import org.bukkit.Location; +import org.bukkit.command.CommandSender; +import com.sk89q.minecraft.util.commands.CommandException; +import com.sk89q.worldguard.bukkit.WorldGuardPlugin; /** * @@ -33,4 +36,16 @@ public LocationFlag(String name, char legacyCode) { public LocationFlag(String name) { super(name); } + + @Override + public Location parseInput(WorldGuardPlugin plugin, CommandSender sender, + String input) throws InvalidFlagFormat { + input = input.trim(); + + try { + return plugin.checkPlayer(sender).getLocation(); + } catch (CommandException e) { + throw new InvalidFlagFormat(e.getMessage()); + } + } } diff --git a/src/com/sk89q/worldguard/protection/flags/RegionGroupFlag.java b/src/com/sk89q/worldguard/protection/flags/RegionGroupFlag.java index 3e8711bb..dbeae976 100644 --- a/src/com/sk89q/worldguard/protection/flags/RegionGroupFlag.java +++ b/src/com/sk89q/worldguard/protection/flags/RegionGroupFlag.java @@ -18,6 +18,9 @@ */ package com.sk89q.worldguard.protection.flags; +import org.bukkit.command.CommandSender; +import com.sk89q.worldguard.bukkit.WorldGuardPlugin; + /** * * @author sk89q @@ -31,5 +34,11 @@ public RegionGroupFlag(String name, char legacyCode) { public RegionGroupFlag(String name) { super(name); } + + @Override + public String parseInput(WorldGuardPlugin plugin, CommandSender sender, + String input) throws InvalidFlagFormat { + return ""; + } } diff --git a/src/com/sk89q/worldguard/protection/flags/StateFlag.java b/src/com/sk89q/worldguard/protection/flags/StateFlag.java index f4aa4e7b..08a9902e 100644 --- a/src/com/sk89q/worldguard/protection/flags/StateFlag.java +++ b/src/com/sk89q/worldguard/protection/flags/StateFlag.java @@ -18,6 +18,9 @@ */ package com.sk89q.worldguard.protection.flags; +import org.bukkit.command.CommandSender; +import com.sk89q.worldguard.bukkit.WorldGuardPlugin; + /** * * @author sk89q @@ -44,5 +47,21 @@ public StateFlag(String name, boolean def) { public boolean getDefault() { return def; } + + @Override + public State parseInput(WorldGuardPlugin plugin, CommandSender sender, + String input) throws InvalidFlagFormat { + input = input.trim(); + + if (input.equalsIgnoreCase("allow")) { + return State.ALLOW; + } else if (input.equalsIgnoreCase("deny")) { + return State.DENY; + } else if (input.equalsIgnoreCase("none")) { + return null; + } else { + throw new InvalidFlagFormat("Not none/allow/deny: " + input); + } + } } diff --git a/src/com/sk89q/worldguard/protection/flags/StringFlag.java b/src/com/sk89q/worldguard/protection/flags/StringFlag.java index 8ae49436..1ea5163c 100644 --- a/src/com/sk89q/worldguard/protection/flags/StringFlag.java +++ b/src/com/sk89q/worldguard/protection/flags/StringFlag.java @@ -18,6 +18,9 @@ */ package com.sk89q.worldguard.protection.flags; +import org.bukkit.command.CommandSender; +import com.sk89q.worldguard.bukkit.WorldGuardPlugin; + /** * * @author sk89q @@ -31,5 +34,11 @@ public StringFlag(String name, char legacyCode) { public StringFlag(String name) { super(name); } + + @Override + public String parseInput(WorldGuardPlugin plugin, CommandSender sender, + String input) throws InvalidFlagFormat { + return input; + } } diff --git a/src/com/sk89q/worldguard/protection/managers/FlatRegionManager.java b/src/com/sk89q/worldguard/protection/managers/FlatRegionManager.java index e42c3fd3..cfcf5c97 100644 --- a/src/com/sk89q/worldguard/protection/managers/FlatRegionManager.java +++ b/src/com/sk89q/worldguard/protection/managers/FlatRegionManager.java @@ -80,7 +80,7 @@ public void setRegions(Map regions) { */ @Override public void addRegion(ProtectedRegion region) { - regions.put(region.getId(), region); + regions.put(region.getId().toLowerCase(), region); } /** @@ -90,9 +90,8 @@ public void addRegion(ProtectedRegion region) { */ @Override public void removeRegion(String id) { - ProtectedRegion region = regions.get(id); - - regions.remove(id); + ProtectedRegion region = regions.get(id.toLowerCase()); + regions.remove(id.toLowerCase()); if (region != null) { List removeRegions = new ArrayList(); @@ -100,12 +99,11 @@ public void removeRegion(String id) { while (iter.hasNext()) { ProtectedRegion curRegion = iter.next(); if (curRegion.getParent() == region) { - removeRegions.add(curRegion.getId()); + removeRegions.add(curRegion.getId().toLowerCase()); } } - for(String remId : removeRegions) - { + for (String remId : removeRegions) { removeRegion(remId); } } @@ -119,7 +117,7 @@ public void removeRegion(String id) { */ @Override public boolean hasRegion(String id) { - return regions.containsKey(id); + return regions.containsKey(id.toLowerCase()); } /** @@ -129,7 +127,7 @@ public boolean hasRegion(String id) { */ @Override public ProtectedRegion getRegion(String id) { - return regions.get(id); + return regions.get(id.toLowerCase()); } /** diff --git a/src/com/sk89q/worldguard/protection/managers/PRTreeRegionManager.java b/src/com/sk89q/worldguard/protection/managers/PRTreeRegionManager.java index 83f493eb..a07c1c3c 100644 --- a/src/com/sk89q/worldguard/protection/managers/PRTreeRegionManager.java +++ b/src/com/sk89q/worldguard/protection/managers/PRTreeRegionManager.java @@ -90,7 +90,7 @@ public void setRegions(Map regions) { */ @Override public void addRegion(ProtectedRegion region) { - regions.put(region.getId(), region); + regions.put(region.getId().toLowerCase(), region); tree = new PRTree(converter, BRANCH_FACTOR); tree.load(regions.values()); } @@ -103,7 +103,7 @@ public void addRegion(ProtectedRegion region) { */ @Override public boolean hasRegion(String id) { - return regions.containsKey(id); + return regions.containsKey(id.toLowerCase()); } /** @@ -113,7 +113,7 @@ public boolean hasRegion(String id) { */ @Override public ProtectedRegion getRegion(String id) { - return regions.get(id); + return regions.get(id.toLowerCase()); } /** @@ -123,9 +123,9 @@ public ProtectedRegion getRegion(String id) { */ @Override public void removeRegion(String id) { - ProtectedRegion region = regions.get(id); + ProtectedRegion region = regions.get(id.toLowerCase()); - regions.remove(id); + regions.remove(id.toLowerCase()); if (region != null) { List removeRegions = new ArrayList(); @@ -133,12 +133,11 @@ public void removeRegion(String id) { while (iter.hasNext()) { ProtectedRegion curRegion = iter.next(); if (curRegion.getParent() == region) { - removeRegions.add(curRegion.getId()); + removeRegions.add(curRegion.getId().toLowerCase()); } } - for(String remId : removeRegions) - { + for (String remId : removeRegions) { removeRegion(remId); } } diff --git a/src/com/sk89q/worldguard/protection/regions/ProtectedRegion.java b/src/com/sk89q/worldguard/protection/regions/ProtectedRegion.java index 353010e8..e9883b0b 100644 --- a/src/com/sk89q/worldguard/protection/regions/ProtectedRegion.java +++ b/src/com/sk89q/worldguard/protection/regions/ProtectedRegion.java @@ -276,7 +276,11 @@ public , V> V getFlag(T flag) { * @param val */ public , V> void setFlag(T flag, V val) { - flags.put(flag, val); + if (val == null) { + flags.remove(flag); + } else { + flags.put(flag, val); + } } /**