Allow /region flag to set group and value at once

The usage for "/region flag" implies both can be set at once:
/region flag <id> <flag> [-g group] [value]

However if the [-g group] [value] portions are specified, only
the group is set and the value is silently ignored. With this
commit, both values can be set at once. All other behavior of
the "flag" command is preserved. If only the [-g group] part is
given, then only the group flag changes and the regular flag's
value is unaffected. If neither part is given, then the flag is
cleared.
This commit is contained in:
Wojciech Stryjewski 2012-10-24 14:53:26 -05:00 committed by sk89q
parent 2cbc2f5226
commit 7e419c116e

View File

@ -673,6 +673,7 @@ public void flag(CommandContext args, CommandSender sender) throws CommandExcept
String id = args.getString(0); String id = args.getString(0);
String flagName = args.getString(1); String flagName = args.getString(1);
String value = null; String value = null;
RegionGroup groupValue = null;
if (args.argsLength() >= 3) { if (args.argsLength() >= 3) {
value = args.getJoinedStrings(2); value = args.getJoinedStrings(2);
@ -771,41 +772,51 @@ public void flag(CommandContext args, CommandSender sender) throws CommandExcept
+ "' does not have a group flag!"); + "' does not have a group flag!");
} }
// Parse the [-g group] separately so entire command can abort if parsing
// the [value] part throws an error.
try { try {
// If group set to default value then clear the group flag groupValue = groupFlag.parseInput(plugin, sender, group);
RegionGroup groupValue = groupFlag.parseInput(plugin, sender, group);
if (groupValue == groupFlag.getDefault()) {
region.setFlag(groupFlag, null);
sender.sendMessage(ChatColor.YELLOW
+ "Region group flag for '" + foundFlag.getName() + "' reset to default.");
} else {
region.setFlag(groupFlag, groupValue);
sender.sendMessage(ChatColor.YELLOW
+ "Region group flag for '" + foundFlag.getName() + "' set.");
}
} catch (InvalidFlagFormat e) { } catch (InvalidFlagFormat e) {
throw new CommandException(e.getMessage()); throw new CommandException(e.getMessage());
} }
} else { }
if (value != null) {
try {
setFlag(region, foundFlag, sender, value);
} catch (InvalidFlagFormat e) {
throw new CommandException(e.getMessage());
}
if (value != null) {
// Set the flag if [value] was given even if [-g group] was given as well
try {
setFlag(region, foundFlag, sender, value);
} catch (InvalidFlagFormat e) {
throw new CommandException(e.getMessage());
}
sender.sendMessage(ChatColor.YELLOW
+ "Region flag '" + foundFlag.getName() + "' set.");
}
if (value == null && !args.hasFlag('g')) {
// Clear the flag only if neither [value] nor [-g group] was given
region.setFlag(foundFlag, null);
sender.sendMessage(ChatColor.YELLOW
+ "Region flag '" + foundFlag.getName() + "' cleared.");
}
if (groupValue != null) {
RegionGroupFlag groupFlag = foundFlag.getRegionGroupFlag();
// If group set to the default, then clear the group flag
if (groupValue == groupFlag.getDefault()) {
region.setFlag(groupFlag, null);
sender.sendMessage(ChatColor.YELLOW sender.sendMessage(ChatColor.YELLOW
+ "Region flag '" + foundFlag.getName() + "' set."); + "Region group flag for '" + foundFlag.getName() + "' reset to default.");
} else { } else {
// Clear the flag region.setFlag(groupFlag, groupValue);
region.setFlag(foundFlag, null);
sender.sendMessage(ChatColor.YELLOW sender.sendMessage(ChatColor.YELLOW
+ "Region flag '" + foundFlag.getName() + "' cleared."); + "Region group flag for '" + foundFlag.getName() + "' set.");
} }
} }
try { try {
mgr.save(); mgr.save();
} catch (ProtectionDatabaseException e) { } catch (ProtectionDatabaseException e) {