PageRenderTime 83ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/worldguard-legacy/src/main/java/com/sk89q/worldguard/bukkit/commands/region/RegionCommands.java

https://gitlab.com/igserfurtmcschulserver/CustomWorldGuard
Java | 1038 lines | 688 code | 159 blank | 191 comment | 123 complexity | 2e26abd7831afa62ed07cb2341edf638 MD5 | raw file
  1. /*
  2. * WorldGuard, a suite of tools for Minecraft
  3. * Copyright (C) sk89q <http://www.sk89q.com>
  4. * Copyright (C) WorldGuard team and contributors
  5. *
  6. * This program is free software: you can redistribute it and/or modify it
  7. * under the terms of the GNU Lesser General Public License as published by the
  8. * Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
  14. * for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.sk89q.worldguard.bukkit.commands.region;
  20. import com.google.common.util.concurrent.Futures;
  21. import com.google.common.util.concurrent.ListenableFuture;
  22. import com.sk89q.minecraft.util.commands.Command;
  23. import com.sk89q.minecraft.util.commands.CommandContext;
  24. import com.sk89q.minecraft.util.commands.CommandException;
  25. import com.sk89q.minecraft.util.commands.CommandPermissionsException;
  26. import com.sk89q.worldedit.Location;
  27. import com.sk89q.worldedit.bukkit.BukkitUtil;
  28. import com.sk89q.worldguard.LocalPlayer;
  29. import com.sk89q.worldguard.bukkit.ConfigurationManager;
  30. import com.sk89q.worldguard.bukkit.RegionContainer;
  31. import com.sk89q.worldguard.bukkit.WorldConfiguration;
  32. import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
  33. import com.sk89q.worldguard.bukkit.commands.AsyncCommandHelper;
  34. import com.sk89q.worldguard.bukkit.commands.CommandUtils;
  35. import com.sk89q.worldguard.bukkit.commands.FutureProgressListener;
  36. import com.sk89q.worldguard.bukkit.commands.MessageFutureCallback.Builder;
  37. import com.sk89q.worldguard.bukkit.commands.task.RegionAdder;
  38. import com.sk89q.worldguard.bukkit.commands.task.RegionLister;
  39. import com.sk89q.worldguard.bukkit.commands.task.RegionManagerReloader;
  40. import com.sk89q.worldguard.bukkit.commands.task.RegionManagerSaver;
  41. import com.sk89q.worldguard.bukkit.commands.task.RegionRemover;
  42. import com.sk89q.worldguard.bukkit.permission.RegionPermissionModel;
  43. import com.sk89q.worldguard.bukkit.util.logging.LoggerToChatHandler;
  44. import com.sk89q.worldguard.protection.ApplicableRegionSet;
  45. import com.sk89q.worldguard.protection.flags.DefaultFlag;
  46. import com.sk89q.worldguard.protection.flags.Flag;
  47. import com.sk89q.worldguard.protection.flags.FlagContext;
  48. import com.sk89q.worldguard.protection.flags.InvalidFlagFormat;
  49. import com.sk89q.worldguard.protection.flags.RegionGroup;
  50. import com.sk89q.worldguard.protection.flags.RegionGroupFlag;
  51. import com.sk89q.worldguard.protection.flags.registry.FlagRegistry;
  52. import com.sk89q.worldguard.protection.managers.RegionManager;
  53. import com.sk89q.worldguard.protection.managers.RemovalStrategy;
  54. import com.sk89q.worldguard.protection.managers.migration.DriverMigration;
  55. import com.sk89q.worldguard.protection.managers.migration.MigrationException;
  56. import com.sk89q.worldguard.protection.managers.migration.UUIDMigration;
  57. import com.sk89q.worldguard.protection.managers.storage.DriverType;
  58. import com.sk89q.worldguard.protection.managers.storage.RegionDriver;
  59. import com.sk89q.worldguard.protection.regions.GlobalProtectedRegion;
  60. import com.sk89q.worldguard.protection.regions.ProtectedPolygonalRegion;
  61. import com.sk89q.worldguard.protection.regions.ProtectedRegion;
  62. import com.sk89q.worldguard.protection.regions.ProtectedRegion.CircularInheritanceException;
  63. import com.sk89q.worldguard.protection.util.DomainInputResolver.UserLocatorPolicy;
  64. import com.sk89q.worldguard.util.Enums;
  65. import org.bukkit.Bukkit;
  66. import org.bukkit.ChatColor;
  67. import org.bukkit.World;
  68. import org.bukkit.command.CommandSender;
  69. import org.bukkit.entity.Player;
  70. import java.util.ArrayList;
  71. import java.util.List;
  72. import java.util.logging.Level;
  73. import java.util.logging.Logger;
  74. import static com.google.common.base.Preconditions.checkNotNull;
  75. /**
  76. * Implements the /region commands for WorldGuard.
  77. */
  78. public final class RegionCommands extends RegionCommandsBase {
  79. private static final Logger log = Logger.getLogger(RegionCommands.class.getCanonicalName());
  80. private final WorldGuardPlugin plugin;
  81. public RegionCommands(WorldGuardPlugin plugin) {
  82. checkNotNull(plugin);
  83. this.plugin = plugin;
  84. }
  85. /**
  86. * Defines a new region.
  87. *
  88. * @param args the arguments
  89. * @param sender the sender
  90. * @throws CommandException any error
  91. */
  92. @Command(aliases = {"define", "def", "d", "create"},
  93. usage = "<id> [<owner1> [<owner2> [<owners...>]]]",
  94. flags = "ng",
  95. desc = "Defines a region",
  96. min = 1)
  97. public void define(CommandContext args, CommandSender sender) throws CommandException {
  98. warnAboutSaveFailures(sender);
  99. Player player = plugin.checkPlayer(sender);
  100. // Check permissions
  101. if (!getPermissionModel(sender).mayDefine()) {
  102. throw new CommandPermissionsException();
  103. }
  104. String id = checkRegionId(args.getString(0), false);
  105. RegionManager manager = checkRegionManager(plugin, player.getWorld());
  106. checkRegionDoesNotExist(manager, id, true);
  107. ProtectedRegion region;
  108. if (args.hasFlag('g')) {
  109. region = new GlobalProtectedRegion(id);
  110. } else {
  111. region = checkRegionFromSelection(player, id);
  112. warnAboutDimensions(player, region);
  113. informNewUser(player, manager, region);
  114. }
  115. RegionAdder task = new RegionAdder(plugin, manager, region);
  116. task.addOwnersFromCommand(args, 2);
  117. ListenableFuture<?> future = plugin.getExecutorService().submit(task);
  118. AsyncCommandHelper.wrap(future, plugin, player)
  119. .formatUsing(id)
  120. .registerWithSupervisor("Adding the region '%s'...")
  121. .sendMessageAfterDelay("(Please wait... adding '%s'...)")
  122. .thenRespondWith(
  123. "A new region has been made named '%s'.",
  124. "Failed to add the region '%s'");
  125. }
  126. /**
  127. * Re-defines a region with a new selection.
  128. *
  129. * @param args the arguments
  130. * @param sender the sender
  131. * @throws CommandException any error
  132. */
  133. @Command(aliases = {"redefine", "update", "move"},
  134. usage = "<id>",
  135. desc = "Re-defines the shape of a region",
  136. flags = "g",
  137. min = 1, max = 1)
  138. public void redefine(CommandContext args, CommandSender sender) throws CommandException {
  139. warnAboutSaveFailures(sender);
  140. Player player = plugin.checkPlayer(sender);
  141. World world = player.getWorld();
  142. String id = checkRegionId(args.getString(0), false);
  143. RegionManager manager = checkRegionManager(plugin, world);
  144. ProtectedRegion existing = checkExistingRegion(manager, id, false);
  145. // Check permissions
  146. if (!getPermissionModel(sender).mayRedefine(existing)) {
  147. throw new CommandPermissionsException();
  148. }
  149. ProtectedRegion region;
  150. if (args.hasFlag('g')) {
  151. region = new GlobalProtectedRegion(id);
  152. } else {
  153. region = checkRegionFromSelection(player, id);
  154. warnAboutDimensions(player, region);
  155. informNewUser(player, manager, region);
  156. }
  157. region.copyFrom(existing);
  158. RegionAdder task = new RegionAdder(plugin, manager, region);
  159. ListenableFuture<?> future = plugin.getExecutorService().submit(task);
  160. AsyncCommandHelper.wrap(future, plugin, player)
  161. .formatUsing(id)
  162. .registerWithSupervisor("Updating the region '%s'...")
  163. .sendMessageAfterDelay("(Please wait... updating '%s'...)")
  164. .thenRespondWith(
  165. "Region '%s' has been updated with a new area.",
  166. "Failed to update the region '%s'");
  167. }
  168. /**
  169. * Claiming command for users.
  170. *
  171. * <p>This command is a joke and it needs to be rewritten. It was contributed
  172. * code :(</p>
  173. *
  174. * @param args the arguments
  175. * @param sender the sender
  176. * @throws CommandException any error
  177. */
  178. @Command(aliases = {"claim"},
  179. usage = "<id>",
  180. desc = "Claim a region",
  181. min = 1, max = 1)
  182. public void claim(CommandContext args, CommandSender sender) throws CommandException {
  183. warnAboutSaveFailures(sender);
  184. Player player = plugin.checkPlayer(sender);
  185. LocalPlayer localPlayer = plugin.wrapPlayer(player);
  186. RegionPermissionModel permModel = getPermissionModel(sender);
  187. // Check permissions
  188. if (!permModel.mayClaim()) {
  189. throw new CommandPermissionsException();
  190. }
  191. String id = checkRegionId(args.getString(0), false);
  192. RegionManager manager = checkRegionManager(plugin, player.getWorld());
  193. checkRegionDoesNotExist(manager, id, false);
  194. ProtectedRegion region = checkRegionFromSelection(player, id);
  195. WorldConfiguration wcfg = plugin.getGlobalStateManager().get(player.getWorld());
  196. // Check whether the player has created too many regions
  197. if (!permModel.mayClaimRegionsUnbounded()) {
  198. int maxRegionCount = wcfg.getMaxRegionCount(player);
  199. if (maxRegionCount >= 0
  200. && manager.getRegionCountOfPlayer(localPlayer) >= maxRegionCount) {
  201. throw new CommandException(
  202. "You own too many regions, delete one first to claim a new one.");
  203. }
  204. }
  205. ProtectedRegion existing = manager.getRegion(id);
  206. // Check for an existing region
  207. if (existing != null) {
  208. if (!existing.getOwners().contains(localPlayer)) {
  209. throw new CommandException(
  210. "This region already exists and you don't own it.");
  211. }
  212. }
  213. // We have to check whether this region violates the space of any other reion
  214. ApplicableRegionSet regions = manager.getApplicableRegions(region);
  215. // Check if this region overlaps any other region
  216. if (regions.size() > 0) {
  217. if (!regions.isOwnerOfAll(localPlayer)) {
  218. throw new CommandException("This region overlaps with someone else's region.");
  219. }
  220. } else {
  221. if (wcfg.claimOnlyInsideExistingRegions) {
  222. throw new CommandException("You may only claim regions inside " +
  223. "existing regions that you or your group own.");
  224. }
  225. }
  226. if (wcfg.maxClaimVolume >= Integer.MAX_VALUE) {
  227. throw new CommandException("The maximum claim volume get in the configuration is higher than is supported. " +
  228. "Currently, it must be " + Integer.MAX_VALUE+ " or smaller. Please contact a server administrator.");
  229. }
  230. // Check claim volume
  231. if (!permModel.mayClaimRegionsUnbounded()) {
  232. if (region instanceof ProtectedPolygonalRegion) {
  233. throw new CommandException("Polygons are currently not supported for /rg claim.");
  234. }
  235. if (region.volume() > wcfg.maxClaimVolume) {
  236. player.sendMessage(ChatColor.RED + "This region is too large to claim.");
  237. player.sendMessage(ChatColor.RED +
  238. "Max. volume: " + wcfg.maxClaimVolume + ", your volume: " + region.volume());
  239. return;
  240. }
  241. }
  242. RegionAdder task = new RegionAdder(plugin, manager, region);
  243. task.setLocatorPolicy(UserLocatorPolicy.UUID_ONLY);
  244. task.setOwnersInput(new String[]{player.getName()});
  245. ListenableFuture<?> future = plugin.getExecutorService().submit(task);
  246. AsyncCommandHelper.wrap(future, plugin, player)
  247. .formatUsing(id)
  248. .registerWithSupervisor("Claiming the region '%s'...")
  249. .sendMessageAfterDelay("(Please wait... claiming '%s'...)")
  250. .thenRespondWith(
  251. "A new region has been claimed named '%s'.",
  252. "Failed to claim the region '%s'");
  253. }
  254. /**
  255. * Get a WorldEdit selection from a region.
  256. *
  257. * @param args the arguments
  258. * @param sender the sender
  259. * @throws CommandException any error
  260. */
  261. @Command(aliases = {"select", "sel", "s"},
  262. usage = "[id]",
  263. desc = "Load a region as a WorldEdit selection",
  264. min = 0, max = 1)
  265. public void select(CommandContext args, CommandSender sender) throws CommandException {
  266. Player player = plugin.checkPlayer(sender);
  267. World world = player.getWorld();
  268. RegionManager manager = checkRegionManager(plugin, world);
  269. ProtectedRegion existing;
  270. // If no arguments were given, get the region that the player is inside
  271. if (args.argsLength() == 0) {
  272. existing = checkRegionStandingIn(manager, player);
  273. } else {
  274. existing = checkExistingRegion(manager, args.getString(0), false);
  275. }
  276. // Check permissions
  277. if (!getPermissionModel(sender).maySelect(existing)) {
  278. throw new CommandPermissionsException();
  279. }
  280. // Select
  281. setPlayerSelection(player, existing);
  282. }
  283. /**
  284. * Get information about a region.
  285. *
  286. * @param args the arguments
  287. * @param sender the sender
  288. * @throws CommandException any error
  289. */
  290. @Command(aliases = {"info", "i"},
  291. usage = "[id]",
  292. flags = "usw:",
  293. desc = "Get information about a region",
  294. min = 0, max = 1)
  295. public void info(CommandContext args, CommandSender sender) throws CommandException {
  296. warnAboutSaveFailures(sender);
  297. World world = checkWorld(args, sender, 'w'); // Get the world
  298. RegionPermissionModel permModel = getPermissionModel(sender);
  299. // Lookup the existing region
  300. RegionManager manager = checkRegionManager(plugin, world);
  301. ProtectedRegion existing;
  302. if (args.argsLength() == 0) { // Get region from where the player is
  303. if (!(sender instanceof Player)) {
  304. throw new CommandException("Please specify " +
  305. "the region with /region info -w world_name region_name.");
  306. }
  307. existing = checkRegionStandingIn(manager, (Player) sender, true);
  308. } else { // Get region from the ID
  309. existing = checkExistingRegion(manager, args.getString(0), true);
  310. }
  311. // Check permissions
  312. if (!permModel.mayLookup(existing)) {
  313. throw new CommandPermissionsException();
  314. }
  315. // Let the player select the region
  316. if (args.hasFlag('s')) {
  317. // Check permissions
  318. if (!permModel.maySelect(existing)) {
  319. throw new CommandPermissionsException();
  320. }
  321. setPlayerSelection(plugin.checkPlayer(sender), existing);
  322. }
  323. // Print region information
  324. RegionPrintoutBuilder printout = new RegionPrintoutBuilder(existing, args.hasFlag('u') ? null : plugin.getProfileCache());
  325. ListenableFuture<?> future = Futures.transform(
  326. plugin.getExecutorService().submit(printout),
  327. CommandUtils.messageFunction(sender));
  328. // If it takes too long...
  329. FutureProgressListener.addProgressListener(
  330. future, sender, "(Please wait... fetching region information...)");
  331. // Send a response message
  332. Futures.addCallback(future,
  333. new Builder(plugin, sender)
  334. .onFailure("Failed to fetch region information")
  335. .build());
  336. }
  337. /**
  338. * List regions.
  339. *
  340. * @param args the arguments
  341. * @param sender the sender
  342. * @throws CommandException any error
  343. */
  344. @Command(aliases = {"list"},
  345. usage = "[page]",
  346. desc = "Get a list of regions",
  347. flags = "np:w:",
  348. max = 1)
  349. public void list(CommandContext args, CommandSender sender) throws CommandException {
  350. warnAboutSaveFailures(sender);
  351. World world = checkWorld(args, sender, 'w'); // Get the world
  352. String ownedBy;
  353. // Get page
  354. int page = args.getInteger(0, 1) - 1;
  355. if (page < 0) {
  356. page = 0;
  357. }
  358. // -p flag to lookup a player's regions
  359. if (args.hasFlag('p')) {
  360. ownedBy = args.getFlag('p');
  361. } else {
  362. ownedBy = null; // List all regions
  363. }
  364. // Check permissions
  365. if (!getPermissionModel(sender).mayList(ownedBy)) {
  366. ownedBy = sender.getName(); // assume they only want their own
  367. if (!getPermissionModel(sender).mayList(ownedBy)) {
  368. throw new CommandPermissionsException();
  369. }
  370. }
  371. RegionManager manager = checkRegionManager(plugin, world);
  372. RegionLister task = new RegionLister(plugin, manager, sender);
  373. task.setPage(page);
  374. if (ownedBy != null) {
  375. task.filterOwnedByName(ownedBy, args.hasFlag('n'));
  376. }
  377. ListenableFuture<?> future = plugin.getExecutorService().submit(task);
  378. AsyncCommandHelper.wrap(future, plugin, sender)
  379. .registerWithSupervisor("Getting list of regions...")
  380. .sendMessageAfterDelay("(Please wait... fetching region list...)")
  381. .thenTellErrorsOnly("Failed to fetch region list");
  382. }
  383. /**
  384. * Set a flag.
  385. *
  386. * @param args the arguments
  387. * @param sender the sender
  388. * @throws CommandException any error
  389. */
  390. @Command(aliases = {"flag", "f"},
  391. usage = "<id> <flag> [-w world] [-g group] [value]",
  392. flags = "g:w:e",
  393. desc = "Set flags",
  394. min = 2)
  395. public void flag(CommandContext args, CommandSender sender) throws CommandException {
  396. warnAboutSaveFailures(sender);
  397. World world = checkWorld(args, sender, 'w'); // Get the world
  398. String flagName = args.getString(1);
  399. String value = args.argsLength() >= 3 ? args.getJoinedStrings(2) : null;
  400. RegionGroup groupValue = null;
  401. FlagRegistry flagRegistry = plugin.getFlagRegistry();
  402. RegionPermissionModel permModel = getPermissionModel(sender);
  403. if (args.hasFlag('e')) {
  404. if (value != null) {
  405. throw new CommandException("You cannot use -e(mpty) with a flag value.");
  406. }
  407. value = "";
  408. }
  409. // Add color codes
  410. if (value != null) {
  411. value = CommandUtils.replaceColorMacros(value);
  412. }
  413. // Lookup the existing region
  414. RegionManager manager = checkRegionManager(plugin, world);
  415. ProtectedRegion existing = checkExistingRegion(manager, args.getString(0), true);
  416. // Check permissions
  417. if (!permModel.maySetFlag(existing)) {
  418. throw new CommandPermissionsException();
  419. }
  420. Flag<?> foundFlag = DefaultFlag.fuzzyMatchFlag(flagRegistry, flagName);
  421. // We didn't find the flag, so let's print a list of flags that the user
  422. // can use, and do nothing afterwards
  423. if (foundFlag == null) {
  424. StringBuilder list = new StringBuilder();
  425. // Need to build a list
  426. for (Flag<?> flag : flagRegistry) {
  427. // Can the user set this flag?
  428. if (!permModel.maySetFlag(existing, flag)) {
  429. continue;
  430. }
  431. if (list.length() > 0) {
  432. list.append(", ");
  433. }
  434. list.append(flag.getName());
  435. }
  436. sender.sendMessage(ChatColor.RED + "Unknown flag specified: " + flagName);
  437. sender.sendMessage(ChatColor.RED + "Available " + "flags: " + list);
  438. return;
  439. }
  440. // Also make sure that we can use this flag
  441. // This permission is confusing and probably should be replaced, but
  442. // but not here -- in the model
  443. if (!permModel.maySetFlag(existing, foundFlag, value)) {
  444. throw new CommandPermissionsException();
  445. }
  446. // -g for group flag
  447. if (args.hasFlag('g')) {
  448. String group = args.getFlag('g');
  449. RegionGroupFlag groupFlag = foundFlag.getRegionGroupFlag();
  450. if (groupFlag == null) {
  451. throw new CommandException("Region flag '" + foundFlag.getName()
  452. + "' does not have a group flag!");
  453. }
  454. // Parse the [-g group] separately so entire command can abort if parsing
  455. // the [value] part throws an error.
  456. try {
  457. groupValue = groupFlag.parseInput(FlagContext.create().setSender(sender).setInput(group).setObject("region", existing).build());
  458. } catch (InvalidFlagFormat e) {
  459. throw new CommandException(e.getMessage());
  460. }
  461. }
  462. // Set the flag value if a value was set
  463. if (value != null) {
  464. // Set the flag if [value] was given even if [-g group] was given as well
  465. try {
  466. setFlag(existing, foundFlag, sender, value);
  467. } catch (InvalidFlagFormat e) {
  468. throw new CommandException(e.getMessage());
  469. }
  470. sender.sendMessage(ChatColor.YELLOW
  471. + "Region flag " + foundFlag.getName() + " set on '" +
  472. existing.getId() + "' to '" + ChatColor.stripColor(value) + "'.");
  473. // No value? Clear the flag, if -g isn't specified
  474. } else if (!args.hasFlag('g')) {
  475. // Clear the flag only if neither [value] nor [-g group] was given
  476. existing.setFlag(foundFlag, null);
  477. // Also clear the associated group flag if one exists
  478. RegionGroupFlag groupFlag = foundFlag.getRegionGroupFlag();
  479. if (groupFlag != null) {
  480. existing.setFlag(groupFlag, null);
  481. }
  482. sender.sendMessage(ChatColor.YELLOW
  483. + "Region flag " + foundFlag.getName() + " removed from '" +
  484. existing.getId() + "'. (Any -g(roups) were also removed.)");
  485. }
  486. // Now set the group
  487. if (groupValue != null) {
  488. RegionGroupFlag groupFlag = foundFlag.getRegionGroupFlag();
  489. // If group set to the default, then clear the group flag
  490. if (groupValue == groupFlag.getDefault()) {
  491. existing.setFlag(groupFlag, null);
  492. sender.sendMessage(ChatColor.YELLOW
  493. + "Region group flag for '" + foundFlag.getName() + "' reset to " +
  494. "default.");
  495. } else {
  496. existing.setFlag(groupFlag, groupValue);
  497. sender.sendMessage(ChatColor.YELLOW
  498. + "Region group flag for '" + foundFlag.getName() + "' set.");
  499. }
  500. }
  501. // Print region information
  502. RegionPrintoutBuilder printout = new RegionPrintoutBuilder(existing, null);
  503. printout.append(ChatColor.GRAY);
  504. printout.append("(Current flags: ");
  505. printout.appendFlagsList(false);
  506. printout.append(")");
  507. printout.send(sender);
  508. }
  509. /**
  510. * Set the priority of a region.
  511. *
  512. * @param args the arguments
  513. * @param sender the sender
  514. * @throws CommandException any error
  515. */
  516. @Command(aliases = {"setpriority", "priority", "pri"},
  517. usage = "<id> <priority>",
  518. flags = "w:",
  519. desc = "Set the priority of a region",
  520. min = 2, max = 2)
  521. public void setPriority(CommandContext args, CommandSender sender) throws CommandException {
  522. warnAboutSaveFailures(sender);
  523. World world = checkWorld(args, sender, 'w'); // Get the world
  524. int priority = args.getInteger(1);
  525. // Lookup the existing region
  526. RegionManager manager = checkRegionManager(plugin, world);
  527. ProtectedRegion existing = checkExistingRegion(manager, args.getString(0), false);
  528. // Check permissions
  529. if (!getPermissionModel(sender).maySetPriority(existing)) {
  530. throw new CommandPermissionsException();
  531. }
  532. existing.setPriority(priority);
  533. sender.sendMessage(ChatColor.YELLOW
  534. + "Priority of '" + existing.getId() + "' set to "
  535. + priority + " (higher numbers override).");
  536. }
  537. /**
  538. * Set the parent of a region.
  539. *
  540. * @param args the arguments
  541. * @param sender the sender
  542. * @throws CommandException any error
  543. */
  544. @Command(aliases = {"setparent", "parent", "par"},
  545. usage = "<id> [parent-id]",
  546. flags = "w:",
  547. desc = "Set the parent of a region",
  548. min = 1, max = 2)
  549. public void setParent(CommandContext args, CommandSender sender) throws CommandException {
  550. warnAboutSaveFailures(sender);
  551. World world = checkWorld(args, sender, 'w'); // Get the world
  552. ProtectedRegion parent;
  553. ProtectedRegion child;
  554. // Lookup the existing region
  555. RegionManager manager = checkRegionManager(plugin, world);
  556. // Get parent and child
  557. child = checkExistingRegion(manager, args.getString(0), false);
  558. if (args.argsLength() == 2) {
  559. parent = checkExistingRegion(manager, args.getString(1), false);
  560. } else {
  561. parent = null;
  562. }
  563. // Check permissions
  564. if (!getPermissionModel(sender).maySetParent(child, parent)) {
  565. throw new CommandPermissionsException();
  566. }
  567. try {
  568. child.setParent(parent);
  569. } catch (CircularInheritanceException e) {
  570. // Tell the user what's wrong
  571. RegionPrintoutBuilder printout = new RegionPrintoutBuilder(parent, null);
  572. printout.append(ChatColor.RED);
  573. assert parent != null;
  574. printout.append("Uh oh! Setting '" + parent.getId() + "' to be the parent " +
  575. "of '" + child.getId() + "' would cause circular inheritance.\n");
  576. printout.append(ChatColor.GRAY);
  577. printout.append("(Current inheritance on '" + parent.getId() + "':\n");
  578. printout.appendParentTree(true);
  579. printout.append(ChatColor.GRAY);
  580. printout.append(")");
  581. printout.send(sender);
  582. return;
  583. }
  584. // Tell the user the current inheritance
  585. RegionPrintoutBuilder printout = new RegionPrintoutBuilder(child, null);
  586. printout.append(ChatColor.YELLOW);
  587. printout.append("Inheritance set for region '" + child.getId() + "'.\n");
  588. if (parent != null) {
  589. printout.append(ChatColor.GRAY);
  590. printout.append("(Current inheritance:\n");
  591. printout.appendParentTree(true);
  592. printout.append(ChatColor.GRAY);
  593. printout.append(")");
  594. }
  595. printout.send(sender);
  596. }
  597. /**
  598. * Remove a region.
  599. *
  600. * @param args the arguments
  601. * @param sender the sender
  602. * @throws CommandException any error
  603. */
  604. @Command(aliases = {"remove", "delete", "del", "rem"},
  605. usage = "<id>",
  606. flags = "fuw:",
  607. desc = "Remove a region",
  608. min = 1, max = 1)
  609. public void remove(CommandContext args, CommandSender sender) throws CommandException {
  610. warnAboutSaveFailures(sender);
  611. World world = checkWorld(args, sender, 'w'); // Get the world
  612. boolean removeChildren = args.hasFlag('f');
  613. boolean unsetParent = args.hasFlag('u');
  614. // Lookup the existing region
  615. RegionManager manager = checkRegionManager(plugin, world);
  616. ProtectedRegion existing = checkExistingRegion(manager, args.getString(0), true);
  617. // Check permissions
  618. if (!getPermissionModel(sender).mayDelete(existing)) {
  619. throw new CommandPermissionsException();
  620. }
  621. RegionRemover task = new RegionRemover(manager, existing);
  622. if (removeChildren && unsetParent) {
  623. throw new CommandException("You cannot use both -u (unset parent) and -f (remove children) together.");
  624. } else if (removeChildren) {
  625. task.setRemovalStrategy(RemovalStrategy.REMOVE_CHILDREN);
  626. } else if (unsetParent) {
  627. task.setRemovalStrategy(RemovalStrategy.UNSET_PARENT_IN_CHILDREN);
  628. }
  629. AsyncCommandHelper.wrap(plugin.getExecutorService().submit(task), plugin, sender)
  630. .formatUsing(existing.getId())
  631. .registerWithSupervisor("Removing the region '%s'...")
  632. .sendMessageAfterDelay("(Please wait... removing '%s'...)")
  633. .thenRespondWith(
  634. "The region named '%s' has been removed.",
  635. "Failed to remove the region '%s'");
  636. }
  637. /**
  638. * Reload the region database.
  639. *
  640. * @param args the arguments
  641. * @param sender the sender
  642. * @throws CommandException any error
  643. */
  644. @Command(aliases = {"load", "reload"},
  645. usage = "[world]",
  646. desc = "Reload regions from file",
  647. flags = "w:")
  648. public void load(CommandContext args, final CommandSender sender) throws CommandException {
  649. warnAboutSaveFailures(sender);
  650. World world = null;
  651. try {
  652. world = checkWorld(args, sender, 'w'); // Get the world
  653. } catch (CommandException e) {
  654. // assume the user wants to reload all worlds
  655. }
  656. // Check permissions
  657. if (!getPermissionModel(sender).mayForceLoadRegions()) {
  658. throw new CommandPermissionsException();
  659. }
  660. if (world != null) {
  661. RegionManager manager = checkRegionManager(plugin, world);
  662. if (manager == null) {
  663. throw new CommandException("No region manager exists for world '" + world.getName() + "'.");
  664. }
  665. ListenableFuture<?> future = plugin.getExecutorService().submit(new RegionManagerReloader(manager));
  666. AsyncCommandHelper.wrap(future, plugin, sender)
  667. .forRegionDataLoad(world, false);
  668. } else {
  669. // Load regions for all worlds
  670. List<RegionManager> managers = new ArrayList<RegionManager>();
  671. for (World w : Bukkit.getServer().getWorlds()) {
  672. RegionManager manager = plugin.getRegionContainer().get(w);
  673. if (manager != null) {
  674. managers.add(manager);
  675. }
  676. }
  677. ListenableFuture<?> future = plugin.getExecutorService().submit(new RegionManagerReloader(managers));
  678. AsyncCommandHelper.wrap(future, plugin, sender)
  679. .registerWithSupervisor("Loading regions for all worlds")
  680. .sendMessageAfterDelay("(Please wait... loading region data for all worlds...)")
  681. .thenRespondWith(
  682. "Successfully load the region data for all worlds.",
  683. "Failed to load regions for all worlds");
  684. }
  685. }
  686. /**
  687. * Re-save the region database.
  688. *
  689. * @param args the arguments
  690. * @param sender the sender
  691. * @throws CommandException any error
  692. */
  693. @Command(aliases = {"save", "write"},
  694. usage = "[world]",
  695. desc = "Re-save regions to file",
  696. flags = "w:")
  697. public void save(CommandContext args, final CommandSender sender) throws CommandException {
  698. warnAboutSaveFailures(sender);
  699. World world = null;
  700. try {
  701. world = checkWorld(args, sender, 'w'); // Get the world
  702. } catch (CommandException e) {
  703. // assume user wants to save all worlds
  704. }
  705. // Check permissions
  706. if (!getPermissionModel(sender).mayForceSaveRegions()) {
  707. throw new CommandPermissionsException();
  708. }
  709. if (world != null) {
  710. RegionManager manager = checkRegionManager(plugin, world);
  711. if (manager == null) {
  712. throw new CommandException("No region manager exists for world '" + world.getName() + "'.");
  713. }
  714. ListenableFuture<?> future = plugin.getExecutorService().submit(new RegionManagerSaver(manager));
  715. AsyncCommandHelper.wrap(future, plugin, sender)
  716. .forRegionDataSave(world, false);
  717. } else {
  718. // Save for all worlds
  719. List<RegionManager> managers = new ArrayList<RegionManager>();
  720. for (World w : Bukkit.getServer().getWorlds()) {
  721. RegionManager manager = plugin.getRegionContainer().get(w);
  722. if (manager != null) {
  723. managers.add(manager);
  724. }
  725. }
  726. ListenableFuture<?> future = plugin.getExecutorService().submit(new RegionManagerSaver(managers));
  727. AsyncCommandHelper.wrap(future, plugin, sender)
  728. .registerWithSupervisor("Saving regions for all worlds")
  729. .sendMessageAfterDelay("(Please wait... saving region data for all worlds...)")
  730. .thenRespondWith(
  731. "Successfully saved the region data for all worlds.",
  732. "Failed to save regions for all worlds");
  733. }
  734. }
  735. /**
  736. * Migrate the region database.
  737. *
  738. * @param args the arguments
  739. * @param sender the sender
  740. * @throws CommandException any error
  741. */
  742. @Command(aliases = {"migratedb"}, usage = "<from> <to>",
  743. flags = "y",
  744. desc = "Migrate from one Protection Database to another.", min = 2, max = 2)
  745. public void migrateDB(CommandContext args, CommandSender sender) throws CommandException {
  746. // Check permissions
  747. if (!getPermissionModel(sender).mayMigrateRegionStore()) {
  748. throw new CommandPermissionsException();
  749. }
  750. DriverType from = Enums.findFuzzyByValue(DriverType.class, args.getString(0));
  751. DriverType to = Enums.findFuzzyByValue(DriverType.class, args.getString(1));
  752. if (from == null) {
  753. throw new CommandException("The value of 'from' is not a recognized type of region data database.");
  754. }
  755. if (to == null) {
  756. throw new CommandException("The value of 'to' is not a recognized type of region region data database.");
  757. }
  758. if (from.equals(to)) {
  759. throw new CommandException("It is not possible to migrate between the same types of region data databases.");
  760. }
  761. if (!args.hasFlag('y')) {
  762. throw new CommandException("This command is potentially dangerous.\n" +
  763. "Please ensure you have made a backup of your data, and then re-enter the command with -y tacked on at the end to proceed.");
  764. }
  765. ConfigurationManager config = plugin.getGlobalStateManager();
  766. RegionDriver fromDriver = config.regionStoreDriverMap.get(from);
  767. RegionDriver toDriver = config.regionStoreDriverMap.get(to);
  768. if (fromDriver == null) {
  769. throw new CommandException("The driver specified as 'from' does not seem to be supported in your version of WorldGuard.");
  770. }
  771. if (toDriver == null) {
  772. throw new CommandException("The driver specified as 'to' does not seem to be supported in your version of WorldGuard.");
  773. }
  774. DriverMigration migration = new DriverMigration(fromDriver, toDriver, plugin.getFlagRegistry());
  775. LoggerToChatHandler handler = null;
  776. Logger minecraftLogger = null;
  777. if (sender instanceof Player) {
  778. handler = new LoggerToChatHandler(sender);
  779. handler.setLevel(Level.ALL);
  780. minecraftLogger = Logger.getLogger("com.sk89q.worldguard");
  781. minecraftLogger.addHandler(handler);
  782. }
  783. try {
  784. RegionContainer container = plugin.getRegionContainer();
  785. sender.sendMessage(ChatColor.YELLOW + "Now performing migration... this may take a while.");
  786. container.migrate(migration);
  787. sender.sendMessage(ChatColor.YELLOW +
  788. "Migration complete! This only migrated the data. If you already changed your settings to use " +
  789. "the target driver, then WorldGuard is now using the new data. If not, you have to adjust your " +
  790. "configuration to use the new driver and then restart your server.");
  791. } catch (MigrationException e) {
  792. log.log(Level.WARNING, "Failed to migrate", e);
  793. throw new CommandException("Error encountered while migrating: " + e.getMessage());
  794. } finally {
  795. if (minecraftLogger != null) {
  796. minecraftLogger.removeHandler(handler);
  797. }
  798. }
  799. }
  800. /**
  801. * Migrate the region databases to use UUIDs rather than name.
  802. *
  803. * @param args the arguments
  804. * @param sender the sender
  805. * @throws CommandException any error
  806. */
  807. @Command(aliases = {"migrateuuid"},
  808. desc = "Migrate loaded databases to use UUIDs", max = 0)
  809. public void migrateUuid(CommandContext args, CommandSender sender) throws CommandException {
  810. // Check permissions
  811. if (!getPermissionModel(sender).mayMigrateRegionNames()) {
  812. throw new CommandPermissionsException();
  813. }
  814. LoggerToChatHandler handler = null;
  815. Logger minecraftLogger = null;
  816. if (sender instanceof Player) {
  817. handler = new LoggerToChatHandler(sender);
  818. handler.setLevel(Level.ALL);
  819. minecraftLogger = Logger.getLogger("com.sk89q.worldguard");
  820. minecraftLogger.addHandler(handler);
  821. }
  822. try {
  823. ConfigurationManager config = plugin.getGlobalStateManager();
  824. RegionContainer container = plugin.getRegionContainer();
  825. RegionDriver driver = container.getDriver();
  826. UUIDMigration migration = new UUIDMigration(driver, plugin.getProfileService(), plugin.getFlagRegistry());
  827. migration.setKeepUnresolvedNames(config.keepUnresolvedNames);
  828. sender.sendMessage(ChatColor.YELLOW + "Now performing migration... this may take a while.");
  829. container.migrate(migration);
  830. sender.sendMessage(ChatColor.YELLOW + "Migration complete!");
  831. } catch (MigrationException e) {
  832. log.log(Level.WARNING, "Failed to migrate", e);
  833. throw new CommandException("Error encountered while migrating: " + e.getMessage());
  834. } finally {
  835. if (minecraftLogger != null) {
  836. minecraftLogger.removeHandler(handler);
  837. }
  838. }
  839. }
  840. /**
  841. * Teleport to a region
  842. *
  843. * @param args the arguments
  844. * @param sender the sender
  845. * @throws CommandException any error
  846. */
  847. @Command(aliases = {"teleport", "tp"},
  848. usage = "<id>",
  849. flags = "s",
  850. desc = "Teleports you to the location associated with the region.",
  851. min = 1, max = 1)
  852. public void teleport(CommandContext args, CommandSender sender) throws CommandException {
  853. Player player = plugin.checkPlayer(sender);
  854. Location teleportLocation;
  855. // Lookup the existing region
  856. RegionManager regionManager = checkRegionManager(plugin, player.getWorld());
  857. ProtectedRegion existing = checkExistingRegion(regionManager, args.getString(0), false);
  858. // Check permissions
  859. if (!getPermissionModel(sender).mayTeleportTo(existing)) {
  860. throw new CommandPermissionsException();
  861. }
  862. // -s for spawn location
  863. if (args.hasFlag('s')) {
  864. teleportLocation = existing.getFlag(DefaultFlag.SPAWN_LOC);
  865. if (teleportLocation == null) {
  866. throw new CommandException(
  867. "The region has no spawn point associated.");
  868. }
  869. } else {
  870. teleportLocation = existing.getFlag(DefaultFlag.TELE_LOC);
  871. if (teleportLocation == null) {
  872. throw new CommandException(
  873. "The region has no teleport point associated.");
  874. }
  875. }
  876. player.teleport(BukkitUtil.toLocation(teleportLocation));
  877. sender.sendMessage("Teleported you to the region '" + existing.getId() + "'.");
  878. }
  879. }