Add exception handlers to all the commands.

This commit is contained in:
Kristian S. Stangeland 2012-11-04 16:14:07 +01:00
parent 5b279660e0
commit 0108c3390e
4 changed files with 95 additions and 39 deletions

View File

@ -5,6 +5,8 @@ import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import com.comphenix.protocol.error.ErrorReporter;
/** /**
* Base class for all our commands. * Base class for all our commands.
* *
@ -17,12 +19,14 @@ abstract class CommandBase implements CommandExecutor {
private String permission; private String permission;
private String name; private String name;
private int minimumArgumentCount; private int minimumArgumentCount;
private ErrorReporter reporter;
public CommandBase(String permission, String name) {
this(permission, name, 0); public CommandBase(ErrorReporter reporter, String permission, String name) {
this(reporter, permission, name, 0);
} }
public CommandBase(String permission, String name, int minimumArgumentCount) { public CommandBase(ErrorReporter reporter, String permission, String name, int minimumArgumentCount) {
this.reporter = reporter;
this.name = name; this.name = name;
this.permission = permission; this.permission = permission;
this.minimumArgumentCount = minimumArgumentCount; this.minimumArgumentCount = minimumArgumentCount;
@ -30,22 +34,58 @@ abstract class CommandBase implements CommandExecutor {
@Override @Override
public final boolean onCommand(CommandSender sender, Command command, String label, String[] args) { public final boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
// Make sure we're dealing with the correct command try {
if (!command.getName().equalsIgnoreCase(name)) { // Make sure we're dealing with the correct command
return false; if (!command.getName().equalsIgnoreCase(name)) {
} return false;
if (!sender.hasPermission(permission)) { }
sender.sendMessage(ChatColor.RED + "You haven't got permission to run this command."); if (permission != null && !sender.hasPermission(permission)) {
sender.sendMessage(ChatColor.RED + "You haven't got permission to run this command.");
return true;
}
// Check argument length
if (args != null && args.length >= minimumArgumentCount) {
return handleCommand(sender, args);
} else {
return false;
}
} catch (Exception e) {
reporter.reportDetailed(this, "Cannot execute command " + name, e, sender, label, args);
return true; return true;
} }
// Check argument length
if (args != null && args.length >= minimumArgumentCount) {
return handleCommand(sender, args);
} else {
return false;
}
} }
/**
* Retrieve the permission necessary to execute this command.
* @return The permission, or NULL if not needed.
*/
public String getPermission() {
return permission;
}
/**
* Retrieve the primary name of this command.
* @return Primary name.
*/
public String getName() {
return name;
}
/**
* Retrieve the error reporter.
* @return Error reporter.
*/
protected ErrorReporter getReporter() {
return reporter;
}
/**
* Main implementation of this command.
* @param sender - command sender.
* @param args
* @return
*/
protected abstract boolean handleCommand(CommandSender sender, String[] args); protected abstract boolean handleCommand(CommandSender sender, String[] args);
} }

View File

