Allow a lot of commands to be sent from the console.

Examples:
 /rg addmem <id> <player> -w <worldname>
 /rg flag <id> <flag> -w <worldname> <flagvalue>
 /rg remove <id> -w <worldname>
Command blocks are currently untested.
List of commands: flag, priority, parent, remove,
 addmember, removemember, addowner, removeowner
This commit is contained in:
Wizjany 2013-01-30 14:39:22 -05:00
parent 72a9472a57
commit b5f80351ec
2 changed files with 320 additions and 227 deletions

View File

@ -71,40 +71,40 @@ public class RegionCommands {
private MigratorKey migrateDBRequest; private MigratorKey migrateDBRequest;
private Date migrateDBRequestDate; private Date migrateDBRequestDate;
public RegionCommands(WorldGuardPlugin plugin) { public RegionCommands(WorldGuardPlugin plugin) {
this.plugin = plugin; this.plugin = plugin;
} }
@Command(aliases = {"define", "def", "d"}, usage = "<id> [<owner1> [<owner2> [<owners...>]]]", @Command(aliases = {"define", "def", "d"}, usage = "<id> [<owner1> [<owner2> [<owners...>]]]",
desc = "Defines a region", min = 1) desc = "Defines a region", min = 1)
@CommandPermissions({"worldguard.region.define"}) @CommandPermissions({"worldguard.region.define"})
public void define(CommandContext args, CommandSender sender) throws CommandException { public void define(CommandContext args, CommandSender sender) throws CommandException {
Player player = plugin.checkPlayer(sender); Player player = plugin.checkPlayer(sender);
WorldEditPlugin worldEdit = plugin.getWorldEdit(); WorldEditPlugin worldEdit = plugin.getWorldEdit();
String id = args.getString(0); String id = args.getString(0);
if (!ProtectedRegion.isValidId(id)) { if (!ProtectedRegion.isValidId(id)) {
throw new CommandException("Invalid region ID specified!"); throw new CommandException("Invalid region ID specified!");
} }
if (id.equalsIgnoreCase("__global__")) { if (id.equalsIgnoreCase("__global__")) {
throw new CommandException("A region cannot be named __global__"); throw new CommandException("A region cannot be named __global__");
} }
// Attempt to get the player's selection from WorldEdit // Attempt to get the player's selection from WorldEdit
Selection sel = worldEdit.getSelection(player); Selection sel = worldEdit.getSelection(player);
if (sel == null) { if (sel == null) {
throw new CommandException("Select a region with WorldEdit first."); throw new CommandException("Select a region with WorldEdit first.");
} }
RegionManager mgr = plugin.getGlobalRegionManager().get(sel.getWorld()); RegionManager mgr = plugin.getGlobalRegionManager().get(sel.getWorld());
if (mgr.hasRegion(id)) { if (mgr.hasRegion(id)) {
throw new CommandException("That region is already defined. Use redefine instead."); throw new CommandException("That region is already defined. Use redefine instead.");
} }
ProtectedRegion region; ProtectedRegion region;
// Detect the type of region from WorldEdit // Detect the type of region from WorldEdit
@ -126,9 +126,9 @@ public void define(CommandContext args, CommandSender sender) throws CommandExce
if (args.argsLength() > 1) { if (args.argsLength() > 1) {
region.setOwners(RegionDBUtil.parseDomainString(args.getSlice(1), 1)); region.setOwners(RegionDBUtil.parseDomainString(args.getSlice(1), 1));
} }
mgr.addRegion(region); mgr.addRegion(region);
try { try {
mgr.save(); mgr.save();
sender.sendMessage(ChatColor.YELLOW + "Region saved as " + id + "."); sender.sendMessage(ChatColor.YELLOW + "Region saved as " + id + ".");
@ -137,17 +137,17 @@ public void define(CommandContext args, CommandSender sender) throws CommandExce
+ e.getMessage()); + e.getMessage());
} }
} }
@Command(aliases = {"redefine", "update", "move"}, usage = "<id>", @Command(aliases = {"redefine", "update", "move"}, usage = "<id>",
desc = "Re-defines the shape of a region", min = 1, max = 1) desc = "Re-defines the shape of a region", min = 1, max = 1)
public void redefine(CommandContext args, CommandSender sender) throws CommandException { public void redefine(CommandContext args, CommandSender sender) throws CommandException {
Player player = plugin.checkPlayer(sender); Player player = plugin.checkPlayer(sender);
World world = player.getWorld(); World world = player.getWorld();
WorldEditPlugin worldEdit = plugin.getWorldEdit(); WorldEditPlugin worldEdit = plugin.getWorldEdit();
LocalPlayer localPlayer = plugin.wrapPlayer(player); LocalPlayer localPlayer = plugin.wrapPlayer(player);
String id = args.getString(0); String id = args.getString(0);
if (id.equalsIgnoreCase("__global__")) { if (id.equalsIgnoreCase("__global__")) {
throw new CommandException("The region cannot be named __global__"); throw new CommandException("The region cannot be named __global__");
} }
@ -166,16 +166,16 @@ public void redefine(CommandContext args, CommandSender sender) throws CommandEx
} else { } else {
plugin.checkPermission(sender, "worldguard.region.redefine"); plugin.checkPermission(sender, "worldguard.region.redefine");
} }
// Attempt to get the player's selection from WorldEdit // Attempt to get the player's selection from WorldEdit
Selection sel = worldEdit.getSelection(player); Selection sel = worldEdit.getSelection(player);
if (sel == null) { if (sel == null) {
throw new CommandException("Select a region with WorldEdit first."); throw new CommandException("Select a region with WorldEdit first.");
} }
ProtectedRegion region; ProtectedRegion region;
// Detect the type of region from WorldEdit // Detect the type of region from WorldEdit
if (sel instanceof Polygonal2DSelection) { if (sel instanceof Polygonal2DSelection) {
Polygonal2DSelection polySel = (Polygonal2DSelection) sel; Polygonal2DSelection polySel = (Polygonal2DSelection) sel;
@ -199,11 +199,11 @@ public void redefine(CommandContext args, CommandSender sender) throws CommandEx
region.setParent(existing.getParent()); region.setParent(existing.getParent());
} catch (CircularInheritanceException ignore) { } catch (CircularInheritanceException ignore) {
} }
mgr.addRegion(region); mgr.addRegion(region);
sender.sendMessage(ChatColor.YELLOW + "Region updated with new area."); sender.sendMessage(ChatColor.YELLOW + "Region updated with new area.");
try { try {
mgr.save(); mgr.save();
} catch (ProtectionDatabaseException e) { } catch (ProtectionDatabaseException e) {
@ -211,38 +211,38 @@ public void redefine(CommandContext args, CommandSender sender) throws CommandEx
+ e.getMessage()); + e.getMessage());
} }
} }
@Command(aliases = {"claim"}, usage = "<id> [<owner1> [<owner2> [<owners...>]]]", @Command(aliases = {"claim"}, usage = "<id> [<owner1> [<owner2> [<owners...>]]]",
desc = "Claim a region", min = 1) desc = "Claim a region", min = 1)
@CommandPermissions({"worldguard.region.claim"}) @CommandPermissions({"worldguard.region.claim"})
public void claim(CommandContext args, CommandSender sender) throws CommandException { public void claim(CommandContext args, CommandSender sender) throws CommandException {
Player player = plugin.checkPlayer(sender); Player player = plugin.checkPlayer(sender);
LocalPlayer localPlayer = plugin.wrapPlayer(player); LocalPlayer localPlayer = plugin.wrapPlayer(player);
WorldEditPlugin worldEdit = plugin.getWorldEdit(); WorldEditPlugin worldEdit = plugin.getWorldEdit();
String id = args.getString(0); String id = args.getString(0);
if (!ProtectedRegion.isValidId(id)) { if (!ProtectedRegion.isValidId(id)) {
throw new CommandException("Invalid region ID specified!"); throw new CommandException("Invalid region ID specified!");
} }
if (id.equalsIgnoreCase("__global__")) { if (id.equalsIgnoreCase("__global__")) {
throw new CommandException("A region cannot be named __global__"); throw new CommandException("A region cannot be named __global__");
} }
// Attempt to get the player's selection from WorldEdit // Attempt to get the player's selection from WorldEdit
Selection sel = worldEdit.getSelection(player); Selection sel = worldEdit.getSelection(player);
if (sel == null) { if (sel == null) {
throw new CommandException("Select a region with WorldEdit first."); throw new CommandException("Select a region with WorldEdit first.");
} }
RegionManager mgr = plugin.getGlobalRegionManager().get(sel.getWorld()); RegionManager mgr = plugin.getGlobalRegionManager().get(sel.getWorld());
if (mgr.hasRegion(id)) { if (mgr.hasRegion(id)) {
throw new CommandException("That region already exists. Please choose a different name."); throw new CommandException("That region already exists. Please choose a different name.");
} }
ProtectedRegion region; ProtectedRegion region;
// Detect the type of region from WorldEdit // Detect the type of region from WorldEdit
@ -266,7 +266,7 @@ public void claim(CommandContext args, CommandSender sender) throws CommandExcep
} }
WorldConfiguration wcfg = plugin.getGlobalStateManager().get(player.getWorld()); WorldConfiguration wcfg = plugin.getGlobalStateManager().get(player.getWorld());
if (!plugin.hasPermission(sender, "worldguard.region.unlimited")) { if (!plugin.hasPermission(sender, "worldguard.region.unlimited")) {
// Check whether the player has created too many regions // Check whether the player has created too many regions
int maxRegionCount = wcfg.getMaxRegionCount(player); int maxRegionCount = wcfg.getMaxRegionCount(player);
@ -284,9 +284,9 @@ public void claim(CommandContext args, CommandSender sender) throws CommandExcep
throw new CommandException("This region already exists and you don't own it."); throw new CommandException("This region already exists and you don't own it.");
} }
} }
ApplicableRegionSet regions = mgr.getApplicableRegions(region); ApplicableRegionSet regions = mgr.getApplicableRegions(region);
// Check if this region overlaps any other region // Check if this region overlaps any other region
if (regions.size() > 0) { if (regions.size() > 0) {
if (!regions.isOwnerOfAll(localPlayer)) { if (!regions.isOwnerOfAll(localPlayer)) {
@ -333,7 +333,7 @@ public void claim(CommandContext args, CommandSender sender) throws CommandExcep
region.getOwners().addPlayer(player.getName()); region.getOwners().addPlayer(player.getName());
mgr.addRegion(region); mgr.addRegion(region);
try { try {
mgr.save(); mgr.save();
sender.sendMessage(ChatColor.YELLOW + "Region saved as " + id + "."); sender.sendMessage(ChatColor.YELLOW + "Region saved as " + id + ".");
@ -402,7 +402,7 @@ public void selectRegion(Player player, LocalPlayer localPlayer, ProtectedRegion
world, poly2d.getPoints(), world, poly2d.getPoints(),
poly2d.getMinimumPoint().getBlockY(), poly2d.getMinimumPoint().getBlockY(),
poly2d.getMaximumPoint().getBlockY() poly2d.getMaximumPoint().getBlockY()
); );
worldEdit.setSelection(player, selection); worldEdit.setSelection(player, selection);
player.sendMessage(ChatColor.YELLOW + "Region selected as a polygon."); player.sendMessage(ChatColor.YELLOW + "Region selected as a polygon.");
} else if (region instanceof GlobalProtectedRegion) { } else if (region instanceof GlobalProtectedRegion) {
@ -537,7 +537,7 @@ public void displayRegionInfo(CommandSender sender, final LocalPlayer localPlaye
sender.sendMessage(ChatColor.LIGHT_PURPLE + "Bounds:" sender.sendMessage(ChatColor.LIGHT_PURPLE + "Bounds:"
+ " (" + min.getBlockX() + "," + min.getBlockY() + "," + min.getBlockZ() + ")" + " (" + min.getBlockX() + "," + min.getBlockY() + "," + min.getBlockZ() + ")"
+ " (" + max.getBlockX() + "," + max.getBlockY() + "," + max.getBlockZ() + ")" + " (" + max.getBlockX() + "," + max.getBlockY() + "," + max.getBlockZ() + ")"
); );
} }
public class RegionEntry implements Comparable<RegionEntry>{ public class RegionEntry implements Comparable<RegionEntry>{
@ -662,14 +662,21 @@ public void list(CommandContext args, CommandSender sender) throws CommandExcept
} }
} }
@Command(aliases = {"flag", "f"}, usage = "<id> <flag> [-g group] [value]", flags = "g:", @Command(aliases = {"flag", "f"}, usage = "<id> <flag> [-g group] [value]", flags = "g:w:",
desc = "Set flags", min = 2) desc = "Set flags", min = 2)
public void flag(CommandContext args, CommandSender sender) throws CommandException { public void flag(CommandContext args, CommandSender sender) throws CommandException {
Player player = plugin.checkPlayer(sender); final World world;
World world = player.getWorld(); Player player;
LocalPlayer localPlayer = plugin.wrapPlayer(player); LocalPlayer localPlayer = null;
if (args.hasFlag('w')) {
world = plugin.matchWorld(sender, args.getFlag('w'));
} else {
player = plugin.checkPlayer(sender);
localPlayer = plugin.wrapPlayer(player);
world = player.getWorld();
}
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;
@ -693,20 +700,24 @@ public void flag(CommandContext args, CommandSender sender) throws CommandExcept
// @TODO deprecate "flag.[own./member./blank]" // @TODO deprecate "flag.[own./member./blank]"
boolean hasPerm = false; boolean hasPerm = false;
if (region.isOwner(localPlayer)) { if (localPlayer == null) {
if (plugin.hasPermission(sender, "worldguard.region.flag.own." + id.toLowerCase())) hasPerm = true; hasPerm = true;
else if (plugin.hasPermission(sender, "worldguard.region.flag.regions.own." + id.toLowerCase())) hasPerm = true;
} else if (region.isMember(localPlayer)) {
if (plugin.hasPermission(sender, "worldguard.region.flag.member." + id.toLowerCase())) hasPerm = true;
else if (plugin.hasPermission(sender, "worldguard.region.flag.regions.member." + id.toLowerCase())) hasPerm = true;
} else { } else {
if (plugin.hasPermission(sender, "worldguard.region.flag." + id.toLowerCase())) hasPerm = true; if (region.isOwner(localPlayer)) {
else if (plugin.hasPermission(sender, "worldguard.region.flag.regions." + id.toLowerCase())) hasPerm = true; if (plugin.hasPermission(sender, "worldguard.region.flag.own." + id.toLowerCase())) hasPerm = true;
else if (plugin.hasPermission(sender, "worldguard.region.flag.regions.own." + id.toLowerCase())) hasPerm = true;
} else if (region.isMember(localPlayer)) {
if (plugin.hasPermission(sender, "worldguard.region.flag.member." + id.toLowerCase())) hasPerm = true;
else if (plugin.hasPermission(sender, "worldguard.region.flag.regions.member." + id.toLowerCase())) hasPerm = true;
} else {
if (plugin.hasPermission(sender, "worldguard.region.flag." + id.toLowerCase())) hasPerm = true;
else if (plugin.hasPermission(sender, "worldguard.region.flag.regions." + id.toLowerCase())) hasPerm = true;
}
} }
if (!hasPerm) throw new CommandPermissionsException(); if (!hasPerm) throw new CommandPermissionsException();
Flag<?> foundFlag = null; Flag<?> foundFlag = null;
// Now time to find the flag! // Now time to find the flag!
for (Flag<?> flag : DefaultFlag.getFlags()) { for (Flag<?> flag : DefaultFlag.getFlags()) {
// Try to detect the flag // Try to detect the flag
@ -715,30 +726,32 @@ public void flag(CommandContext args, CommandSender sender) throws CommandExcept
break; break;
} }
} }
if (foundFlag == null) { if (foundFlag == null) {
StringBuilder list = new StringBuilder(); StringBuilder list = new StringBuilder();
// Need to build a list // Need to build a list
for (Flag<?> flag : DefaultFlag.getFlags()) { for (Flag<?> flag : DefaultFlag.getFlags()) {
// @TODO deprecate inconsistant "owner" permission // @TODO deprecate inconsistant "owner" permission
if (region.isOwner(localPlayer)) { if (localPlayer != null) {
if (!plugin.hasPermission(sender, "worldguard.region.flag.flags." if (region.isOwner(localPlayer)) {
+ flag.getName() + ".owner." + id.toLowerCase()) if (!plugin.hasPermission(sender, "worldguard.region.flag.flags."
&& !plugin.hasPermission(sender, "worldguard.region.flag.flags." + flag.getName() + ".owner." + id.toLowerCase())
+ flag.getName() + ".own." + id.toLowerCase())) { && !plugin.hasPermission(sender, "worldguard.region.flag.flags."
continue; + flag.getName() + ".own." + id.toLowerCase())) {
} continue;
} else if (region.isMember(localPlayer)) { }
if (!plugin.hasPermission(sender, "worldguard.region.flag.flags." } else if (region.isMember(localPlayer)) {
+ flag.getName() + ".member." + id.toLowerCase())) { if (!plugin.hasPermission(sender, "worldguard.region.flag.flags."
continue; + flag.getName() + ".member." + id.toLowerCase())) {
} continue;
} else { }
if (!plugin.hasPermission(sender, "worldguard.region.flag.flags." } else {
if (!plugin.hasPermission(sender, "worldguard.region.flag.flags."
+ flag.getName() + "." + id.toLowerCase())) { + flag.getName() + "." + id.toLowerCase())) {
continue; continue;
}
} }
} }
@ -748,20 +761,22 @@ public void flag(CommandContext args, CommandSender sender) throws CommandExcept
list.append(flag.getName()); list.append(flag.getName());
} }
player.sendMessage(ChatColor.RED + "Unknown flag specified: " + flagName); sender.sendMessage(ChatColor.RED + "Unknown flag specified: " + flagName);
player.sendMessage(ChatColor.RED + "Available flags: " + list); sender.sendMessage(ChatColor.RED + "Available flags: " + list);
return; return;
} }
if (region.isOwner(localPlayer)) { if (localPlayer != null) {
plugin.checkPermission(sender, "worldguard.region.flag.flags." if (region.isOwner(localPlayer)) {
+ foundFlag.getName() + ".owner." + id.toLowerCase()); plugin.checkPermission(sender, "worldguard.region.flag.flags."
} else if (region.isMember(localPlayer)) { + foundFlag.getName() + ".owner." + id.toLowerCase());
plugin.checkPermission(sender, "worldguard.region.flag.flags." } else if (region.isMember(localPlayer)) {
+ foundFlag.getName() + ".member." + id.toLowerCase()); plugin.checkPermission(sender, "worldguard.region.flag.flags."
} else { + foundFlag.getName() + ".member." + id.toLowerCase());
plugin.checkPermission(sender, "worldguard.region.flag.flags." } else {
+ foundFlag.getName() + "." + id.toLowerCase()); plugin.checkPermission(sender, "worldguard.region.flag.flags."
+ foundFlag.getName() + "." + id.toLowerCase());
}
} }
if (args.hasFlag('g')) { if (args.hasFlag('g')) {
@ -830,27 +845,37 @@ public void flag(CommandContext args, CommandSender sender) throws CommandExcept
+ e.getMessage()); + e.getMessage());
} }
} }
public <V> void setFlag(ProtectedRegion region, public <V> void setFlag(ProtectedRegion region,
Flag<V> flag, CommandSender sender, String value) Flag<V> flag, CommandSender sender, String value)
throws InvalidFlagFormat { throws InvalidFlagFormat {
region.setFlag(flag, flag.parseInput(plugin, sender, value)); region.setFlag(flag, flag.parseInput(plugin, sender, value));
} }
@Command(aliases = {"setpriority", "priority", "pri"}, usage = "<id> <priority>", @Command(aliases = {"setpriority", "priority", "pri"},
desc = "Set the priority of a region", min = 2, max = 2) usage = "<id> <priority>",
flags = "w:",
desc = "Set the priority of a region",
min = 2, max = 2)
public void setPriority(CommandContext args, CommandSender sender) throws CommandException { public void setPriority(CommandContext args, CommandSender sender) throws CommandException {
Player player = plugin.checkPlayer(sender); final World world;
World world = player.getWorld(); Player player;
LocalPlayer localPlayer = plugin.wrapPlayer(player); LocalPlayer localPlayer = null;
if (args.hasFlag('w')) {
world = plugin.matchWorld(sender, args.getFlag('w'));
} else {
player = plugin.checkPlayer(sender);
localPlayer = plugin.wrapPlayer(player);
world = player.getWorld();
}
String id = args.getString(0); String id = args.getString(0);
int priority = args.getInteger(1); int priority = args.getInteger(1);
if (id.equalsIgnoreCase("__global__")) { if (id.equalsIgnoreCase("__global__")) {
throw new CommandException("The region cannot be named __global__"); throw new CommandException("The region cannot be named __global__");
} }
RegionManager mgr = plugin.getGlobalRegionManager().get(world); RegionManager mgr = plugin.getGlobalRegionManager().get(world);
ProtectedRegion region = mgr.getRegion(id); ProtectedRegion region = mgr.getRegion(id);
if (region == null) { if (region == null) {
@ -859,20 +884,22 @@ public void setPriority(CommandContext args, CommandSender sender) throws Comman
id = region.getId(); id = region.getId();
if (region.isOwner(localPlayer)) { if (localPlayer != null) {
plugin.checkPermission(sender, "worldguard.region.setpriority.own." + id.toLowerCase()); if (region.isOwner(localPlayer)) {
} else if (region.isMember(localPlayer)) { plugin.checkPermission(sender, "worldguard.region.setpriority.own." + id.toLowerCase());
plugin.checkPermission(sender, "worldguard.region.setpriority.member." + id.toLowerCase()); } else if (region.isMember(localPlayer)) {
} else { plugin.checkPermission(sender, "worldguard.region.setpriority.member." + id.toLowerCase());
plugin.checkPermission(sender, "worldguard.region.setpriority." + id.toLowerCase()); } else {
} plugin.checkPermission(sender, "worldguard.region.setpriority." + id.toLowerCase());
}
}
region.setPriority(priority); region.setPriority(priority);
sender.sendMessage(ChatColor.YELLOW sender.sendMessage(ChatColor.YELLOW
+ "Priority of '" + region.getId() + "' set to " + "Priority of '" + region.getId() + "' set to "
+ priority + "."); + priority + ".");
try { try {
mgr.save(); mgr.save();
} catch (ProtectionDatabaseException e) { } catch (ProtectionDatabaseException e) {
@ -880,20 +907,30 @@ public void setPriority(CommandContext args, CommandSender sender) throws Comman
+ e.getMessage()); + e.getMessage());
} }
} }
@Command(aliases = {"setparent", "parent", "par"}, usage = "<id> [parent-id]", @Command(aliases = {"setparent", "parent", "par"},
desc = "Set the parent of a region", min = 1, max = 2) usage = "<id> [parent-id]",
flags = "w:",
desc = "Set the parent of a region",
min = 1, max = 2)
public void setParent(CommandContext args, CommandSender sender) throws CommandException { public void setParent(CommandContext args, CommandSender sender) throws CommandException {
Player player = plugin.checkPlayer(sender); final World world;
World world = player.getWorld(); Player player;
LocalPlayer localPlayer = plugin.wrapPlayer(player); LocalPlayer localPlayer = null;
if (args.hasFlag('w')) {
world = plugin.matchWorld(sender, args.getFlag('w'));
} else {
player = plugin.checkPlayer(sender);
localPlayer = plugin.wrapPlayer(player);
world = player.getWorld();
}
String id = args.getString(0); String id = args.getString(0);
if (id.equalsIgnoreCase("__global__")) { if (id.equalsIgnoreCase("__global__")) {
throw new CommandException("The region cannot be named __global__"); throw new CommandException("The region cannot be named __global__");
} }
RegionManager mgr = plugin.getGlobalRegionManager().get(world); RegionManager mgr = plugin.getGlobalRegionManager().get(world);
ProtectedRegion region = mgr.getRegion(id); ProtectedRegion region = mgr.getRegion(id);
if (region == null) { if (region == null) {
@ -907,44 +944,45 @@ public void setParent(CommandContext args, CommandSender sender) throws CommandE
region.setParent(null); region.setParent(null);
} catch (CircularInheritanceException ignore) { } catch (CircularInheritanceException ignore) {
} }
sender.sendMessage(ChatColor.YELLOW sender.sendMessage(ChatColor.YELLOW
+ "Parent of '" + region.getId() + "' cleared."); + "Parent of '" + region.getId() + "' cleared.");
} else { } else {
String parentId = args.getString(1); String parentId = args.getString(1);
ProtectedRegion parent = mgr.getRegion(parentId); ProtectedRegion parent = mgr.getRegion(parentId);
if (parent == null) { if (parent == null) {
throw new CommandException("Could not find the parent region by that ID."); throw new CommandException("Could not find the parent region by that ID.");
} }
if (region.isOwner(localPlayer)) { if (localPlayer != null) {
plugin.checkPermission(sender, "worldguard.region.setparent.own." + id.toLowerCase()); if (region.isOwner(localPlayer)) {
} else if (region.isMember(localPlayer)) { plugin.checkPermission(sender, "worldguard.region.setparent.own." + id.toLowerCase());
plugin.checkPermission(sender, "worldguard.region.setparent.member." + id.toLowerCase()); } else if (region.isMember(localPlayer)) {
} else { plugin.checkPermission(sender, "worldguard.region.setparent.member." + id.toLowerCase());
plugin.checkPermission(sender, "worldguard.region.setparent." + id.toLowerCase()); } else {
} plugin.checkPermission(sender, "worldguard.region.setparent." + id.toLowerCase());
}
if (parent.isOwner(localPlayer)) {
plugin.checkPermission(sender, "worldguard.region.setparent.own." + parentId.toLowerCase()); if (parent.isOwner(localPlayer)) {
} else if (parent.isMember(localPlayer)) { plugin.checkPermission(sender, "worldguard.region.setparent.own." + parentId.toLowerCase());
plugin.checkPermission(sender, "worldguard.region.setparent.member." + parentId.toLowerCase()); } else if (parent.isMember(localPlayer)) {
} else { plugin.checkPermission(sender, "worldguard.region.setparent.member." + parentId.toLowerCase());
plugin.checkPermission(sender, "worldguard.region.setparent." + parentId.toLowerCase()); } else {
plugin.checkPermission(sender, "worldguard.region.setparent." + parentId.toLowerCase());
}
} }
try { try {
region.setParent(parent); region.setParent(parent);
} catch (CircularInheritanceException e) { } catch (CircularInheritanceException e) {
throw new CommandException("Circular inheritance detected!"); throw new CommandException("Circular inheritance detected!");
} }
sender.sendMessage(ChatColor.YELLOW sender.sendMessage(ChatColor.YELLOW
+ "Parent of '" + region.getId() + "' set to '" + "Parent of '" + region.getId() + "' set to '"
+ parent.getId() + "'."); + parent.getId() + "'.");
} }
try { try {
mgr.save(); mgr.save();
} catch (ProtectionDatabaseException e) { } catch (ProtectionDatabaseException e) {
@ -952,15 +990,24 @@ public void setParent(CommandContext args, CommandSender sender) throws CommandE
+ e.getMessage()); + e.getMessage());
} }
} }
@Command(aliases = {"remove", "delete", "del", "rem"}, usage = "<id>", @Command(aliases = {"remove", "delete", "del", "rem"},
desc = "Remove a region", min = 1, max = 1) usage = "<id>",
flags = "w:",
desc = "Remove a region",
min = 1, max = 1)
public void remove(CommandContext args, CommandSender sender) throws CommandException { public void remove(CommandContext args, CommandSender sender) throws CommandException {
final World world;
Player player = plugin.checkPlayer(sender); Player player;
World world = player.getWorld(); LocalPlayer localPlayer = null;
LocalPlayer localPlayer = plugin.wrapPlayer(player); if (args.hasFlag('w')) {
world = plugin.matchWorld(sender, args.getFlag('w'));
} else {
player = plugin.checkPlayer(sender);
localPlayer = plugin.wrapPlayer(player);
world = player.getWorld();
}
String id = args.getString(0); String id = args.getString(0);
RegionManager mgr = plugin.getGlobalRegionManager().get(world); RegionManager mgr = plugin.getGlobalRegionManager().get(world);
@ -969,20 +1016,22 @@ public void remove(CommandContext args, CommandSender sender) throws CommandExce
if (region == null) { if (region == null) {
throw new CommandException("Could not find a region by that ID."); throw new CommandException("Could not find a region by that ID.");
} }
if (region.isOwner(localPlayer)) { if (localPlayer != null) {
plugin.checkPermission(sender, "worldguard.region.remove.own." + id.toLowerCase()); if (region.isOwner(localPlayer)) {
} else if (region.isMember(localPlayer)) { plugin.checkPermission(sender, "worldguard.region.remove.own." + id.toLowerCase());
plugin.checkPermission(sender, "worldguard.region.remove.member." + id.toLowerCase()); } else if (region.isMember(localPlayer)) {
} else { plugin.checkPermission(sender, "worldguard.region.remove.member." + id.toLowerCase());
plugin.checkPermission(sender, "worldguard.region.remove." + id.toLowerCase()); } else {
plugin.checkPermission(sender, "worldguard.region.remove." + id.toLowerCase());
}
} }
mgr.removeRegion(id); mgr.removeRegion(id);
sender.sendMessage(ChatColor.YELLOW sender.sendMessage(ChatColor.YELLOW
+ "Region '" + id + "' removed."); + "Region '" + id + "' removed.");
try { try {
mgr.save(); mgr.save();
} catch (ProtectionDatabaseException e) { } catch (ProtectionDatabaseException e) {
@ -990,21 +1039,21 @@ public void remove(CommandContext args, CommandSender sender) throws CommandExce
+ e.getMessage()); + e.getMessage());
} }
} }
@Command(aliases = {"load", "reload"}, usage = "[world]", @Command(aliases = {"load", "reload"}, usage = "[world]",
desc = "Reload regions from file", max = 1) desc = "Reload regions from file", max = 1)
@CommandPermissions({"worldguard.region.load"}) @CommandPermissions({"worldguard.region.load"})
public void load(CommandContext args, CommandSender sender) throws CommandException { public void load(CommandContext args, CommandSender sender) throws CommandException {
World world = null; World world = null;
if (args.argsLength() > 0) { if (args.argsLength() > 0) {
world = plugin.matchWorld(sender, args.getString(0)); world = plugin.matchWorld(sender, args.getString(0));
} }
if (world != null) { if (world != null) {
RegionManager mgr = plugin.getGlobalRegionManager().get(world); RegionManager mgr = plugin.getGlobalRegionManager().get(world);
try { try {
mgr.load(); mgr.load();
sender.sendMessage(ChatColor.YELLOW sender.sendMessage(ChatColor.YELLOW
@ -1016,7 +1065,7 @@ public void load(CommandContext args, CommandSender sender) throws CommandExcept
} else { } else {
for (World w : plugin.getServer().getWorlds()) { for (World w : plugin.getServer().getWorlds()) {
RegionManager mgr = plugin.getGlobalRegionManager().get(w); RegionManager mgr = plugin.getGlobalRegionManager().get(w);
try { try {
mgr.load(); mgr.load();
} catch (ProtectionDatabaseException e) { } catch (ProtectionDatabaseException e) {
@ -1024,26 +1073,26 @@ public void load(CommandContext args, CommandSender sender) throws CommandExcept
+ e.getMessage()); + e.getMessage());
} }
} }
sender.sendMessage(ChatColor.YELLOW sender.sendMessage(ChatColor.YELLOW
+ "Region databases loaded."); + "Region databases loaded.");
} }
} }
@Command(aliases = {"save", "write"}, usage = "[world]", @Command(aliases = {"save", "write"}, usage = "[world]",
desc = "Re-save regions to file", max = 1) desc = "Re-save regions to file", max = 1)
@CommandPermissions({"worldguard.region.save"}) @CommandPermissions({"worldguard.region.save"})
public void save(CommandContext args, CommandSender sender) throws CommandException { public void save(CommandContext args, CommandSender sender) throws CommandException {
World world = null; World world = null;
if (args.argsLength() > 0) { if (args.argsLength() > 0) {
world = plugin.matchWorld(sender, args.getString(0)); world = plugin.matchWorld(sender, args.getString(0));
} }
if (world != null) { if (world != null) {
RegionManager mgr = plugin.getGlobalRegionManager().get(world); RegionManager mgr = plugin.getGlobalRegionManager().get(world);
try { try {
mgr.save(); mgr.save();
sender.sendMessage(ChatColor.YELLOW sender.sendMessage(ChatColor.YELLOW
@ -1055,7 +1104,7 @@ public void save(CommandContext args, CommandSender sender) throws CommandExcept
} else { } else {
for (World w : plugin.getServer().getWorlds()) { for (World w : plugin.getServer().getWorlds()) {
RegionManager mgr = plugin.getGlobalRegionManager().get(w); RegionManager mgr = plugin.getGlobalRegionManager().get(w);
try { try {
mgr.save(); mgr.save();
} catch (ProtectionDatabaseException e) { } catch (ProtectionDatabaseException e) {
@ -1063,7 +1112,7 @@ public void save(CommandContext args, CommandSender sender) throws CommandExcept
+ e.getMessage()); + e.getMessage());
} }
} }
sender.sendMessage(ChatColor.YELLOW sender.sendMessage(ChatColor.YELLOW
+ "Region databases saved."); + "Region databases saved.");
} }

View File

@ -30,11 +30,11 @@
import com.sk89q.worldguard.LocalPlayer; import com.sk89q.worldguard.LocalPlayer;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin; import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.sk89q.worldguard.domains.DefaultDomain; import com.sk89q.worldguard.domains.DefaultDomain;
import com.sk89q.worldguard.protection.databases.ProtectionDatabaseException;
import com.sk89q.worldguard.protection.databases.RegionDBUtil;
import com.sk89q.worldguard.protection.flags.DefaultFlag; import com.sk89q.worldguard.protection.flags.DefaultFlag;
import com.sk89q.worldguard.protection.managers.RegionManager; import com.sk89q.worldguard.protection.managers.RegionManager;
import com.sk89q.worldguard.protection.regions.ProtectedRegion; import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import com.sk89q.worldguard.protection.databases.ProtectionDatabaseException;
import com.sk89q.worldguard.protection.databases.RegionDBUtil;
// @TODO: A lot of code duplication here! Need to fix. // @TODO: A lot of code duplication here! Need to fix.
@ -44,15 +44,24 @@ public class RegionMemberCommands {
public RegionMemberCommands(WorldGuardPlugin plugin) { public RegionMemberCommands(WorldGuardPlugin plugin) {
this.plugin = plugin; this.plugin = plugin;
} }
@Command(aliases = {"addmember", "addmember"}, usage = "<id> <members...>", @Command(aliases = {"addmember", "addmember"},
desc = "Add a member to a region", min = 2) usage = "<id> <members...>",
flags = "w:",
desc = "Add a member to a region",
min = 2)
public void addMember(CommandContext args, CommandSender sender) throws CommandException { public void addMember(CommandContext args, CommandSender sender) throws CommandException {
final World world;
Player player = plugin.checkPlayer(sender); Player player;
World world = player.getWorld(); LocalPlayer localPlayer = null;
LocalPlayer localPlayer = plugin.wrapPlayer(player); if (args.hasFlag('w')) {
world = plugin.matchWorld(sender, args.getFlag('w'));
} else {
player = plugin.checkPlayer(sender);
localPlayer = plugin.wrapPlayer(player);
world = player.getWorld();
}
String id = args.getString(0); String id = args.getString(0);
RegionManager mgr = plugin.getGlobalRegionManager().get(world); RegionManager mgr = plugin.getGlobalRegionManager().get(world);
@ -64,19 +73,21 @@ public void addMember(CommandContext args, CommandSender sender) throws CommandE
id = region.getId(); id = region.getId();
if (region.isOwner(localPlayer)) { if (localPlayer != null) {
plugin.checkPermission(sender, "worldguard.region.addmember.own." + id.toLowerCase()); if (region.isOwner(localPlayer)) {
} else if (region.isMember(localPlayer)) { plugin.checkPermission(sender, "worldguard.region.addmember.own." + id.toLowerCase());
plugin.checkPermission(sender, "worldguard.region.addmember.member." + id.toLowerCase()); } else if (region.isMember(localPlayer)) {
} else { plugin.checkPermission(sender, "worldguard.region.addmember.member." + id.toLowerCase());
plugin.checkPermission(sender, "worldguard.region.addmember." + id.toLowerCase()); } else {
plugin.checkPermission(sender, "worldguard.region.addmember." + id.toLowerCase());
}
} }
RegionDBUtil.addToDomain(region.getMembers(), args.getPaddedSlice(2, 0), 0); RegionDBUtil.addToDomain(region.getMembers(), args.getPaddedSlice(2, 0), 0);
sender.sendMessage(ChatColor.YELLOW sender.sendMessage(ChatColor.YELLOW
+ "Region '" + id + "' updated."); + "Region '" + id + "' updated.");
try { try {
mgr.save(); mgr.save();
} catch (ProtectionDatabaseException e) { } catch (ProtectionDatabaseException e) {
@ -84,15 +95,24 @@ public void addMember(CommandContext args, CommandSender sender) throws CommandE
+ e.getMessage()); + e.getMessage());
} }
} }
@Command(aliases = {"addowner", "addowner"}, usage = "<id> <owners...>", @Command(aliases = {"addowner", "addowner"},
desc = "Add an owner to a region", min = 2) usage = "<id> <owners...>",
flags = "w:",
desc = "Add an owner to a region",
min = 2)
public void addOwner(CommandContext args, CommandSender sender) throws CommandException { public void addOwner(CommandContext args, CommandSender sender) throws CommandException {
final World world;
Player player = plugin.checkPlayer(sender); Player player = null;
World world = player.getWorld(); LocalPlayer localPlayer = null;
LocalPlayer localPlayer = plugin.wrapPlayer(player); if (args.hasFlag('w')) {
world = plugin.matchWorld(sender, args.getFlag('w'));
} else {
player = plugin.checkPlayer(sender);
localPlayer = plugin.wrapPlayer(player);
world = player.getWorld();
}
String id = args.getString(0); String id = args.getString(0);
RegionManager mgr = plugin.getGlobalRegionManager().get(world); RegionManager mgr = plugin.getGlobalRegionManager().get(world);
@ -106,22 +126,24 @@ public void addOwner(CommandContext args, CommandSender sender) throws CommandEx
Boolean flag = region.getFlag(DefaultFlag.BUYABLE); Boolean flag = region.getFlag(DefaultFlag.BUYABLE);
DefaultDomain owners = region.getOwners(); DefaultDomain owners = region.getOwners();
if (flag != null && flag && owners != null && owners.size() == 0) { if (localPlayer != null) {
if (!plugin.hasPermission(player, "worldguard.region.unlimited")) { if (flag != null && flag && owners != null && owners.size() == 0) {
int maxRegionCount = plugin.getGlobalStateManager().get(world).getMaxRegionCount(player); if (!plugin.hasPermission(player, "worldguard.region.unlimited")) {
if (maxRegionCount >= 0 && mgr.getRegionCountOfPlayer(localPlayer) int maxRegionCount = plugin.getGlobalStateManager().get(world).getMaxRegionCount(player);
>= maxRegionCount) { if (maxRegionCount >= 0 && mgr.getRegionCountOfPlayer(localPlayer)
throw new CommandException("You already own the maximum allowed amount of regions."); >= maxRegionCount) {
throw new CommandException("You already own the maximum allowed amount of regions.");
}
} }
} plugin.checkPermission(sender, "worldguard.region.addowner.unclaimed." + id.toLowerCase());
plugin.checkPermission(sender, "worldguard.region.addowner.unclaimed." + id.toLowerCase());
} else {
if (region.isOwner(localPlayer)) {
plugin.checkPermission(sender, "worldguard.region.addowner.own." + id.toLowerCase());
} else if (region.isMember(localPlayer)) {
plugin.checkPermission(sender, "worldguard.region.addowner.member." + id.toLowerCase());
} else { } else {
plugin.checkPermission(sender, "worldguard.region.addowner." + id.toLowerCase()); if (region.isOwner(localPlayer)) {
plugin.checkPermission(sender, "worldguard.region.addowner.own." + id.toLowerCase());
} else if (region.isMember(localPlayer)) {
plugin.checkPermission(sender, "worldguard.region.addowner.member." + id.toLowerCase());
} else {
plugin.checkPermission(sender, "worldguard.region.addowner." + id.toLowerCase());
}
} }
} }
@ -129,7 +151,7 @@ public void addOwner(CommandContext args, CommandSender sender) throws CommandEx
sender.sendMessage(ChatColor.YELLOW sender.sendMessage(ChatColor.YELLOW
+ "Region '" + id + "' updated."); + "Region '" + id + "' updated.");
try { try {
mgr.save(); mgr.save();
} catch (ProtectionDatabaseException e) { } catch (ProtectionDatabaseException e) {
@ -137,15 +159,24 @@ public void addOwner(CommandContext args, CommandSender sender) throws CommandEx
+ e.getMessage()); + e.getMessage());
} }
} }
@Command(aliases = {"removemember", "remmember", "removemem", "remmem"}, usage = "<id> <owners...>", @Command(aliases = {"removemember", "remmember", "removemem", "remmem"},
desc = "Remove an owner to a region", min = 2) usage = "<id> <owners...>",
flags = "w:",
desc = "Remove an owner to a region",
min = 2)
public void removeMember(CommandContext args, CommandSender sender) throws CommandException { public void removeMember(CommandContext args, CommandSender sender) throws CommandException {
final World world;
Player player = plugin.checkPlayer(sender); Player player;
World world = player.getWorld(); LocalPlayer localPlayer = null;
LocalPlayer localPlayer = plugin.wrapPlayer(player); if (args.hasFlag('w')) {
world = plugin.matchWorld(sender, args.getFlag('w'));
} else {
player = plugin.checkPlayer(sender);
localPlayer = plugin.wrapPlayer(player);
world = player.getWorld();
}
String id = args.getString(0); String id = args.getString(0);
RegionManager mgr = plugin.getGlobalRegionManager().get(world); RegionManager mgr = plugin.getGlobalRegionManager().get(world);
@ -157,19 +188,21 @@ public void removeMember(CommandContext args, CommandSender sender) throws Comma
id = region.getId(); id = region.getId();
if (region.isOwner(localPlayer)) { if (localPlayer != null) {
plugin.checkPermission(sender, "worldguard.region.removemember.own." + id.toLowerCase()); if (region.isOwner(localPlayer)) {
} else if (region.isMember(localPlayer)) { plugin.checkPermission(sender, "worldguard.region.removemember.own." + id.toLowerCase());
plugin.checkPermission(sender, "worldguard.region.removemember.member." + id.toLowerCase()); } else if (region.isMember(localPlayer)) {
} else { plugin.checkPermission(sender, "worldguard.region.removemember.member." + id.toLowerCase());
plugin.checkPermission(sender, "worldguard.region.removemember." + id.toLowerCase()); } else {
plugin.checkPermission(sender, "worldguard.region.removemember." + id.toLowerCase());
}
} }
RegionDBUtil.removeFromDomain(region.getMembers(), args.getPaddedSlice(2, 0), 0); RegionDBUtil.removeFromDomain(region.getMembers(), args.getPaddedSlice(2, 0), 0);
sender.sendMessage(ChatColor.YELLOW sender.sendMessage(ChatColor.YELLOW
+ "Region '" + id + "' updated."); + "Region '" + id + "' updated.");
try { try {
mgr.save(); mgr.save();
} catch (ProtectionDatabaseException e) { } catch (ProtectionDatabaseException e) {
@ -177,16 +210,25 @@ public void removeMember(CommandContext args, CommandSender sender) throws Comma
+ e.getMessage()); + e.getMessage());
} }
} }
@Command(aliases = {"removeowner", "remowner"}, usage = "<id> <owners...>", @Command(aliases = {"removeowner", "remowner"},
desc = "Remove an owner to a region", min = 2) usage = "<id> <owners...>",
flags = "w:",
desc = "Remove an owner to a region",
min = 2)
public void removeOwner(CommandContext args, public void removeOwner(CommandContext args,
CommandSender sender) throws CommandException { CommandSender sender) throws CommandException {
final World world;
Player player = plugin.checkPlayer(sender); Player player;
World world = player.getWorld(); LocalPlayer localPlayer = null;
LocalPlayer localPlayer = plugin.wrapPlayer(player); if (args.hasFlag('w')) {
world = plugin.matchWorld(sender, args.getFlag('w'));
} else {
player = plugin.checkPlayer(sender);
localPlayer = plugin.wrapPlayer(player);
world = player.getWorld();
}
String id = args.getString(0); String id = args.getString(0);
RegionManager mgr = plugin.getGlobalRegionManager().get(world); RegionManager mgr = plugin.getGlobalRegionManager().get(world);
@ -198,19 +240,21 @@ public void removeOwner(CommandContext args,
id = region.getId(); id = region.getId();
if (region.isOwner(localPlayer)) { if (localPlayer != null) {
plugin.checkPermission(sender, "worldguard.region.removeowner.own." + id.toLowerCase()); if (region.isOwner(localPlayer)) {
} else if (region.isMember(localPlayer)) { plugin.checkPermission(sender, "worldguard.region.removeowner.own." + id.toLowerCase());
plugin.checkPermission(sender, "worldguard.region.removeowner.member." + id.toLowerCase()); } else if (region.isMember(localPlayer)) {
} else { plugin.checkPermission(sender, "worldguard.region.removeowner.member." + id.toLowerCase());
plugin.checkPermission(sender, "worldguard.region.removeowner." + id.toLowerCase()); } else {
plugin.checkPermission(sender, "worldguard.region.removeowner." + id.toLowerCase());
}
} }
RegionDBUtil.removeFromDomain(region.getOwners(), args.getPaddedSlice(2, 0), 0); RegionDBUtil.removeFromDomain(region.getOwners(), args.getPaddedSlice(2, 0), 0);
sender.sendMessage(ChatColor.YELLOW sender.sendMessage(ChatColor.YELLOW
+ "Region '" + id + "' updated."); + "Region '" + id + "' updated.");
try { try {
mgr.save(); mgr.save();
} catch (ProtectionDatabaseException e) { } catch (ProtectionDatabaseException e) {