From b5f80351ec787838eed37ebe25ad0ca8d633ef07 Mon Sep 17 00:00:00 2001 From: Wizjany Date: Wed, 30 Jan 2013 14:39:22 -0500 Subject: [PATCH] Allow a lot of commands to be sent from the console. Examples: /rg addmem -w /rg flag -w /rg remove -w Command blocks are currently untested. List of commands: flag, priority, parent, remove, addmember, removemember, addowner, removeowner --- .../bukkit/commands/RegionCommands.java | 363 ++++++++++-------- .../bukkit/commands/RegionMemberCommands.java | 184 +++++---- 2 files changed, 320 insertions(+), 227 deletions(-) diff --git a/src/main/java/com/sk89q/worldguard/bukkit/commands/RegionCommands.java b/src/main/java/com/sk89q/worldguard/bukkit/commands/RegionCommands.java index 3120f619..2aad976c 100644 --- a/src/main/java/com/sk89q/worldguard/bukkit/commands/RegionCommands.java +++ b/src/main/java/com/sk89q/worldguard/bukkit/commands/RegionCommands.java @@ -71,40 +71,40 @@ public class RegionCommands { private MigratorKey migrateDBRequest; private Date migrateDBRequestDate; - + public RegionCommands(WorldGuardPlugin plugin) { this.plugin = plugin; } - + @Command(aliases = {"define", "def", "d"}, usage = " [ [ []]]", desc = "Defines a region", min = 1) @CommandPermissions({"worldguard.region.define"}) public void define(CommandContext args, CommandSender sender) throws CommandException { - + Player player = plugin.checkPlayer(sender); WorldEditPlugin worldEdit = plugin.getWorldEdit(); 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); - + if (sel == null) { throw new CommandException("Select a region with WorldEdit first."); } - + RegionManager mgr = plugin.getGlobalRegionManager().get(sel.getWorld()); if (mgr.hasRegion(id)) { throw new CommandException("That region is already defined. Use redefine instead."); } - + ProtectedRegion region; // Detect the type of region from WorldEdit @@ -126,9 +126,9 @@ public void define(CommandContext args, CommandSender sender) throws CommandExce if (args.argsLength() > 1) { region.setOwners(RegionDBUtil.parseDomainString(args.getSlice(1), 1)); } - + mgr.addRegion(region); - + try { mgr.save(); sender.sendMessage(ChatColor.YELLOW + "Region saved as " + id + "."); @@ -137,17 +137,17 @@ public void define(CommandContext args, CommandSender sender) throws CommandExce + e.getMessage()); } } - + @Command(aliases = {"redefine", "update", "move"}, usage = "", desc = "Re-defines the shape of a region", min = 1, max = 1) public void redefine(CommandContext args, CommandSender sender) throws CommandException { - + Player player = plugin.checkPlayer(sender); World world = player.getWorld(); 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__"); } @@ -166,16 +166,16 @@ public void redefine(CommandContext args, CommandSender sender) throws CommandEx } else { plugin.checkPermission(sender, "worldguard.region.redefine"); } - + // Attempt to get the player's selection from WorldEdit Selection sel = worldEdit.getSelection(player); - + if (sel == null) { throw new CommandException("Select a region with WorldEdit first."); } - + ProtectedRegion region; - + // Detect the type of region from WorldEdit if (sel instanceof Polygonal2DSelection) { Polygonal2DSelection polySel = (Polygonal2DSelection) sel; @@ -199,11 +199,11 @@ public void redefine(CommandContext args, CommandSender sender) throws CommandEx region.setParent(existing.getParent()); } catch (CircularInheritanceException ignore) { } - + mgr.addRegion(region); - + sender.sendMessage(ChatColor.YELLOW + "Region updated with new area."); - + try { mgr.save(); } catch (ProtectionDatabaseException e) { @@ -211,38 +211,38 @@ public void redefine(CommandContext args, CommandSender sender) throws CommandEx + e.getMessage()); } } - + @Command(aliases = {"claim"}, usage = " [ [ []]]", desc = "Claim a region", min = 1) @CommandPermissions({"worldguard.region.claim"}) public void claim(CommandContext args, CommandSender sender) throws CommandException { - + Player player = plugin.checkPlayer(sender); LocalPlayer localPlayer = plugin.wrapPlayer(player); WorldEditPlugin worldEdit = plugin.getWorldEdit(); 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); - + if (sel == null) { throw new CommandException("Select a region with WorldEdit first."); } - + RegionManager mgr = plugin.getGlobalRegionManager().get(sel.getWorld()); if (mgr.hasRegion(id)) { throw new CommandException("That region already exists. Please choose a different name."); } - + ProtectedRegion region; // 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()); - + if (!plugin.hasPermission(sender, "worldguard.region.unlimited")) { // Check whether the player has created too many regions 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."); } } - + ApplicableRegionSet regions = mgr.getApplicableRegions(region); - + // Check if this region overlaps any other region if (regions.size() > 0) { if (!regions.isOwnerOfAll(localPlayer)) { @@ -333,7 +333,7 @@ public void claim(CommandContext args, CommandSender sender) throws CommandExcep region.getOwners().addPlayer(player.getName()); mgr.addRegion(region); - + try { mgr.save(); sender.sendMessage(ChatColor.YELLOW + "Region saved as " + id + "."); @@ -402,7 +402,7 @@ public void selectRegion(Player player, LocalPlayer localPlayer, ProtectedRegion world, poly2d.getPoints(), poly2d.getMinimumPoint().getBlockY(), poly2d.getMaximumPoint().getBlockY() - ); + ); worldEdit.setSelection(player, selection); player.sendMessage(ChatColor.YELLOW + "Region selected as a polygon."); } else if (region instanceof GlobalProtectedRegion) { @@ -537,7 +537,7 @@ public void displayRegionInfo(CommandSender sender, final LocalPlayer localPlaye sender.sendMessage(ChatColor.LIGHT_PURPLE + "Bounds:" + " (" + min.getBlockX() + "," + min.getBlockY() + "," + min.getBlockZ() + ")" + " (" + max.getBlockX() + "," + max.getBlockY() + "," + max.getBlockZ() + ")" - ); + ); } public class RegionEntry implements Comparable{ @@ -662,14 +662,21 @@ public void list(CommandContext args, CommandSender sender) throws CommandExcept } } - @Command(aliases = {"flag", "f"}, usage = " [-g group] [value]", flags = "g:", + @Command(aliases = {"flag", "f"}, usage = " [-g group] [value]", flags = "g:w:", desc = "Set flags", min = 2) public void flag(CommandContext args, CommandSender sender) throws CommandException { - - Player player = plugin.checkPlayer(sender); - World world = player.getWorld(); - LocalPlayer localPlayer = plugin.wrapPlayer(player); - + + final World world; + Player 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 flagName = args.getString(1); String value = null; @@ -693,20 +700,24 @@ public void flag(CommandContext args, CommandSender sender) throws CommandExcept // @TODO deprecate "flag.[own./member./blank]" boolean hasPerm = false; - if (region.isOwner(localPlayer)) { - 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; + if (localPlayer == null) { + 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 (region.isOwner(localPlayer)) { + 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(); - + Flag foundFlag = null; - + // Now time to find the flag! for (Flag flag : DefaultFlag.getFlags()) { // Try to detect the flag @@ -715,30 +726,32 @@ public void flag(CommandContext args, CommandSender sender) throws CommandExcept break; } } - + if (foundFlag == null) { StringBuilder list = new StringBuilder(); - + // Need to build a list for (Flag flag : DefaultFlag.getFlags()) { // @TODO deprecate inconsistant "owner" permission - if (region.isOwner(localPlayer)) { - if (!plugin.hasPermission(sender, "worldguard.region.flag.flags." - + flag.getName() + ".owner." + id.toLowerCase()) - && !plugin.hasPermission(sender, "worldguard.region.flag.flags." - + flag.getName() + ".own." + id.toLowerCase())) { - continue; - } - } else if (region.isMember(localPlayer)) { - if (!plugin.hasPermission(sender, "worldguard.region.flag.flags." - + flag.getName() + ".member." + id.toLowerCase())) { - continue; - } - } else { - if (!plugin.hasPermission(sender, "worldguard.region.flag.flags." + if (localPlayer != null) { + if (region.isOwner(localPlayer)) { + if (!plugin.hasPermission(sender, "worldguard.region.flag.flags." + + flag.getName() + ".owner." + id.toLowerCase()) + && !plugin.hasPermission(sender, "worldguard.region.flag.flags." + + flag.getName() + ".own." + id.toLowerCase())) { + continue; + } + } else if (region.isMember(localPlayer)) { + if (!plugin.hasPermission(sender, "worldguard.region.flag.flags." + + flag.getName() + ".member." + id.toLowerCase())) { + continue; + } + } else { + if (!plugin.hasPermission(sender, "worldguard.region.flag.flags." + flag.getName() + "." + id.toLowerCase())) { - continue; + continue; + } } } @@ -748,20 +761,22 @@ public void flag(CommandContext args, CommandSender sender) throws CommandExcept list.append(flag.getName()); } - player.sendMessage(ChatColor.RED + "Unknown flag specified: " + flagName); - player.sendMessage(ChatColor.RED + "Available flags: " + list); + sender.sendMessage(ChatColor.RED + "Unknown flag specified: " + flagName); + sender.sendMessage(ChatColor.RED + "Available flags: " + list); return; } - if (region.isOwner(localPlayer)) { - plugin.checkPermission(sender, "worldguard.region.flag.flags." - + foundFlag.getName() + ".owner." + id.toLowerCase()); - } else if (region.isMember(localPlayer)) { - plugin.checkPermission(sender, "worldguard.region.flag.flags." - + foundFlag.getName() + ".member." + id.toLowerCase()); - } else { - plugin.checkPermission(sender, "worldguard.region.flag.flags." - + foundFlag.getName() + "." + id.toLowerCase()); + if (localPlayer != null) { + if (region.isOwner(localPlayer)) { + plugin.checkPermission(sender, "worldguard.region.flag.flags." + + foundFlag.getName() + ".owner." + id.toLowerCase()); + } else if (region.isMember(localPlayer)) { + plugin.checkPermission(sender, "worldguard.region.flag.flags." + + foundFlag.getName() + ".member." + id.toLowerCase()); + } else { + plugin.checkPermission(sender, "worldguard.region.flag.flags." + + foundFlag.getName() + "." + id.toLowerCase()); + } } if (args.hasFlag('g')) { @@ -830,27 +845,37 @@ public void flag(CommandContext args, CommandSender sender) throws CommandExcept + e.getMessage()); } } - + public void setFlag(ProtectedRegion region, Flag flag, CommandSender sender, String value) - throws InvalidFlagFormat { + throws InvalidFlagFormat { region.setFlag(flag, flag.parseInput(plugin, sender, value)); } - - @Command(aliases = {"setpriority", "priority", "pri"}, usage = " ", - desc = "Set the priority of a region", min = 2, max = 2) + + @Command(aliases = {"setpriority", "priority", "pri"}, + usage = " ", + flags = "w:", + desc = "Set the priority of a region", + min = 2, max = 2) public void setPriority(CommandContext args, CommandSender sender) throws CommandException { - Player player = plugin.checkPlayer(sender); - World world = player.getWorld(); - LocalPlayer localPlayer = plugin.wrapPlayer(player); - + final World world; + Player 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); 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); if (region == null) { @@ -859,20 +884,22 @@ public void setPriority(CommandContext args, CommandSender sender) throws Comman id = region.getId(); - if (region.isOwner(localPlayer)) { - plugin.checkPermission(sender, "worldguard.region.setpriority.own." + id.toLowerCase()); - } else if (region.isMember(localPlayer)) { - plugin.checkPermission(sender, "worldguard.region.setpriority.member." + id.toLowerCase()); - } else { - plugin.checkPermission(sender, "worldguard.region.setpriority." + id.toLowerCase()); - } + if (localPlayer != null) { + if (region.isOwner(localPlayer)) { + plugin.checkPermission(sender, "worldguard.region.setpriority.own." + id.toLowerCase()); + } else if (region.isMember(localPlayer)) { + plugin.checkPermission(sender, "worldguard.region.setpriority.member." + id.toLowerCase()); + } else { + plugin.checkPermission(sender, "worldguard.region.setpriority." + id.toLowerCase()); + } + } region.setPriority(priority); sender.sendMessage(ChatColor.YELLOW + "Priority of '" + region.getId() + "' set to " + priority + "."); - + try { mgr.save(); } catch (ProtectionDatabaseException e) { @@ -880,20 +907,30 @@ public void setPriority(CommandContext args, CommandSender sender) throws Comman + e.getMessage()); } } - - @Command(aliases = {"setparent", "parent", "par"}, usage = " [parent-id]", - desc = "Set the parent of a region", min = 1, max = 2) + + @Command(aliases = {"setparent", "parent", "par"}, + usage = " [parent-id]", + flags = "w:", + desc = "Set the parent of a region", + min = 1, max = 2) public void setParent(CommandContext args, CommandSender sender) throws CommandException { - Player player = plugin.checkPlayer(sender); - World world = player.getWorld(); - LocalPlayer localPlayer = plugin.wrapPlayer(player); - + final World world; + Player 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); - + if (id.equalsIgnoreCase("__global__")) { throw new CommandException("The region cannot be named __global__"); } - + RegionManager mgr = plugin.getGlobalRegionManager().get(world); ProtectedRegion region = mgr.getRegion(id); if (region == null) { @@ -907,44 +944,45 @@ public void setParent(CommandContext args, CommandSender sender) throws CommandE region.setParent(null); } catch (CircularInheritanceException ignore) { } - + sender.sendMessage(ChatColor.YELLOW + "Parent of '" + region.getId() + "' cleared."); } else { String parentId = args.getString(1); ProtectedRegion parent = mgr.getRegion(parentId); - + if (parent == null) { throw new CommandException("Could not find the parent region by that ID."); } - - if (region.isOwner(localPlayer)) { - plugin.checkPermission(sender, "worldguard.region.setparent.own." + id.toLowerCase()); - } else if (region.isMember(localPlayer)) { - plugin.checkPermission(sender, "worldguard.region.setparent.member." + id.toLowerCase()); - } else { - plugin.checkPermission(sender, "worldguard.region.setparent." + id.toLowerCase()); - } - - if (parent.isOwner(localPlayer)) { - plugin.checkPermission(sender, "worldguard.region.setparent.own." + parentId.toLowerCase()); - } else if (parent.isMember(localPlayer)) { - plugin.checkPermission(sender, "worldguard.region.setparent.member." + parentId.toLowerCase()); - } else { - plugin.checkPermission(sender, "worldguard.region.setparent." + parentId.toLowerCase()); + + if (localPlayer != null) { + if (region.isOwner(localPlayer)) { + plugin.checkPermission(sender, "worldguard.region.setparent.own." + id.toLowerCase()); + } else if (region.isMember(localPlayer)) { + plugin.checkPermission(sender, "worldguard.region.setparent.member." + id.toLowerCase()); + } else { + plugin.checkPermission(sender, "worldguard.region.setparent." + id.toLowerCase()); + } + + if (parent.isOwner(localPlayer)) { + plugin.checkPermission(sender, "worldguard.region.setparent.own." + parentId.toLowerCase()); + } else if (parent.isMember(localPlayer)) { + plugin.checkPermission(sender, "worldguard.region.setparent.member." + parentId.toLowerCase()); + } else { + plugin.checkPermission(sender, "worldguard.region.setparent." + parentId.toLowerCase()); + } } - try { region.setParent(parent); } catch (CircularInheritanceException e) { throw new CommandException("Circular inheritance detected!"); } - + sender.sendMessage(ChatColor.YELLOW + "Parent of '" + region.getId() + "' set to '" + parent.getId() + "'."); } - + try { mgr.save(); } catch (ProtectionDatabaseException e) { @@ -952,15 +990,24 @@ public void setParent(CommandContext args, CommandSender sender) throws CommandE + e.getMessage()); } } - - @Command(aliases = {"remove", "delete", "del", "rem"}, usage = "", - desc = "Remove a region", min = 1, max = 1) + + @Command(aliases = {"remove", "delete", "del", "rem"}, + usage = "", + flags = "w:", + desc = "Remove a region", + min = 1, max = 1) public void remove(CommandContext args, CommandSender sender) throws CommandException { - - Player player = plugin.checkPlayer(sender); - World world = player.getWorld(); - LocalPlayer localPlayer = plugin.wrapPlayer(player); - + final World world; + Player 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); RegionManager mgr = plugin.getGlobalRegionManager().get(world); @@ -969,20 +1016,22 @@ public void remove(CommandContext args, CommandSender sender) throws CommandExce if (region == null) { throw new CommandException("Could not find a region by that ID."); } - - if (region.isOwner(localPlayer)) { - plugin.checkPermission(sender, "worldguard.region.remove.own." + id.toLowerCase()); - } else if (region.isMember(localPlayer)) { - plugin.checkPermission(sender, "worldguard.region.remove.member." + id.toLowerCase()); - } else { - plugin.checkPermission(sender, "worldguard.region.remove." + id.toLowerCase()); + + if (localPlayer != null) { + if (region.isOwner(localPlayer)) { + plugin.checkPermission(sender, "worldguard.region.remove.own." + id.toLowerCase()); + } else if (region.isMember(localPlayer)) { + plugin.checkPermission(sender, "worldguard.region.remove.member." + id.toLowerCase()); + } else { + plugin.checkPermission(sender, "worldguard.region.remove." + id.toLowerCase()); + } } - + mgr.removeRegion(id); - + sender.sendMessage(ChatColor.YELLOW + "Region '" + id + "' removed."); - + try { mgr.save(); } catch (ProtectionDatabaseException e) { @@ -990,21 +1039,21 @@ public void remove(CommandContext args, CommandSender sender) throws CommandExce + e.getMessage()); } } - + @Command(aliases = {"load", "reload"}, usage = "[world]", desc = "Reload regions from file", max = 1) @CommandPermissions({"worldguard.region.load"}) public void load(CommandContext args, CommandSender sender) throws CommandException { - + World world = null; - + if (args.argsLength() > 0) { world = plugin.matchWorld(sender, args.getString(0)); } if (world != null) { RegionManager mgr = plugin.getGlobalRegionManager().get(world); - + try { mgr.load(); sender.sendMessage(ChatColor.YELLOW @@ -1016,7 +1065,7 @@ public void load(CommandContext args, CommandSender sender) throws CommandExcept } else { for (World w : plugin.getServer().getWorlds()) { RegionManager mgr = plugin.getGlobalRegionManager().get(w); - + try { mgr.load(); } catch (ProtectionDatabaseException e) { @@ -1024,26 +1073,26 @@ public void load(CommandContext args, CommandSender sender) throws CommandExcept + e.getMessage()); } } - + sender.sendMessage(ChatColor.YELLOW + "Region databases loaded."); } } - + @Command(aliases = {"save", "write"}, usage = "[world]", desc = "Re-save regions to file", max = 1) @CommandPermissions({"worldguard.region.save"}) public void save(CommandContext args, CommandSender sender) throws CommandException { - + World world = null; - + if (args.argsLength() > 0) { world = plugin.matchWorld(sender, args.getString(0)); } if (world != null) { RegionManager mgr = plugin.getGlobalRegionManager().get(world); - + try { mgr.save(); sender.sendMessage(ChatColor.YELLOW @@ -1055,7 +1104,7 @@ public void save(CommandContext args, CommandSender sender) throws CommandExcept } else { for (World w : plugin.getServer().getWorlds()) { RegionManager mgr = plugin.getGlobalRegionManager().get(w); - + try { mgr.save(); } catch (ProtectionDatabaseException e) { @@ -1063,7 +1112,7 @@ public void save(CommandContext args, CommandSender sender) throws CommandExcept + e.getMessage()); } } - + sender.sendMessage(ChatColor.YELLOW + "Region databases saved."); } diff --git a/src/main/java/com/sk89q/worldguard/bukkit/commands/RegionMemberCommands.java b/src/main/java/com/sk89q/worldguard/bukkit/commands/RegionMemberCommands.java index 17f5d834..ceb955b4 100644 --- a/src/main/java/com/sk89q/worldguard/bukkit/commands/RegionMemberCommands.java +++ b/src/main/java/com/sk89q/worldguard/bukkit/commands/RegionMemberCommands.java @@ -30,11 +30,11 @@ import com.sk89q.worldguard.LocalPlayer; import com.sk89q.worldguard.bukkit.WorldGuardPlugin; 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.managers.RegionManager; 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. @@ -44,15 +44,24 @@ public class RegionMemberCommands { public RegionMemberCommands(WorldGuardPlugin plugin) { this.plugin = plugin; } - - @Command(aliases = {"addmember", "addmember"}, usage = " ", - desc = "Add a member to a region", min = 2) + + @Command(aliases = {"addmember", "addmember"}, + usage = " ", + flags = "w:", + desc = "Add a member to a region", + min = 2) public void addMember(CommandContext args, CommandSender sender) throws CommandException { - - Player player = plugin.checkPlayer(sender); - World world = player.getWorld(); - LocalPlayer localPlayer = plugin.wrapPlayer(player); - + final World world; + Player 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); RegionManager mgr = plugin.getGlobalRegionManager().get(world); @@ -64,19 +73,21 @@ public void addMember(CommandContext args, CommandSender sender) throws CommandE id = region.getId(); - if (region.isOwner(localPlayer)) { - plugin.checkPermission(sender, "worldguard.region.addmember.own." + id.toLowerCase()); - } else if (region.isMember(localPlayer)) { - plugin.checkPermission(sender, "worldguard.region.addmember.member." + id.toLowerCase()); - } else { - plugin.checkPermission(sender, "worldguard.region.addmember." + id.toLowerCase()); + if (localPlayer != null) { + if (region.isOwner(localPlayer)) { + plugin.checkPermission(sender, "worldguard.region.addmember.own." + id.toLowerCase()); + } else if (region.isMember(localPlayer)) { + plugin.checkPermission(sender, "worldguard.region.addmember.member." + id.toLowerCase()); + } else { + plugin.checkPermission(sender, "worldguard.region.addmember." + id.toLowerCase()); + } } RegionDBUtil.addToDomain(region.getMembers(), args.getPaddedSlice(2, 0), 0); sender.sendMessage(ChatColor.YELLOW + "Region '" + id + "' updated."); - + try { mgr.save(); } catch (ProtectionDatabaseException e) { @@ -84,15 +95,24 @@ public void addMember(CommandContext args, CommandSender sender) throws CommandE + e.getMessage()); } } - - @Command(aliases = {"addowner", "addowner"}, usage = " ", - desc = "Add an owner to a region", min = 2) + + @Command(aliases = {"addowner", "addowner"}, + usage = " ", + flags = "w:", + desc = "Add an owner to a region", + min = 2) public void addOwner(CommandContext args, CommandSender sender) throws CommandException { - - Player player = plugin.checkPlayer(sender); - World world = player.getWorld(); - LocalPlayer localPlayer = plugin.wrapPlayer(player); - + final World world; + Player player = null; + 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); 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); DefaultDomain owners = region.getOwners(); - if (flag != null && flag && owners != null && owners.size() == 0) { - if (!plugin.hasPermission(player, "worldguard.region.unlimited")) { - int maxRegionCount = plugin.getGlobalStateManager().get(world).getMaxRegionCount(player); - if (maxRegionCount >= 0 && mgr.getRegionCountOfPlayer(localPlayer) - >= maxRegionCount) { - throw new CommandException("You already own the maximum allowed amount of regions."); + if (localPlayer != null) { + if (flag != null && flag && owners != null && owners.size() == 0) { + if (!plugin.hasPermission(player, "worldguard.region.unlimited")) { + int maxRegionCount = plugin.getGlobalStateManager().get(world).getMaxRegionCount(player); + if (maxRegionCount >= 0 && mgr.getRegionCountOfPlayer(localPlayer) + >= maxRegionCount) { + throw new CommandException("You already own the maximum allowed amount of regions."); + } } - } - 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()); + plugin.checkPermission(sender, "worldguard.region.addowner.unclaimed." + id.toLowerCase()); } 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 + "Region '" + id + "' updated."); - + try { mgr.save(); } catch (ProtectionDatabaseException e) { @@ -137,15 +159,24 @@ public void addOwner(CommandContext args, CommandSender sender) throws CommandEx + e.getMessage()); } } - - @Command(aliases = {"removemember", "remmember", "removemem", "remmem"}, usage = " ", - desc = "Remove an owner to a region", min = 2) + + @Command(aliases = {"removemember", "remmember", "removemem", "remmem"}, + usage = " ", + flags = "w:", + desc = "Remove an owner to a region", + min = 2) public void removeMember(CommandContext args, CommandSender sender) throws CommandException { - - Player player = plugin.checkPlayer(sender); - World world = player.getWorld(); - LocalPlayer localPlayer = plugin.wrapPlayer(player); - + final World world; + Player 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); RegionManager mgr = plugin.getGlobalRegionManager().get(world); @@ -157,19 +188,21 @@ public void removeMember(CommandContext args, CommandSender sender) throws Comma id = region.getId(); - if (region.isOwner(localPlayer)) { - plugin.checkPermission(sender, "worldguard.region.removemember.own." + id.toLowerCase()); - } else if (region.isMember(localPlayer)) { - plugin.checkPermission(sender, "worldguard.region.removemember.member." + id.toLowerCase()); - } else { - plugin.checkPermission(sender, "worldguard.region.removemember." + id.toLowerCase()); + if (localPlayer != null) { + if (region.isOwner(localPlayer)) { + plugin.checkPermission(sender, "worldguard.region.removemember.own." + id.toLowerCase()); + } else if (region.isMember(localPlayer)) { + plugin.checkPermission(sender, "worldguard.region.removemember.member." + id.toLowerCase()); + } else { + plugin.checkPermission(sender, "worldguard.region.removemember." + id.toLowerCase()); + } } RegionDBUtil.removeFromDomain(region.getMembers(), args.getPaddedSlice(2, 0), 0); sender.sendMessage(ChatColor.YELLOW + "Region '" + id + "' updated."); - + try { mgr.save(); } catch (ProtectionDatabaseException e) { @@ -177,16 +210,25 @@ public void removeMember(CommandContext args, CommandSender sender) throws Comma + e.getMessage()); } } - - @Command(aliases = {"removeowner", "remowner"}, usage = " ", - desc = "Remove an owner to a region", min = 2) + + @Command(aliases = {"removeowner", "remowner"}, + usage = " ", + flags = "w:", + desc = "Remove an owner to a region", + min = 2) public void removeOwner(CommandContext args, CommandSender sender) throws CommandException { - - Player player = plugin.checkPlayer(sender); - World world = player.getWorld(); - LocalPlayer localPlayer = plugin.wrapPlayer(player); - + final World world; + Player 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); RegionManager mgr = plugin.getGlobalRegionManager().get(world); @@ -198,19 +240,21 @@ public void removeOwner(CommandContext args, id = region.getId(); - if (region.isOwner(localPlayer)) { - plugin.checkPermission(sender, "worldguard.region.removeowner.own." + id.toLowerCase()); - } else if (region.isMember(localPlayer)) { - plugin.checkPermission(sender, "worldguard.region.removeowner.member." + id.toLowerCase()); - } else { - plugin.checkPermission(sender, "worldguard.region.removeowner." + id.toLowerCase()); + if (localPlayer != null) { + if (region.isOwner(localPlayer)) { + plugin.checkPermission(sender, "worldguard.region.removeowner.own." + id.toLowerCase()); + } else if (region.isMember(localPlayer)) { + plugin.checkPermission(sender, "worldguard.region.removeowner.member." + id.toLowerCase()); + } else { + plugin.checkPermission(sender, "worldguard.region.removeowner." + id.toLowerCase()); + } } RegionDBUtil.removeFromDomain(region.getOwners(), args.getPaddedSlice(2, 0), 0); sender.sendMessage(ChatColor.YELLOW + "Region '" + id + "' updated."); - + try { mgr.save(); } catch (ProtectionDatabaseException e) {