@ -76,11 +76,10 @@ class CommandPacket extends CommandBase {
private AbstractIntervalTree<Integer, DetailedPacketListener> clientListeners = createTree(ConnectionSide.CLIENT_SIDE); private AbstractIntervalTree<Integer, DetailedPacketListener> clientListeners = createTree(ConnectionSide.CLIENT_SIDE);
private AbstractIntervalTree<Integer, DetailedPacketListener> serverListeners = createTree(ConnectionSide.SERVER_SIDE); private AbstractIntervalTree<Integer, DetailedPacketListener> serverListeners = createTree(ConnectionSide.SERVER_SIDE);
public CommandPacket(Plugin plugin, Logger logger, ErrorReporter reporter, ProtocolManager manager) { public CommandPacket(ErrorReporter reporter, Plugin plugin, Logger logger, ProtocolManager manager) {
super(CommandBase.PERMISSION_ADMIN, NAME, 2); super(reporter, CommandBase.PERMISSION_ADMIN, NAME, 2);
this.plugin = plugin; this.plugin = plugin;
this.logger = logger; this.logger = logger;
this.reporter = reporter;
this.manager = manager; this.manager = manager;
this.chatter = new ChatExtensions(manager); this.chatter = new ChatExtensions(manager);
} }

View File

@ -4,6 +4,7 @@ import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import com.comphenix.protocol.error.ErrorReporter;
import com.comphenix.protocol.metrics.Updater; import com.comphenix.protocol.metrics.Updater;
import com.comphenix.protocol.metrics.Updater.UpdateResult; import com.comphenix.protocol.metrics.Updater.UpdateResult;
import com.comphenix.protocol.metrics.Updater.UpdateType; import com.comphenix.protocol.metrics.Updater.UpdateType;
@ -22,9 +23,9 @@ class CommandProtocol extends CommandBase {
private Plugin plugin; private Plugin plugin;
private Updater updater; private Updater updater;
private ProtocolConfig config; private ProtocolConfig config;
public CommandProtocol(Plugin plugin, Updater updater, ProtocolConfig config) { public CommandProtocol(ErrorReporter reporter, Plugin plugin, Updater updater, ProtocolConfig config) {
super(CommandBase.PERMISSION_ADMIN, NAME, 1); super(reporter, CommandBase.PERMISSION_ADMIN, NAME, 1);
this.plugin = plugin; this.plugin = plugin;
this.updater = updater; this.updater = updater;
this.config = config; this.config = config;
@ -51,8 +52,12 @@ class CommandProtocol extends CommandBase {
plugin.getServer().getScheduler().scheduleAsyncDelayedTask(plugin, new Runnable() { plugin.getServer().getScheduler().scheduleAsyncDelayedTask(plugin, new Runnable() {
@Override @Override
public void run() { public void run() {
UpdateResult result = updater.update(UpdateType.NO_DOWNLOAD, true); try {
sender.sendMessage(ChatColor.BLUE + "[ProtocolLib] " + result.toString()); UpdateResult result = updater.update(UpdateType.NO_DOWNLOAD, true);
sender.sendMessage(ChatColor.BLUE + "[ProtocolLib] " + result.toString());
} catch (Exception e) {
getReporter().reportDetailed(this, "Cannot check updates for ProtocolLib.", e, sender);
}
} }
}); });
@ -64,8 +69,12 @@ class CommandProtocol extends CommandBase {
plugin.getServer().getScheduler().scheduleAsyncDelayedTask(plugin, new Runnable() { plugin.getServer().getScheduler().scheduleAsyncDelayedTask(plugin, new Runnable() {
@Override @Override
public void run() { public void run() {
UpdateResult result = updater.update(UpdateType.DEFAULT, true); try {
sender.sendMessage(ChatColor.BLUE + "[ProtocolLib] " + result.toString()); UpdateResult result = updater.update(UpdateType.DEFAULT, true);
sender.sendMessage(ChatColor.BLUE + "[ProtocolLib] " + result.toString());
} catch (Exception e) {
getReporter().reportDetailed(this, "Cannot update ProtocolLib.", e, sender);
}
} }
}); });

View File

@ -75,6 +75,7 @@ public class ProtocolLibrary extends JavaPlugin {
// Updater // Updater
private Updater updater; private Updater updater;
private boolean updateDisabled;
// Logger // Logger
private Logger logger; private Logger logger;
@ -108,8 +109,8 @@ public class ProtocolLibrary extends JavaPlugin {
reporter.addGlobalParameter("manager", protocolManager); reporter.addGlobalParameter("manager", protocolManager);
// Initialize command handlers // Initialize command handlers
commandProtocol = new CommandProtocol(this, updater, config); commandProtocol = new CommandProtocol(reporter, this, updater, config);
commandPacket = new CommandPacket(this, logger, reporter, protocolManager); commandPacket = new CommandPacket(reporter, this, logger, protocolManager);
// Send logging information to player listeners too // Send logging information to player listeners too
broadcastUsers(PERMISSION_INFO); broadcastUsers(PERMISSION_INFO);
@ -223,7 +224,9 @@ public class ProtocolLibrary extends JavaPlugin {
manager.sendProcessedPackets(tickCounter++, true); manager.sendProcessedPackets(tickCounter++, true);
// Check for updates too // Check for updates too
checkUpdates(); if (!updateDisabled) {
checkUpdates();
}
} }
}, ASYNC_PACKET_DELAY, ASYNC_PACKET_DELAY); }, ASYNC_PACKET_DELAY, ASYNC_PACKET_DELAY);
@ -238,15 +241,20 @@ public class ProtocolLibrary extends JavaPlugin {
// Ignore milliseconds - it's pointless // Ignore milliseconds - it's pointless
long currentTime = System.currentTimeMillis() / MILLI_PER_SECOND; long currentTime = System.currentTimeMillis() / MILLI_PER_SECOND;
// Should we update? try {
if (currentTime > config.getAutoLastTime() + config.getAutoDelay()) { // Should we update?
// Initiate the update as if it came from the console if (currentTime > config.getAutoLastTime() + config.getAutoDelay()) {
if (config.isAutoDownload()) // Initiate the update as if it came from the console
commandProtocol.updateVersion(getServer().getConsoleSender()); if (config.isAutoDownload())
else if (config.isAutoNotify()) commandProtocol.updateVersion(getServer().getConsoleSender());
commandProtocol.checkVersion(getServer().getConsoleSender()); else if (config.isAutoNotify())
else commandProtocol.checkVersion(getServer().getConsoleSender());
commandProtocol.updateFinished(); else
commandProtocol.updateFinished();
}
} catch (Exception e) {
reporter.reportDetailed(this, "Cannot perform automatic updates.", e);
updateDisabled = true;
} }
} }