PageRenderTime 90ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/sk89q/worldguard/bukkit/commands/WorldGuardCommands.java

https://github.com/talmor/worldguard
Java | 147 lines | 107 code | 20 blank | 20 comment | 9 complexity | b3a2101970667af2ebb6dd93b2d4c46d MD5 | raw file
  1. // $Id$
  2. /*
  3. * WorldGuard
  4. * Copyright (C) 2010 sk89q <http://www.sk89q.com>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the 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,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.sk89q.worldguard.bukkit.commands;
  20. import java.io.File;
  21. import java.io.IOException;
  22. import java.util.logging.Level;
  23. import java.util.logging.Logger;
  24. import org.bukkit.ChatColor;
  25. import org.bukkit.command.CommandSender;
  26. import org.bukkit.entity.Player;
  27. import com.sk89q.minecraft.util.commands.Command;
  28. import com.sk89q.minecraft.util.commands.CommandContext;
  29. import com.sk89q.minecraft.util.commands.CommandException;
  30. import com.sk89q.minecraft.util.commands.CommandPermissions;
  31. import com.sk89q.worldguard.bukkit.LoggerToChatHandler;
  32. import com.sk89q.worldguard.bukkit.ReportWriter;
  33. import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
  34. import com.sk89q.worldguard.util.PastebinPoster;
  35. import com.sk89q.worldguard.util.PastebinPoster.PasteCallback;
  36. public class WorldGuardCommands {
  37. @Command(aliases = {"version"},
  38. usage = "",
  39. desc = "Get the WorldGuard version",
  40. flags = "", min = 0, max = 0)
  41. public static void version(CommandContext args, WorldGuardPlugin plugin,
  42. CommandSender sender) throws CommandException {
  43. sender.sendMessage(ChatColor.YELLOW
  44. + "WorldGuard " + plugin.getDescription().getVersion());
  45. sender.sendMessage(ChatColor.YELLOW
  46. + "http://www.sk89q.com");
  47. }
  48. @Command(aliases = {"reload"},
  49. usage = "",
  50. desc = "Reload WorldGuard configuration",
  51. flags = "", min = 0, max = 0)
  52. @CommandPermissions({"worldguard.reload"})
  53. public static void relload(CommandContext args, WorldGuardPlugin plugin,
  54. CommandSender sender) throws CommandException {
  55. LoggerToChatHandler handler = null;
  56. Logger minecraftLogger = null;
  57. if (sender instanceof Player) {
  58. handler = new LoggerToChatHandler(sender);
  59. handler.setLevel(Level.ALL);
  60. minecraftLogger = Logger.getLogger("Minecraft");
  61. minecraftLogger.addHandler(handler);
  62. }
  63. try {
  64. plugin.getGlobalStateManager().unload();
  65. plugin.getGlobalRegionManager().unload();
  66. plugin.getGlobalStateManager().load();
  67. plugin.getGlobalRegionManager().preload();
  68. sender.sendMessage("WorldGuard configuration reloaded.");
  69. } catch (Throwable t) {
  70. sender.sendMessage("Error while reloading: "
  71. + t.getMessage());
  72. } finally {
  73. if (minecraftLogger != null) {
  74. minecraftLogger.removeHandler(handler);
  75. }
  76. }
  77. }
  78. @Command(aliases = {"report"},
  79. usage = "",
  80. desc = "Writes a report on WorldGuard",
  81. flags = "p", min = 0, max = 0)
  82. @CommandPermissions({"worldguard.report"})
  83. public static void report(CommandContext args, WorldGuardPlugin plugin,
  84. final CommandSender sender) throws CommandException {
  85. File dest = new File(plugin.getDataFolder(), "report.txt");
  86. ReportWriter report = new ReportWriter(plugin);
  87. try {
  88. report.write(dest);
  89. sender.sendMessage(ChatColor.YELLOW + "WorldGuard report written to "
  90. + dest.getAbsolutePath());
  91. } catch (IOException e) {
  92. throw new CommandException("Failed to write report: " + e.getMessage());
  93. }
  94. if (args.hasFlag('p')) {
  95. plugin.checkPermission(sender, "worldguard.report.pastebin");
  96. sender.sendMessage(ChatColor.YELLOW + "Now uploading to Pastebin...");
  97. PastebinPoster.paste(report.toString(), new PasteCallback() {
  98. public void handleSuccess(String url) {
  99. // Hope we don't have a thread safety issue here
  100. sender.sendMessage(ChatColor.YELLOW + "WorldGuard report (1 hour): " + url);
  101. }
  102. public void handleError(String err) {
  103. // Hope we don't have a thread safety issue here
  104. sender.sendMessage(ChatColor.YELLOW + "WorldGuard report pastebin error: " + err);
  105. }
  106. });
  107. }
  108. }
  109. @Command(aliases = {"flushstates", "clearstates"},
  110. usage = "[player]",
  111. desc = "Flush the state manager",
  112. flags = "", min = 0, max = 1)
  113. @CommandPermissions("worldguard.flushstates")
  114. public static void flushStates(CommandContext args, WorldGuardPlugin plugin,
  115. CommandSender sender) throws CommandException {
  116. if (args.argsLength() == 0) {
  117. plugin.getFlagStateManager().forgetAll();
  118. sender.sendMessage("Cleared all states.");
  119. } else {
  120. Player player = plugin.getServer().getPlayer(args.getString(0));
  121. if (player != null) {
  122. plugin.getFlagStateManager().forget(player);
  123. sender.sendMessage("Cleared states for player \"" + player.getName() + "\".");
  124. }
  125. }
  126. }
  127. }