Added /region flag. DON'T USE THIS VERSION ON A PRODUCTION SERVER.

This commit is contained in:
sk89q 2011-04-01 13:39:29 -07:00
parent d1cab0da7f
commit 726e3a5c20
13 changed files with 245 additions and 24 deletions

View File

@ -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<String, ProtectedRegion> regions = mgr.getRegions();
@ -336,4 +337,86 @@ public static void list(CommandContext args, WorldGuardPlugin plugin,
}
}
}
@Command(aliases = {"flag"},
usage = "<id> <flag> [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 <V> void setFlag(ProtectedRegion region,
Flag<V> flag, WorldGuardPlugin plugin, CommandSender sender, String value)
throws InvalidFlagFormat {
region.setFlag(flag, flag.parseInput(plugin, sender, value));
}
}

View File

@ -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);
}
}
}

View File

@ -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);
}
}
}

View File

@ -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;
}

View File

@ -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);
}
}
}

View File

@ -0,0 +1,29 @@
// $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.flags;
public class InvalidFlagFormat extends Exception {
private static final long serialVersionUID = 8101615074524004172L;
public InvalidFlagFormat(String msg) {
super(msg);
}
}

View File

@ -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());
}
}
}

View File

@ -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 "";
}
}

View File

@ -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);
}
}
}

View File

@ -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;
}
}

View File

@ -80,7 +80,7 @@ public void setRegions(Map<String, ProtectedRegion> 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<String> removeRegions = new ArrayList<String>();
@ -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());
}
/**

View File

@ -90,7 +90,7 @@ public void setRegions(Map<String, ProtectedRegion> regions) {
*/
@Override
public void addRegion(ProtectedRegion region) {
regions.put(region.getId(), region);
regions.put(region.getId().toLowerCase(), region);
tree = new PRTree<ProtectedRegion>(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<String> removeRegions = new ArrayList<String>();
@ -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);
}
}

View File

@ -276,7 +276,11 @@ public <T extends Flag<V>, V> V getFlag(T flag) {
* @param val
*/
public <T extends Flag<V>, V> void setFlag(T flag, V val) {
flags.put(flag, val);
if (val == null) {
flags.remove(flag);
} else {
flags.put(flag, val);
}
}
/**