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) { public CommandBase(ErrorReporter reporter, String permission, String name) {
this(permission, name, 0); 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,11 +34,12 @@ 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) {
try {
// Make sure we're dealing with the correct command // Make sure we're dealing with the correct command
if (!command.getName().equalsIgnoreCase(name)) { if (!command.getName().equalsIgnoreCase(name)) {
return false; return false;
} }
if (!sender.hasPermission(permission)) { if (permission != null && !sender.hasPermission(permission)) {
sender.sendMessage(ChatColor.RED + "You haven't got permission to run this command."); sender.sendMessage(ChatColor.RED + "You haven't got permission to run this command.");
return true; return true;
} }
@ -45,7 +50,42 @@ abstract class CommandBase implements CommandExecutor {
} else { } else {
return false; return false;
} }
} catch (Exception e) {
reporter.reportDetailed(this, "Cannot execute command " + name, e, sender, label, args);
return true;
}
} }
/**
* 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;
@ -23,8 +24,8 @@ class CommandProtocol extends CommandBase {
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() {
try {
UpdateResult result = updater.update(UpdateType.NO_DOWNLOAD, true); UpdateResult result = updater.update(UpdateType.NO_DOWNLOAD, true);
sender.sendMessage(ChatColor.BLUE + "[ProtocolLib] " + result.toString()); 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() {
try {
UpdateResult result = updater.update(UpdateType.DEFAULT, true); UpdateResult result = updater.update(UpdateType.DEFAULT, true);
sender.sendMessage(ChatColor.BLUE + "[ProtocolLib] " + result.toString()); 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,8 +224,10 @@ public class ProtocolLibrary extends JavaPlugin {
manager.sendProcessedPackets(tickCounter++, true); manager.sendProcessedPackets(tickCounter++, true);
// Check for updates too // Check for updates too
if (!updateDisabled) {
checkUpdates(); checkUpdates();
} }
}
}, ASYNC_PACKET_DELAY, ASYNC_PACKET_DELAY); }, ASYNC_PACKET_DELAY, ASYNC_PACKET_DELAY);
} catch (Throwable e) { } catch (Throwable e) {
@ -238,6 +241,7 @@ 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;
try {
// Should we update? // Should we update?
if (currentTime > config.getAutoLastTime() + config.getAutoDelay()) { if (currentTime > config.getAutoLastTime() + config.getAutoDelay()) {
// Initiate the update as if it came from the console // Initiate the update as if it came from the console
@ -248,6 +252,10 @@ public class ProtocolLibrary extends JavaPlugin {
else else
commandProtocol.updateFinished(); commandProtocol.updateFinished();
} }
} catch (Exception e) {
reporter.reportDetailed(this, "Cannot perform automatic updates.", e);
updateDisabled = true;
}
} }
private void checkForIncompatibility(PluginManager manager) { private void checkForIncompatibility(PluginManager manager) {