PageRenderTime 33ms CodeModel.GetById 27ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/Erati/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
 20package com.sk89q.worldguard.bukkit.commands;
 21
 22import java.io.File;
 23import java.io.IOException;
 24import java.util.logging.Level;
 25import java.util.logging.Logger;
 26
 27import org.bukkit.ChatColor;
 28import org.bukkit.command.CommandSender;
 29import org.bukkit.entity.Player;
 30
 31import com.sk89q.minecraft.util.commands.Command;
 32import com.sk89q.minecraft.util.commands.CommandContext;
 33import com.sk89q.minecraft.util.commands.CommandException;
 34import com.sk89q.minecraft.util.commands.CommandPermissions;
 35import com.sk89q.worldguard.bukkit.LoggerToChatHandler;
 36import com.sk89q.worldguard.bukkit.ReportWriter;
 37import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
 38import com.sk89q.worldguard.util.PastebinPoster;
 39import com.sk89q.worldguard.util.PastebinPoster.PasteCallback;
 40
 41public class WorldGuardCommands {
 42
 43    @Command(aliases = {"version"},
 44            usage = "",
 45            desc = "Get the WorldGuard version",
 46            flags = "", min = 0, max = 0)
 47    public static void version(CommandContext args, WorldGuardPlugin plugin,
 48            CommandSender sender) throws CommandException {
 49        sender.sendMessage(ChatColor.YELLOW
 50                + "WorldGuard " + plugin.getDescription().getVersion());
 51        sender.sendMessage(ChatColor.YELLOW
 52                + "http://www.sk89q.com");
 53    }
 54
 55    @Command(aliases = {"reload"},
 56            usage = "",
 57            desc = "Reload WorldGuard configuration",
 58            flags = "", min = 0, max = 0)
 59    @CommandPermissions({"worldguard.reload"})
 60    public static void relload(CommandContext args, WorldGuardPlugin plugin,
 61            CommandSender sender) throws CommandException {
 62        
 63        LoggerToChatHandler handler = null;
 64        Logger minecraftLogger = null;
 65        
 66        if (sender instanceof Player) {
 67            handler = new LoggerToChatHandler(sender);
 68            handler.setLevel(Level.ALL);
 69            minecraftLogger = Logger.getLogger("Minecraft");
 70            minecraftLogger.addHandler(handler);
 71        }
 72
 73        try {
 74            plugin.getGlobalStateManager().unload();
 75            plugin.getGlobalRegionManager().unload();
 76            plugin.getGlobalStateManager().load();
 77            plugin.getGlobalRegionManager().preload();
 78            sender.sendMessage("WorldGuard configuration reloaded.");
 79        } catch (Throwable t) {
 80            sender.sendMessage("Error while reloading: "
 81                    + t.getMessage());
 82        } finally {
 83            if (minecraftLogger != null) {
 84                minecraftLogger.removeHandler(handler);
 85            }
 86        }
 87    }
 88    
 89    @Command(aliases = {"report"},
 90            usage = "",
 91            desc = "Writes a report on WorldGuard",
 92            flags = "p", min = 0, max = 0)
 93    @CommandPermissions({"worldguard.report"})
 94    public static void report(CommandContext args, WorldGuardPlugin plugin,
 95            final CommandSender sender) throws CommandException {
 96        
 97        File dest = new File(plugin.getDataFolder(), "report.txt");
 98        ReportWriter report = new ReportWriter(plugin);
 99        
100        try {
101            report.write(dest);
102            sender.sendMessage(ChatColor.YELLOW + "WorldGuard report written to "
103                    + dest.getAbsolutePath());
104        } catch (IOException e) {
105            throw new CommandException("Failed to write report: " + e.getMessage());
106        }
107        
108        if (args.hasFlag('p')) {
109            plugin.checkPermission(sender, "worldguard.report.pastebin");
110            
111            sender.sendMessage(ChatColor.YELLOW + "Now uploading to Pastebin...");
112            PastebinPoster.paste(report.toString(), new PasteCallback() {
113                
114                public void handleSuccess(String url) {
115                    // Hope we don't have a thread safety issue here
116                    sender.sendMessage(ChatColor.YELLOW + "WorldGuard report (1 hour): " + url);
117                }
118                
119                public void handleError(String err) {
120                    // Hope we don't have a thread safety issue here
121                    sender.sendMessage(ChatColor.YELLOW + "WorldGuard report pastebin error: " + err);
122                }
123            });
124        }
125
126    }
127
128    @Command(aliases = {"flushstates", "clearstates"},
129            usage = "[player]",
130            desc = "Flush the state manager",
131            flags = "", min = 0, max = 1)
132    @CommandPermissions("worldguard.flushstates")
133    public static void flushStates(CommandContext args, WorldGuardPlugin plugin,
134            CommandSender sender) throws CommandException {
135        if (args.argsLength() == 0) {
136            plugin.getFlagStateManager().forgetAll();
137            sender.sendMessage("Cleared all states.");
138        } else {
139            Player player = plugin.getServer().getPlayer(args.getString(0));
140            if (player != null) {
141                plugin.getFlagStateManager().forget(player);
142                sender.sendMessage("Cleared states for player \"" + player.getName() + "\".");
143            }
144        }
145    }
146
147}